red-black tree · red-black tree red-black tree is a self-balancing binary search tree (bst) where...

Post on 21-Aug-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Red-Black Tree

SWE2016-44

Example of a Red-Black Tree

2

Red-Black Tree

Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:

3

1) Every node has a color either red or black

Red-Black Tree

Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:

4

1) Every node has a color either red or black

2) Root of tree is always black

Red-Black Tree

Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:

5

1) Every node has a color either red or black

2) Root of tree is always black

3) There are no two adjacent red nodes (A red node cannot have a red parent or red child)

Red-Black Tree

Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:

6

1) Every node has a color either red or black

2) Root of tree is always black

3) There are no two adjacent red nodes (A red node cannot have a red parent or red child)

4) Every path from root to a NULL node has same number of black nodes

Why Red-Black Trees?

Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST.

→ O(n) for a skewed Binary tree.

7

Why Red-Black Trees?

Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST.

→ O(n) for a skewed Binary tree.

→ Since a Red-Black Tree ensures almost balanced, the height of the tree remains O(Log n) after every insertion and deletion.

→ Red-Black Tree is not always a balanced tree.

8

How does a Red-Black Tree ensure balance?

We can try any combination of colors and see all of them violate Red-Black tree property.

9

How does a Red-Black Tree ensure balance?

We can try any combination of colors and see all of them violate Red-Black tree property.

10

Black Height of a Red-Black Tree

Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also counted black nodes.

11

Black Height of a Red-Black Tree

Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also counted black nodes.

From properties 3 (no two adjacent red nodes) and 4 (same number of black nodes),

Black-height >= h/2.

12

Height of a Red-Black Tree

Every Red Black Tree with n nodes has Height <= 2Log2(n+1).

13

Height of a Red-Black Tree

Every Red Black Tree with n nodes has Height <= 2Log2(n+1).

Proof)

1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).

14

Height of a Red-Black Tree

Every Red Black Tree with n nodes has Height <= 2Log2(n+1).

Proof)

1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).

2) From 1) and property 4, there is a root to leaf path with at most Log2 (n+1) black nodes: h' <= Log2 (n+1)

15

Height of a Red-Black Tree

Every Red Black Tree with n nodes has Height <= 2Log2(n+1).

Proof)

1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).

2) From 1) and property 4, there is a root to leaf path with at most Log2 (n+1) black nodes: h' <= Log2 (n+1)

3) Black-height is at least h/2: h' >= h/2

16

Height of a Red-Black Tree

Every Red Black Tree with n nodes has Height <= 2Log2(n+1).

Proof)

1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).

2) From 1) and property 4, there is a root to leaf path with at most Log2 (n+1) black nodes: h' <= Log2 (n+1)

3) Black-height is at least h/2: h' >= h/2

4) From 2) and 3), h <= 2Log2 (n+1)17

Insertion

The goal of the insert operation is to insert key K into tree T, maintaining T’s red-black tree properties

18

Insertion

If T is a non-empty tree, then we do the following:

1. Use the BST insert algorithm to add K to the tree

2. Color the node containing K red

3. Restore red-black tree properties (if necessary)

19

Insertion

To restore the violated property, we use:

1. Recoloring

2. Rotation (Left, Right, Double)

We try recoloring first, if recoloring doesn’t work, then we go for rotation.

20

Insertion

1) Perform standard BST insertion and make the color of newly inserted nodes as RED.

2) If x is root, change color of x as BLACK (Black height of complete tree increases by 1).

3) Do following if color of x’s parent is not BLACK and x is not root.

21

Insertion

a. If x’s uncle is RED (Grand parent must have been black from property 4)I. Change color of parent and uncle as BLACK.

II. color of grand parent as RED.

III. Change x = x’s grandparent, repeat steps 2 and 3 for new x.

22

Insertion

b. If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g)I. Left Left Case (p is left child of g and x is left child of p)

II. Left Right Case (p is left child of g and x is right child of p)

III. Right Right Case (Mirror of case i)

IV. Right Left Case (Mirror of case ii)

23

Insertion

I. Left Left Case (p is left child of g and x is left child of p)

24

rotateRight(root, g)

swap(p→color, g→color)

Insertion

II. Left Right Case (p is left child of g and x is right child of p)

25

rotateLeft(root, p)

rotateRight(root, g)

swap(x→color, g→color)

Insertion

III. Right Right Case (Mirror of case i)

26

rotateLeft(root, g)

swap(p→color, g→color)

Insertion

IV. Right Left Case (Mirror of case ii)

27

rotateRight(root, p)

rotateLeft(root, g)

swap(x→color, g→color)

Insertion Analysis

• Go up the tree performing Case 3-a), which only recolors nodes.

• If Case 3-b) occurs, perform 1 or 2 rotations, and terminate.

→ Running time: O(log n) with O(1) rotations.

28

Example of Insertion

29

11

1

14

2

7

15

Example of Insertion

30

11

1

14

2

7

15

Insert 1111

Example of Insertion

31

11

1

14

2

7

15

Insert 1111

Example of Insertion

32

11

1

14

2

7

15

Insert 1

1

11

Example of Insertion

33

11

1

14

2

7

15

Insert 14

1

11

14

Example of Insertion

34

11

1

14

2

7

15

Insert 2

1

11

14

2

Example of Insertion

35

11

1

14

2

7

15

Insert 2

1

11

14

2

If X’s uncle is RED and X’s parent is not BLACK,

change color of parent and uncle as BLACK

Example of Insertion

36

11

1

14

2

7

15

Insert 2

1

11

14

2

Example of Insertion

37

11

1

14

2

7

15

Insert 2

1

11

14

2

Color of Grand parent as RED

Example of Insertion

38

11

1

14

2

7

15

Insert 2

1

11

14

2

Example of Insertion

39

11

1

14

2

7

15

Insert 2

1

11

14

2

As 11 is a root node, change its color to black

Example of Insertion

40

11

1

14

2

7

15

Insert 2

1

11

14

2

Example of Insertion

41

11

1

14

2

7

15

Insert 7

1

11

14

2

7

Example of Insertion

42

11

1

14

2

7

15

Insert 7

1

11

14

2

7

Left rotate(2) and recolor nodes

Example of Insertion

43

11

1

14

2

7

15

Insert 7

2

11

14

71

Example of Insertion

44

11

1

14

2

7

15

Insert 7

2

11

14

71

Example of Insertion

45

11

1

14

2

7

15

Insert 15

2

11

14

71 15

Exercise

46

Insert 3, 1, 5, 7, 6, 8, 9, 10

Exercise

47

Insert 3, 1, 5, 7, 6, 8, 9, 10

3

6

8

51 97

10

Deletion

Like Insertion, recoloring and rotations are used to maintain the Red-Black properties.

In delete operation, we check color of sibling to decide the appropriate case.

48

Deletion Algorithm

1. Perform standard BST delete- A node which is either leaf or has only one child is deleted

- For an internal node: Copy the successor → call delete for successor

49

Deletion Algorithm

1. Perform standard BST delete- A node which is either leaf or has only one child is deleted

- For an internal node: Copy the successor → call delete for successor

2. Handle cases where a node is leaf or has one child

50

Deletion Algorithm

1. Perform standard BST delete- A node which is either leaf or has only one child is deleted

- For an internal node: Copy the successor → call delete for successor

2. Handle cases where a node is leaf or has one child

3. Let v be the node to be deleted and u be the child that replaces v- Note that u is NULL when v is a leaf and color of NULL is considered

as Black

51

Deletion Algorithm

a) If Either u or v is RED- Mark the replaced child as black

b) If Both u and v are BLACK- Color u as a double black

- If u is root, make it single black and return52

v

u

v

uor → u

v

u→ u

a) If Either u or v is RED

b) If Both u and v are BLACK

Deletion Algorithm

53

30

20 40

10

30

10 40v

u

Delete 20

30

20 40

30

NULL 40vDelete 20

50NULL NULL 50u

u

u

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

- Case 2: If s is black and its both children are BLACK

- Case 3: If s is RED

54

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

- Let the red child of s be r

I. Left Left Case (Mirror of III)

II. Left Right Case (Mirror of IV)

III. Right Right Case

IV. Right Left Case

55

30

NULL 40

50

su

r

p

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

III. Right Right Case (r → outer node)

56

30

20 40v

5035u

Delete 20

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

III. Right Right Case (r → outer node)

57

30

20 40v

5035

30

40u

5035

NULL

u

Delete 20

s

r

p

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

III. Right Right Case (r → outer node)

58

30

20 40v

5035

30

40u

5035

NULL

u

Delete 20

s

r

p Rotate Left (p)

Color exchange

(p&r→s, s→p)

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

III. Right Right Case (r → outer node)

59

30

20 40v

5035

30

40u

5035

NULL

u

40

50

35

30

Delete 20

s

r

p Rotate Left (p)

Color exchange

(p&r→s, s→p)

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

60

30

20 40v

35u

Delete 20

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

61

30

20 40v

35

30

40u

35

NULL

u

Delete 20

s

r

p

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

62

30

20 40v

35

30

40u

35

NULL

u

Delete 20

s

r

p Rotate Right (s)

Color exchange (r, s)

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

63

30

20 40v

35

30

40u

35

NULL

u

Delete 20

s

r

p 30

35u

40

NULL r

s

pRotate Right (s)

Color exchange (r, s)

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

64

30

20 40v

35

30

40u

35

NULL

u

Delete 20

s

r

p 30

35u

40

NULL s

r

pRotate Right (s)

Color exchange (r, s)

Same as case III

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

65

30

20 40v

35

30

40u

35

NULL

u

Delete 20

s

r

p 30

35u

40

NULL s

r

pRotate Right (s)

Color exchange (r, s)

Same as case III

Rotate Left (p)

Color exchange

(p&r→s, s→p)

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 1: If s is black and at least one of s’s children is RED

IV. Right Left Case

66

30

20 40v

35

30

40u

35

NULL

u

Delete 20

s

r

p 30

35u

40

NULL s

r

pRotate Right (s)

Color exchange (r, s) 35

30 40

Same as case III

Rotate Left (p)

Color exchange

(p&r→s, s→p)

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED

2) If parent is BLACK, recur for parent

67

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED

2) If parent is BLACK, recur for parent

68

30

20 40v

u

Delete 20

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED

2) If parent is BLACK, recur for parent

69

30

20 40v

u

Delete 20

30

40u NULL s

p

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED

2) If parent is BLACK, recur for parent

70

30

20 40v

u

Delete 20

30

40u NULL s

ps → RED

p → DB

30

40NULL

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED

2) If parent is BLACK, recur for parent

71

30

20 40v

u

Delete 20

30

40u NULL s

ps → RED

p → BLACK

30

40NULL

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

72

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

73

Delete 20

30

20 40v

5035u

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

74

Delete 20

30

20 40v

5035u

30

NULL 40u

5035

s

p

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

75

Delete 20

Rotate

p → RED

s → BLACK

30

20 40v

5035u

30

NULL 40u

5035

s

p 40

50

35

30

NULL

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

76

Delete 20

Rotate

p → RED

s → BLACK

30

20 40v

5035u

30

NULL 40u

5035

s

p 40

50

35

30

NULLu

p

s

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

77

Delete 20

Rotate

p → RED

s → BLACK

30

20 40v

5035u

30

NULL 40u

5035

s

p 40

50

35

30

NULLu

p

s

Case 2

Deletion Algorithm

b) If Both u and v are BLACK- Let sibling of node be s

- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK

2) Case I or Case II

78

Delete 20

Rotate

p → RED

s → BLACK

30

20 40v

5035u

30

NULL 40u

5035

s

p 40

50

35

30

NULLu

p

s

Case 2 40

50

35

30

NULLu

p

s

Deletion Analysis

• Case 2 is the only case in which more iterations occur.

→ u moves up 1 level. Hence, O(log n) iterations.

• Each of cases 1 and 3 has at most ≤ 3 rotations in all

→ Running time: O(log n)

79

Example of Deletion (1)

80

Example of Deletion (2)

81

Example of Deletion (3)

82

Example of Deletion (4)

83

Example of Deletion (5)

84

Example of Deletion (6)

85

Example of Deletion (7)

86

Example of Deletion (8)

87

Example of Deletion (9)

88

Example of Deletion (10)

89

Reference

• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September 29, 2004

• https://www.geeksforgeeks.org

top related