cs 473lecture x1 cs473-algorithms lecture rbt - insertion

20
CS 473 Lecture X 1 CS473-Algorithms Lecture RBT - INSERTION

Upload: corey-mcdaniel

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

CS 473 Lecture X 1

CS473-Algorithms

Lecture

RBT - INSERTION

Page 2: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 2

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β

y

x

Page 3: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 3

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

Page 4: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 4

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)

Page 5: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 5

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)

Page 6: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 6

Rotationx

y

Right-Rotate(T,y)

Tα Tβ

y

x

Right-Rotate(T,y)

Left-Rotate(T,y)

y

x

y

x Tγ

TαTβ

Page 7: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 7

A Sample Left-Rotation

Page 8: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 8

InsertionRB-INSERT(x)TREE-INSERT(x);color[x] ← RED;while x ≠ root and color[p[x]] = RED do

if p[x] = left[p[p[x]]] theny ← 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]] thenx ← 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

Page 9: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 9

InsertionKey Idea

• Insert x into tree, color x RED

• Only R-B property-3 might be violatedif 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

Page 10: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 10

Insertion• At the beginning of each iteration of the

while-loopx 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

Page 11: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 11

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.

Page 12: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 12

InsertionCASE 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

Page 13: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 13

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.

Page 14: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 14

Insertion• bh(α) = bh(β) = bh(γ) = bh(δ) = bh(ε)• Each of the subtrees rooted at α, β, γ, δ, ε have black

roots

(B)

(B) (B)

(B) (B) (B)

(B) (B)

(B) (B)

(B)(B)

(B)(B)

(B)(B)

(B)(B)

(B) (B)

x

x

x

x

Page 15: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 15

Insertion

Case 2 & 3 : color[y] = black• Two cases distinguished by whether• Each of the subtrees rooted at α, β, γ, δ, ε have black

roots

Case 2 Case 3

Tδ(y)

Tα Tβ Tγ Tδ(y)

B(R)

(B)(B)R

Rxx

Page 16: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 16

InsertionCase 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

Page 17: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 17

InsertionCase 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

Page 18: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 18

Rotation

(B)

(C)

(D)

(A)

C

A

B

D

x

x

Page 19: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 19

Rotation

A

B δ

C

x

x

Page 20: CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION

CS 473 Lecture X 20

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