二叉树遍历(中序,先序,后序,层次等)
来源:互联网 发布:php解析html 编辑:程序博客网 时间:2024/06/04 18:52
二叉树遍历(中序,先序,后序,层次等)
遍历:
中序:左子树->根节点->右子树
先序:根节点->左子树->右子树
后序:左子树->右子树->根节点
1.中序遍历
中序遍历递归实现
1 //中序遍历 2 template<class T> void tree<T>::in_order() 3 { 4 if (root) 5 { 6 order_in(root); 7 8 }else 9 {10 cout<<"empty tree ";11 }12 13 cout<<endl;14 15 }16 17 template<class T> void tree<T>::order_in(tree_node<T> *p)18 {19 if (p)20 {21 order_in(p->lchild);22 cout<<p->value;23 order_in(p->rchild);24 }25 }
中序遍历迭代实现
1 //先将栈顶元素左节点入栈,如果没有最左,则出栈,压入出栈元素的右结点,依次知道栈为空 2 template<class T> void tree<T>::iter_in_order() 3 { 4 if (root) 5 { 6 tree_node<T> *ptr=root; 7 stack<tree_node<T>* > stack; 8 while (1) 9 {10 //左子树左节压栈11 while (ptr)12 {13 stack.push(ptr);14 ptr=ptr->lchild;15 }16 17 if (stack.isEmpty())18 break;19 20 ptr=stack.Top();21 cout<<ptr->value;22 stack.pop();23 ptr=ptr->rchild;24 }25 26 }else27 {28 cout<<"empty tree ";29 }30 31 cout<<endl;32 33 }
2.先序遍历
先序递归实现
//先序遍历template<class T>void tree<T>::pre_order(){ if (root) { order_pre(root); }else { cout<<"empty tree "; } cout<<endl;}template<class T> void tree<T>::order_pre(tree_node<T> *p){ if(p) { cout<<p->value; order_pre(p->lchild); order_pre(p->rchild); }}
先序迭代实现
1 //栈顶出栈,压入栈顶元素右结点,左结点,依次循环直到栈空 2 template<class T>void tree<T>::iter_pre_order() 3 { 4 if (!root) 5 { 6 cout<<"empty tree "; 7 }else 8 { 9 tree_node<T> *ptr=root;10 stack_list<tree_node<T> *>stack;11 stack.push(root);12 while (1)13 {14 if (stack.isEmpty())15 {16 break;17 }18 ptr=stack.Top();19 stack.pop();20 cout<<ptr->value;21 if (ptr->rchild)22 {23 stack.push(ptr->rchild);24 }25 if (ptr->lchild)26 {27 stack.push(ptr->lchild);28 }29 }30 31 }32 cout<<endl;33 }
3.后序遍历
后序递归实现
1 //后序遍历 2 template<class T>void tree<T>::post_order() 3 { 4 if (root) 5 { 6 order_post(root); 7 8 }else 9 {10 cout<<"empty tree ";11 }12 13 cout<<endl;14 }15 16 template<class T>void tree<T>::order_post(tree_node<T> *p)17 {18 if (p)19 {20 order_post(p->lchild);21 order_post(p->rchild);22 cout<<p->value;23 }24 }
后序迭代实现
1 template<class T> void tree<T>::iter_post_order() 2 { 3 if(root) 4 { 5 tree_node<T> *ptr=root,*pre=NULL; 6 stack_list<tree_node<T> *>stack; 7 stack.push(root); 8 while (1) 9 {10 if (stack.isEmpty())11 {12 break;13 }14 ptr=stack.Top();15 if (pre!=ptr->rchild&&pre!=ptr->lchild)16 {17 if (ptr->rchild)18 stack.push(ptr->rchild);19 if (ptr->lchild)20 stack.push(ptr->lchild);21 22 }23 if(ptr->lchild==NULL&&ptr->rchild==NULL||pre==ptr->lchild||pre==ptr->rchild)24 {25 cout<<ptr->value;26 stack.pop(); 27 28 }29 pre=ptr;30 }31 32 }else33 {34 cout<<"empty tree ";35 }36 37 cout<<endl;38 39 }
4.层次遍历
层次遍历
1 //层次遍历,队列实现,依次取对头元素,将左,右结点入队 2 template <class T> void tree<T>::level_order() 3 { 4 if (root) 5 { 6 tree_node<T> *ptr=root; 7 Queue_list<tree_node<T>*> queue; 8 queue.push(root); 9 while (1)10 {11 if (queue.isEmpty())12 {13 break;14 }15 ptr=queue.Front();16 cout<<ptr->value;17 if (ptr->lchild)18 queue.push(ptr->lchild);19 if (ptr->rchild)20 queue.push(ptr->rchild);21 queue.pop();22 23 }24 25 }26 else27 {28 cout<<"empty tree";29 }30 31 cout<<endl;32 33 }
- 二叉树遍历(中序,先序,后序,层次等)
- 先序,中序,后序,层次遍历二叉树
- 二叉树 的先序 中序、后序遍历、层次遍历以及树状打印等操作
- 二叉树的先序遍历,后序遍历,中序遍历,层次遍历
- 二叉树 非递归 先序遍历 中序遍历 后序遍历 层次遍历
- 二叉树-建树,层次遍历,先序遍历,中序遍历,后序遍历
- 二叉树建立以及先序、中序、后序、层次遍历(JAVA 实现)
- 二叉树的先、中、后序遍历及层次遍历的迭代版算法
- 二叉树2:层次遍历方式及先序、中序、后序(递归与非递归)遍历方式
- 二叉树的先序遍历、中序遍历、后序遍历、层次遍历的递归实现
- JAVA版二叉树的先序、中序、后序和层次遍历
- 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 二叉树的先序、中序、后序以及层次遍历
- 二叉树的先序/中序/后序/层次遍历
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- 树遍历方式总结:层次遍历、先序遍历、中序遍历、后序遍历
- 二叉树 (先序,中序,后序遍历 )
- 二叉树的建立及遍历(先序遍历、中序遍历、后续遍历、层次遍历)
- asp.net二级域名的解析
- 只要有你
- win2008安装 apache+php+mysql
- svn安装
- 二叉树的前序遍历、中序遍历和后序遍历及其算法
- 二叉树遍历(中序,先序,后序,层次等)
- 第一周工作小结
- page152
- MySQL编码、Spring配置中编码、Struts中文问题
- linux下C语言多线程(二)线程的私有数据
- Java DecimalFormat的主要功能及使用方法
- 开启当前会话的10046事件
- static变量初始化
- 双色球程序