1020. Tree Traversals (25)
2015-02-05 10:16
204 查看
/*----------------------------------------------------------------------- 本题采用先根据后序和中序遍历的结果建树,然后再根据建好的树进行层序遍历的方法来完成 -------------------------------------------------------------------------*/ #include<stdio.h> #include<stdlib.h> typedef struct queue* q; typedef struct Node* node; struct Node{ int digit; node left; node right; };//树节点的基本结构 struct queue{ node nodee; struct queue* next; };//树的层序遍历要用到队列 q head=NULL,tail=NULL; node build_tree(int post[],int in[],int amount);//通过后序遍历和中序遍历的结果建树 int find_root(int post[],int in[],int amount);//找到树(或子树)的根节点 void level_order(node root);//对已经建好的树进行层序遍历 void enqueue(node this_node); node dequeue(); int main(){ node root; root = (node)malloc(sizeof(struct Node)); int i,num; int mid; int postorder[30],inorder[30]; scanf("%d",&num); for(i=0;i<num;i++) scanf("%d",&postorder[i]); for(i=0;i<num;i++) scanf("%d",&inorder[i]); mid = find_root(postorder,inorder,num); root->digit = postorder[num-1];// or root->digit = inorder[mid]; root->left = build_tree(postorder,inorder,mid); root->right = build_tree(&postorder[mid],&inorder[mid+1],num-mid-1); /*-------------------------------------------------------------------------------------------------------- 后序遍历的结果为 左子树+右子树+根节点;中序遍历的结果为 左子树+根节点+右子树 因此,树的根节点必然是后序遍历的最后一个元素。在中序遍历的结果中找到这个元素,以此为界,左右分别是根的左右子树 采用递归的方式逐步劈分左右子树,直到树被建完 ---------------------------------------------------------------------------------------------------------*/ level_order(root); return 0; } node build_tree(int post[],int in[],int amount){ node root; int mid; if(amount == 0) return NULL; root = (node)malloc(sizeof(struct Node)); mid = find_root(post,in,amount); root->digit = post[amount-1]; root->left = build_tree(post,in,mid); root->right = build_tree(&post[mid],&in[mid+1],amount-mid-1); return root; } int find_root(int post[],int in[],int amount){ int i; for(i=0;i<amount;i++) if(post[amount-1] == in[i]) return i; } /*----------------------------------------------------------------------- 层序遍历的基本方法(使用队列的数据结构,非递归)好像也没有递归方法=_=|| 1、根入队 2、根的所有儿子入队 3、进行一次出队,对出队的元素进行visit,并令出队元素为新的根 4、从2开始重复步骤,直到队列为空 -------------------------------------------------------------------------*/ void level_order(node root){ enqueue(root); while(root){ root = dequeue(); if(root){//not a empty queue enqueue(root->left); enqueue(root->right); printf("%d",root->digit); if(head) printf(" "); } } return ; } /*入队操作*/ void enqueue(node this_node){ if(this_node==NULL) return ; if(head == NULL){ tail = (q)malloc(sizeof(struct queue)); head = tail; } else{ tail->next = (q)malloc(sizeof(struct queue)); tail = tail->next; } tail->nodee = this_node; tail->next = NULL; return ; } /*出队操作*/ node dequeue(){ if(head == NULL) return NULL; else { node p; q temp; p = head->nodee; temp = head; head=head->next; free(temp); return p; } }
相关文章推荐
- 一步一步学Silverlight 2系列(25):综合实例之Live Search
- Root of AVL Tree (25)
- 1079.Total Sales of Supply Chain (25)
- 1082. Read Number in Chinese (25)
- 25大软件编程错误不可赦
- PAT 1089. Insert or Merge (25)
- PAT A1121 damn single (25)
- C语言学习笔记25——栈,堆和静态存储区
- 25大软件编程错误不可赦
- 1090. Highest Price in Supply Chain (25) -计层的BFS改进
- 1015. 德才论 (25)
- 04-树5 Root of AVL Tree(25 分)
- 3/24大盘分析以及3/25操作策略
- 2-08. 用扑克牌计算24点(25)
- 1002. A+B for Polynomials (25)
- 1110. Com 4000 plete Binary Tree (25)
- C语言例题25:
- 1002.A+B for Polynomials (25) 合并同类项
- 1098.Insertion or Heap Sort (25)
- JavaWeb学习笔记-mybatis-25-与spring整合