The order of a Tree HDU - 3999 (二叉搜索树板子)

The order of a Tree  HDU - 3999 

题意:

给出二叉树的插入顺序求二叉树的先序遍历。

注意:

题目要求输出字典序最小的序列,对于二叉搜索树来说,生成序列的左右子树不会影响树的形态,但必须先输出父节点。

所以输出序列的顺序就是 头结点->左结点->右结点 即先序遍历。

 

模版:

#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int maxn=1e5+10;;

struct node
{
    int value;
    node *l,*r;

    node()
    {
        this->l=NULL;
        this->r=NULL;
    }

    node(int v)
    {
        this->value=v;
        this->l=NULL;
        this->r=NULL;
    }
};

class binarySearchTree
{
public:
    node* root;
    /**
     二叉搜索树建树
     @a 输入数组 a[0]为根结点
     @n  数组a的长度
     */
    void buildTree(int a[],int n)
    {
        this->root=new node(a[0]);
        for(int i=1;i<n;i++)
        {
            node *now=new node(a[i]);
            node *fa=this->root;
            while(1)
            {
                if(now->value<fa->value)
                {
                    if(fa->l==NULL)
                    {
                        fa->l=now;
                        break;
                    }
                    else fa=fa->l;
                }
                else
                {
                    if(fa->r==NULL)
                    {
                        fa->r=now;
                        break;
                    }
                    else fa=fa->r;
                }
            }
        }
    }
    /**
     层序遍历序列
     返回一个数组
     */
    int* getLevelOrder()
    {
        int* a=new int[maxn];
        int cnt=0;
        queue<node>q;
        q.push(*this->root);
        while(!q.empty())
        {
            node now=q.front();
            a[cnt++]=now.value;
            q.pop();
            if(now.l) q.push(*now.l);
            if(now.r) q.push(*now.r);
        }
        return a;
    }
    /**
     先序遍历序列
     返回一个数组
     */
    int* getPreOrder()
    {
        int* a=new int[maxn];
        int cnt=0;
        node *now=this->root;
        stack<node*>st;
        while(now||st.size())
        {
            while(now)
            {
                a[cnt++]=now->value;
                st.push(now);
                now=now->l;
            }
            if(st.size())
            {
                now=st.top();
                st.pop();
                now=now->r;
            }
        }
        return a;
    }
    /**
     中序遍历序列
     返回一个数组
     */
    int* getInOrder()
    {
        int* a=new int[maxn];
        int cnt=0;
        node *now=this->root;
        stack<node*>st;
        while(now||st.size())
        {
            while(now)
            {
                st.push(now);
                now=now->l;
            }
            if(st.size())
            {
                now=st.top();
                st.pop();
                a[cnt++]=now->value;
                now=now->r;
            }
        }
        return a;
    }
    /**
     后序遍历序列
     返回一个数组
     */
    int* getPostOrder()
    {
        int* a=new int[maxn];
        int cnt=0;
        node* now=this->root;
        stack<node*>st1;
        stack<node*>st2;
        while(now||st1.size())
        {
            while(now)
            {
                st1.push(now);
                st2.push(now);
                now=now->r;
            }
            if(st1.size())
            {
                now=st1.top();
                st1.pop();
                now=now->l;
            }
        }
        while(st2.size())
        {
            now=st2.top();
            st2.pop();
            a[cnt++]=now->value;
        }
        return a;
    }
};
板子

 

 

代码:

  1 #include <iostream>
  2 #include <queue>
  3 #include <stack>
  4 using namespace std;
  5 const int maxn=1e5+10;;
  6 
  7 struct node
  8 {
  9     int value;
 10     node *l,*r;
 11     
 12     node()
 13     {
 14         this->l=NULL;
 15         this->r=NULL;
 16     }
 17     
 18     node(int v)
 19     {
 20         this->value=v;
 21         this->l=NULL;
 22         this->r=NULL;
 23     }
 24 };
 25 
 26 class binarySearchTree
 27 {
 28 public:
 29     node* root;
 30     /**
 31      二叉搜索树建树
 32      @a 输入数组 a[0]为根结点
 33      @n  数组a的长度
 34      */
 35     void buildTree(int a[],int n)
 36     {
 37         this->root=new node(a[0]);
 38         for(int i=1;i<n;i++)
 39         {
 40             node *now=new node(a[i]);
 41             node *fa=this->root;
 42             while(1)
 43             {
 44                 if(now->value<fa->value)
 45                 {
 46                     if(fa->l==NULL)
 47                     {
 48                         fa->l=now;
 49                         break;
 50                     }
 51                     else fa=fa->l;
 52                 }
 53                 else
 54                 {
 55                     if(fa->r==NULL)
 56                     {
 57                         fa->r=now;
 58                         break;
 59                     }
 60                     else fa=fa->r;
 61                 }
 62             }
 63         }
 64     }
 65     /**
 66      层序遍历序列
 67      返回一个数组
 68      */
 69     int* getLevelOrder()
 70     {
 71         int* a=new int[maxn];
 72         int cnt=0;
 73         queue<node>q;
 74         q.push(*this->root);
 75         while(!q.empty())
 76         {
 77             node now=q.front();
 78             a[cnt++]=now.value;
 79             q.pop();
 80             if(now.l) q.push(*now.l);
 81             if(now.r) q.push(*now.r);
 82         }
 83         return a;
 84     }
 85     /**
 86      先序遍历序列
 87      返回一个数组
 88      */
 89     int* getPreOrder()
 90     {
 91         int* a=new int[maxn];
 92         int cnt=0;
 93         node *now=this->root;
 94         stack<node*>st;
 95         while(now||st.size())
 96         {
 97             while(now)
 98             {
 99                 a[cnt++]=now->value;
100                 st.push(now);
101                 now=now->l;
102             }
103             if(st.size())
104             {
105                 now=st.top();
106                 st.pop();
107                 now=now->r;
108             }
109         }
110         return a;
111     }
112     /**
113      中序遍历序列
114      返回一个数组
115      */
116     int* getInOrder()
117     {
118         int* a=new int[maxn];
119         int cnt=0;
120         node *now=this->root;
121         stack<node*>st;
122         while(now||st.size())
123         {
124             while(now)
125             {
126                 st.push(now);
127                 now=now->l;
128             }
129             if(st.size())
130             {
131                 now=st.top();
132                 st.pop();
133                 a[cnt++]=now->value;
134                 now=now->r;
135             }
136         }
137         return a;
138     }
139     /**
140      后序遍历序列
141      返回一个数组
142      */
143     int* getPostOrder()
144     {
145         int* a=new int[maxn];
146         int cnt=0;
147         node* now=this->root;
148         stack<node*>st1;
149         stack<node*>st2;
150         while(now||st1.size())
151         {
152             while(now)
153             {
154                 st1.push(now);
155                 st2.push(now);
156                 now=now->r;
157             }
158             if(st1.size())
159             {
160                 now=st1.top();
161                 st1.pop();
162                 now=now->l;
163             }
164         }
165         while(st2.size())
166         {
167             now=st2.top();
168             st2.pop();
169             a[cnt++]=now->value;
170         }
171         return a;
172     }
173 };
174 
175 
176 int main()
177 {
178     int a[maxn];
179     int n;
180     cin>>n;
181     for(int i=0;i<n;i++) cin>>a[i];
182     
183     binarySearchTree t;
184     t.buildTree(a,n);
185     
186     int *b=t.getPreOrder();
187     for(int i=0;i<n;i++) cout<<b[i]<<(i==n-1?'\n':' ');
188     return 0;
189 }

 

posted @ 2020-10-31 10:53  lihaji  阅读(113)  评论(0编辑  收藏  举报