lecture 12: balanced binary search trees shang-hua teng

29
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng

Post on 19-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Lecture 12:Balanced Binary Search Trees

Shang-Hua Teng

Insertion and Deletion on dynamic sets

• Insert(S,x)– A modifying operation that augments the set S

with the element pointed by x

• Delete– Given a pointer x to an element in the set S,

removes x from S – Notice that this operation uses a pointer to an

element x, not a key value

Querying on dynamic sets• Search(S,k)

– given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or NIL if no such element belongs S

• Minimum(S)– on a totally ordered set S that returns a pointer to the element S with the

smallest key

• Maximum(S)

• Successor(S,x)– Given an element x whose key is from a totally ordered set S, returns a

pointer to the next larger element in S, or NIL if x is the maximum element

• Predecessor(S,x)

Binary Search Trees

• All operations can be supported in– O(h) time, where h = height of tree

• What is the height of a binary search tree?– worst case: h = O(n) when tree is just a linear

string of left or right children

• Today we’ll see how to maintain h = O(lg n)

Restructuring Binary Search Trees: Rotations

• Our basic operation for changing tree structure is called rotation:

• Preserves binary search tree properties• O(1) time…just changes some pointers

C

rightRotate(y)

leftRotate(x)

y

x

A B

x

A y

B C

Balanced Binary Search Tree

• Apply rotations during insertion and deletion to ensure that the height of the tree is O(lg n).

• Define a balanced condition for simple maintenance.

• Example: for each internal node, the heights of the left and right subtree differs by at most 1– AVL trees (homework)

• We will discuss a scheme that uses color to balance the tree– Red-black trees

Red-Black Tree: Coloring nodes with red and black

30

70

85

5

60

80

10

90

15

20

50

40 55

65

Properties of Red-Black Trees

• A Red-Black tree satisfies the following properties:

1 Every node is colored either red or black

2 The root is black

3 Every leaf (NIL, NULL) is black

4 If a node is red, both of its children are black.

5 Every path from a node to a leaf reference has the same number of black nodes

Red-Black Tree

30

70

85

5

60

80

10

90

15

20

50

40 55

65

Height of Red-Black trees

• If every path from the root to a null reference contains B black nodes, then there must be at least 2B - 1 black nodes in the tree.

• Since the root is black and there cannot be two consecutive red nodes on a path, the height of a red-black tree is at most 2log(N + 1)

RB Trees: Worst-Case Time

• So we’ve proved that a red-black tree has O(lg n) height

• Corollary: These operations take O(lg n) time: – Minimum(), Maximum()– Successor(), Predecessor()– Search()

• Insert() and Delete():– Will also take O(lg n) time– But will need special care since they modify tree

Red-black trees -- Insert

1. Find the location for the target node, x.

2. Insert x and color it red.

• Why red? Maintain the black height property.

• Potential problem: the parent of x is also red!

• Method: move this violation up the tree while always maintaining the black height property.

• Perform rotations and re-colorings as necessary

RedBlackTreeInsert(z) treeInsert(z); x->color = RED; // Move red-red up tree, maintaining black height as invariant: while (z!=root and p(z)->color == RED) if (p(z) == p(p(z))->left) y = p(p(z))->right; if (y->color == RED) p(z)->color = BLACK; y->color = BLACK; p(p(z))->color = RED; x = p(p(z)); else // y->color == BLACK if (z = p(z)->right) z = p(z); leftRotate(z); p(z)->color = BLACK; p(p(z))->color = RED; rightRotate(p(p(z))); else // x->p == p(p(z))->right (same as above, but with “right” & “left” exchanged)

Case 1: uncle is RED

Case 2

Case 3

Insert: Case 1

if (y->color == RED)

p(z)->color = BLACK;

y->color = BLACK;

p(p(z))->color = RED;

z= p(p(z));

• Case 1: “uncle” is red

• In figures below, all ’s are equal-black-height subtrees

CA D

B

z

y

C

A D

B

new z

Change colors of some nodes, preserving: all downward paths have equal b.h.The while loop now continues with z’s grandparent as the new z

case 1

Insert: Case 2

if (z == p(z)->right)

z = p(z);

leftRotate(z);

// continue with case 3 code

• Case 2:– “Uncle” is black– Node z is a right child

• Transform to case 3 via a left-rotation

case 2

B

z

CA y

C

B

A

z

y

Transform case 2 into case 3 (z is left child) with a left rotationThis preserves: all downward paths contain same number of black nodes

Insert: Case 3

p(z)->color = BLACK;

P(p(z))->color = RED;

rightRotate(p(p(z)));

• Case 3:– “Uncle” is black

– Node z is a left child

• Change colors; rotate right

case 3

y

CB

A

z

BAz

C

Perform some color changes and do a right rotationAgain, preserves: all downward paths contain same number of black nodes

Red-black trees -- Insert• What we described is what happens when p[z] is

a left child. The case when it is a right child is symmetrical.

• Note:– In case 3 we fixed the problem by a single rotation

(single or double).– In case 1 we recolored the nodes and the problem

may have propagated upwards.– In any case, we need at most one restructuring of the

tree at the level.

• Total insert time : O(lgn)

Red-black trees -- Delete• Let z be the node that we want to delete.• Let y be the actual node that we delete.

– if z has at most one non-NIL child, then y is z

– if z has two children, then y is z's successor

• Let x be the child of y.– if y is z's successor, x is its right child (possibly a NIL

child)

– if y is z, x is its non-NIL child, or NIL

RB-Delete

• The code is similar to the delete operation for a BST

• References to nil are replaced with the sentinnel nil[T]

• This allows the assignment in line 7 to be unconditional since the nil is like other nodes

• If the node y that is removed is black, the fixup routine restores the black height of the tree

Red-black trees -- Delete• When y is eliminated, x becomes the new child of

y's parent. • If y was red, no properties are violated.• If y was black, we need to restructure the tree• Idea! Let's transfer y's blackness to x

– if x was red, it becomes black.• all properties are now satisfied

– if x was black, it becomes doubly black.• the red-or-black property is violated!

Red-black trees -- Delete• To solve the double-black problem:

– propagate the extra "blackness" up the tree until• we find a red node

– then, just make it black

-- OR --

• we reach the root– then, just eliminate the extra black

-- OR --

• the problem gets fixed by rotating

• Parameter x was the removed node’s sole child (may be nil) which has a double black count

• The loop moves x with its double count up the tree until:

– x points to a red node which is colored black – x points to the root and the extra black count is discarded– rotations and recoloring can solve the problem

Red-black trees -- Delete

• There are four cases we need to consider, some of which reduce to others.

• In all of these cases, the next action depends on the color of x’s sibling, w, and its children.

Red-black trees -- DeleteCase 1

w is red => it has black childrenswitch colors of w, p[w] and left-rotate p[w]Result: one of the other two cases

x wa

b

c

d

e x new wa

b

c

d

e

Red-black trees -- DeleteCase 2

w is black with black childrenTake one black off x and w and add an extra to p[x]. Repeat the whole procedure for p[x] as the new x

x wa

b

c

d

e

for nodes that may be either red or black

a

b

c

d

e

new x

Red-black trees -- DeleteCase 3

w is black, left[w] is red, right[w] is blackDouble rotate and recolor

x wa

b

c

d

e a

b

c

d

e

Red-black trees -- DeleteCase 4

w is black, right[w] is black (we don’t care about left[w])

color[w] = color1

color[b] = color[e] = black

left-rotate at b AND WE ARE DONE!

x wa

b

c

d

e

color1

color2

b

d color1

c

e

color2a

Red-black trees -- Delete

x wa

b

c

d

e

a

b

c

d

e

new w

Case 3 NotesCase 3 NotesThe first half of the rotation in case 3 is a single rotation that The first half of the rotation in case 3 is a single rotation that actually results is the situation covered by case 4. Here is actually results is the situation covered by case 4. Here is exactly what happens in this intermediate step:exactly what happens in this intermediate step:

w is black, left[w is black, left[ww] is red, right[] is red, right[ww] is black] is black

Switch colors of Switch colors of ww, left[, left[ww] and right rotate ] and right rotate ww

Red-black trees -- Delete• Running time of "fixing" algorithm:

– Cases 1, 3 terminate after some constant color changes and at most three rotations.

– Case 2 may cause us to travel all the way to the root : O(lgn)

– Total running time for fixing algorithm: O(lgn)

• Total time for Delete : O(lgn)– even if the fixing algorithm is not called, it will

take O(lgn) if it needs to find the successor.