Chptr 10 BINARY TREES 1. Gnrl Binry Trs 2. Binry Srch Trs 3. Builing Binry Srch Tr 4. Hight Blnc: AVL Trs 5. Sply Trs: A Slf-Ajusting Dt Structur Outlin Trnsp. 1, Chptr 10, Binry Trs 243 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Binry Trs DEFINITION A inry tr is ithr mpty, or it consists of no cll th root togthr with two inry trs cll th lft sutr n th right sutr of th root. Thr is on mpty inry tr, on inry tr with on no, n two with two nos: n Ths r iffrnt from ch othr. W nvr rw ny prt of inry tr to look lik Th inry trs with thr nos r: Binry Trs Trnsp. 2, Sct. 10.1, Introuction to Binry Trs 244 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Trvrsl of Binry Trs At givn no thr r thr tsks to o in som orr: Visit th no itslf (V); trvrs its lft sutr (L); trvrs its right sutr (R). Thr r six wys to rrng ths tsks: VLR LVR LRV VRL RVL RLV. By stnr convntion, ths r ruc to thr y consiring only th wys in which th lft sutr is trvrs for th right. VLR LVR LRV prorr inorr postorr Ths thr nms r chosn ccoring to th stp t which th givn no is visit. With prorr trvrsl w first visit no, thn trvrs its lft sutr, n thn trvrs its right sutr. With inorr trvrsl w first trvrs th lft sutr, thn visit th no, n thn trvrs its right sutr. With postorr trvrsl w first trvrs th lft sutr, thn trvrs th right sutr, n finlly visit th no. Trvrsl of Binry Trs Trnsp. 3, Sct. 10.1, Introuction to Binry Trs 245 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Exprssion Trs + log! x n + log x n! or < < c c ( c) ( < ) or (c < ) Exprssion: + log x n! ( c) ( < ) or (c < ) Prorr : + log x! n c or < < c Inorr : + log x n! c < or c < Postorr : + x log n! c < c < or Exprssion Trs Trnsp. 4, Sct. 10.1, Introuction to Binry Trs 246 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
:= / + 0.5 2 c 4 x := ( + ( 2 4 c) 0.5)/(2 ) 2 x Exprssion tr of th qurtic formul Trnsp. 5, Sct. 10.1, Introuction to Binry Trs 247 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Link Binry Trs Comprison tr: Jim Dot Ron Amy Guy Ky Tim Ann Ev Jn Jon Kim Roy Tom Link implmnttion of inry tr: Jim Dot Ron Amy Guy Ky Tim Ann Ev Jn Jon Kim Roy Tom Link Binry Trs Trnsp. 6, Sct. 10.1, Introuction to Binry Trs 248 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Link Binry Tr Spcifictions Binry tr clss: tmplt <clss Entry> clss Binry tr { pulic: // A mthos hr. protct: // A uxiliry function prototyps hr. Binry no<entry> *root; ; Binry no clss: tmplt <clss Entry> struct Binry no { // t mmrs: Entry t; Binry no<entry> *lft; Binry no<entry> *right; // constructors: Binry no( ); Binry no(const Entry &x); ; Link Binry Tr Spcifictions Trnsp. 7, Sct. 10.1, Introuction to Binry Trs 249 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Constructor: tmplt <clss Entry> Binry tr<entry> ::Binry tr( ) /* Post: An mpty inry tr hs n crt. */ { root = NULL; Empty: tmplt <clss Entry> ool Binry tr<entry> ::mpty( ) const /* Post: A rsult of tru is rturn if th inry tr is mpty. Othrwis, fls is rturn. */ { rturn root == NULL; Binry tr mthos Trnsp. 8, Sct. 10.1, Introuction to Binry Trs 250 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Inorr trvrsl: tmplt <clss Entry> voi Binry tr<entry> ::inorr(voi (*visit)(entry &)) /* Post: Th tr hs n n trvrs in inorr squnc. Uss: Th function rcursiv inorr */ { rcursiv inorr(root, visit); Most Binry tr mthos scri y rcursiv procsss cn implmnt y clling n uxiliry rcursiv function tht pplis to sutrs. tmplt <clss Entry> voi Binry tr<entry> :: rcursiv inorr(binry no<entry> *su root, voi (*visit)(entry &)) /* Pr: su root is ithr NULL or points to sutr of th Binry tr. Post: Th sutr hs n n trvrs in inorr squnc. Uss: Th function rcursiv inorr rcursivly */ { if (su root!= NULL) { rcursiv inorr(su root->lft, visit); (*visit)(su root->t); rcursiv inorr(su root->right, visit); Binry tr mthos Trnsp. 9, Sct. 10.1, Introuction to Binry Trs 251 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Binry Tr Clss Spcifiction tmplt <clss Entry> clss Binry tr { pulic: Binry tr( ); ool mpty( ) const; voi prorr(voi (*visit)(entry &)); voi inorr(voi (*visit)(entry &)); voi postorr(voi (*visit)(entry &)); int siz( ) const; voi clr( ); int hight( ) const; voi insrt(const Entry &); Binry tr (const Binry tr<entry> &originl); Binry tr & oprtor = (const Binry tr<entry> &originl); Binry tr( ); protct: // A uxiliry function prototyps hr. Binry no<entry> *root; ; Binry Tr Clss Spcifiction Trnsp. 10, Sct. 10.1, Introuction to Binry Trs 252 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Binry Srch Trs Cn w fin n implmnttion for orr lists in which w cn srch quickly (s with inry srch on contiguous list) n in which w cn mk insrtions n ltions quickly (s with link list)? DEFINITION A inry srch tr is inry tr tht is ithr mpty or in which th t ntry of vry no hs ky n stisfis th conitions: 1. Th ky of th lft chil of no (if it xists) is lss thn th ky of its prnt no. 2. Th ky of th right chil of no (if it xists) is grtr thn th ky of its prnt no. 3. Th lft n right sutrs of th root r gin inry srch trs. W lwys rquir: No two ntris in inry srch tr my hv qul kys. W cn rgr inry srch trs s nw ADT. W my rgr inry srch trs s spciliztion of inry trs. W my stuy inry srch trs s nw implmnttion of th ADT orr list. Binry Srch Trs Trnsp. 11, Sct. 10.2, Binry Srch Trs 253 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Th Binry Srch Tr Clss Th inry srch tr clss will riv from th inry tr clss; hnc ll inry tr mthos r inhrit. tmplt <clss Rcor> clss Srch tr: pulic Binry tr<rcor> { pulic: Error co insrt(const Rcor &nw t); Error co rmov(const Rcor &ol t); Error co tr srch(rcor &trgt) const; privt: // A uxiliry function prototyps hr. ; Th inhrit mthos inclu th constructors, th structor, clr, mpty, siz, hight, n th trvrsls prorr, inorr, n postorr. A inry srch tr lso mits spciliz mthos cll insrt, rmov, n tr srch. Th clss Rcor hs th hvior outlin in Chptr 7: Ech Rcor is ssocit with Ky. Th kys cn compr with th usul comprison oprtors. By csting rcors to thir corrsponing kys, th comprison oprtors pply to rcors s wll s to kys. Th Binry Srch Tr Clss Trnsp. 12, Sct. 10.2, Binry Srch Trs 254 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Tr Srch Error co Srch tr<rcor> :: tr srch(rcor &trgt) const; Post: If thr is n ntry in th tr whos ky mtchs tht in trgt, th prmtr trgt is rplc y th corrsponing rcor from th tr n co of succss is rturn. Othrwis co of not prsnt is rturn. This mtho will oftn cll with prmtr trgt tht contins only ky vlu. Th mtho will fill trgt with th complt t longing to ny corrsponing Rcor in th tr. To srch for th trgt, w first compr it with th ntry t th root of th tr. If thir kys mtch, thn w r finish. Othrwis, w go to th lft sutr or right sutr s pproprit n rpt th srch in tht sutr. W progrm this procss y clling n uxiliry rcursiv function. Th procss trmints whn it ithr fins th trgt or hits n mpty sutr. Th uxiliry srch function rturns pointr to th no tht contins th trgt ck to th clling progrm. Sinc it is privt in th clss, this pointr mnipultion will not compromis tr ncpsultion. Binry no<rcor> *Srch tr<rcor> ::srch for no( Binry no<rcor>* su root, const Rcor &trgt) const; Pr: su root is NULL or points to sutr of Srch tr Post: If th ky of trgt is not in th sutr, rsult of NULL is rturn. Othrwis, pointr to th sutr no contining th trgt is rturn. Tr Srch Trnsp. 13, Sct. 10.2, Binry Srch Trs 255 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Rcursiv uxiliry function: tmplt <clss Rcor> Binry no<rcor> *Srch tr<rcor> ::srch for no( Binry no<rcor>* su root, const Rcor &trgt) const { if (su root == NULL su root->t == trgt) rturn su root; ls if (su root->t < trgt) rturn srch for no(su root->right, trgt); ls rturn srch for no(su root->lft, trgt); Nonrcursiv vrsion: tmplt <clss Rcor> Binry no<rcor> *Srch tr<rcor> ::srch for no( Binry no<rcor> *su root, const Rcor &trgt) const { whil (su root!= NULL && su root->t!= trgt) if (su root->t < trgt) su root = su root->right; ls su root = su root->lft; rturn su root; Auxiliry functions, tr srch Trnsp. 14, Sct. 10.2, Binry Srch Trs 256 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Pulic mtho for tr srch: tmplt <clss Rcor> Error co Srch tr<rcor> :: tr srch(rcor &trgt) const /* Post: If thr is n ntry in th tr whos ky mtchs tht in trgt, th prmtr trgt is rplc y th corrsponing rcor from th tr n co of succss is rturn. Othrwis co of not prsnt is rturn. Uss: function srch for no */ { Error co rsult = succss; Binry no<rcor> *foun = srch for no(root, trgt); if (foun == NULL) rsult = not prsnt; ls trgt = foun->t; rturn rsult; Tr srch functions Trnsp. 15, Sct. 10.2, Binry Srch Trs 257 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Binry Srch Trs with th Sm Kys f f c g g () c () g g c f f c c (c) f () g () Binry Srch Trs with th Sm Kys Trnsp. 16, Sct. 10.2, Binry Srch Trs 258 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Anlysis of Tr Srch Drw th comprison tr for inry srch (on n orr list). Binry srch on th list os xctly th sm comprisons s tr srch will o if it is ppli to th comprison tr. By Sction 7.4, inry srch prforms O(log n) comprisons for list of lngth n. This prformnc is xcllnt in comprison to othr mthos, sinc log n grows vry slowly s n incrss. Th sm kys my uilt into inry srch trs of mny iffrnt shps. If inry srch tr is nrly compltly lnc ( ushy ), thn tr srch on tr with n vrtics will lso o O(log n) comprisons of kys. If th tr gnrts into long chin, thn tr srch coms th sm s squntil srch, oing (n) comprsions on n vrtics. This is th worst cs for tr srch. Th numr of vrtics twn th root n th trgt, inclusiv, is th numr of comprisons tht must on to fin th trgt. Th ushir th tr, th smllr th numr of comprisons tht will usully n to on. It is oftn not possil to prict (in vnc of uiling it) wht shp of inry srch tr will occur. In prctic, if th kys r uilt into inry srch tr in rnom orr, thn it is xtrmly unlikly tht inry srch tr gnrts ly; tr srch usully prforms lmost s wll s inry srch. Anlysis of Tr Srch Trnsp. 17, Sct. 10.2, Binry Srch Trs 259 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Insrtion into Binry Srch Tr Error co Srch tr<rcor> :: insrt(const Rcor &nw t); Post: If Rcor with ky mtching tht of nw t lry longs to th Srch tr co of uplict rror is rturn. Othrwis, th Rcor nw t is insrt into th tr in such wy tht th proprtis of inry srch tr r prsrv, n co of succss is rturn. () Insrt () Insrt (c) Insrt f f () Insrt f () Insrt f f g g c (f) Insrt g (g) Insrt c Insrtion into Binry Srch Tr Trnsp. 18, Sct. 10.2, Binry Srch Trs 260 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458
Mtho for Insrtion tmplt <clss Rcor> Error co Srch tr<rcor> ::insrt(const Rcor &nw t) { rturn srch n insrt(root, nw t); tmplt <clss Rcor> Error co Srch tr<rcor> ::srch n insrt( Binry no<rcor> * &su root, const Rcor &nw t) { if (su root == NULL) { su root = nw Binry no<rcor>(nw t); rturn succss; ls if (nw t < su root->t) rturn srch n insrt(su root->lft, nw t); ls if (nw t > su root->t) rturn srch n insrt(su root->right, nw t); ls rturn uplict rror; Th mtho insrt cn usully insrt nw no into rnom inry srch tr with n nos in O(log n) stps. It is possil, ut xtrmly unlikly, tht rnom tr my gnrt so tht insrtions rquir s mny s n stps. If th kys r insrt in sort orr into n mpty tr, howvr, this gnrt cs will occur. Mtho for Insrtion Trnsp. 19, Sct. 10.2, Binry Srch Trs 261 1999 Prntic-Hll, Inc., Uppr Sl Rivr, N.J. 07458