Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii...

48
Red Black Trees

description

Rudolf Bayer He has been a Professor of informatics at the Technical University of Munich since –Informatics is a science concerned with collection, classification, manipulation, storage, retrieval and dissemination of information. He if famous for inventing 2 sorting structures, B-trees (now called red black trees), with Edward M. McCreight. And UB-tress (with Volker Markl). –UB-Trees is another type of balanced tree used for storing and for efficiently retrieving multidimensional data.

Transcript of Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii...

Page 1: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Red Black Trees

Page 2: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

History• The concept of a balancing tree was first invented by

Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree.

• In 1970, J. E. Hopcroft invented the 2-3 trees (another balancing Search Tree.

• The Red Black Tree was first invented by Rudolf Bayer (1972), who called them “symmetric binary B-trees”.

• In 1978, Leo J Guibas and Robert Sedgewick came up with the term, Red Black Binary Tree.

Page 3: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Rudolf Bayer• He has been a Professor of informatics at the Technical

University of Munich since 1972.– Informatics is a science concerned with collection,

classification, manipulation, storage, retrieval and dissemination of information.

• He if famous for inventing 2 sorting structures, B-trees (now called red black trees), with Edward M. McCreight. And UB-tress (with Volker Markl). – UB-Trees is another type of balanced tree used for

storing and for efficiently retrieving multidimensional data.

Page 4: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

4

Red-Black Trees• “Balanced” binary trees guarantee an O(lgn) running

time on the basic dynamic-set operations• Red-black-tree

– Binary tree with an additional attribute for its nodes: color which can be red or black

– Constrains the way nodes can be colored on any path from the root to a leaf

• Ensures that no path is more than twice as long as another the tree is balanced

– The nodes inherit all the other attributes from the binary-search trees: key, left, right, p

Page 5: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Red-Black Trees (RBT) Red-Black tree: BST in which each node is colored red or

black.Constraints on the coloring and connection of nodes

ensure thatno root to leaf path is more than twice as long as any

other, sotree is approximately balanced.Each RBT node contains fields left, right, parent, color, and

key.LEFT

PARENT RIGHT

KEYCOLOR

Page 6: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

6

Red-Black-Trees Properties

1. Every node is either red or black2. The root is black3. Every leaf (NIL) is black4. If a node is red, then both its children are black

• No two red nodes in a row on a simple path from the root to a leaf

5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

Page 7: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

7

Example: RED-BLACK-TREE

• For convenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs– NIL[T] has the same fields as an ordinary node– Color[NIL[T]] = BLACK– The other fields may be set to arbitrary values

26

17 41

30 47

38 50

NIL NIL

NIL

NIL NIL NIL NIL

NIL

Page 8: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

8

Black-Height of a Node

• Height of a node: the number of edges in a longest path to a leaf

• Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x

26

17 41

30 47

38 50

NIL NIL

NIL

NIL NIL NIL NIL

NIL

h = 4bh = 2

h = 3bh = 2

h = 2bh = 1

h = 1bh = 1

h = 1bh = 1

h = 2bh = 1 h = 1

bh = 1

Page 9: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

9

Properties of Red-Black-Trees• Claim

– Any node with height h has black-height ≥ h/2• Proof

– By property 4, at most h/2 red nodes on the path from the node to a leaf

– Hence at least h/2 are black26

17 41

30 47

38 50

Property 4: if a node is red then both its children are black

Page 10: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

10

Properties of Red-Black-TreesClaim

– The subtree rooted at any node x contains at least 2bh(x) - 1 internal nodes

Proof: By induction on height of xBasis: height[x] = 0

x is a leaf (NIL[T])

bh(x) = 0

# of internal nodes: 20 - 1 = 0

NIL

x

Page 11: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

11

Properties of Red-Black-TreesInductive step: • Let height(x) = h and bh(x) = b• Any child y of x has:

– bh (y) =– bh (y) =

26

17 41

30 47

38 50

b (if the child is red), orb - 1 (if the child is black)

Page 12: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

12

Properties of Red-Black-Trees• Want to prove:

– The subtree rooted at any node x contains at least 2bh(x) - 1 internal nodes

• Internal nodes for each child of x:

2bh(x) - 1 - 1• The subtree rooted at x contains at least:

(2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 = 2 · (2bh(x) - 1 - 1) + 1 = 2bh(x) - 1 internal nodes

x

l r

Page 13: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

13

Properties of Red-Black-TreesLemma: A red-black tree with n internal nodes has height

at most 2lg(n + 1).Proof:

n

• Add 1 to both sides and then take logs: n + 1 ≥ 2b ≥ 2h/2

lg(n + 1) ≥ h/2 h ≤ 2 lg(n + 1)

root

l r

height(root) = hbh(root) = b

number n of internal nodes

≥ 2b - 1 ≥ 2h/2 - 1since b h/2

Page 14: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

14

Operations on Red-Black-Trees• The non-modifying binary-search-tree operations

MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR,

and SEARCH run in O(h) time

– They take O(lgn) time on red-black trees

• What about TREE-INSERT and TREE-DELETE?

– They will still run on O(lgn) – We have to guarantee that the modified tree will still be

a red-black tree

Page 15: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

15

INSERTINSERT: what color to make the new node?• Red? Let’s insert 35!

– Property 4: if a node is red, then both its children are black

• Black? Let’s insert 14!– Property 5: all paths from a node to its leaves contain

the same number of black nodes26

17 41

30 47

38 50

Page 16: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

16

DELETEDELETE: what color was the node that was removed? Red? 1. Every node is either red or black2. The root is black3. Every leaf (NIL) is black4. If a node is red, then both its children are black

5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

OK!OK!OK!

OK! Does not createtwo red nodes in a rowOK! Does not change

any black heights

26

17 41

30 47

38 50

Page 17: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

17

DELETEDELETE: what color was the node that was removed? Black? 1. Every node is either red or black2. The root is black3. Every leaf (NIL) is black4. If a node is red, then both its children are black

5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

OK!

OK!

Not OK! Could createtwo red nodes in a row

Not OK! Could change theblack heights of some nodes

26

17 41

30 47

38 50

Not OK! If removing the root and the child that replaces it is red

Page 18: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

18

Rotations• Operations for restructuring the tree after insert and delete

operations on red-black trees

• Rotations take a red-black-tree and a node within the tree and:

– Together with some node re-coloring they help restore the red-black-tree property

– Change some of the pointer structure

– Do not change the binary-search tree property

• Two types of rotations:

– Left & right rotations

Page 19: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

19

Left Rotations• Assumptions for a left rotation on a node x:

– The right child of x (y) is not NIL– Root’s parent is NIL

• Idea:– Pivots around the link from x to y– Makes y the new root of the subtree– x becomes y’s left child– y’s left child becomes x’s right child

Page 20: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

20

LEFT-ROTATE(T, x)1. y ← right[x] ►Set y2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree3. if left[y] NIL4. then p[left[y]] ← x ► Set the parent relation from left[y] to x5. p[y] ← p[x] ► The parent of x becomes the parent of y6. if p[x] = NIL7. then root[T] ← y8. else if x = left[p[x]]9. then left[p[x]] ← y10. else right[p[x]] ← y11. left[y] ← x ► Put x on y’s left12. p[x] ← y ► y becomes x’s parent

Page 21: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

21

Example: LEFT-ROTATE

Page 22: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

22

Right Rotations• Assumptions for a right rotation on a node x:

– The left child of y (x) is not NIL– Root’s parent is NIL

• Idea:– Pivots around the link from y to x– Makes x the new root of the subtree– y becomes x’s right child– x’s right child becomes y’s left child

Page 23: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

23

Insertion

• Goal:

– Insert a new node z into a red-black-tree

• Idea:

– Insert node z into the tree as for an ordinary binary

search tree

– Color the node red

– Restore the red-black-tree properties

• Use an auxiliary procedure RB-INSERT-FIXUP

Page 24: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

24

RB-INSERT(T, z)1. y ← NIL2. x ← root[T]3. while x NIL4. do y ← x5. if key[z] < key[x]6. then x ← left[x]7. else x ← right[x]8. p[z] ← y

• Initialize nodes x and y• Throughout the algorithm y points

to the parent of x

• Go down the tree untilreaching a leaf• At that point y is theparent of the node to beinserted

• Sets the parent of z to be y

26

17 41

30 47

38 50

Page 25: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

25

RB-INSERT(T, z)9. if y = NIL10. then root[T] ← z11. else if key[z] < key[y]12. then left[y] ← z13. else right[y] ← z14. left[z] ← NIL15. right[z] ← NIL16. color[z] ← RED17. RB-INSERT-FIXUP(T, z)

The tree was empty: set the new node to be the root

Otherwise, set z to be the left orright child of y, depending on whether the inserted node is smaller or larger than y’s key

Set the fields of the newly added node

Fix any inconsistencies that could havebeen introduced by adding this new rednode

26

17 41

30 47

38 50

Page 26: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

26

RB Properties Affected by Insert1. Every node is either red or black2. The root is black3. Every leaf (NIL) is black4. If a node is red, then both its children are black

5. For each node, all paths

from the node to descendant

leaves contain the same number

of black nodes

OK!If z is the root not OKOK!

26

17 41

4738

50

If p(z) is red not OKz and p(z) are both redOK!

Page 27: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

27

RB-INSERT-FIXUP – Case 1z’s “uncle” (y) is red

Idea: (z is a right child)

• p[p[z]] (z’s grandparent) must be

black: z and p[z] are both red

• Color p[z] black

• Color y black

• Color p[p[z]] red

– Push the “red” violation up the tree

• Make z = p[p[z]]

Page 28: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

CS 477/677 28

RB-INSERT-FIXUP – Case 1z’s “uncle” (y) is red

Idea: (z is a left child)

• p[p[z]] (z’s grandparent) must be

black: z and p[z] are both red

• color[p[z]] black

• color[y] black

• color p[p[z]] red

• z = p[p[z]]– Push the “red” violation up the tree

Case1

Page 29: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

29

RB-INSERT-FIXUP – Case 3Case 3: • z’s “uncle” (y) is black• z is a left child

Case 3

Idea:• color[p[z]] black • color[p[p[z]]] red• RIGHT-ROTATE(T, p[p[z]])• No longer have 2 reds in a row• p[z] is now black

Case3

Page 30: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

30

RB-INSERT-FIXUP – Case 2Case 2: • z’s “uncle” (y) is black• z is a right childIdea:• z p[z]• LEFT-ROTATE(T, z) now z is a left child, and both z and p[z] are red case 3

Case 2 Case 3

Case2

Page 31: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

31

RB-INSERT-FIXUP(T, z)1. while color[p[z]] = RED2. do if p[z] = left[p[p[z]]]3. then y ← right[p[p[z]]]4. if color[y] = RED5. then Case16. else if z = right[p[z]]7. then Case28. Case39. else (same as then clause with “right”

and “left” exchanged)10. color[root[T]] ← BLACK

The while loop repeats only whencase1 is executed: O(lgn) times

Set the value of x’s “uncle”

We just inserted the root, orThe red violation reached the root

Page 32: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

32

Example11

Insert 4

2 14

1 157

85

4

y

11

2 14

1 157

85

4

z

Case 1

y

z and p[z] are both redz’s uncle y is redz

z and p[z] are both redz’s uncle y is blackz is a right child

Case 2

11

2

14

1

15

7

8

5

4

zy Case 3

z and p[z] are redz’s uncle y is blackz is a left child

112

141

15

7

85

4

z

Page 33: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

33

Analysis of RB-INSERT

• Inserting the new element into the tree O(lgn)

• RB-INSERT-FIXUP

– The while loop repeats only if CASE 1 is executed

– The number of times the while loop can be executed

is O(lgn)

• Total running time of RB-INSERT: O(lgn)

Page 34: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

DeletionDeletion from a Binary Search tree: IF the node to delete has 2 children, we find the successor

node, move it’s information into the node we want to delete, and remove the successor node.

ELSE we remove the node with the given value.

In either case, the node that is actually removed will have 1 or 0 children.

If it has a child, we set the child’s parent to the parent of the removed node. Else, if no children, we set the nil (leaf) to be the child of the parent of the removed node.

Page 35: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Deletion• Deletion from a red black tree, is similar to deletion for a binary

search tree, with a few exception:– Always set the parent of a deleted node, to be the parent of one

of the deleted nodes children.– Red black fix-up method called if removed node is black.

• After a deletion of a red node (no violations occur):– No black-heights have been affected.

• The simple path from any node to a leaf contain the same # of black nodes.

– No red nodes have been made adjacent (parent and child both red).

– Deleted node is not the root since the root is black.

Page 36: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Delete Con’t

• After Deletion of a Black node a restore function must be called to fix red-black properties that might be violated. There are 3 possible initial violations.– If deleted node was the root, a red child might now be the root, Violation

of property 2.– If both the parent of removed node, and a child of removed node are

red, we have a violation of property 4.– The removal of a black node can cause the black-height of one path to

be shorter (by 1), violating property 5.– We correct the problem of rule 5 by adding an extra “black” to the node

passed into the fix-up procedure. This leads to violations in rules 1 since this node is now neither red or black.

• The goal now is to move the extra black to the root of the tree, and then get rid of the extra black.

Page 37: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.37

13.4 Deletion• RB-DELETE(T,z)

1 if left[z] = nil[T] or right[z] = nil[T]2 then y ← z3 else y ← TREE-SUCCESSOR(z)4 if left[y] ≠ nil[T]5 then x ← left[y]6 else x ← right[y]7 p[x] ← p[y] 8 if p[y] = nil[T]

Page 38: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.38

9 then root[T] ← x10 else if y = left[p[y]]11 then left[p[y]] ← x12 else right[p[y]] ← x13 if y ≠ z14 then key[z] ← key[y]15 copy y’s satellite data into z16 if color[y] = BLACK17 then RB-DELETE-FIXUP(T, x)18 return y

Page 39: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.39

RB-DELETE-FIXUP(T, x)• RB-DELETE-FIXUP

1 while x ≠ root[T] and color[x] = BLACK2 do if x = left[p[x]]3 then w ← right[p[x]]4 if color[w] = RED5 then color[w] ← BLACK Case16 color[p[x]] = RED Case17 LEFT-ROTATE(T,p[x]) Case18 w ← right[p[x]] Case1

Page 40: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.40

9 if color[left[w]] = BLACK and color[right[w]= BLACK

10 then color[w] ← RED Case211 x ← p[x] Case212 else if color[right[w]] = BLACK 13 then color[left[w]] ← BLACK Case314 color[w] ← RED Case315 RIGHT-ROTATE(T,w) Case3 16 w ← right[p[x]] Case317 color[w] ← color[p[x]] Case4

Page 41: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.41

18 color[p[x]] ← BLACK Case419 color[right[w]] ← BLACK Case420 LEFT-ROTATE(T,p[x]) Case421 x ← root[T] Case422 else (same as then clause with “right”

and “left” exchanged)23 color[x] ← BLACK

Page 42: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.42

the case in the while loop of RB-DELETE-FIXUP

Page 43: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

Chapter 13 P.43

Page 44: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

44

65

50 80

10 60 70 90

62

Perform the following deletions, in the order specifiedDelete 90, Delete 80, Delete 70

Page 45: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

45

65

50

10 60

80

62

Delete 90: Case 2

70

Page 46: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

46

65

50

10 60

70

62

Delete 80: Case 0

Page 47: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

47

Delete 70: Case 1, then Case 3, then Case 4

50

6510

60

62

+

50

6510

62 +

60

50

6210

60 65

after Case 1 after Case 3 after Case 4

Page 48: Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In.

48

Red-Black Trees - Summary• Operations on red-black-trees:

– SEARCH O(h)– PREDECESSOR O(h)– SUCCESOR O(h)– MINIMUM O(h)– MAXIMUM O(h)– INSERT O(h)– DELETE O(h)

• Red-black-trees guarantee that the height of the tree will be O(lgn)