二叉树--(建树,前序,中序,后序)--递归和非递归实现

Posted soyosuyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树--(建树,前序,中序,后序)--递归和非递归实现相关的知识,希望对你有一定的参考价值。

    #include<iostream>
    #include<string.h>
    #include<stack>
    using namespace std;
    typedef struct BTree
    {
        int val;
        struct BTree *left,*right;
    }BTree;
    class Tree
    {
        public:
        BTree *create_node(int level,string pos);
        void PreOrder(BTree *t);  //先序遍历
        void InOrder(BTree *t);  //中序遍历
        void PostOrder(BTree *t);  //后序遍历
        void NonRecursivePreOrder(BTree*t); //非递归前序遍历
        void NonRecursiveInOrder(BTree*t);  //非递归中序遍历
        void NonRecursivePostOrder(BTree*t);//非递归后序遍历
        BTree *root;
    };
    BTree* Tree::create_node(int level,string pos)
    {
        int data;
        BTree *node = new BTree;
        int a[]={100,99,98,97,0,0,20,0,0,10,0,0,5,1,0,0,2,0,0};
        static int t=0;
        cout<<"please enter data:level "<<level<<" "<<pos<<"--->值为:"<<a[t]<<endl;
           data=a[t++];
        if(data == 0)
        {
            return NULL;
        }
        node->val= data;
        node->left = create_node(level+1,"left");
        node->right= create_node(level+1,"right");
        return node;
    }

    void Tree::PreOrder(BTree *t)
    {
        if(t)
        {
            cout<<t->val<<" ";;
            PreOrder(t->left);
            PreOrder(t->right);
        }
    }

    void Tree::InOrder(BTree *t)
    {
        if(t)
        {
            InOrder(t->left);
            cout<<t->val<<" ";;
            InOrder(t->right);
        }
    }

    void Tree::PostOrder(BTree *t)
    {
        if(t)
        {
            PostOrder(t->left);
            PostOrder(t->right);
            cout<<t->val<<" ";
        }
    }
    void Tree::NonRecursivePreOrder(BTree*t)
   {
      if(t==NULL)
          return;
      stack<BTree*>s;
      BTree *p;
      p=t;
      while(p||!s.empty())
      {
          if(p)
          {
              cout<<p->val<<" ";
              s.push(p);
              p=p->left;
          }
          else{
              p=s.top();
              p=p->right;
              s.pop();
          }
      }
   }
    void Tree::NonRecursiveInOrder(BTree*t)
    {
        if(t==NULL)
            return;
        stack<BTree*>s;
        BTree*p;
        p=t;
        while(p||!s.empty())
        {
            if(p)
            {
                s.push(p);
                p=p->left;
            }
            else
            {
                p=s.top();
                cout<<p->val<<" ";
                p=p->right;
                s.pop();
            }
        }
    }
  void Tree::NonRecursivePostOrder(BTree*t)
  {
      if(t==NULL)
          return;
      stack<BTree*>s;
      BTree*p=t;
      BTree*r;
      while(p||!s.empty())
      {
          if(p)
          {
              s.push(p);
              p=p->left;
          }
          else
          {
              p=s.top();
              if(p->right&&p->right!=r)
              {
                  p=p->right;
                  s.push(p);
                  p=p->left;
              }
              else
              {
                  cout<<p->val<<" ";
                  r=p;
                  s.pop();
                  p=NULL;
              }
          }
      }

  }


    int main()
    {
        Tree tree;
        tree.root = tree.create_node(1,"root");
        cout<<"Pre"<<endl;
        tree.PreOrder(tree.root);
        cout<<endl;
        cout<<"非递归前序遍历"<<endl;
        tree.NonRecursivePreOrder(tree.root);
        cout<<endl;
        cout<<"In"<<endl;
        tree.InOrder(tree.root);
        cout<<endl;
        cout<<"非递归中序遍历"<<endl;
        tree.NonRecursiveInOrder(tree.root);
        cout<<endl;
        cout<<"Post"<<endl;
        tree.PostOrder(tree.root);
        cout<<endl;
        cout<<"非递归后序遍历"<<endl;
        tree.NonRecursivePostOrder(tree.root);
        return 0;
    }

结果:

please enter data:level 1 root--->值为:100
please enter data:level 2 left--->值为:99
please enter data:level 3 left--->值为:98
please enter data:level 4 left--->值为:97
please enter data:level 5 left--->值为:0
please enter data:level 5 right--->值为:0
please enter data:level 4 right--->值为:20
please enter data:level 5 left--->值为:0
please enter data:level 5 right--->值为:0
please enter data:level 3 right--->值为:10
please enter data:level 4 left--->值为:0
please enter data:level 4 right--->值为:0
please enter data:level 2 right--->值为:5
please enter data:level 3 left--->值为:1
please enter data:level 4 left--->值为:0
please enter data:level 4 right--->值为:0
please enter data:level 3 right--->值为:2
please enter data:level 4 left--->值为:0
please enter data:level 4 right--->值为:0
Pre
100 99 98 97 20 10 5 1 2 
非递归前序遍历
100 99 98 97 20 10 5 1 2 
In
97 98 20 99 10 100 1 5 2 
非递归中序遍历
97 98 20 99 10 100 1 5 2 
Post
97 20 98 10 99 1 2 5 100 
非递归后序遍历
97 20 98 10 99 1 2 5 100 

 

以上是关于二叉树--(建树,前序,中序,后序)--递归和非递归实现的主要内容,如果未能解决你的问题,请参考以下文章

二叉树的递归和非递归遍历(前序中序后序层序)

二叉树详解及二叉树的前序中序后序遍历(递归和非递归)

二叉树详解及二叉树的前序中序后序遍历(递归和非递归)

C语言根据层次遍历和中序遍历求二叉树的前序遍历和后序遍历。下面有我的建树函数,有注释的。

二叉树遍历(先序中序后序)

[无需建树]已知前序或后序和中序遍历结果,输出前序或后序或层次遍历的方法汇总