2-3-4 Trees and Red-Black Trees

66
1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton

description

2-3-4 Trees and Red-Black Trees. Gordon College Prof. Brinton. Regular Binary Trees. Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40. Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40. Balanced Trees. - PowerPoint PPT Presentation

Transcript of 2-3-4 Trees and Red-Black Trees

Page 1: 2-3-4 Trees and Red-Black Trees

1

2-3-4 Trees and Red-Black Trees

Gordon College

Prof. Brinton

Page 2: 2-3-4 Trees and Red-Black Trees

2

Regular Binary Trees

5

18

35

25

17 75

6

127

209

153

40

5

18

35

25

17

75

6 12

7

20

9

153

40

Depth = 6Average comparisons per search = 4.0

Depth = 4Average comparisons per search = 3.47

(a) (b)

100

100

Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40

Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40

Page 3: 2-3-4 Trees and Red-Black Trees

3

Balanced TreesNeed a new search-tree structure - a balanced binary

search tree

1. Maintains balanced node when adding or removing them

Extra time needed at insert and remove2. Guarantees at worst O(log n) search time

We start with 2-3-4 trees…- Perfectly balanced- Difficult and inefficient to implement

Page 4: 2-3-4 Trees and Red-Black Trees

4

2-3-4 Trees Definitions

2-node - a data value and pointers to 2 subtrees

3-node - two data values and pointers to 3 subtrees

4-node - three data values and pointers to 4 subtrees

A<B

A<B<C

Page 5: 2-3-4 Trees and Red-Black Trees

5

2-3-4 Tree

2 15 35 55

12

254 8 10

115 7 9

2-3-4 trees have search tree propertiesWhat is the basic 2-3-4 search algorithm?

Page 6: 2-3-4 Trees and Red-Black Trees

6

Inserting into a 2-3-4 Tree

A

A B C

S T VU

A

B

S T VUInsert begins with a single nodeand adds elements until it is full Insert another item

1. split the 4 node using the medianvalue as the parent (promoting the medianvalue to the parent level) 2. Insert the new item(inserted based on the BST rules for insertion)(always insert node in leaf of tree)

C

Page 7: 2-3-4 Trees and Red-Black Trees

7

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Page 8: 2-3-4 Trees and Red-Black Trees

8

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 12 152 2 15

Insert 2 Insert 12Insert 15

Page 9: 2-3-4 Trees and Red-Black Trees

9

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 12 152 2 15

Insert 2 Insert 12Insert 15

152

12

Split 4-node (2, 12, 15)

15

12

2 4

Insert 4

Page 10: 2-3-4 Trees and Red-Black Trees

10

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 12 152 2 15

Insert 2 Insert 12Insert 15

152

12

Split 4-node (2, 12, 15)

15

12

2 4

Insert 4

15

12

2 4 8

Page 11: 2-3-4 Trees and Red-Black Trees

11

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 12 152 2 15

Insert 2 Insert 12Insert 15

152

12

Split 4-node (2, 12, 15)

15

12

2 4

Insert 4

15

12

2 4 8

2

4 12

158 2

4 12

158 10

Split 4-node (2, 4, 8) Insert 10

Page 12: 2-3-4 Trees and Red-Black Trees

12

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 12 152 2 15

Insert 2 Insert 12Insert 15

152

12

Split 4-node (2, 12, 15)

15

12

2 4

Insert 4

15

12

2 4 8

2

4 12

158 2

4 12

158 10

Split 4-node (2, 4, 8) Insert 10

2

4 12

8 10 15 25 2

4 12

2

4 12 25

8 10 15 35 2

4 12 25

8 10 15 35 55

15 25 358 10

Insert 25 Insert 35

Insert 55Split 4-node (15, 25, 35)

Page 13: 2-3-4 Trees and Red-Black Trees

13

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 8 10 15 35 55

254

12

2 8 10 11 15 35 55

254

12

Split 4-node (15, 25, 35) Insert 11(4,12,25)

Proactive top-down approach to splitting a 4-node

Page 14: 2-3-4 Trees and Red-Black Trees

14

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 8 10 15 35 55

254

12

2 8 10 11 15 35 55

254

12

Split 4-node (15, 25, 35) Insert 11

2 15 35 55

25

12

4 10

118 2 15 35 55

25

12

4 10

118 9

Split 4-node (8, 10, 11) Insert 9

(4,12,25)

Page 15: 2-3-4 Trees and Red-Black Trees

15

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 15 35 55

25

12

4 10

115 8 9 2 15 35 55

25

12

4 8 10

1195

Insert 5 Split 4-node (5, 8, 9)

Page 16: 2-3-4 Trees and Red-Black Trees

16

2-3-4 Insertion ExampleInsertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

2 15 35 55

25

12

4 8 10

1195 7

2 15 35 55

25

12

4 10

115 8 9 2 15 35 55

25

12

4 8 10

1195

Insert 5 Split 4-node (5, 8, 9)

Page 17: 2-3-4 Trees and Red-Black Trees

17

Another example

Keys: A S E R C H I N G X

What would the 2-3-4 tree look like after inserting this set of keys?

Page 18: 2-3-4 Trees and Red-Black Trees

18

Another example

Keys: A S E R C H I N G X

A

Page 19: 2-3-4 Trees and Red-Black Trees

19

Another example

Keys: A S E R C H I N G X

A S

Page 20: 2-3-4 Trees and Red-Black Trees

20

Another example

Keys: A S E R C H I N G X

A E S

Page 21: 2-3-4 Trees and Red-Black Trees

21

Another example

Keys: A S E R C H I N G X

R S

E

A

Page 22: 2-3-4 Trees and Red-Black Trees

22

Another example

Keys: A S E R C H I N G X

R S

E

A C

Page 23: 2-3-4 Trees and Red-Black Trees

23

Another example

Keys: A S E R C H I N G X

H R S

E

A C

Page 24: 2-3-4 Trees and Red-Black Trees

24

Another example

Keys: A S E R C H I N G X

H I SA C

E R

Page 25: 2-3-4 Trees and Red-Black Trees

25

Another example

Keys: A S E R C H I N G X

H I N SA C

E R

Page 26: 2-3-4 Trees and Red-Black Trees

26

Another example

Keys: A S E R C H I N G X

G H NA C

E I R

S

Page 27: 2-3-4 Trees and Red-Black Trees

27

Another example

Keys: A S E R C H I N G X

G H NA C S X

I

ER

Page 28: 2-3-4 Trees and Red-Black Trees

28

2-3-4 Tree Facts

• With N elements, the max number of nodes visited during the search for an element is int(log2 n) + 1

• Inserting an element into a tree with n elements requires splitting no more than int(log2 n) + 1 4-nodes (often far fewer)

• Problem: allocated a large amount of wasted space3n + 1 unused pointers (n - nodes)

2 15 35 55

25

12

4 8 10

1195 7

28 wasted pointers

Page 29: 2-3-4 Trees and Red-Black Trees

29

2-3-4 Warmup Exercise

Create a 2-3-4 tree from the following sequence of numbers:

45 6 23 5 78 9 10 11 48 99 12 55

Page 30: 2-3-4 Trees and Red-Black Trees

30

2-3-4 Warmup Exercise

Create a 2-3-4 tree from the following sequence of numbers:

45 6 23 5 78 9 10 11 48 99 12 55

Page 31: 2-3-4 Trees and Red-Black Trees

31

Red-Black Trees• Designed to represent

2-3-4 tree without the additional link overhead.

• Colors are used to represent the 3-node and 4-node.

Red-Black trees are simple binary trees with a color indicator that is used to maintain certain properties - these properties keep the tree balanced.

Page 32: 2-3-4 Trees and Red-Black Trees

32

Red-Black Nodes

2-nodes simple binary node

(black node)

4-nodes center value becomes the parent (black) with outside values becoming the children (red)

N

H I N

I

H N

Page 33: 2-3-4 Trees and Red-Black Trees

33

Red-Black Nodes

3-nodes

A B

A

B

B

A

or

Note:1. Red-black trees are not

unique2. However, the

corresponding 2-3-4 tree is unique

Page 34: 2-3-4 Trees and Red-Black Trees

34

Red-Black Nodes

A

B

B

A

N

I

H N

Use color grouping of nodes to indicate the corresponding nodes in the 2-3-4 tree

Page 35: 2-3-4 Trees and Red-Black Trees

35

Converting a 2-3-4 Tree to Red-Black Tree Example

12 15

8 10 20

9 30 401 3 4

Top-down conversion algorithm: (start at the root)1. Apply red-black tree representation to each node2. Repeat for next level…

Page 36: 2-3-4 Trees and Red-Black Trees

36

Converting a 2-3-4 Tree to Red-Black Tree Example

12 159 30 401 3 4

10

8 20

12 15

8 10 20

9 30 401 3 4

Page 37: 2-3-4 Trees and Red-Black Trees

37

Converting a 2-3-4 Tree to Red-Black Tree Example

12 159 30 401 3 4

10

8 20

12 15

8 10 20

9 30 401 3 4

H I N I

H N

Page 38: 2-3-4 Trees and Red-Black Trees

38

Converting a 2-3-4 Tree to Red-Black Tree Example

12 159 30 401 3 4

10

8 20

12 15 30 40

10

8 20

3 9

1 4

Page 39: 2-3-4 Trees and Red-Black Trees

39

Converting a 2-3-4 Tree to Red-Black Tree Example

12 15 30 40

10

8 20

3 9

1 4

10

8 20

3 9

1 4 30

12 40

15

Page 40: 2-3-4 Trees and Red-Black Trees

40

Converting a 2-3-4 Tree to Red-Black Tree Example

12 15 30 40

10

8 20

3 9

1 4

10

8 20

3 9

1 4 30

12 40

15

How could this be different?

Page 41: 2-3-4 Trees and Red-Black Trees

41

Red-Black Tree Properties

1. The Root of a red-black tree is BLACK

2. A RED parent never has a RED child – there are never 2 RED nodes in succession

3. Every path from the root to an empty subtree (NULL pointer) has the same number of BLACK nodes.

a BLACK node corresponds to a level change in the 2-3-4 tree

10

8 20

3 9

1 4 30

12 40

15

12 15

8 10 20

9 30 401 3 4

Page 42: 2-3-4 Trees and Red-Black Trees

42

Inserting Nodes Guidelines

1. Maintain root as BLACK node

2. Enter a new node as a RED node – since each new node enters a 2-node or a 3-node.

3. Whenever it results in two RED nodes in succession – rotate nodes to create a BLACK parent.

4. When scanning down a path to find insertion location – split any 4-node.

10

8 20

3 9

1 4 30

12 40

15

Page 43: 2-3-4 Trees and Red-Black Trees

43

Inserting Nodes

Inserting a 2

3-node

5

12

Page 44: 2-3-4 Trees and Red-Black Trees

44

Inserting Nodes

Inserting a 2

3-node

4-node

5

12

5

122

Page 45: 2-3-4 Trees and Red-Black Trees

45

Inserting Nodes

Inserting a 145

12

Page 46: 2-3-4 Trees and Red-Black Trees

46

Inserting Nodes

Inserting a 145

12

5

12

14

Page 47: 2-3-4 Trees and Red-Black Trees

47

Inserting Nodes

Inserting a 145

12

12

145

5

12

14

Single left rotation

Page 48: 2-3-4 Trees and Red-Black Trees

48

Inserting Nodes

Inserting a 105

12

Page 49: 2-3-4 Trees and Red-Black Trees

49

Inserting Nodes

Inserting a 105

12

5

12

10

Page 50: 2-3-4 Trees and Red-Black Trees

50

Inserting Nodes

Inserting a 105

12

5

12

10

10

125right - left rotation

Page 51: 2-3-4 Trees and Red-Black Trees

51

Splitting a 4-node

Parent P is BLACKX is a left-child

X

A B

P

C

Parent P is RED X is a right-child

Parent P is RED X is a left-child

X

A B

P

C

Parent P is BLACKX is a right-child

X

A B

P

C X

A B

P

C

4 possible situations

Step1: color flip – parent x becomes RED and its two children become BLACK.

Page 52: 2-3-4 Trees and Red-Black Trees

52

Splitting a 4-nodeParent: BLACK

2-3-4 tree viewRed-black tree

before the color flip

A X B

P

C X P

BA

C

X

A B

P

C

Red-black treeafter the color flip 2-3-4 tree view

X

A B

P

C

Color flip is enough.

Page 53: 2-3-4 Trees and Red-Black Trees

53

Splitting a 4-nodeand inserting node 55

2-3-4 tree view

50

40 60

30

2040 50 60 50

40 60

30

20

55

55 60

20 30 50

2-3-4 tree view

4-nodebefore color-flip

4-node after color-flipand insertion of 55

20 30

C D

C D C D

C D 40

Color flip is enough.

Page 54: 2-3-4 Trees and Red-Black Trees

54

Splitting a 4-nodeParent: RED

Single right rotation withpivot P and color changes 2-3-4- tree view

2-3-4 tree view Red-black treebefore color flip

Red-black treeafter color flip

P G

A X B C D

G

BA

X

P

C

D

G

BA

X

P

C

D

G

BA

X

P

C D

X P G

A B C D

Splitting a 4-node oriented left-left from node G using a single right rotation

Page 55: 2-3-4 Trees and Red-Black Trees

55

Splitting a 4-nodeParent: RED

Splitting a 4-node oriented left-right from node G after the color flip

P G

A X B

BA

X

G

P

C

DCD

BA

X

G

P

C

D

2-3-4 tree view Red-black treebefore color flip

Red-black treeafter color flip

Page 56: 2-3-4 Trees and Red-Black Trees

56

Splitting a 4-nodeParent: RED

Splitting a 4-node oriented left-right from node G after the color flip

Red-Black Tree Property Violation?

P G

A X B

BA

X

G

P

C

DCD

BA

X

G

P

C

D

2-3-4 tree view Red-black treebefore color flip

Red-black treeafter color flip

Page 57: 2-3-4 Trees and Red-Black Trees

57

Two REDs: rotations

X

GP

A B

A

P X G

BC

D

C

D

A

G

P

X

B

D

C

SingleLeft-rotationAbout X

P

G

X

A

B

D

C

SingleRight-rotationAbout X

Page 58: 2-3-4 Trees and Red-Black Trees

58

Building a RED-BLACK tree

2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

Page 59: 2-3-4 Trees and Red-Black Trees

59

Building a RED-BLACK tree

2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

Split whenGoing down?

55

11

Page 60: 2-3-4 Trees and Red-Black Trees

60

Building a RED-BLACK tree

2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

Correct5

122

4 node

Page 61: 2-3-4 Trees and Red-Black Trees

61

Building a RED-BLACK tree

2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

Page 62: 2-3-4 Trees and Red-Black Trees

62

Building a RED-BLACK tree

2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

Page 63: 2-3-4 Trees and Red-Black Trees

63

Building a RED-BLACK tree

10, 20, 30, 40, 50, 31, 34, 36

No problem up to this point.What would the 2-3-4 tree look like?

Page 64: 2-3-4 Trees and Red-Black Trees

64

Deleting RED-BLACK nodes

To Delete: same strategy as deleting any node from a binary tree (Chapter 10)

Delete node 25

Replace deleted node with value nearest to deleted value: either 15 or 30

Page 65: 2-3-4 Trees and Red-Black Trees

65

Deleting RED-BLACK nodes

If replace node is RED then level height of tree is unchanged - no other action is needed except making sure color properties are maintained

Page 66: 2-3-4 Trees and Red-Black Trees

66

Deleting RED-BLACK nodes

If replace node is BLACK then adjustments must be made: a bottom-up algorithm is used to flip and rotate. DELETE: O(log n)