Tee Tee Lectues : oontee Kuatachue Room no. Kitawan Siiboon Room no. Text : ata Stuctues & lgoithm nalysis in, ++, Mak llen Weiss, ddison Wesley. Tee efinitions. inay Tee Tavesals inay Seach Tee Repesentations pplication : Expession Tee. VL Tee. Which Repesentions?. n-ay Tee. eneic Tee. Multiway Seach Tee 8. -Tees...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee teecan be empty with no nodes called thenull o empty teeo a tee is a stuctue consisting of one node called the ootand one o moe subtees. Node in a tee must be disjoint. t must not be in common in oot o subtees. Root = fathe (paent) of subtee s oot. Subtee s oot = son(child child) of his fathe. Tee efinitions The name of the tee usually called by its oot. Root node R fathe paent of,, R,, : E subtee T son, child of R T H J Kisnot disjoint (be in subtee & subtee J) R is not a tee. K T Leaf ntenal Siblings and Paent and hild Leaf node (extenal, oute, teminal) node with no son anch node(intenal, inne, inode (fo shot) node leaf Siblings (bothes) node with same fathe and Paent fathe of fathe and hild son of son (fo shot)) E J K L H...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee
epth = Level = 0 Path, Path Length, epth, Height Path (fom n to d)sequence of nodes and edges connecting a node nwith a descendant d n tee, only path fom node to node Heightof node = longest path length fomnode to leaf epth = Level = epth = Level = epth = Level = epth = Level = E Path,, length = epth(level level) of node = path length fom oot to node epth of = Path length= # edge in path Height of = Tee Height = Root Heigth = Empty Tee Height = -...ก KMTL 00 ata Stuctues & lgoithms : Tee J K L H = Heightof =? ncesto & ecendent ncesto fathe of ancesto = ancesto of if has path fom to ecendent ก son of decendent = Popencesto of if = Popeecendent of if E H J K L...ก KMTL 00 ata Stuctues & lgoithms : Tee Tee Examples Tee Examples...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 8
Thai Royal amily Tee Tee. Tee efinitions. inay Tee Tavesals inay Seach Tee Repesentations pplication : Expession Tee. VL Tee. Which Repesentions?. n-ay Tee. eneic Tee. Multiway Seach Tee 8. -Tees...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 0 inay Tee inay Tee bi = has at most subtees (has 0, o subtees) q p Pefect inay Tee Pefect inay Tee (mbiguously also called omple inay Tee ) Evey level is completely filled epth = Level = 0 = L = = L = = L = 8 0 # Nodes in each level : = 0 = = 8= = L = 8 0 8 0 = = H Tee Height Leave epth H = 0 8 Nomally not daw banch diection null pt s N =จ านวน node ใน pefect binay tee N= 0 + +...+ H N n+ i N= H+ =, i= 0 - N= ( H extenal) + ( H - intenal) H= = log (N+) O( log (N)) <> Pefect inay Tee of N nodes: H= log (N+) N= H+ -...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee
omplete inaytee omple inay Tee Evey level, except possibly the last, is completely filled & ll nodes ae as fa left as possible. 8 0 omplete binay tee 8 0 Without node : Not a complete binay tee...ก KMTL 00 ata Stuctues & lgoithms : Tee ull (pope, stictly, -tee) inaytee ull Node = node with childen ull binay tee node = full node o leaf # extenal = # intenal + edge top edge bottom Poof : edge top = #intenal edge bottom = #extenal + #intenal (oot) H J K edge bottom = edge top #extenal + #intenal = #intenal #extenal = #intenal + How good? Ty add evey leaf with sons. id you see? E edge top...ก KMTL 00 ata Stuctues & lgoithms : Tee L M efoe : N = Max Seach Path = Tee s Height Q N R P dd = 8 moe Max Seach Path + Tee s Height ull omplete Pefect How can 0 nodes in a binay tee has shotest seach path? 8 0 Linked list : N Wost ase epth omplete inay Tee : H = = log (N+) 8 0 deal tee How much diffeent N vs log N? Log,000,000 < 0...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee
inay Seach Tee Why Tee? inay SeachTee : fo evey node left decendents < ight decendents 8 < 8 < 0 mplicit (Sequential ) ay nsetion eletion Poblems Tee Tee Seach O(log n) Linea Linked list Linea Seach O(n) UNX ile system of seveal popula OS...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 8 Tee inay Tee Tavesals. Tee efinitions. inay Tee Tavesals inay Seach Tee Repesentations pplication : Expession Tee. VL Tee. Which Repesentions?. n-ay Tee. eneic Tee. Multiway Seach Tee 8. -Tees Tee Tavesal การไปเย ยม (visit เช นการพ มพ การ update ข อม ล) ท ก node node ละ คร ง อย างม ระบบ แบ งตามล าด บของการ visit eadth ist (Level Ode) จากoot ไปด านข างก อน. ล กคนแรก ล กคนท ล กคนท...จนหมดล กท กคน. ท าข อ. ก บท กคนท ไปเย ยมมาตามล าด บ epth-ist Ode จาก oot ไปด านล กก อนไปด านข าง ล กคนแรก แล วไปหลานคนแรกก อนไปท ล กคนท...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 0
eadth - ist (Level Ode) epth ist Ode eadth ist จากoot ไปด านข างก อน ใช queueช วยในการหา ไล ไปท ละ level จ งเร ยกอ กอย างหน งว า Level Ode epth-ist Ode จาก oot ไปด านล กก อนไปด านข าง ใช stack ช วยในการหา queue enq ( oot ) while( notemptyq ){ n = deq () E if ( n->left ) enq ( n->left ) H if ( n->ight ) enq ( n-> ight ) E H แบ งเป น แบบ ข นก บการวางต าแหน งการ visit oot ไว ท ใด node (Symmetic Ode). inode(leftsubtee). visit_oot. inode(ightsubtee) Peode. visit_oot. peode(leftsubtee). peode(ightsubtee) Postode. postode(leftsubtee). postode(ightsubtee). visit_oot...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee node (Symmetic Ode) Peode inode( oot ) { if (oot){ inode(oot->left) visit(oot) inode(oot->ight) E H pe ข ก in ค post peode( oot ) { if (oot){ visit(oot) peode(oot->left) peode(oot->ight) E H pe ข ก in ค post E H E H...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee
Postode Test You Self postode( oot ) { if (oot){ postode(oot->left) postode(oot->ight) visit(oot) pe ข ก in ค post pe ข ก in E ค post H inay Seach Tee 8 0 node: LootR. inode(l). visit_oot. inode(r) Postode : LRoot Peode: ootlr E J K L H node E J K H L 8 0 sscending Ode! H E Peode E J H K L 8 0 Postode E J K L H 0 8...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee Tee inay SeachTee. Tee efinitions. inay Tee Tavesals inay Seach Tee Repesentations pplication : Expession Tee. VL Tee. Which Repesentions?. n-ay Tee. eneic Tee. Multiway Seach Tee 8. -Tees inay SeachTee : fo evey node < left decendents < ight decendents 8 < 8 < 0...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 8
nsetion elete a : Leaf, Node with only child < < < 8 0 8 0 8 nsets : 8 0 elete Leaf 0 can be deleted ight away, still be a binay seach tee elete a node with only one child elete, eplace a subtee at the deleted node...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 0 elete a Node with oth hilden : Lazy eletion elete a Node with oth hilden : Lazy eletion 8 0 8 8 0 nceases tee height. => Lazy eletion 8 0 0 8 0 8 0 8 nceases tee height. => Lazy eletion 0 8 0 elete Whee can we put tee, and tee? elete an eplace only at the deleted node. Let choose. Whee to put tee? an eplace only at the deleted node. t s a binay seach tee! Whee is it s place? f we choose. Whee is s place?...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee
eletion : Using node Successo / Pedecesso 8 0 8 0 = inode successo of Replace with with data elete instead as child deletion. 8 0 8 0 Not inceasing tee height!!! = inode pedecesso of. Replace with data. elete instead as child deletion. 8 0 8 0...ก KMTL 00 ata Stuctues & lgoithms : Tee 8 0 8 0. Tee efinitions. inay Tee Tavesals inay Seach Tee Repesentations Tee pplication : Expession Tee. VL Tee. Which Repesentions?. n-ay Tee. eneic Tee. Multiway Seach Tee 8. -Tees...ก KMTL 00 ata Stuctues & lgoithms : Tee inay Tee Repesentations (Sequential) mplicit ay. ynamic typedef int T; stuct node{ T data; stuct node *left, *ight; ; typedef stuct node node;. Linked ay. Sequential (mplicit) ay oot left data ight fathe 0 - - - - 0 - - - 8 0 0 L M L M 0 8 0 H 8 0 E J K L M N O 0 E H J K L M N O 0 8 0 8 0 0 L M L M 0 8 0 E H J K L M N O 8 0 E H J K L M N O 0 8 0 8 0 L M L M 0 8 0...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee
8 0 (Sequential) mplicit ay 0 L M L M 0 8 0 8 0 L M L M 0 8 0 Stat at index 0 Stat at index Whee is the oot? 0 The node at index i s left son? i + i ight son? i + i + fathe? (i -) div i div So good! No memoy fo link! Easy to calculate! What happen if we have only node at indices : 0,,,? What shape of tee should be best fo sequential aay? Tee. Tee efinitions. inay Tee Tavesals inay Seach Tee Repesentations pplication : Expession Tee. VL Tee. Which Repesentions?. n-ay Tee. eneic Tee. Multiway Seach Tee 8. -Tees...ก KMTL 00 ata Stuctues & lgoithms : Tee...ก KMTL 00 ata Stuctues & lgoithms : Tee 8 Example : Expession Tee onstucting an Expession Tee + stack S a b input postfix fom a b + c d e + * * + * a + c d e b a * + g b c * f d e (a + b * c) + ((d * e + f) * g)...ก KMTL 00 ata Stuctues & lgoithms : Tee + c + a b d e + a b * c + d e * + * a b c + d e Scan input fom left to ight. Opeand eate an opeand node and push to the stack Opeato eate an opeato node. Pop opeands to be its childen. Push to the stack....ก KMTL 00 ata Stuctues & lgoithms : Tee 0