66
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

  • View
    220

  • Download
    2

Embed Size (px)

Citation preview

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

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

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

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?

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

7

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

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

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

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

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

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)

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

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)

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)

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)

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?

18

Another example

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

A

19

Another example

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

A S

20

Another example

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

A E S

21

Another example

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

R S

E

A

22

Another example

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

R S

E

A C

23

Another example

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

H R S

E

A C

24

Another example

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

H I SA C

E R

25

Another example

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

H I N SA C

E R

26

Another example

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

G H NA C

E I R

S

27

Another example

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

G H NA C S X

I

ER

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

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

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

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.

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

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

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

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…

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

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

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

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

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?

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

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

43

Inserting Nodes

Inserting a 2

3-node

5

12

44

Inserting Nodes

Inserting a 2

3-node

4-node

5

12

5

122

45

Inserting Nodes

Inserting a 145

12

46

Inserting Nodes

Inserting a 145

12

5

12

14

47

Inserting Nodes

Inserting a 145

12

12

145

5

12

14

Single left rotation

48

Inserting Nodes

Inserting a 105

12

49

Inserting Nodes

Inserting a 105

12

5

12

10

50

Inserting Nodes

Inserting a 105

12

5

12

10

10

125right - left rotation

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.

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.

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.

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

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

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

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

58

Building a RED-BLACK tree

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

59

Building a RED-BLACK tree

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

Split whenGoing down?

55

11

60

Building a RED-BLACK tree

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

Correct5

122

4 node

61

Building a RED-BLACK tree

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

62

Building a RED-BLACK tree

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

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?

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

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

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)