Sdut 3374 Data structure experiment find two: Balanced binary tree

Source: Internet
Author: User

Data structure experiment find two: Balanced binary treeTime limit:400ms Memory limit:65536kb SubmitStatisticProblem Description

Based on the given input sequence, a balanced binary tree is established to find the root of the balanced binary tree.

Input

Enter a set of test data. The 1th line of the data gives a positive integer n (n <=), n denotes the number of elements of the input sequence, and the 2nd line gives n positive integers, establishing a balanced binary tree in the order in which the data is given.

Output

Outputs the root of the balanced binary tree.

Example Input
588 70 61) 96 120
Example Output
70
DQE:

The node balance factor is changed to the node tree depth, and the rotation operation remembers to update the node depth.
1#include <iostream>2#include <cstdio>3 using namespacestd;4 /*5 Full test Data6 inch7 Ten8 3 7 99  outTen 7 3 9 One 3 7 9 A */ - structBT - { the     intD; -     intx; -BT *lt,*RT; - }; +  - intDP (BT *R) + { A     returnR?r->d:0; at } - voidRFD (BT *R) - { -     intLD=DP (R-&GT;LT) +1, RD=DP (R-&GT;RT) +1; -R->d=ld>rd?Ld:rd; - } in  - voidLL (BT *&R) to { + //printf ("ll#%d#%d*%d", R->X,DP (R->LT), DP (R->RT)); -BT *a=r,*b=a->lt,*br=b->RT; theR=b;//use references to modify superiors to point to *b->rt=A; $a->lt=BR;Panax Notoginseng     //depth re-set - RfD (a); the } + voidRR (BT *&R) A { the //printf ("rr#%d#%d*%d", R->X,DP (R->RT), DP (R->LT)); +BT *a=r,*b=a->rt,*bl=b->lt; -R=b; $b->lt=A; $a->rt=BL; -  - RfD (a); the } - voidLR (BT *&R)Wuyi { theRR (r->LT); -RfD (r->LT); Wu LL (r); - } About voidRL (BT *&R) $ { -LL (r->RT); -RfD (r->RT); - RR (r); A } +  the voidInsert (BT *&root,inte) - { $     if(!root) the     { theBT *r=NewBT; ther->x=e; theR->d=1; -r->lt=r->rt=NULL; inroot=R; the     } the     Else About     { the         if(e<root->x) the         { theInsert (root->lt,e); +             if(DP (ROOT-&GT;LT)-DP (ROOT-&GT;RT) >1) -             { the                 if(e<root->lt->x)Bayi LL (root); the                 Else the LR (root); -             } -         } the         Else if(e>root->x) the         { theInsert (root->rt,e); the             if(DP (ROOT-&GT;RT)-DP (ROOT-&GT;LT) >1) -             { the                 if(e>root->rt->x) the RR (root); the                 Else94 RL (root); the             } the         } the         //Update Root Depth98 RfD (root); About     } - }101 102 voidXout (BT *R)103{if(r) {printf ("%d", r->x); Xout (R-&GT;LT); Xout (r->RT);}}104 voidMout (BT *R) the{if(r) {mout (R-&GT;LT);p rintf ("%d", r->x); Mout (r->RT);}}106 intMain ()107 {108BT *root=NULL;109     intn,i; thescanf"%d",&n);111      while(n--) the     {113scanf"%d",&i); the Insert (root,i); the     } the     /*117 Putchar (' \ n ');118 xout (root);119 Putchar (' \ n '); - mout (root);121     */122printf"%d\n",root->x);123     return 0;124 } the 126 /***************************************************127 User Name: * * * - result:accepted129 Take time:0ms the Take memory:156kb131 Submit time:2016-11-29 16:23:30 the ****************************************************/

Sdut 3374 Data structure experiment find two: Balanced binary 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.