递归、栈、非递归非栈实现二叉树的遍历
2016-11-07 23:12
399 查看
基于迎春花开365天利用栈实现二叉树的先序、中序、后序遍历的非递归操作
以及 Alvin-Qu非递归,不用栈实现二叉树中序遍历
的算法,本人对二叉树的遍历做出系统的总结(C++实现),包含先序、中序、后序的递归、栈实现、非递归非栈实现。其中以双栈作为辅助数据结构的后序遍历算法,来源于jimolangge123二叉树的后序遍历实现(基于两个栈实现)
先序遍历的三种算法:
中序遍历:
后序遍历:
以及 Alvin-Qu非递归,不用栈实现二叉树中序遍历
的算法,本人对二叉树的遍历做出系统的总结(C++实现),包含先序、中序、后序的递归、栈实现、非递归非栈实现。其中以双栈作为辅助数据结构的后序遍历算法,来源于jimolangge123二叉树的后序遍历实现(基于两个栈实现)
先序遍历的三种算法:
//递归实现先序遍历 void preorderTree(binNode<Elem>* root){ if(root==NULL){ return; } else{ cout<<root->getElement();//print the value preorderTree(root->left()); preorderTree(root->right()); } } //非递归实现先序遍历(使用栈作为辅助数据结构) void preorderTree(binNode<Elem>* root){ stack<binNode<Elem>*> Stack; if(root==NULL){ return; } while(root||!Stack.empty()){ while(root){ Stack.push(root); cout<<root->getElement();//遍历 root=root->left(); } root=Stack.top(); Stack.pop(); root=root->right(); } } //不使用栈,非递归先序遍历BST /* *每个节点有个parent指针进行回溯 */ void preorderTree(binNode<Elem>* root){ if(root==NULL){ return; } while(root->left()){ cout<<root->getElement();//遍历 root=root->left(); } while(root!=NULL){ if(root->right(){//该节点有右孩子 root=root->right(); cout<<root->getElement();//遍历 while(root->left()){//指向该子树的最左端 root=root->left(); cout<<root->getElement();//遍历 } } else{//没有右孩子 binNode<Elem>* temp=NULL; do{//溯回 temp=root; root=root->parent(); }while(root!=NULL&&temp==root->right()); } } }
中序遍历:
//递归实现中序遍历 void inorderTree(binNode<Elem>* root){ if(root==NULL){ return; } else{ inorderTree(root->left()); cout<<root->getElement();//print the value inorderTree(root->right()); } } //非递归实现中序遍历(使用栈作为辅助数据结构) void inorderTree(binNode<Elem>* root){ stack<binNode<Elem>*> Stack; if(root==NULL){ return; } while(root||!Stack.empty()){ while(root){ Stack.push(root); root=root->left(); } root=Stack.top(); Stack.pop(); cout<<root->getElement(); root=root->right(); } } //不使用栈,非递归中序遍历BST /* *每个节点有个parent指针进行回溯 */ void inorderTree(binNode<Elem>* root){ if(root==NULL){ return; } while(root->left()){ root=root->left(); } while(root!=NULL){ cout<<root->getElement();//遍历 if(root->right(){//该节点有右孩子 root=root->right(); while(root->left()){//指向该子树的最左端 root=root->left(); } } else{//没有右孩子 binNode<Elem>* temp=NULL; do{//溯回 temp=root; root=root->parent(); }while(root!=NULL&&temp==root->right()); } } }
后序遍历:
//递归实现后序遍历 void postorderTree(binNode<Elem>* root){ if(root==NULL){ return; } else{ inorderTree(root->left()); inorderTree(root->right()); cout<<root->getElement();//print the value } } //非递归实现后序遍历 /* *使用双栈作为辅助数据结构 */ void postorderTree(binNode<Elem>* root){ stack<binNode<Elem>*> Stack1; stack<binNode<Elem>*> Stack2; if(root==NULL){ return; } Stack1.push(root); while(!Stack1.empty()){ root=Stack1.top(); Stack2.push(root); Stack1.pop(); if(root->left()) Stack1.push(root->left()); if(root->right()) Stack1.push(root->right()); } while(!Stack2.empty()){ root=Stack2.top(); Stack2.pop(); cout<<root->getElement(); } } //不使用栈,非递归后序序遍历BST /* *每个节点有个parent指针进行回溯 */ void postorderTree(binNode<Elem>* root){ if(root==NULL){ return; } while(root->left()){ root=root->left(); } while(root!=NULL){ if(root->right()){//该节点有右孩子 root=root->right(); while(root->left()){//指向该子树的最左端 root=root->left(); } } else{//没有右孩子 //溯回操作 binNode<Elem>* temp=root; root=root->parent(); if(temp==root->left()){ cout<<temp->getElement(); } while(temp==root->right()||root){ cout<<temp->getElement(); temp=root; root=root->parent(); } } } }
相关文章推荐
- 二叉树的遍历方法及递归实现
- 二叉树三种遍历方式的递归与非递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的三种遍历的非递归实现
- 二叉树(前中后序递归非递归遍历,层次遍历,C++实现)
- ::递归实现——创建二叉树 ----> 装入数据--->遍历---> 显示 --->销毁
- 二叉树的遍历 非递归 C实现——先序遍历篇
- 树——二叉树的遍历之递归实现
- 递归方法实现二叉树的创建,遍历
- 非递归实现二叉树的遍历
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- 二叉树的遍历(C++非递归实现)
- 二叉树的遍历(非递归实现)
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- 二叉树三种非递归遍历实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- C++实现二叉树 前序遍历, 后序遍历, 中序遍历, 层序遍历(不用递归)
- 递归、非递归实现二叉树的前中后序遍历
- 【应聘笔记系列】二叉树的递归与非递归遍历实现