04-ツリー6 Complete Binary Search Tree(30分)


A Binary Search Tree(BST)is recursively defined as a binary tree which has the following properties:
 
  • The left subtree of a node contains only node s with keys less than the node's key.   
  • The right subtree of a node contains only node s with keys greater than or equal to the node's key.   
  • Both the left and right subtrees must also be binary search trees.  A Complettee Binary Tree(CBT)is a tree that is complettely filled,with the possible exception of the bottom level,which is filled from left to right.Now given-a sequence of distinct non-negative initve ine integera unique BST can be constructed if it is required that the tree must also be a CBT.You are supposed to out put the level order troversal Sequence of this BST.Input Specifeation:Each input Prot fitable thers contest iness.cast iness.cast iness.cast.cast. N (≦1000).The n N distinctnon-negative integative keyse e e e e e e e e e given in the nextライン.All the numbes in in in inラインエリアエリアseparated by a space and are e e e e e e e e e e greater than 2000.Output Speciication:For each test case,prininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininina space、and there must beのextra space at the end of the line.Sample Input:
    10
    1 2 3 4 5 6 7 8 9 0
    
    Sample Output:
    6 3 8 1 5 7 9 0 2 4
  • テーマの実現: 
    #include
    #include
    using namespace std;
    
    const int maxn = 1005;
    int a[maxn],t[maxn]; //t[]    ,            
    int n,pos=1;//pos  a[]   , 1  
    
    //                      
    void inOrder(int index)
    {
        if(index*2 <= n)    inOrder(index*2);
        t[index] = a[pos++];//         ,           
        if(index*2+1 <= n)  inOrder(index*2+1);
    }
    
    int main()
    {
        cin>>n;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+n+1);//    a[]             
        inOrder(1);//    1      
    
        //    ,              ,          
        for(int i=1; i<=n; i++)
        {
            cout<