binarysearchtr adttedtoses
play

BinarySearchTr ADTtedtoSes : unordered collection of values Iobjects - PowerPoint PPT Presentation

BinarySearchTr ADTtedtoSes : unordered collection of values Iobjects - Set insert H ) ; member lx ) ; remove 4) ; empty D; sized , dear D; ) ) ( find LX - We call the values we store keys , - We assume the keys are from some ordered sets ie , for


  1. BinarySearchTr

  2. ADTtedtoSes : unordered collection of values Iobjects - Set insert H ) ; member lx ) ; remove 4) ; empty D; sized , dear D; ) ) ( find LX - We call the values we store keys , - We assume the keys are from some ordered sets ie , for any two keys ix. yes , we have one of exactly , x=y , ya x day - Want implementations where all operations efficient Hast possible as are as - Compare with our list implementations some operations where we could have fast , but others slow " fast " ? Q : What will count as

  3. someRelatedlontainerADTSMottiset.li he set , but with multiplicities ( aka beg ) Count ( x ) : unordered collection of Lkey , take pairs , Map associating at most one valve with each key . put Ikey , ral ) 4in place of insert × Arcturus value associated with key get Ckey ) : like map , but associates a collection Dictionary values with each key of . - Implementations of these are simple extortions to implementations of sets , which we focus on .

  4. searchtrees.czoat.SI , Multi set , Mgp , . . . with ( at least ) ADTS insert , find , removes efficient all , - - by ff¥¥im n= # keys in the set like binary where search ( Our first attempt will not - succeed at . ) giving 0110g n ) operations

  5. BinarySearohT A BST is a binary tree - labelled by keys - with nodes btreeoe } - for every two v.v nodes : subtree of v in the left ' if is u a key 4) key (a) then III. rig it BST

  6. ⇐ Ex = - ⑤ ⑤ ← T o % ⑤ r ' ③

  7. i ¥6 ④

  8. Ey : Reasoning with the W µ order invariant . / ↳ v3 1.1 \v2 . vi. T o LI a key Cu ) key Cut ) key ( re ) > key Cu ) ? key Cu ) key Crs ) a key Cvs ) a key ( w ) Kayla ) ⇒ key Cnn ) a key Los )

  9. BSTmemberkdpseudo-codef-indlt.ir ) 4 return true if t appears in 4 subtree rooted at it . { if key G) ! left has subtree t v a a find Lt , left Iv ) ) return > key Cr ) a right subtree has 4 if t v find ( t , right Cr ) ) return if key 4) = t return true return false } is in the tree return true ift member ( t ) t . return find It , root ) }

  10. BSTfindltivlpseudo-code-alternater-er.in find Lt , r ) 4 return true if t appears in 4 subtree rooted at it . { if key 4) = t return true if ! left key G) subtree has t v a a find Lt , left Iv ) ) return > key ( r ) a right subtree has I if t v find ( t , right Cr ) ) return return false } Since key G) =t Oi . Which version is better ? will almost always be false , the first version so be faster . do fewer comparisons , should

  11. BSTinserttxlpseudo.co# insert Had ds t to the tree . already in the tree Has some s t is not find It , root ) terminates a ← node at which a key (a) if t with key t new left child give a u else new right child with key t a give m . }

  12. BSTInsertExamp 2 insert thy insert 7 ← ③ %

  13. - alternate version insert 4) Pseudo - code BST f - insert Lt , DX insert t in the subtree rooted at v , if it is not there . { if key G) ! left v has subtree t a a insert Lt , left Iv ) ) > key Cr ) a right subtree has I if t v insert Ct , right GD no left child a key G) 11 here if has t v if with key t key 4) at new left child give a v return true no right child t > key 4) 4 here has it r new right child with key t a r give . we reach here , t= keylvl , so do nothing Kit . } not already there . insert It ){ Dodds t to the tree , if it is insert It , root ) }

  14. Ini¥BTiExamp 1) with an empty start e. in tae given order insert E ? ' Ii ' . ← with an empty BST 2) ' start order given - insert in the ' 1,213,516,78 - ② - ③ - ⑤ - ⑥ - ⑨ - - - ⑧

  15. remove It ) BST - consider 3 cases , of increasing difficulty - We . - Catisatal ' th key G) = t i ) find the node r wi ii ) delete ✓ remove 4) ,¥ ⑤ E ⑤ F ② * " ⑨ L

  16. BSTremoretttfki.tisatanodewithtchild.is with key Cut = t find the V node child of r u be the let with the subtree rooted at a iii ) replace v remove 4) remove (3) Exampte , then : → ← ¥7 ¥ , 1- ③ ⑧ ⑤

  17. no deft ) has 1 child E × amp BST remove It ) where remove oo ⇒ of ' ⑧ T ③ ' ⑥ oeCT *

  18. BST remove : Case 3 Preparation - . 7 f =L . Si - I , Si , Siti , sit 2 - In an ordered set . . . . is the predecessor of si Sin of is the Si successor Sit , be the set of keys in the Best - Let s , and t our target be K . - We will want to locate the predecessor / successor of Tins .

  19. BST remove : Case 3 Preparation - in - order traversal of a BST - 0bse An nodes in the order of their keys visits . be the set of nodes in the BST - Let 4=4 , vz . un ) . . . in the order visited by in - order traversal an X , and xtX , write - For an ordered set × in X. successor of succxtx ) for the - Then we have = succslk ) key ( succubi ) ) a) if key (a) =k then node ( suds CKD = such then b) If no deck ) =u

  20. BSTremoveicasezpreparation.IT is easy to find a right child has it node v , its successor : 4. ↳ is the first ← succor ) node visited by \ an in - order traversal - of the right subtree of v. E%f → such µ § Succor ) v ⇒ 4. I F. such # ↳ to Do T o skew )

  21. smallest key a B5T in . → ¥884 ¥0

  22. BSTremoveilase3preparation.TO v ( assuming has a successor of v find the node right child ) use : suck ) { u ⇐ right G) ) exists ) { while ( left Cm U * left Cns } return a } contains the The next node after next r smallest key that i. e. the key G) key after - is larger than key G) .

  23. Bs Trema# is at node with 2 children : t Casey a with key G) =t i ) find the node v ii ) find the successor of V - call it u . ← key G) iii ) key f) iv ) delete re : it , delete a leaf a) if is u one child leaf it has w b) if not a is u , rooted at w u with the subtree replace in Case 2) ( as

  24. BSTremovelhwhennodelldhas2chddreurem.ve (5) 7¥ µ⑤ - ⑤ € T o ← ¥ , µ ? ' ① ¥10

  25. BSeCk)whenhdren. , ② T ⑧ D removed " ⇒ A " I ④ # A , ④ , A A -40 ✓ ⑤ ④ replace with ⑨ A -

  26. # keys Complexity of B5T Operations or site Ln ) : - 1h ) as a function of : height - Measure path from involve traversing - All operations essentially a v in the worst v , where case the root to a node maximum depth a leaf of - is it o / T member :O ( h ) , 0 ( n ) o - so : ÷ :::÷:÷ : ' h= ? remove :O ( h ) , old eg.tt ) his small relative to n . - " trees leg Tbh is proportional to n " tall skinny . • For ⇐ % .IS?I3sBshIEe ' r ' 90

  27. ↳ A perfeutbinarytree of height h is - a binary tree of height h with the number of nodes : Max . • I an £5 . T o 1ha ✓ I DAH ha r ✓ X I → ×

  28. Notice : Because a perfect binary tree of height h is like this : ÷ :/ 2h - I 2h leaves htt = 2. 2h 2 " t 2h - I = 2 - I . I

  29. n keys , there For every S of Claim set exists : for S with height at most It Logan a BST Pro¥ Let h be the smallest integer st . 2h > n , and m=2h . So : 2h In > 2h - I log , 2 " slogan > log , 2h " logznyh-lha.lt h = Logan h= login - let T be the perfect binary tree of height nodes of T ( as visited . label the first n in - order traversal ) with the by an and delete the remaining keys of 5 , ' ) nodes ( to get T . It login = log . m h a B5T for S with height < ' • T is - log n is always a B5T with height . So , there

  30. ⇒ 0ptimalBSTTnsertionorder-G.ir of set can insert them keys , en we a as to get minimum height BST : so a 0 ← What we say about can Consider : the key / \ here ? ( It is the - 0 0 6 to ← to median 68 610 key ) 6 to ← to Observe : The first key inserted into a root forever at the BST is BST ) ( unless from the remove it we

  31. Optimal BST Insertion Order . ¥ → 0 0 D I * AI 1II median key " seethe principle

  32. ⇒ - log n is always a B5T with height - So , there min . height with Ollogn ) we maintain - Can remove ? insert 4 . Consider : insert I ⑤ - ← TO ⑥ ② ⑤ T o 66 6 ← ④ ⑦ is the only ' B min height B5T for 1 . . 7 . - A → B required " " moving node every - To get 0( log n ) operations , we need another kind of search tree , other than plain BSTS .

  33. To get efficient at least search trees , give up one of : - binary - min height .to/logn)operatims-

  34. Ends

  35. Testt up to last friday t At . - Coverage : lectures . Short or exercises answer Focus : data structures - properties % operations use versions from lectures . - Note : . BST remove cqn pied OR F ⇒ a use . D D. Sufc pied - Usetheouefromclas-webp.ge • Slides coming

Recommend


More recommend