Dt Strutures (INE2011) Eletronis nd Communition Engineering Hnyng University Hewoon Nm Leture 7 INE2011 Dt Strutures 1
Binry Tree Trversl Mny inry tree opertions re done y perorming trversl o the inry tree. In trversl, eh element o the inry tree is visited extly one. Binry tree trversl methods Pre-order In-order Post-order Level order INE2011 Dt Strutures 2
Binry Tree Trversl Trversing eh node nd sutree L : moving let V : visit R : moving right Six possile omintions o trversl LVR, LRV, VLR, VRL, RVL, RLV Trverse let eore right VLR : pre-order LVR : in-order LRV : post-order INE2011 Dt Strutures 3
Binry Tree Trversl Pre-order exmple d e g h i d g h e i INE2011 Dt Strutures 4
Binry Tree Trversl In-order exmple d e g h i g d h e i INE2011 Dt Strutures 5
Binry Tree Trversl Post-order exmple d e g h i g h d i e INE2011 Dt Strutures 6
Binry Tree Trversl Pre-order, in-order, post-order void PreOrder(TreeNode *t) { i (t!= NULL) { Visit(t); PreOrder(t->letChild); PreOrder(t->rightChild); } } void InOrder(TreeNode *t) { i (t!= NULL) { InOrder(t->letChild); Visit(t); InOrder(t->rightChild); } } void PostOrder(TreeNode *t) { i (t!= NULL) { PostOrder(t->letChild); PostOrder(t->rightChild); Visit(t); } } INE2011 Dt Strutures 7
Binry Tree Trversl Level order exmple d e g h i d e g h i INE2011 Dt Strutures 8
Threded Binry Trees Threds Mny null pointers in inry tree Reple null pointers with the in-order predeessor or the inorder suessor d e d e g h i g h i g d h e i INE2011 Dt Strutures 9
Threded Binry Trees Clss or threded node lss treenode { privte: dt int dt; let hild treenode *let; ool letthred; treenode *right; ool rightthred; right hild letthred rightthred d e }; g h i - I letthred is true, *let ontins thred, otherwise pointer. - Sme or rightthred. g d h e i INE2011 Dt Strutures 10
Binry Tree Constrution Cn you onstrut the inry tree rom given trversl sequene? When trversl sequene hs more thn one element, the inry tree is not uniquely deined Thereore, the tree rom whih the sequene ws otined nnot e reonstruted uniquely Pre-order = Cn you onstrut the inry tree, given two trversl sequenes? INE2011 Dt Strutures 11
Binry Tree Constrution Pre-order = In-order = d g h e i g d h e i Sn the pre-order let to right using the in-order to seprte let nd right sutrees Step 1 is the root o the tree. gdhei re in the let sutree. re in the right sutree. g d h e i INE2011 Dt Strutures 12
Binry Tree Constrution Pre-order = In-order = d g h e i g d h e i Step 2 is the next root o the tree. gdh re in the let sutree. ei re in the right sutree. g d h e i INE2011 Dt Strutures 13
Binry Tree Constrution Pre-order = In-order = d g h e i g d h e i Step 3 d is the next root o the tree. g is in the let sutree. h is in the right sutree. d e i g h INE2011 Dt Strutures 14
Binry Tree Constrution Pre-order = In-order = d g h e i g d h e i Step 4 e is the next root o the tree. i is in the right sutree. d e g h i INE2011 Dt Strutures 15
Binry Tree Constrution Pre-order = In-order = d g h e i g d h e i Step 5 is the next root o the tree. is in the let sutree. d e g h i INE2011 Dt Strutures 16
Binry Tree Constrution Pre-order = In-order = d g h e i g d h e i Step 6 is the next root o the tree. is in the right sutree. d e g h i INE2011 Dt Strutures 17
Binry Tree Constrution Post-order = g h d i e In-order = g d h e i Sn the pre-order let to right using the in-order to seprte let nd right sutrees Step 1 is the root o the tree. gdhei re in the let sutree. re in the right sutree. g d h e i Step 2 6 Similr to the previous exmple with pre-order nd in-order INE2011 Dt Strutures 18
Binry Tree Constrution Level order = In-order = d e g h i g d h e i Sn the pre-order let to right using the in-order to seprte let nd right sutrees Step 1 is the root o the tree. gdhei re in the let sutree. re in the right sutree. g d h e i Step 2 6 Similr to the previous exmple with pre-order nd in-order INE2011 Dt Strutures 19
Joining nd Splitting Binry Tree Joining(Grting) nd Splitting(Pruning) Grting s Pruning p q e s e p q INE2011 Dt Strutures
Binry Serh Tree Ditionry opertions IsEmpty( ) Get(key) Insert(key, vlue) Delete(key) Treenode Eh node hs (key, vlue) pir 10 8 12 d e 15 17 20 INE2011 Dt Strutures lss treenode { privte: int key; hr vlue; treenode *let; treenode *right; }; 5 9 13 g h i 19 21
Binry Serh Tree Asend( ) Do n in-order trversl 15 10 20 8 12 d e 17 5 9 13 g h i 19 g d h e i INE2011 Dt Strutures 22
Binry Serh Tree Get(key) Serh the node with the key (input) Return the vlue o the node 15 Get(12) returns e 10 8 12 d e 5 9 13 g h i 17 20 19 INE2011 Dt Strutures 23
Binry Serh Tree Insert(key, vlue) Insert node t proper position sed on the key (input) 15 Insert(23, k) 10 8 12 d e 5 9 13 g h i 17 20 19 23 k INE2011 Dt Strutures 24
Binry Serh Tree Delete(key) No element with the delete key Element is in le Element is in degree 1 node Element is in degree 2 node INE2011 Dt Strutures 25
Binry Serh Tree Delete(key) Element is in le 15 Delete(9) 10 20 8 12 d e 17 5 9 13 g h i 19 INE2011 Dt Strutures 26
Binry Serh Tree Delete(key) Element is in degree 1 node 15 Delete(20) 10 20 8 12 d e 17 5 9 13 g h i 19 INE2011 Dt Strutures 27
Binry Serh Tree Delete(key) Element is in degree 1 node 15 Delete(12) 10 20 8 12 d e 17 5 9 13 g h i 19 INE2011 Dt Strutures 28
Binry Serh Tree Delete(key) Element is in degree 2 node Exmple 1 15 Delete(10) 10 20 Find the lrgest key in the let sutree Or the smllest key in the right sutree 7 12 d e 17 5 9 13 g h i 8 m 19 INE2011 Dt Strutures 29
Binry Serh Tree Delete(key) Element is in degree 2 node Exmple 1 15 Delete(10) 10 20 Find the lrgest key in the let sutree Or the smllest key in the right sutree 7 12 d e 17 5 9 13 g h i 8 m 19 INE2011 Dt Strutures 30
Binry Serh Tree Delete(key) Element is in degree 2 node Exmple 1 15 Delete(10) 9 h 20 7 12 d e 17 5 g 8 m 13 i 19 INE2011 Dt Strutures 31
Binry Serh Tree Delete(key) Element is in degree 2 node Exmple 2 15 Delete(15) 10 20 7 12 d e 17 5 9 13 g h i 19 INE2011 Dt Strutures 8 m 32
Binry Serh Tree Delete(key) Element is in degree 2 node Exmple 2 15 Delete(15) 10 20 Find the lrgest key in the let sutree 7 12 d e 17 5 9 13 g h i 19 8 m INE2011 Dt Strutures 33