You are on page 1of 30

2-3-4 Trees

Regular Binary Trees


D ep th = 4
A v e r a g e c o m p a r is o n s p e r s e a r c h = 3 .4 7

D ep th = 6
A v e r a g e c o m p a r is o n s p e r s e a r c h = 4 .0
5

15
20

12

17

75

18

20

15

75

18

100

25

25

35

(a)

(b )
40

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

100

35

17

12

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

40

Balanced Trees
Need 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 remove
2. Guarantees at worst O(log n) search time
We start with 2-3-4 trees
- Perfectly balanced
- Difficult and inefficient to implement

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
A<B

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


A<B<C

2-3-4 Tree
What is the basic 2-3-4
search algorithm?

2-3-4 trees have search tree properties

12
4

25

10

11

15

35

55

Inserting into a 2-3-4 Tree


B
A

C
A

A
C

V
S

Insert begins with a single node


and adds elements until it is full

Insert another item


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

2-3-4 Insertion Example


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

2-3-4 Insertion Example


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

2 15

2 12 15

In s ert 2

In sert 1 5

In s ert 1 2

2-3-4 Insertion Example


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

2 15

2 12 15

In s ert 2

In sert 1 5

In s ert 1 2

12

12

15

S p lit 4 - n o d e ( 2 , 1 2 , 1 5 )

15

In s ert 4

2-3-4 Insertion Example


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

2 15

2 12 15

In s ert 2

In sert 1 5

In s ert 1 2

12

12

15

S p lit 4 - n o d e ( 2 , 1 2 , 1 5 )

15

In s ert 4
1 2

1 5

10

2-3-4 Insertion Example


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

2 15

2 12 15

In s ert 2

In sert 1 5

In s ert 1 2

12

15

S p lit 4 - n o d e ( 2 , 1 2 , 1 5 )

12

15

S p lit 4 - n o d e ( 2 , 4 , 8 )

12

12

10

In s ert 1 0

15

In s ert 4
1 2

1 5

11

15

2-3-4 Insertion Example


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

2 15

2 12 15

In s ert 2

In sert 1 5

In s ert 1 2

12

15

S p lit 4 - n o d e ( 2 , 1 2 , 1 5 )

15

1 5

15

25

12

10

12

10

15

In s ert 1 0

12

10

12

10

15

25 35

In sert 3 5

In sert 2 5

In s ert 4
1 2

15

S p lit 4 - n o d e ( 2 , 4 , 8 )

12

12

25

15

35

12

25

10

In sert 5 5

S p lit 4 - n o d e ( 1 5 , 2 5 , 3 5 )

12

15

35

55

2-3-4 Insertion Example


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

12

25

10

15

S p l i t 4 - n o d e ( 1(4,12,25)
5, 25, 35)

35

55

25

10

11

15

In s ert 1 1

Proactive top-down approach to splitting a 4-node

13

35

55

2-3-4 Insertion Example


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

12

25

15

10

35

55

25

10

S p l i t 4 - n o d e ( 1(4,12,25)
5, 25, 35)

11

15

12

10

25

11

55

In s ert 1 1

12
4

35

15

S p lit 4 - n o d e ( 8 , 1 0 , 1 1 )

35

55

10

25

11
14

In sert 9

15

35

55

2-3-4 Insertion Example


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

12

10

25

11

In sert 5

15

35

55

8 10

25

11

15

S p l it 4 - n o d e ( 5 , 8 , 9 )

15

35

55

2-3-4 Insertion Example


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

12

10

25

11

15

35

55

In sert 5

8 10

25

11

15

35

S p l it 4 - n o d e ( 5 , 8 , 9 )
1 2

1 0

2 5

1 1

1 5

16

3 5

5 5

55

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?

17

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

18

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

19

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

20

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

RS

21

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

RS

22

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

HRS

23

Another example
Keys: A S E R C H I N G X
ER
AC

HI

24

Another example
Keys: A S E R C H I N G X
ER
AC

HIN

25

Another example
Keys: A S E R C H I N G X
E IR
AC

GH

26

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

AC

R
GH

SX

27

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 4nodes (often far fewer)
Problem: allocated a large amount of wasted space
3n + 1 unused pointers (n - nodes)
28 wasted
pointers

1 2
4

1 0

2 5

1 1

1 5

3 5

5 5

28

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

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

You might also like