您的位置:首页 > 其它

递归、栈、非递归非栈实现二叉树的遍历

2016-11-07 23:12 399 查看
基于迎春花开365天利用栈实现二叉树的先序、中序、后序遍历的非递归操作

以及 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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息