zoukankan      html  css  js  c++  java
  • 二叉树的遍历(转)

    #include <iostream>
    #include <stack>
    #include <queue>

    using namespace std;

    typedef struct Node
    {
      int data;
      Node *lchild,*rchild;
    }btree;

    //创建二叉树

    btree *create(int a[],int n, int i)
    {
      btree *t;
      if(i>n)
        t=NULL;
      else
      {
        t=new btree;
        t->data=a[i-1];
        t->lchild=create(a,n,2*i);
        t->rchild=create(a,n,2*i+1);
      }

      return t;
    }



    //递归前序遍历

    void preorder(btree *p)
    {
      if(p!=NULL)
      {
        cout<<p->data<<endl;
        preorder(p->lchild);
        preorder(p->rchild);
      }
    }

    //非递归前序遍历

    void preorder1(btree *p)
    {
      stack<btree *> s;
      while(!s.empty()||p!=NULL)
      {
        while(p!=NULL)
        {
          cout<<p->data<<endl;
          s.push(p);
          p=p->lchild;
        }
        p=s.top();
        s.pop();
        p=p->rchild;
      }
    }

    //递归中序遍历

    void inorder(btree *p)
    {
      if(p!=NULL)
      {
        inorder(p->lchild);
        cout<<p->data<<endl;
        inorder(p->rchild);
      }
    }

    //非递归中序遍历

    void inorder1(btree *p)
    {
      stack<btree *> s;
      while(!s.empty()||p!=NULL)
      {
        while(p!=NULL)
        {
          s.push(p);
          p=p->lchild;
        }
        p=s.top();
        cout<<p->data<<endl;
        s.pop();
        p=p->rchild;
      }
    }

    //递归后续遍历

    void postorder(btree *p)
    {
      if(p!=NULL)
      {
        postorder(p->lchild);
        postorder(p->rchild);
        cout<<p->data<<endl;
      }
    }

    struct node
    {
    btree *t;
    int flag;
    };

    //非递归后续遍历

    void postorder1(btree *p)
    {
      stack<node> s;
      node post;
      while(!s.empty()||p!=NULL)
      {
        while(p!=NULL)
        {
          post.t=p;
          post.flag=0;
          s.push(post);
          p=p->lchild;
        }
        if(!s.empty())
        {
          post=s.top();
          s.pop();
            if(post.flag==0)
            {
              post.flag=1;
              s.push(post);
              p=(post.t)->rchild;
            }
            else
            {
              cout<<(post.t)->data<<endl;
              p=NULL;
            }
        }
      }
    }

    //非递归层次遍历

    void layerorder(btree *p)
    {
      queue<btree *>q;
      btree *t;
      if(p!=NULL)
        q.push(p);
      while(!q.empty())
      {
        t=q.front();
        cout<<t->data<<endl;
        q.pop();
        if(t->lchild!=NULL)
          q.push(t->lchild);
        if(t->rchild!=NULL)
          q.push(t->rchild);
      }
    }

    //交换左右子树

    void exchange(btree *p)
    {
      btree *t;
      if(p!=NULL)
      {
        t=p->lchild;
        p->lchild=p->rchild;
        p->rchild=t;
        exchange(p->lchild);
        exchange(p->rchild);
      }
    }

    int main()
    {
      btree *root;
      int a[7]={1,2,3,4,5,6,7};
      root=create(a,7,1);
      cout<<"preorder:"<<endl;
      preorder(root);
      cout<<"preorder1:"<<endl;
      preorder1(root);
      cout<<"inorder"<<endl;
      inorder(root);
      cout<<"inorder1"<<endl;
      inorder1(root);
      cout<<"postorder"<<endl;
      postorder(root);
      cout<<"postorder1"<<endl;
      postorder1(root);
      cout<<"layerorder"<<endl;
      layerorder(root);
      exchange(root);
      cout<<"After exchange(layerorder):"<<endl;
      layerorder(root);
      return 0;
    }


    
    



  • 相关阅读:
    (OK) Use Android Code to Enable USB Debugging
    add software "mouse cursor" in Android-x86
    Subject: [android-porting] Mouse cursor:issue with dispatchPointer
    BUG实例分析五:binder alloc buf, no vma
    【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) A】Packets
    【ACM-ICPC 2018 南京赛区网络预赛 I】Skr
    【ACM-ICPC 2018 南京赛区网络预赛 A】An Olympian Math Problem
    【AIM Tech Round 5 (rated, Div. 1 + Div. 2) 总结】【题解往前或往后翻,不在这】
    【AIM Tech Round 5 (rated, Div. 1 + Div. 2) A】 Find Square
    【AIM Tech Round 5 (rated, Div. 1 + Div. 2) B】Unnatural Conditions
  • 原文地址:https://www.cnblogs.com/jasonliu/p/2407789.html
Copyright © 2011-2022 走看看