二叉树的前序中序后序遍历

本次分享二叉树的前序中序后序输出的非递归程序,运行时并未察觉出错误,希望童鞋们帮助我改进代码,感谢!

云共享:http://yunpan.cn/cspRCjAPP8dK4  提取码 bc99

#include<iostream>

#include<string>

#include <stack>//引入标准模板库(STL)里的容器类—stack类①

using namespace std;

struct BiNode

{

      char data;

      struct BiNode *rchild,*lchild;

};//二叉链表存储结点

class BiTree

{

public:

      BiTree()

      {

            cout<<"请输入根节点:"<<endl;

            Create(root);

            if (NULL != root)

            {

                  cout<<"root="<<root->data<<endl;

            }

            else

            {

                  cout << "The BinaryTree is empty." << endl;

            }

      }

      ~BiTree(){Release(root);}

      void InOrderTraverse();

      void PreOrderTraverse();

      void PostOrderTraverse();

      BiNode *root;

      void Create(BiNode* &bt);

      void Release(BiNode *bt);

};

void BiTree::Release(BiNode *bt) //析构函数

{

      if(bt!=NULL)

      {

            Release(bt->lchild );

            Release(bt->rchild );

            delete bt;

      }

}

void BiTree::Create(BiNode* &bt) //建立二叉树

{

      char ch;

      cin>>ch;

      if(ch=='#')bt=NULL;

      else

      {

            bt=new BiNode;

            bt->data =ch;

            cout<<"请输入"<<ch<<"的左孩子,若没有请输入#"<<endl;

            Create(bt->lchild );

            cout<<"请输入"<<ch<<"的右孩子,若没有请输入#"<<endl;

            Create(bt->rchild );

      }

}

void BiTree::PreOrderTraverse()

{

      stack<BiNode*> sta; //定义一个存放BiNode型指针的空栈sta

      BiNode* p = root; //定义一个指向BiNode型结点的指针,并指向树的根节点

      sta.push(p); //将根指针入栈

      while(!sta.empty())

      {

            while (NULL != p)

            {//向左走到尽头,并保留所经过的节点指针入栈

                  cout << p->data << " ";

                  p = p->lchild;

                  if (NULL != p)

                  {

                        sta.push(p);

                  }

            }

            if (!sta.empty())

            {

                  p = sta.top();

                  sta.pop(); //栈顶元素出栈

                  p = p->rchild; //向右一步

                  if (NULL != p)

                  {

                        sta.push(p);

                  }

            }

      }

}

void BiTree::InOrderTraverse()

{

      stack<BiNode*> sta; //定义一个存放BiNode型指针的空栈sta

      BiNode* p = root; //定义一个指向BiNode型结点的指针,并指向树的根节点

      sta.push(p); //将根指针入栈

      while(!sta.empty())

      {

            while (NULL != p)

            {

                  //向左走到尽头,并保留所经过的节点指针入栈

                  p = p->lchild;

                  if (NULL != p)

                  {

                        sta.push(p);

                  }

            }

            if (!sta.empty())

            {

                  p = sta.top();

                  cout<<p->data<<" ";

                  sta.pop(); //栈顶元素出栈

                  p = p->rchild; //向右一步

                  if (NULL != p)

                  {

                        sta.push(p);

                  }

            }

      }

}

void BiTree::PostOrderTraverse()

{

      stack<BiNode*> sta; //定义一个存放BiNode型指针的空栈sta

      stack<BiNode*> stb;

      BiNode* p = root; //定义一个指向BiNode型结点的指针,并指向树的根节点

      sta.push(p);

      while(!sta.empty())

      {

            while (NULL != p)

            {

                  stb.push(p);

                  p = p->rchild;

                  if (NULL != p)

                  {

                        sta.push(p);

                  }

            }

            if (!sta.empty())

            {

                  p = sta.top();

                  sta.pop(); //栈顶元素出栈

                  p = p->lchild;

                  if (NULL != p)

                  {

                        sta.push(p);

                  }

            }

      }

      while(!stb.empty())

      {

            p=stb.top();

            stb.pop();

            cout<<p->data<<" ";

      }

}

int main()

{

      BiTree bt;

      if(bt.root!=NULL)

      {

            cout << "The InOrderTraverse is: " ;

            bt.InOrderTraverse();

            cout << endl;

            cout << "The PreOrderTraverse is: " ;

            bt.PreOrderTraverse();

            cout << endl;

            cout<<"后序是:" ;

            bt.PostOrderTraverse();

            cout<<endl;

      }

      system("pause");

      return 0;

}


你可能感兴趣的:(二叉树,遍历)