CS122 Algorithms n Dt Struturs MW 11:00 m 12:15 pm, MSEC 101 Instrutor: Xio Qin Ltur 10: Binry Srh Trs n Binry Exprssion Trs Uss or Binry Trs Binry Srh Trs n Us or storing n rtriving inormtion n Insrt, lt, n srh str thn with link list n Tk vntg o logn hight n I: Stor inormtion in n orr wy (kys) A Proprty o Binry Srh Trs Trvrsing Binry Srh Trs n Th ky o th root is lrgr thn ny ky in th lt sutr n Th ky o th root is smllr thn ny ky in th right sutr n Not: Duplit kys r not llow n Thr r thr wys to trvrs inry tr n Inorr Trvrsl: Visit lt sutr; Visit root; Visit right sutr; Trvrsing Binry Srh Trs (ont.) n An Exmpl: voi inorr_print(no root) i (root!= NULL) inorr_print(root>lt_hil); ount << root>ino; inorr_print(root>right_hil); Srhing Binry Srh Trs n Lot n lmnt in inry srh tr voi srh(no root, ojt ky) i (root == NULL) rturn 1; i (root>ino == ky) rturn root>ino; ls i (ky < root>ino) srh(root>lt_hil, ky); ls srh(root>right_hil, ky); 1
Insrting n Elmnt in Binry Srh Trs n Srh or th Position in th tr whr th lmnt woul oun n Insrt th lmnt in th position n Not: nwly insrt no is l n Running tim is: O(n) th worst s O(lgn) i th tr is ln Uss or Binry Trs Binry Exprssion Trs n Binry trs r goo wy to xprss rithmti xprssions. Th lvs r oprns n th othr nos r oprtors. Th lt n right sutrs o n oprtor no rprsnt suxprssions tht must vlut or pplying th oprtor t th root o th sutr. Binry Exprssion Trs: Exmpls / Mrits o Binry Tr Form n Lt n right oprns r sy to visuliz n Co optimiztion lgorithms work with th inry tr orm o n xprssion n Simpl rursiv vlution o xprssion / Lvls Init Prn Th lvls o th nos in th tr init thir rltiv prn o vlution (w o not n prnthss to init prn). Oprtions t lowr lvls o th tr r vlut ltr thn thos t highr lvls. Th oprtion t th root is lwys th lst oprtion prorm. A Binry Exprssion Tr 3 4 2 Wht vlu os it hv? 11 ( 4 2 ) 3 = 18 12 2
Inorr Trvrsl: (A H) / (M Y) Inorr Trvrsl (ont.) tr Print son g Print lt sutr irst Print right sutr lst 13 Inorr trvrsl yils: ( ( )) ((( ) ) g) Prorr Trvrsl: / A H M Y Prorr Trvrsl (ont.) tr Print irst g Print lt sutr son Print right sutr lst 15 Prorr trvrsl yils: ( ( ( )) ( ( ( ) ) g)) Postorr Trvrsl: A H M Y / Postorr Trvrsl (ont.) tr Print lst g Print lt sutr irst Print right sutr son 17 Postorr trvrsl yils: g 3
Trvrsls n Exprssions lss ExprTr n Not tht th postorr trvrsl prous th postix rprsnttion o th xprssion. n Inorr trvrsl prous th inix rprsnttion o th xprssion. n Prorr trvrsl prous rprsnttion tht is th sm s th wy tht th progrmming lngug Lisp prosss rithmti xprssions! ExprTr ~ExprTr uil xprssion vlut ExprTr privt: root 4 2 3 lr 20 lss ExprTr puli: ExprTr ( ); // Construtor ~ExprTr ( ); // Dstrutor voi uil ( ); // uil tr rom prix xprssion voi xprssion ( ) onst; // output xprssion in ully prnthsiz inix orm lot vlut ( ) onst; // vlut xprssion voi lr ( ); // lr tr voi showstrutur ( ) onst; // isply tr privt: voi showsu( ); strut TrNo root; ; // rursiv prtnrs Eh no ontins two pointrs strut TrNo InoNo ino ; // Dt mmr TrNo lt ; // Pointr to lt hil TrNo right ; // Pointr to right hil ; NULL 6000 OPERAND 7 whihtyp oprn lt ino right 22 InoNo hs 2 orms num OpTyp OPERATOR, OPERAND ; strut InoNo OpTyp whihtyp; union hr oprtion ; int oprn ; ; OPERATOR whihtyp oprtion // ANONYMOUS union OPERAND 7 whihtyp oprn 23 int Evl ( TrNo ptr ) swith ( ptr>ino.whihtyp ) s OPERAND : rturn ptr>ino.oprn ; s OPERATOR : swith ( tr>ino.oprtion ) s : rturn ( Evl ( ptr>lt ) Evl ( ptr>right ) ) ; s : rturn ( Evl ( ptr>lt ) Evl ( ptr>right ) ) ; s : rturn ( Evl ( ptr>lt ) Evl ( ptr>right ) ) ; s : rturn ( Evl ( ptr>lt ) / Evl ( ptr>right ) ) ; 24 4
Construting n Exprssion Tr n Thr is simpl O(N) stks lgorithm to onvrt postix xprssion into n xprssion tr. n Rll w lso hv n lgorithm to onvrt n inix xprssion into postix, so w n lso onvrt n inix xprssion into n xprssion tr without iiulty (in O(N) tim). Exprssion Tr Algorithm n R th postix xprssion on symol t t tim: I th symol is n oprn, rt onno tr n push pointr to it onto th stk. I th symol is n oprtor, pop two tr pointrs T1 n T2 rom th stk, n orm nw tr whos root is th oprtor, n whos hilrn r T1 n T2. Push th nw tr pointr on th stk. Exmpl : Exmpl : Not: Ths stks r pit horizontlly. Exmpl : Exmpl : 5