Hdu 39,992 Fork Sorting tree

Source: Internet
Author: User

Thought for a while, YY made a conclusion:

Obviously, for all nodes in any subtree, the root node must be the first one inserted, and after the root node is inserted, all the other nodes of the subtree are split into two.

Greater than the root node weights and small root node weights, these two parts will not affect each other, can be converted into a new two smaller sub-tree construction process.

In the construction process, in order to not destroy the original BST structure, it must meet:

The insertion of any node will not be earlier than its ancestor, otherwise it will be a different BST.

If you do not understand, please recall the insertion process of BST.

Thus found: to meet this law as long as the root of the first traversal can, and to make the dictionary order is minimal, as long as the first order traversal is the answer.

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 intN, CNT;6 7 structNode8 {9Node * ch[2];Ten     intv; One     BOOLcmpintx) A     { -         if(x = = v)return-1; -         returnX < v?0:1; the     } - }; -  -Node *Root; +  - voidInsert (Node * & O,intx) + { A     if(O = =NULL) at     { -o =NewNode (); -o->ch[0] = o->ch[1] =NULL; -O->v =x; -         return ; -     } in     intD = o->CMP (x); -     if(d = =-1)return ; toInsert (o->Ch[d], x); + } -  the /* * node * FIND (Node * o, int x) $ {Panax Notoginseng if (o = = null) return null;  - int d = o->cmp (x); the if (d = =-1) return o;  + return Find (O->ch[d], x); A } the  + void Remove (Node * & O, int x) - { $ if (o = = NULL) return; $ int d = o->cmp (x); - if (d = =-1) -     { the if (o->ch[0] = = NULL) -         {Wuyi Node * tmp = O; the o = o->ch[1]; - Delete tmp; Wu         } - else if (o->ch[1] = = NULL) About         { $ Node * tmp = O; - o = o->ch[0]; - Delete tmp; -         } A Else +         { the Node * p = o, * q = o->ch[0]; - While (q->ch[1]! = NULL) $             { the p = q; the q = q->ch[1]; the             } the o->v = q->v; - if (P! = O) in             { the p->ch[1] = q->ch[0]; the Delete q; About             } the Else the             { the p->ch[0] = q->ch[0]; + Delete q; -             } the         }Bayi     } the Else the     { - Remove (O->ch[d], x); -     } the } the */ the  the void  Free(Node *o) - { the     if(O! =NULL) the     { the          Free(o->ch[0]);94          Free(o->ch[1]); the         Deleteo; the     } the }98  About voidVisit (Node *o) - {101cnt++;102printf"%d", o->v);103     if(cnt! = N) Putchar (' ');104     ElsePutchar ('\ n');  the }106 107 voidPreorder (Node *o)108 {109     if(o) the     {111 visit (o); thePreorder (o->ch[0]);113Preorder (o->ch[1]); the     } the } the 117 /*118 void Inorder (Node * o)119 { - if (o)121     {122 inorder (o->ch[0]);123 visit (o);124 inorder (o->ch[1]); the     }126 }127  - void Postorder (Node * o)129 { the if (o)131     { the Postorder (o->ch[0]);133 Postorder (o->ch[1]);134 visit (o);135     }136 }137 */138 139 intMain () $ {141      while(SCANF ("%d", &n)! =EOF)142     {143Root =NULL;144CNT =0;145          for(inti =0; I < n; i++ )146         {147             inttmp;148scanf"%d", &tmp);149 Insert (root, tmp); Max         }151 preorder (root); the          Free(root); 153     }154     return 0;155}

Hdu 39,992 Fork Sorting tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.