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

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

1

2-3-4 Trees and Red-Black Trees

Gordon College

Prof. Brinton

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

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

7

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

Page 8: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

18

Another example

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

A

Page 19: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

19

Another example

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

A S

Page 20: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

20

Another example

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

A E S

Page 21: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

21

Another example

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

R S

E

A

Page 22: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

22

Another example

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

R S

E

A C

Page 23: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

23

Another example

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

H R S

E

A C

Page 24: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

24

Another example

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

H I SA C

E R

Page 25: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

25

Another example

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

H I N SA C

E R

Page 26: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

26

Another example

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

G H NA C

E I R

S

Page 27: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

27

Another example

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

G H NA C S X

I

ER

Page 28: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

43

Inserting Nodes

Inserting a 2

3-node

5

12

Page 44: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

44

Inserting Nodes

Inserting a 2

3-node

4-node

5

12

5

122

Page 45: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

45

Inserting Nodes

Inserting a 145

12

Page 46: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

46

Inserting Nodes

Inserting a 145

12

5

12

14

Page 47: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

47

Inserting Nodes

Inserting a 145

12

12

145

5

12

14

Single left rotation

Page 48: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

48

Inserting Nodes

Inserting a 105

12

Page 49: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

49

Inserting Nodes

Inserting a 105

12

5

12

10

Page 50: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

50

Inserting Nodes

Inserting a 105

12

5

12

10

10

125right - left rotation

Page 51: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

58

Building a RED-BLACK tree

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

Page 59: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

61

Building a RED-BLACK tree

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

Page 62: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

62

Building a RED-BLACK tree

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

Page 63: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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: 1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

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)