Académique Documents
Professionnel Documents
Culture Documents
Robert Guderian
Robert Guderian
2-3 Trees
Introduction
A 2-3 tree is not a binary tree: some nodes can have three
children.
A 2-3 tree is useful for implementing tables because the trees
are always short.
A 2-3 tree containing n keys is guaranteed to have height
O(log n), and its search, insert and delete algorithms all take
O(log n) time.
Robert Guderian
2-3 Trees
A 2-3 tree of
height h 1
Robert Guderian
A 2-3 tree of
height h 1
2-3 Trees
2-3 tree of
height h 1
2-3 tree of
height h 1
Robert Guderian
2-3 tree of
height h 1
2-3 Trees
Robert Guderian
2-3 Trees
The data are stored in the leaves of the tree they are in
sorted order if you look at the leaves from left to right.
The internal nodes of the tree are like the index in a book.
The internal nodes contain index keys to guide your searches
to the leaf containing the data you want.
Robert Guderian
2-3 Trees
Node v
< k1
k1
TL
TR
Robert Guderian
2-3 Trees
< k1
TL
Node v
k1
< k2
TM
k2
TR
2-3 Trees
31
15,23
9
2
60
27,29
20
9
15
20
23
Robert Guderian
27
40,47
29
2-3 Trees
31
40
67
47
60
67
Robert Guderian
2-3 Trees
Robert Guderian
2-3 Trees
Robert Guderian
2-3 Trees
20
9
15
Robert Guderian
20
23
2-3 Trees
27
29
15,23
9
2
20
9
15
20
Robert Guderian
23
2-3 Trees
27
29
27,29
20
9
15
Robert Guderian
20
23
2-3 Trees
27
29
18 < 20
9
2
20
9
15
20
Robert Guderian
23
2-3 Trees
27
29
27,29
20
9
15
20
23
27
29
Robert Guderian
2-3 Trees
27,29
20
9
15
20
23
18
Robert Guderian
2-3 Trees
27
29
kM
Robert Guderian
kR
2-3 Trees
Leaves
15,23
18,20
9
2
15
18
Robert Guderian
27,29
20
2-3 Trees
23
27
29
9
2
15
18
27,29
20
23
27
29
We end at leaf 27, so its parent (27,29) is the parent of the new
leaf.
Robert Guderian
2-3 Trees
Insert 28. The parent of the new leaf already has three children:
15,23
18,20
9
2
15
18
27,29
20
23
27
29
28
Robert Guderian
2-3 Trees
To resolve the problem of a parent with three children and too full
to accept another child:
Split the four children into the two smaller children and the
two larger children.
The two smaller children stay with the parent, which becomes
a node with just two children.
Create a new node to be the parent of the two larger children.
Robert Guderian
2-3 Trees
9
2
15
18
27,29
20
23
27
?
28
29
The old parent should have just one index key, and the new parent
needs an index key and to be connected to its parent.
Robert Guderian
2-3 Trees
At the level above the leaves, we can get the correct index
keys by simply looking at the data values in the leaves.
The three largest values (27, 28 and 29) in the four children
become index key values in the parents and the parent of the
parents (the grandparent).
The original parent should have index key 27 (the value of its
larger child) and the new parent should have index key 29, the
value of its larger child.
We also pass the middle value (28) of the three values that
become index keys up to the grandparent to be the index key
separating the old parent from the new parent.
Robert Guderian
2-3 Trees
9
2
15
18
27
20
23
29
27
28
29
Robert Guderian
2-3 Trees
9
2
15
18
27
20
23
29
27
28
29
What index keys should be in the old grandparent and the new
grandparent? Because the old grandparent was the root, theres no
parent to which to attach them. How can we attach the new
grandparent into the tree?
Robert Guderian
2-3 Trees
kmid
kmin , kmid , kmax
becomes
T1 T2 T3 T4
Robert Guderian
2-3 Trees
kmin
kmax
T1 T2
T3 T4
root
kmid
becomes
T1 T2 T3 T4
Robert Guderian
2-3 Trees
kmin
kmax
T1 T2
T3 T4
28
18,20
9
2
15
18
Robert Guderian
27
20
23
2-3 Trees
29
27
28
29
Insertion Examples
Robert Guderian
2-3 Trees
Insertion Examples
Now insert 68. A leaf can have only one data value, so we split
and make a new root:
68
34,68
becomes
Robert Guderian
2-3 Trees
34
68
Insertion Examples
Now insert 1. Theres room in the parent for another child, so this
is an easy case:
34,68
1
Robert Guderian
34
68
2-3 Trees
Insertion Examples
34
becomes
68
Robert Guderian
2-3 Trees
68
2
34
68
Insertion Examples
Next insert 88. Theres room in the parent for another child, so we
have an easy case:
34
68,88
2
1
Robert Guderian
34
68
2-3 Trees
88
Insertion Examples
Next insert 56. Theres no room in the parent for another child, so
we have to split the parent:
34
56,68,88
2
1
becomes . . .
34
Robert Guderian
56
2-3 Trees
68
88
Insertion Examples
Next insert 56. Theres no room in the parent for another child, so
we have to split the parent. Fortunately, theres room in the
grandparent for the new parent:
34,68
2
1
56
2
34
Robert Guderian
88
56
2-3 Trees
68
88
Insertion Examples
Next insert 94. Theres room in the parent for another child, so we
have an easy case:
34,68
2
1
88,94
56
2
34
Robert Guderian
56
68
2-3 Trees
88
94
Insertion Examples
Next insert 72. Theres no room in the parent for another child, so
we have to split the parent:
34,68
2
1
72,88,94
56
2
34
56
Robert Guderian
68
2-3 Trees
72
88
94
Insertion Examples
Next insert 72. Theres no room in the parent for another child, so
we have to split the parent. Unfortunately, theres no room in the
grandparent (the root), so we have to split the grandparent and
create a new root:
34,68,88
2
1
56
2
34
72
56
Robert Guderian
68
2-3 Trees
94
72
88
94
Insertion Examples
Next insert 72. Theres no room in the parent for another child, so
we have to split the parent. Unfortunately, theres no room in the
grandparent (the root), so we have to split the grandparent and
create a new root:
68
34
88
2
1
56
2
34
72
56
Robert Guderian
68
2-3 Trees
94
72
88
94
Insertion Examples
Next insert 45. Theres room in the parent for another child, so we
have an easy case:
68
34
88
45,56
2
1
34
45
Robert Guderian
72
56
68
2-3 Trees
94
72
88
94
Insertion Examples
Finally insert 39. Theres no room in the parent for another child,
so we have to split the parent:
68
34
39,45,56
2
1
88
34
39
45
Robert Guderian
72
56
2-3 Trees
68
94
72
88
94
Insertion Examples
Finally insert 39. Theres no room in the parent for another child,
so we have to split the parent. Fortunately, theres room in the
grandparent for the new parent:
68
34,45
2
1
88
39
2
34
56
39
45
Robert Guderian
72
56
2-3 Trees
68
94
72
88
94
Insertions
Robert Guderian
2-3 Trees
Deletion
Robert Guderian
2-3 Trees
Delete 68. The parent of leaf 68 has three children, so it can lose
one child and still be a valid node:
34,68
2
1
88,94
56
2
34
56
68
88
94
The parent will have only two children and should therefore only
have one index key. What should the index key be?
Robert Guderian
2-3 Trees
The parent of two leaves should contain as its index key the
key from the larger child.
When one of three leaf children is deleted, the value from the
largest remaining child becomes the parents index key.
kR
kL
kL Leaves
Robert Guderian
2-3 Trees
56
2
34
Robert Guderian
94
56
2-3 Trees
88
94
Delete 56:
34,68
2
1
88,94
56
2
34
56
68
88
94
When we delete 56, its parent will have only one child, which is
not allowed.
Robert Guderian
2-3 Trees
88,94
56
2
34
68
88
94
Robert Guderian
2-3 Trees
88,94
56
2
34
68
88
94
The index keys in the parent, the sibling, and their parent have to
be adjusted somehow.
Robert Guderian
2-3 Trees
Heres how the index keys get changed, if the sibling is to the right
and kg is the key in the grandparent separating the two siblings:
kg
kg
kl , kr
ka
A
kl
becomes
Robert Guderian
2-3 Trees
kr
B
68
2
34
Robert Guderian
94
68
2-3 Trees
88
94
Delete 68:
56,88
2,34
1
68
34
56
94
68
88
94
When we delete 68, its parent will have only one child, which is
not allowed.
Robert Guderian
2-3 Trees
After deleting 68, its parent has only one child, but it can adopt the
nearest child from the sibling to the left, which has three children:
56,88
2,34
1
68
34
56
94
88
94
When the parent with only one child adopts its left siblings
nearest child, we will have to adjust index keys in the parent, the
sibling, and the grandparent.
Robert Guderian
2-3 Trees
Heres how the index keys get changed, if the sibling is to the left
and kg is the key in the grandparent separating the two siblings:
kg
kl , kr
A
kr
becomes
kd
Robert Guderian
kg
kl
A
2-3 Trees
After deleting 68 and the parent adopts the nearest child from the
sibling to the left, the index keys are as follows:
34,88
2
1
56
2
34
Robert Guderian
94
56
2-3 Trees
88
94
56
2
34
94
56
88
94
No sibling has three children, so the parent with only one child
cannot adopt a spare child. Instead, its sole child must be adopted
by one of the parents siblings.
Robert Guderian
2-3 Trees
56
2
34
94
88
94
Lets choose the parents sibling to the left to adopt its parents
sole child.
Robert Guderian
2-3 Trees
After deleting 56, its parents sole remaining child (34) is adopted
by the parents sibling to the left:
34,88
2
1
94
34
88
94
Now the sibling and its parents index keys must be fixed.
Robert Guderian
2-3 Trees
ks
A
becomes
ks , kg
Note that the granpdarent may now have only one child (if it had
only two children before). This one-child problem may repeat all
the way up to the root.
Robert Guderian
2-3 Trees
After deleting 56, its parents sole remaining child (34) was
adopted by the parents sibling to the left. The grandparent
originally had three children and now has two, so the deletion is
complete:
88
2,34
1
Robert Guderian
94
34
88
2-3 Trees
94
Delete 25:
25
15
35
10
5
20
10
15
30
20
Robert Guderian
25
2-3 Trees
40
30
35
40
After deleting 25, its parent (30) has only one remaining child:
25
15
35
10
5
20
10
15
Robert Guderian
30
20
30
2-3 Trees
40
35
40
35
10
5
35,40
20
10
15
20
30
35
40
Robert Guderian
2-3 Trees
35,40
20
10
15
20
30
Robert Guderian
2-3 Trees
35
40
If the root has only one child (because one of its children had a
sole remaining child that was adopted by a sibling), simply delete
the root and make its child the new root.
root
becomes
root
ks , kg
A
ks , kg
C
Robert Guderian
2-3 Trees
35,40
20
10
15
20
30
Robert Guderian
2-3 Trees
35
40
Deletion Examples
Delete 56:
68
34,45
2
1
88
39
2
34
56
39
45
72
56
68
Robert Guderian
2-3 Trees
94
72
88
94
Deletion Examples
After deleting 56, the parent has only one remaining child:
68
34,45
2
1
88
39
2
34
56
39
72
45
68
94
72
88
94
The parents adjacent sibling does not have an extra child, so the
adjacent sibling must adopt the parents remaining child.
Robert Guderian
2-3 Trees
Deletion Examples
After deleting 56, the parents sibling adopts the parents only
remaining child:
68
34
88
39,45
2
1
34
39
72
45
68
94
72
88
94
(The siblings extra index key came down from its parent.)
Robert Guderian
2-3 Trees
Deletion Examples
Delete 68:
68
34
88
39,45
2
1
34
39
72
45
68
Robert Guderian
2-3 Trees
94
72
88
94
Deletion Examples
After deleting 68, the parents sibling has adoped the parents
remaining child (72):
68
34
88
39,45
2
1
34
39
88,94
45
72
Robert Guderian
2-3 Trees
88
94
Deletion Examples
2
1
34
39
88,94
45
Robert Guderian
2-3 Trees
72
88
94
Deletion Examples
The old root is deleted and its sole child is the new root:
34,68
39,45
2
1
34
39
Robert Guderian
88,94
45
2-3 Trees
72
88
94
Deletion Examples
Delete 1:
34,68
39,45
2
1
34
39
Robert Guderian
88,94
45
2-3 Trees
72
88
94
Deletion Examples
88,94
45
34
39
Robert Guderian
45
72
2-3 Trees
88
94
Deletion Examples
88,94
45
34
39
Robert Guderian
45
72
2-3 Trees
88
94
Deletion Examples
45
34
39
Robert Guderian
94
45
72
2-3 Trees
94
Deletion Examples
45
34
39
94
45
Robert Guderian
2-3 Trees
72
94
Deletion Examples
After deleting 45, its parent had only one child, and the parents
sibling to the right adopted the parents remaining child:
39
68,94
34
2
34
Robert Guderian
39
72
2-3 Trees
94
Deletion Examples
Now delete 2:
39
68,94
34
2
34
39
72
Robert Guderian
2-3 Trees
94
Deletion Examples
After deleting 2, its parent adopts an extra child from the parents
adjacent sibling:
68
39
34
94
39
Robert Guderian
72
2-3 Trees
94
Deletion Examples
94
39
72
94
Robert Guderian
2-3 Trees
Deletion Examples
After deleting 94, its parents sibling adopts the parents remaining
child:
68
39,68
34
39
72
Robert Guderian
2-3 Trees
Deletion Examples
The old root is deleted and its sole child becomes the new root:
39,68
34
Robert Guderian
39
72
2-3 Trees