200 likes | 297 Views
CS473-Algorithms. Lecture RBT - INSERTION. Basic Operation for Changing Tree Structure. ROTATION Key ida: Rotation won’t change. Binary search tree property. y. Right-Rotate(T,y). y. Left-Rotate(T,y). x. T γ. T α. x. T α. T β. T β. T γ.
E N D
CS473-Algorithms Lecture RBT - INSERTION Lecture X
Basic Operation for Changing Tree Structure ROTATION • Key ida: Rotation won’t change. Binary search tree property. y Right-Rotate(T,y) y Left-Rotate(T,y) x Tγ Tα x Tα Tβ Tβ Tγ Lecture X
Basic Operation for Changing Tree Structure • Right-Rotate (T,y) : (left[y] ≠ NIL) • x = left[y] becomes new root of the subtree. • y becomes the right child of x. • y keeps its right child; x keeps its left child • x’s right child becomes the left child of y. • Left-Rotate(T,x) : (right[x] ≠ NIL) • y = right[x] becomes new root of the subtree • x becomes the left child of y • x keeps its left child; y keeps its right child • Y’s left child becomes the right child of x Lecture X
Basic Operation for Changing Tree Structure • A local operation which preserves inorder key ordering • E.g. Tα x Tγ y TβTα Tγ TβArbitrary trees • Runs in O(1) time • Changes a constant number of pointers (local operations) Lecture X
Rotation • A local operation which preserves inorder key ordering • E.g. Tα x Tγ y TβTα Tγ TβArbitrary trees • Runs in O(1) time • Changes a constant number of pointers (local operations) Lecture X
Rotation x y Right-Rotate(T,y) y Tγ Tα x Tα Tβ Tβ Tγ Right-Rotate(T,y) y y Left-Rotate(T,y) Tα x x Tγ Tβ Tβ Tγ Tα Lecture X
A Sample Left-Rotation Tα Tβ Tγ Tα Tγ Tβ Lecture X
Insertion p[x] :x’s parent p[p[x]]:x’s grandparent y :x’s parent’s sibling x’s uncle RB-INSERT(x) TREE-INSERT(x); color[x] ← RED; whilex ≠ root and color[p[x]] =REDdo if p[x] = left[p[p[x]]] then y ← right[p[p[x]]] ; ifcolor[y] =REDthen color[p[x]] ← BLACK; color[y] ← BLACK; color[p[p[x]]] ← RED; x ← p[p[x]]; elseifx= right[p[x]]then x ← p[x]; LEFT-ROTATE(x); color[p[x]] ← BLACK; color[p[p[x]]] ← RED; RIGHT-ROTATE (p[p[x]]) else (smae as” then clause with “right” & “left” exchanged) color[root] ← BLACK Case 1 Case 2 Case 3 Lecture X
Insertion Key Idea • Insert x into tree, color x RED • Only R-B property-3 might be violated if x and p[x] both red • The goal ( while-loop) • Move one violation of property 3 up the tree While preserving property 2 as invariant Lecture X
Insertion • At the beginning of each iteration of the while-loop x points to a red node with a red parent • The only violation in the tree • There are two possible outcomes of each iteration • Pointer x moves up the tree • Some rotations are performed and loop terminates Lecture X
Insertion • 6 Cases to consider in the while loop • 3 of them symmetric to other 3, depending on x’s parent p[x] • Is a left or right child of x’s grandparent p[p[x]] • Important assumption : root of the tree is black. • Violation of P3 (color[x] = color[p[x]] = red) • x’s grandparent (g=p[p[x]]) must be black • Since P3 must have hold before the insert. Lecture X
Insertion CASE 1 : color[y] = RED • Subtrees Tα, Tγ,Tβ, Tδ , Tε all have black roots (D nodes) • Since P3 must hold before the insert • Subtrees Tα, Tγ,Tβ, Tδ , Tε all have the same black height (=bh(g)) • Since P4 must hold before the insert Lecture X
Insertion • Push g’s black to its children and color g as red • Fixing P3 violation between x & p[x] • Maintaining black height of g • Any downward simple path thru g still traverses • One black en route to the roots of all subtrees Tα– Tε • Avoiding P3 violation between g and its children • Move up x pointer to g • No violation below x • Only possible violation above x is P3 i.e. color [ p[x] ] = color [x] = red. Lecture X
Insertion • bh(α) = bh(β) = bh(γ) = bh(δ) = bh(ε) • Each of the subtrees rooted at α, β, γ, δ, ε have black roots x x (B) (B) (B) (B) (B) (B) (B) (B) (B) (B) x x (B) (B) (B) (B) (B) (B) (B) (B) (B) (B) Lecture X
Insertion Case 2 B(R) Case 3 Case 2 & 3 : color[y] = black • Two cases distinguished by whether • Each of the subtrees rooted at α, β, γ, δ, ε have black roots (B) R (B) Tδ(y) Rx Tβ x Tα Tγ Tδ(y) Tα Tβ Tγ Lecture X
Insertion Case 2 : x is a right child of p[x] • Perform a left-rotation to transform to case 3 • Node x becomes a left child of its parent • Because both x & p[x] are red, rotation affects • Neither black height of nodes • Nor property 4 Lecture X
Insertion Case 3 : x is a left child of p[x] • Whether we enter case 3 directly or thru case 2 • color [x’s uncle = y] = black still holds • Improve tree’s balance by performing a right-rotate • e.g. Rearrange A-B-C to be a complete binary tree • Preserve property 4 by retaining • A single black thru this portion of the tree Since the root of this portion remains black • Nothing above here in the tree gets messed up Lecture X
Rotation (C) (B) (D) x (A) C D A B x Lecture X
Rotation C δ B A x x Lecture X
Insertion : Complexity Analysis • Call to tree-insert takes O(lg n) time • Since height of a R-B tree is O(lg n) • While-loop iterates if case 1 is executed ( O(1) operation) • Pointer x moves up the tree • while loop iterates O(lg n) time. • Thus, RB-INSERT takes a total of O( lg n) time • With O(1) (at most 2) rotations • Note, AVL trees may need lg n rotations for insertion Lecture X