#include<stdio.h> #include<stdlib.h> #define datatype int//二叉树数据的类型 typedef struct…
- User
#include<stdio.h> #include<stdlib.h> #define datatype int//二叉树数据的类型 typedef struct BiNode { datatype data;//数据域 struct BiNode* lchild;//左孩子 struct BiNode* rchild;//右孩子 }BiNode,*BiTree;
void CreateBiTree(BiTree& T) { datatype value; scanf("%d", &value); if (value == '#') T = NULL; else { T = new BiNode; T->data = value; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } }
void CreateBiTree_In_Post(BiTree& T, datatype* InStr, datatype* PostStr, int in1, int in2, int post1, int post2) { // 创建新的结点 T = (BiTree)malloc(sizeof(BiNode)); // 新结点数据为 后序序列 的第一个数据,即根节点数据 T->data = PostStr[post2];
// 根据后序最后一个元素为根结点元素,寻找中序序列中的根结点位置 int root;//根结点在中序中的位置 for (root = 0; root <= in2; root++) { if (PostStr[post2] == InStr[root]) { //printf("后序序列根节点(%c)在中序的位置 = %d\n", PreStr[L1], root); break; } } // 判断中序序列中<root>的左边是否存在左子序列 if (root - in1 != 0) { CreateBiTree_In_Post(T->lchild, InStr,PostStr, in1, root-1, post1, post1+root -in1- 1); } //其实这个分割区间很好理解的,你们自己理解吧我懒得讲 else //看看别的博主也行 { T->lchild = NULL; } // 判断中序序列中<root>的右边是否存在右子序列 if (in2 - root != 0) { CreateBiTree_In_Post(T->rchild, InStr,PostStr,root+1, in2, post1+root-in1 , post2-1); } else { T->rchild = NULL; }}
typedef struct QueueNode{ BiTree node; struct QueueNode* next; }QueueNode; typedef struct Queue{ QueueNode* head; QueueNode* tail; }Queue; void QueueInit(Queue* pq){ pq->head=pq->tail=NULL; } void QueueDestory(Queue pq){ QueueNode cur=pq->head; while(cur){ QueueNode* next=cur->next; next=cur->next; free(cur); cur=next; } pq->head=pq->tail=NULL; } void QueuePush(Queue* pq,BiTree x){ QueueNode* newNode=(QueueNode*)malloc(sizeof(QueueNode)); newNode->node =x; newNode->next=NULL; if(pq->tail==NULL) pq->head=pq->tail=newNode; else{ pq->tail->next=newNode; pq->tail=newNode; } } void QueuePop(Queue* pq){ if(pq->head->next==NULL){ free(pq->head); pq->head=pq->tail=NULL; } else{ QueueNode* next=pq->head->next; free(pq->head); pq->head=next; } }
BiTree QueueFront(Queue* pq){ return pq->head->node; } BiTree QueueBack(Queue* pq){ return pq->tail->node; } int QueueSize(Queue* pq){ QueueNode* cur=pq->head; int cnt=0; while(cur){ cur=cur->next; cnt++; } return cnt; } Queue pq; void TreeLevelOrder(BiTree root){ QueueInit(&pq); if(root) QueuePush(&pq, root); BiTree front; int a=QueueSize(&pq),flag=1; while( a!=1||flag==1){ flag=0; front=QueueFront(&pq); printf("%d ",front->data); if(front->lchild) QueuePush(&pq,front->lchild); if(front->rchild ) QueuePush(&pq,front->rchild ); QueuePop(&pq); a=QueueSize(&pq); } front=QueueFront(&pq); printf("%d",front->data); QueueDestory(&pq); } int postStr[50]={0},inStr[50]={0}; BiTree T; int main() { int num,in1 = 0,post1 = 0,in2,post2; scanf("%d",&num); for(int i=0;i<num;i++) scanf("%d",&postStr[i]); for(int i=0;i<num;i++) scanf("%d",&inStr[i]); num--; CreateBiTree_In_Post(T, inStr, postStr, in1, num, post1, num); TreeLevelOrder(T); }
- Assistant
The code you provided is to create a binary tree using inorder and postorder traversal sequences and perform a level order traversal on the tree.
Here is a brief explanation of the code:
-
The
BiTreestructure represents a node in the binary tree. It contains adatafield and pointers to the left and right child nodes. -
The
CreateBiTree()function is used to create the binary tree using recursion. It takes input values and constructs the tree based on the values. A value of '#' represents the NULL node. -
The
CreateBiTree_In_Post()function is used to create a binary tree based on inorder and postorder traversal sequences. It recursively constructs the tree by finding the root node in the postorder sequence and then dividing the inorder sequence into left and right subtrees.
-
- User
correct the code
- Assistant