最近学二叉树懵懵懂懂的,自己看了下例程写了下代码明白了很多。实践才是真理啊...在这里做个总结三种遍历(我这里先详细记录先序遍历的递归,迭代和改进):
先序遍历
递归实现
preordert(x)
{
if( !S.empty )
{
visit(x->data);
preordert(x->left);
preordert(x->right);
}
else return 0;
迭代实现(利用栈)
if (x) S.push(x);//当前根节点压栈
while( !S.empty() )
{
x = S.pop();//出栈
visit(x->data);
if(HasRC(*x)) S.push(x->right);
if(HasLC(*x)) S.push(x->left);//考虑出栈的顺序,入栈的顺序要改变
}
通常,迭代方法在复杂度上来说比递归好
新构思:能否优化呢
如上图所示
先序遍历可以把过程简化为 先遍历左侧链,再依次往回遍历其右子树
优化
template <typename T, typename V>
static void goAlongLeftBranch( BinNodePosi(T) x,VST &visit; Stack <BinNodePosi(T)> & S )
{
while (x) { visit(x->data);S.push(x->rChild); x = x->lChild; }//将右孩子压栈并遍历左孩
}
template <typename T, typename V>
void travIn_I2( BinNodePosi(T) x, V& visit )
{
Stack <BinNodePosi(T)> S; //
while (ture)
{
goAlongLeftBranch(x, visit, S);
if(S.empty()) break;
x = S.pop();没有左孩子就返回最高的右孩子
}
}
中序遍历(来自先序的新构思的新构思):
template <class T>
static void goAlongLeftBranch( BinNodePosi(T) x, Stack <BinNodePosi(T)> & S )
{ while (x) { S.push(x); x = x->lChild } } //压栈,若不为空则进入下一个左孩子
template <typename T, typename v>
void travIn_I1( BinNodePosi(T) x, v& visit )
{
Stack <BinNodePosi(T)> S; //创建栈
while (ture)
{
goAlongLeftBranch(x,S);//压栈到最后一个左孩子
if(S.empty()) break;
visit(x);
S.pop(x->data);
x = x->rChild;//把右孩子当成左孩子看,若右孩子无分支则读出数据并返回上一个左孩子
}
}
最后附上完整代码(代码是我修改的别人的代码)
特别注意,代码建立树时需要引用!!!否则将会产生错误
#include <iostream>
#include <stack>
#include <string>
using namespace std;
struct binode
{
char date;
binode *lchild,*rchild;
bool isfirst;//非递归后续遍历要用到
};
class bitree
{
private:
binode *root;
public:
bitree()
{
create(root);
}
~bitree()
{
}
binode *getroot()
{
return root;
}
void create(binode *(&jj))//建立二叉树
{
char aa;
cin>>aa;
if(aa=='s')
{
jj=NULL;
}
else
{
(jj)=new binode;
jj->date=aa;
cout<<jj->date;
create(jj->lchild);
create(jj->rchild);
jj->isfirst=true;
}
}
void preordert(binode *a)//遍历二叉树--先序遍历递归算法
{
if(a)
{
cout<<a->date;
preordert(a->lchild);
preordert(a->rchild);
}
}
void preordernonrec(binode *aa)//非递归先序遍历
{
stack<binode *> s;
s.push(aa);cout<<aa->date;
while(!s.empty())
{
while(aa->lchild) {aa=aa->lchild;s.push(aa);cout<<aa->date;}//到叶子节点
s.pop();//之后走该节点的第三步--判断该节点的右子树,出栈后aa仍有该节点地址
if(!aa->rchild)
{
aa=s.top()->rchild;//之前已经走过s.top节点的左子树
s.pop();//这里也要让叶子节点的父母节点出栈
}
else {aa=aa->rchild;s.push(aa);cout<<aa->date;}
}
}
void ineordernonrec(binode *aa)//非递归中序遍历
{
stack<binode *> s;
s.push(aa);
while(!s.empty())
{
while(aa->lchild) {aa=aa->lchild;s.push(aa);}
cout<<aa->date;s.pop();
if(!aa->rchild)
{
cout<<s.top()->date;
aa=s.top()->rchild;
s.pop();
}
else {aa=aa->rchild;s.push(aa);}
}
}
/*从根节点开始遍历左子树到叶子节点,之后要遍历节点的右子树,
遍历右子树分为两种情况,第一种右子树为空,可以直接输出,使
该节点出栈;第二种情况右子树不空,(之前放入栈中的该节点已
经出栈)这时要把该节点第二次放入栈中,这里就需要isfirst来标
记,之后按栈中顺序输出。*/
void posordernonrec(binode *aa)//非递后先序遍历
{
stack<binode *> s;
s.push(aa);
while(!s.empty())
{
while(!s.top()->isfirst)
{
cout<<s.top()->date;
s.pop();
}
while(aa->lchild) {aa=aa->lchild;s.push(aa);}//到叶子节点
s.pop();
if(!aa->rchild)
{
cout<<aa->date;
s.pop();
}
else
{
s.push(aa);
aa->isfirst=false;
aa=aa->rchild;
s.push(aa);
}
}
}
};
int main()
{
bitree bb;
bb.preordert(bb.getroot());
return 0;
}
我将慢慢完善这篇博客
补充(0820)
层次遍历
严格满足顺序(利用队列)
Queue <BinNodePosi(T)> Q;
Q.enqueue( this );
while(!Q.empty())
{
BinNodePosi(T) x = Q.dequeue();
visit(x->data);
if( HasLChild) Q.enqueue( x->lChild );//判断很重要;左孩子入队
if( HasRChild) Q.enqueue( x->rChild );
}