Professional Documents
Culture Documents
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 Tree
What is the basic 2-3-4
search algorithm?
12
4
25
10
11
15
35
55
C
A
A
C
V
S
2 15
2 12 15
In s ert 2
In sert 1 5
In s ert 1 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 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 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 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
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
13
35
55
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
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
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
1 2
4
1 0
2 5
1 1
1 5
3 5
5 5
28
29
30