Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION.

Similar presentations


Presentation on theme: "CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION."— Presentation transcript:

1 CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

2 CS 473Lecture X2 Basic Operation for Changing Tree Structure ROTATION Key ida: Rotation won’t change. Binary search tree property. y x Right-Rotate(T,y) Left-Rotate(T,y) TγTγ TαTα TβTβ y x TγTγ TαTα TβTβ

3 CS 473Lecture X3 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

4 CS 473Lecture X4 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)

5 CS 473Lecture X5 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)

6 CS 473Lecture X6 Rotation x y Right-Rotate(T,y) TγTγ TαTα TβTβ y x TγTγ TαTα TβTβ Left-Rotate(T,y) y x TγTγ TαTα TβTβ y x TγTγ TαTα TβTβ

7 CS 473Lecture X7 A Sample Left-Rotation TβTβ TαTα TγTγ TβTβ TγTγ TαTα

8 CS 473Lecture X8 Insertion RB-INSERT(x) TREE-INSERT(x); color[x] ← RED; while x ≠ root and color[p[x]] = RED do if p[x] = left[p[p[x]]] then y ← right[p[p[x]]] ; if color[y] = RED then color[p[x]] ← BLACK; color[y] ← BLACK; color[p[p[x]]] ← RED; x ← p[p[x]]; else if x= 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 p[x] : x’s parent p[p[x]] : x’s grandparent y : x’s parent’s sibling x’s uncle Case 1 Case 2 Case 3

9 CS 473Lecture X9 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

10 CS 473Lecture X10 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

11 CS 473Lecture X11 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.

12 CS 473Lecture X12 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

13 CS 473Lecture X13 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.

14 CS 473Lecture X14 Insertion bh(α) = bh(β) = bh( γ ) = bh(δ) = bh( ε ) Each of the subtrees rooted at α, β, γ, δ, ε have black roots (B) x x x x

15 CS 473Lecture X15 Insertion Case 2 & 3 : color[y] = black Two cases distinguished by whether Each of the subtrees rooted at α, β, γ, δ, ε have black roots Case 2Case 3 TγTγ TαTα TβTβ T δ(y) TαTα TβTβ TγTγ B(R) (B) R RxRx x

16 CS 473Lecture X16 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

17 CS 473Lecture X17 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

18 CS 473Lecture X18 Rotation (B) (C) (D) (A) C A B D x x

19 CS 473Lecture X19 Rotation A B δ C x x

20 CS 473Lecture X20 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


Download ppt "CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION."

Similar presentations


Ads by Google