算法思想:层次遍历二叉树(注意栈中存放的是当前树的结点和所在层数),如果当前遍历结点和前一个结点在同一层,则当前层的结点数加一,如果当前层的结点数大于最大宽度,则更新最大宽度;如果当前遍历结点和前一个结点不在同一层,则更新当前结点层数,并将当前层的节点值置为1;如果当前结点左孩子不为空,左孩子入队;如果当前结点右孩子不为空,右孩子入队;重复上述步骤,直到层次遍历完树中全部结点。
(1)func.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char ElemType;
typedef struct BiTNode {
ElemType data;
struct BiTNode* lchild, * rchild;
}BiTNode,*BiTree;
typedef struct QBiTNode {
BiTNode* pnode;
int nodelevel;
}QBiTNode;
typedef QBiTNode QElemType;
typedef struct LinkNode {
QElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct Queue {
LinkNode* front, * rear;
}Queue;
void preOrder(BiTree T);
void inOrder(BiTree T);
//void leverOrder(BiTree T);
int getmaxwidth(BiTree T);
BiTree creatBiTree(ElemType* preOrderNode,int preFirstNode,int preLastNode,ElemType* inOrderNode,int inFirstNode,int inLastNode);
void initial(Queue &Q);
int isEmpty(Queue Q);
void EnQueue(Queue& Q, QElemType x);
void DeQueue(Queue& Q, QElemType& x);
(2)creatBitree.cpp
#include"func.h"
void preOrder(BiTree T) {
if (T != NULL) {
printf("%c", T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
void inOrder(BiTree T) {
if (T != NULL) {
inOrder(T->lchild);
printf("%c", T->data);
inOrder(T->rchild);
}
}
//void leverOrder(BiTree T) {
// Queue Q;
// initial(Q);
// if (T != NULL) {
// EnQueue(Q, T);
// }
// while (!isEmpty(Q)) {
// BiTNode* pnode;
// DeQueue(Q, pnode);
// printf("%c", pnode->data);
// if (pnode->lchild != NULL) {
// EnQueue(Q, pnode->lchild);
// }
// if (pnode->rchild != NULL) {
// EnQueue(Q, pnode->rchild);
// }
// }
//}
BiTree creatBiTree(ElemType* preOrderNode, int preFirstNode, int preLastNode, ElemType* inOrderNode, int inFirstNode, int inLastNode) {
if (preFirstNode > preLastNode) {
return NULL;
}
BiTNode* pnode = (BiTNode*)malloc(sizeof(BiTNode));
pnode->data = preOrderNode[preFirstNode];
pnode->lchild = pnode->rchild = NULL;
int index;
for (index = inFirstNode; index <= inLastNode; index++) {
if (inOrderNode[index] == pnode->data) {
break;
}
}
pnode->lchild = creatBiTree(preOrderNode, preFirstNode + 1, preFirstNode + index - inFirstNode, inOrderNode, inFirstNode, index - 1);
pnode->rchild = creatBiTree(preOrderNode, preFirstNode + index - inFirstNode + 1, preLastNode, inOrderNode, index + 1, inLastNode);
return pnode;
}
(3)queue.cpp
#include"func.h"
void initial(Queue &Q) {
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
int isEmpty(Queue Q) {
if (Q.rear == Q.front) {
return 1;
}
else {
return 0;
}
}
void EnQueue(Queue &Q, QElemType x) {
LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
pnode->data = x;
pnode->next = Q.rear->next;
Q.rear->next = pnode;
Q.rear = pnode;
}
void DeQueue(Queue& Q, QElemType& x) {
LinkNode* pnode = Q.front->next;
x = pnode->data;
Q.front->next = pnode->next;
if (pnode == Q.rear) {
Q.rear = Q.front;
}
free(pnode);
}
(4)getmaxwidth.cpp
#include"func.h"
/*算法思想:层次遍历二叉树(注意栈中存放的是当前树的结点和所在层数),如果当前遍历结点和前一个结点在同一层,则当前层的结点数加一,
如果当前层的结点数大于最大宽度,则更新最大宽度;如果当前遍历结点和前一个结点不在同一层,则更新当前结点层数,并将当前层的节点值置为1;
如果当前结点左孩子不为空,左孩子入队;如果当前结点右孩子不为空,右孩子入队;重复上述步骤,直到层次遍历完树中全部结点。*/
int getmaxwidth(BiTree T) {
if (T == NULL) {
return 0;
}
Queue Q;
initial(Q);
QBiTNode e;
e.pnode = T;
e.nodelevel = 1;
EnQueue(Q, e);
int maxWidth = 0;//最大宽度
int nodesNum = 0;//一层的节点数
int currentLever = 1;//当前的结点的层数
while (!isEmpty(Q)) {
QBiTNode pcur;
DeQueue(Q, pcur);
if (pcur.nodelevel == currentLever) {
nodesNum += 1;
if (nodesNum > maxWidth) {
maxWidth = nodesNum;
}
}
else {
nodesNum = 1;
currentLever = pcur.nodelevel;
}
QBiTNode temp;
if (pcur.pnode->lchild != NULL) {
temp.pnode = pcur.pnode->lchild;
temp.nodelevel = pcur.nodelevel + 1;
EnQueue(Q, temp);
}
if (pcur.pnode->rchild != NULL) {
temp.pnode = pcur.pnode->rchild;
temp.nodelevel = pcur.nodelevel + 1;
EnQueue(Q, temp);
}
}
return maxWidth;
}
(5)main.cpp
#include"func.h"
#define max_size 100
int main() {
ElemType preOrderNode[max_size] = { 0 };
ElemType inOrderNode[max_size] = { 0 };
int preOrderLength = 0;
int inOrderLength = 0;
gets_s(preOrderNode);
gets_s(inOrderNode);
preOrderLength = strlen(preOrderNode);
inOrderLength = strlen(inOrderNode);
BiTree T = creatBiTree(preOrderNode, 0, preOrderLength - 1, inOrderNode, 0, inOrderLength - 1);
int width = getmaxwidth(T);
printf("树的最大宽度为:%d\n", width);
preOrder(T);
printf("\n");
inOrder(T);
printf("\n");
/*leverOrder(T);
printf("\n");*/
return 0;
}