Course Objective : to teach you some data structures and associated algorithms INF421, Lecture 7 Evaluation : TP noté en salle info le 16 septembre, Contrôle à la fin. Balanced Trees Note: max( CC, 3 4 CC + 1 4 TP ) Organization : fri 26/8, 2/9, 9/9, 16/9, 23/9, 30/9, 7/10, 14/10, 21/10, Leo Liberti amphi 1030-12 (Arago), TD 1330-1530, 1545-1745 (SI31,32,33,34) Books : LIX, ´ Ecole Polytechnique, France 1. Ph. Baptiste & L. Maranget, Programmation et Algorithmique , Ecole Polytechnique (Polycopié), 2009 2. G. Dowek, Les principes des langages de programmation , Editions de l’X, 2008 3. D. Knuth, The Art of Computer Programming , Addison-Wesley, 1997 4. K. Mehlhorn & P . Sanders, Algorithms and Data Structures , Springer, 2008 Website : www.enseignement.polytechnique.fr/informatique/INF421 Contact : liberti@lix.polytechnique.fr (e-mail subject: INF421) INF421, Lecture 7 – p. 1 INF421, Lecture 7 – p. 2 Lecture summary Notation Tree T Binary search trees Node set of T : V ( T ) (with | V ( T ) | = n ) AVL trees Root: r ( T ) Heaps and priority queues Tree rooted at v : T ( v ) Node: v ∈ V ( T ) Tries r ( T ) Root node of left subtree of v : L ( v ) Root node of right subtree of v : R ( v ) L ( r ) R ( r ) If L ( v ) = R ( v ) = ∅ , v is a leaf node D ( T ) Parent node of v : P ( v ) ⇒ T = � L ( r ( T )) , r ( T ) , R ( r ( T )) � For all v ∈ V ( T ) : p ( v ) = unique path r ( T ) → v Path length: λ ( T ) = � | p ( v ) | v ∈ V ( T ) Depth (or height): D ( T ) = max v ∈ V ( T ) | p ( v ) | INF421, Lecture 7 – p. 3 INF421, Lecture 7 – p. 4
The minimal knowledge Let ( V, < ) be a totally ordered set V stored as a binary tree T : L ( v ) = u ⇒ u ≤ v R ( v ) = u ⇒ u > v ( † ) Binary search trees (BST) find, insert, delete, min, max : O (log n ) on average, O ( n ) worst case AVL trees : balance B ( T ) = D ( T ( L ( r ( T )))) − D ( T ( R ( r ( T )))) ∈ {− 1 , 0 , 1 } If an operation unbalances, use a rebalancing operation ⇒ all operations are O (log n ) in the worst case Can use a special balanced tree (a heap ) to implement a priority queue ( min / max, insert, delete ) Tries are k -ary trees that encode words prefix-wise INF421, Lecture 7 – p. 5 INF421, Lecture 7 – p. 6 BST min / max Sorted sequences min ( v ) : Used to store a set V as a sorted sequences 1: if L ( v ) = ∅ then 12 Makes it efficient to answer the question v ∈ V return v ; 2: Each node v in the tree is such that L ( v ) ≤ v < R ( v ) 3: else 5 14 Example: V = { 1 , 3 , 6 , 7 } return min( L ( v )) ; 4: 5: end if 7 13 18 1 7 max ( v ) : 3 3 6 6 ∅ ∅ 1: if R ( v ) = ∅ then 12 return v ; 2: 6 1 6 3 7 3 ∅ ∅ 5 14 3: else return max( R ( v )) ; 4: ∅ 7 7 1 1 ∅ ∅ ∅ 5: end if 7 13 18 Several possibilities INF421, Lecture 7 – p. 7 INF421, Lecture 7 – p. 8
BST find Base cases for recursion find ( k, v ) : 1: ret = not found ; 2: if v = k then ret = v ; 3: All other BST functions f ( k, v ) are assumed to be 4: else if k < v then implemented so that f ( k, ∅ ) returns without doing anything ret = find ( k, L ( v )) ; 5: (base case of recursion) 6: else ret = find ( k, R ( v )) ; 7: 8: end if 9: return ret ; INF421, Lecture 7 – p. 9 INF421, Lecture 7 – p. 10 BST insert Insert example 1/3 insert ( k, v ) : insert (1 , r ( T )) 1: if k = v then return already in set ; // if multiset: 2: add new node 3: else if k < v then 12 if L ( v ) = ∅ then 4: L ( v ) = k ; 5: else 6: insert ( k, L ( v )) ; 7: 5 14 end if 8: 9: else if R ( v ) = ∅ then 10: R ( v ) = k ; 11: 7 13 18 else 12: 1 < 12 , take left branch insert ( k, R ( v )) ; 13: end if 14: 15: end if INF421, Lecture 7 – p. 11 INF421, Lecture 7 – p. 12
Insert example 2/3 Insert example 3/3 insert (1 , r ( T )) insert (1 , r ( T )) 12 12 5 5 14 14 7 13 18 1 7 13 18 1 < 5 , should take left branch but L (5) = ∅ Add k = 1 as L (5) INF421, Lecture 7 – p. 13 INF421, Lecture 7 – p. 14 Deletion is not so easy Replacing a node If node v to delete is a leaf, easy: “cut” it ( unlink ) w w v v − → u u Replace link { P ( v ) , v } with { P ( v ) , u } , then unlink v If R ( v ) = ∅ and L ( v ) � = ∅ , replace with L ( v ) replace ( u, v ) 1: if R ( P ( v )) = v then 2: R ( P ( v )) ← u ; // u is a right subnode 3: else L L 4: L ( P ( v )) ← u ; // u is a left subnode 5: end if If L ( v ) = ∅ and R ( v ) � = ∅ , replace with R ( v ) 6: if u � = ∅ then 7: P ( u ) ← P ( v ) ; 8: end if 9: unlink v ; R R unlink : set L ( v ) = R ( v ) = P ( v ) = ∅ If v has both subtrees, not evident INF421, Lecture 7 – p. 15 INF421, Lecture 7 – p. 16
BST delete Deleting v : L ( v ) � = ∅ ∧ R ( v ) � = ∅ Idea: swap v with u = min ( R ( v )) then delete it delete ( k, v ) : 1: if k < v then The minimum u of a BST is always the leftmost node delete ( k, L ( v )) ; 2: without a left subtree 3: else if k > v then Hence we know how to delete u (case L ( · ) = ∅ in delete ( k, R ( v )) ; 4: previous slide) 5: else We replace the value of v by that of u then delete u if L ( v ) = ∅ ∨ R ( v ) = ∅ then 6: delete v ; // one of the easy cases 7: Because u = min T ( R ( v )) , we have u < w for all else 8: w ∈ T ( R ( v )) u = min ( R ( v )) ; 9: Since the value of v is now the value of u , v is now the swap values ( u, v ) ; 10: minimum over all nodes in T ( R ( v )) ; hence v < r ( R ( v )) delete u ; // an easy case, as L (u)=null 11: end if 12: Moreover, since the value of v used to be u , a node in 13: end if R ( v ) , we have v > r ( L ( v )) , satisfying the BST defn. ( † ) INF421, Lecture 7 – p. 17 INF421, Lecture 7 – p. 18 Delete example Complexity delete (10 , r ( T )) Each IF case involves at most one recursive call 10 Recurse along one branch only 10 5 14 Worst-case complexity proportional to depth D ( T ) 5 14 If tree is balanced, D ( T ) is O (log n ) (see INF311) 7 12 18 7 12 18 In the worst case, D ( T ) is O ( n ) u = min T (14) = 12 v = 10 • 12 12 5 14 5 14 ∅ • 7 18 7 10 18 ∅ • delete 10 swap values of 10 and 12 ∅ • INF421, Lecture 7 – p. 19 INF421, Lecture 7 – p. 20
AVL Trees Try inserting 1 , 3 , 6 , 7 in this order: get unbalanced tree 1 Adelson-Velskii & Landis (AVL) 3 ∅ trees 6 ∅ 7 ∅ Worst case find (i.e., find the key 7) is O ( n ) Need to rebalance the tree to be more efficient AVL trees : at any node, B ( T ) = depth difference between left and right subtrees ∈ {− 1 , 0 , 1 } INF421, Lecture 7 – p. 21 INF421, Lecture 7 – p. 22 Examples In general AVL tree: We can decompose balanced trees operations into: − 1 Non-AVL tree: the operation itself − 2 a sequence of rebalancing operations (when required) , called 1 − 1 rotations 0 − 1 The operations min/max, find, insert, delete 1 0 − 1 0 are as in BST (with one simple modification) 0 − 1 Unbalancing can occur on insertion and deletion 0 0 0 1 − 1 0 0 ∅ Since we insert/delete only one node at a time, unbalance offset is at most 1 unit 0 ∅ ∅ 0 0 0 I.e., B ( T ) = depth difference between left and right Nodes indicate B ( T ( v )) subtrees, could be {− 2 , 2 } INF421, Lecture 7 – p. 23 INF421, Lecture 7 – p. 24
Left and right rotation Algebraic interpretation u v Let α, β, γ be trees, u, v be nodes not in α, β, γ v rotateLeft u Define: rotateLeft ( � α, u, � β, v, γ �� ) = �� α, u, β � , v, γ � rotateRight α γ rotateRight ( �� α, u, β � , v, γ � ) = � α, u, � β, v, γ �� β γ β α A sort of “associativity of trees” Remark: rotateLeft , rotateRight are inverses Thm. rotateRight ( rotateLeft ( T )) = rotateLeft ( rotateRight ( T )) = T Proof Directly from the definition INF421, Lecture 7 – p. 25 INF421, Lecture 7 – p. 26 Rotating and rebalancing Properties of rotation u v Thm. 0 − 2 rotateLeft v u ∀ T , rotateLeft ( T ) , rotateRight ( T ′ ) are BSTs 0 D = h − 1 D = h + 1 Proof α γ D = h D = h + 1 D = h (Sketch): The tree order only changes locally for u, v . In T , T ( v ) = R ( u ) , which implies β γ β α u < v . In rotateLeft ( T ) , T ( u ) = L ( u ) , which is consistent with u < v . Similarly for T ′ . u Suppose D ( α ) = D ( β ) = h and D ( γ ) = h + 1 v 0 2 v u Let T = � α, u, � β, v, γ �� : then B ( T ) = − 2 0 Let T ′ = �� γ, u, β � , v, α � : then B ( T ′ ) = 2 1 D = h rotateRight D = h + 1 α γ D = h + 1 D = h Thm. D = h β γ β α T, T ′ as above ⇒ B ( rotateLeft ( T )) = 0 , B ( rotateRight ( T ′ )) = 0 Proof (Sketch): since subtrees α, γ are swapped, tree depth is D = h for all subtrees INF421, Lecture 7 – p. 27 INF421, Lecture 7 – p. 28
Is this enough? Break γ up into subtrees u u − 2 − 2 v v D = h 1 D = h 1 α α γ D = h D = h D = h + 1 γ β β h − 1 h Rotating leaves γ at its place, doesn’t work Now we can rotate T ( v ) = R ( u ) INF421, Lecture 7 – p. 29 INF421, Lecture 7 – p. 30 Rotate a subtree right Finally, rotate left u u u r ( γ ) − 2 − 2 − 2 v r ( γ ) r ( γ ) 0 u v − 1 rotateRight ( R ( u )) − 1 D = h D = h D = h rotateLeft ( T ) 1 v v Bv Bu − 1 D = h + 1 Bv α γ α α D = h D = h + 1 D = h β h − 1 β h − 1 h − 1 h − 1 α β β h h h h h h h Rotate T left Rotate R ( u ) right INF421, Lecture 7 – p. 31 INF421, Lecture 7 – p. 32
Recommend
More recommend