树的遍历 ALDS1_7_C: Tree Walk
来源:互联网 发布:mac os x怎么更新 编辑:程序博客网 时间:2024/05/29 07:10
递归版
#include <cstdio>#define MAX 10000#define NIL -1using namespace std;struct node{ int parent; int left; int right;};node binaryTree[MAX];int n;//前序遍历void preParse(int u){ if(u == NIL) return; printf(" %d",u); preParse(binaryTree[u].left); preParse(binaryTree[u].right);}//中序遍历void inParse(int u){ if(u==NIL) return; inParse(binaryTree[u].left); printf(" %d",u); inParse(binaryTree[u].right);}//后序遍历void postParse(int u){ if(u==NIL) return; postParse(binaryTree[u].left); postParse(binaryTree[u].right); printf(" %d",u);}//层次遍历void levelOrder(){ queue<node*> q; q.push(root); node* tem; while(!q.empty()){ tem=q.front(); printf("%d ",tem->key); q.pop(); if(tem->left) q.push(tem->left); if(tem->right) q.push(tem->right); }}int main(){ int i; int value; int left; int right; int root; scanf("%d",&n); for(i=0;i<n;i++) binaryTree[i].parent=NIL; for(i=0;i<n;i++){ scanf("%d %d %d",&value,&left,&right); binaryTree[value].left=left; binaryTree[value].right=right; if(left!=NIL) binaryTree[left].parent=value; if(right!=NIL) binaryTree[right].parent=value; } for(i=0;i<n;i++){ if(binaryTree[i].parent==NIL) root=i; } printf("Preorder\n"); preParse(root); printf("\n"); printf("Inorder\n"); inParse(root); printf("\n"); printf("Postorder\n"); postParse(root); printf("\n");}
非递归版
inline void preorder(node* p){ stack<node*> w; do{ while(p){ printf("%d ",p->key); w.push(p); p=p->left; } if(!w.empty()){ p=w.top(); w.pop(); p=p->right; } }while(p||!w.empty());}inline void inorder(node* p){ stack<node*> w; do{ while(p){ w.push(p); p=p->left; } if(!w.empty()){ p=w.top(); printf("%d ",p->key); w.pop(); p=p->right; } }while(p||!w.empty());}//只能遍历一次,不推荐使用inline void postorder(node* p){ stack<node*> w; do{ while(p){ w.push(p); p=p->left; } if(!w.empty()){ p=w.top(); if(p->right&&!p->right->visited) p=p->right; else{ printf("%d ",p->key); p->visited=true; w.pop(); p=NULL; } } }while(p||!w.empty());}//推荐使用inline void postorder(node* p){ stack<node*> w; node* pre=NULL; w.push(p); while(!w.empty()){ p=w.top(); if(p){ if((!p->left)&&(!p->right)||(pre&&(pre==p->left||pre==p->right))){ printf("%d ",p->key); w.pop(); pre=p; } else{ if(p->right) w.push(p->right); if(p->left) w.push(p->left); } } }}
阅读全文
0 0
- 树的遍历 ALDS1_7_C: Tree Walk
- Aizu ALDS1_7_C Tree Walk
- Tree Walk 树三种遍历的OJ
- 树的遍历 Tree traveral
- os.walk 遍历目录
- 全局遍历os.walk
- os.walk 目录遍历
- python使用os模块的os.walk遍历文件夹
- os.walk( )遍历指定目录下的所有文件
- Python os.walk遍历文件夹的小错误解决办法
- Tree 二叉树的建立 和遍历
- poj2255~~Tree Recovery (树的遍历构造~)
- POJ2255 Tree Recovery 【树的遍历】
- 二叉树 (Binary Tree)的遍历
- HDU1710 Binary Tree Traversals【树的遍历】
- ftw, nftw - file tree walk
- python walk dir(遍历目录)
- Python os.walk 遍历目录
- 【Java进阶-Java动态代理与AOP】01 代理类的作用与原理及AOP概念
- 3 Sum leetcode java
- 转载
- Google 的开源技术protobuf 简介与例子
- (转) hash 函数及其重要性
- 树的遍历 ALDS1_7_C: Tree Walk
- MMP的一天
- Java API(Math类和Random类 包装类 Date类、Calendar类和DateFormat类)
- 系统架构-复杂系统的产品设计与开发
- Mysql索引的优化分析-索引优化(1)
- pthread_key_create
- 整数、浮点数在内存中的存储
- 问题1:集合不能一边遍历一边删除
- 数据结构——树(定义)