(2,4) Trees
(2,4) Trees
Multi-way Searching
Similar to binary searching
If search key s < k 1 , search the leftmost child
If s > k d 1 , search the rightmost child
Thats it in a binary tree; what about if d > 2 ?
Find two keys k i 1 and k i between which s falls,
and search the child v i .
Searching
for s = 8
10
22
Searching
for s = 12
14
11 13
25
23 24
27
17 18 19 20 21
Not found!
(2,4) Trees
(2,4) Trees
At most 4 children
All external nodes have same depth
Height h of (2,4) tree is O ( log n ) .
How is this fact useful in searching?
12
5 10
(2,4) Trees
15
11
13 14
17
(2,4) Insertion
Always maintain depth condition
Add elements only to existing nodes
Empty
tree
Insert 4
Insert 6
Insert 12
4
6 12
Insert 15
(2,4) Trees
12
12
6 12 15
rt
e
s
n
I
15
15
12
12
Insert 5
15
12
15
5 12
5
3
15
15
11
5 10 12
13 14 15 17
15
11
13 14
17
12
5 10
15
11
13 14
17
(2,4) Deletion
A little trickier
First of all, find the key
- simple multi-way search
Then, reduce to the case where deletable item is at
the bottom of the tree
- Find item which precedes it in in-order traversal
- Swap them
Remove the item
Delete 13
11
15
13
8 10
14
17
Easy, right?
...but what about removing from 2-nodes?
(2,4) Trees
Delete 4
4
11
u
10
5
v
w
8
11
6 10
12
8
u
6
10
v
8
u
6
8 10
10
11
6
14
15
17
8 10
11
6
u
15
v
17
8 10
11
u
6
5
8 10
15 17
6 11
5
(2,4) Trees
8 10
15 17
11
(2,4) Conclusion
The height of a (2,4) tree is O ( log n ) .
Split, transfer, and fusion each take O ( 1 ) .
Search, insertion and deletion each take O ( log n ) .
Why are we doing this?
- (2,4) trees are fun! Why else would we do it?
- Well, theres another reason, too.
- Theyre pretty fundamental to the idea of Red-Black trees as
well.
- And youre covering Red-Black trees on Monday.
- Perhaps more importantly, your next project is a Red-Black tree.
(2,4) Trees
12