二叉树的遍历(递归实现+非递归实现)

参考代码:

#include<iostream>
#include<stack>
using namespace std;


class Tree{
public:
Tree():element(0),left(NULL),right(NULL){}
Tree(int num):element(num){left=right=NULL;}
Tree* search(int num);
Tree* insert(int num);
int getElement(){return element;}
void xian_xu(void);
void zhong_xu(void);
void hou_xu(void);
void new_xian_xu(void);
void new_zhong_xu(void);
void new_hou_xu(void);
private:
int element;
Tree *left;
Tree *right;
};


Tree* Tree::search(int num)
{
if(element == num)
{
return this;
}
else if(left!=NULL)
{
return left->search(num);
}
else if(right!=NULL)
{
return right->search(num);
}
else return NULL;
}


Tree* Tree::insert(int num)
{


if(num>element)
{
if(right==NULL)
right=new Tree(num);
else
right->insert(num);
}
else if(num<element)
{
if(left == NULL)
left=new Tree(num);
else
left->insert(num);
}
else if(num==element)
{
cout<<"This version do not support two same nodes"<<endl;
exit(1);
}
return this;


}


void Tree::xian_xu()
{
cout<<element<<" ";
if(left!=NULL)
{
left->xian_xu();
}
if(right!=NULL)
{
right->xian_xu();
}


}
void Tree::zhong_xu()
{
if((left==NULL) && (right==NULL))
cout<<element;
else
{
if(left!=NULL)
{
left->zhong_xu();
}
cout<<" "<<element<<" ";
if(right!=NULL)
{
right->zhong_xu();
}
}
}


void Tree::hou_xu()
{
if((left==NULL) && (right==NULL))
cout<<element<<" ";
else
{
if(left!=NULL)
{
left->hou_xu();
}
if(right!=NULL)
{
right->hou_xu();
}
cout<<element<<" ";


}


}


void Tree::new_xian_xu()
{
stack<Tree *> S;
Tree * temp = this;
while(temp)
{
cout<<temp->element<<" ";
S.push(temp);
temp=temp->left;
}
while(!S.empty())
{
temp= S.top();
if(temp->right)
{
temp = temp->right;
S.pop();
while(temp)
{
cout<<temp->element<<" ";
S.push(temp);
temp = temp->left;
}
}
else
S.pop();
}


}


void Tree::new_zhong_xu()
{
stack<Tree *> S;
Tree * temp = this;
while(temp)
{
S.push(temp);
temp=temp->left;
}
while(!S.empty())
{
temp= S.top();
cout<<temp->element<<" ";
if(temp->right)
{
temp = temp->right;
S.pop();
while(temp)
{
S.push(temp);
temp = temp->left;
}
}
else
S.pop();
}
}


void Tree::new_hou_xu()
{
stack<Tree *> S;
Tree * temp = this;
stack<Tree *> second;
while(temp)
{
S.push(temp);
temp=temp->left;
}
while(!S.empty())
{
temp= S.top();
if(temp->right)
{
if( (!second.empty()) && (temp == second.top()))
{
cout<<temp->element<<" ";
S.pop();
second.pop();
}
else
{
second.push(temp);
temp = temp->right;
while(temp)
{
S.push(temp);
temp = temp->left;
}
}
}
else
{
cout<<temp->element<<" ";
S.pop();
}
}
}




int main()
{
Tree* root = new Tree(10);
root->insert(6);
root->insert(14);
root->insert(4);
root->insert(8);
root->insert(12);
root->insert(16);
root->xian_xu();
cout<<endl;
root->zhong_xu();
cout<<endl;
root->hou_xu();
cout<<endl;
root->new_xian_xu();
cout<<endl;
root->new_zhong_xu();
cout<<endl;
root->new_hou_xu();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值