Balanced Trees - 2-3 Tree Balanced Trees 2-3 Tree 2 Node

Balanced Trees
Binary search trees are not guaranteed
to be balanced given random inserts
and deletes
Balanced Trees - 2-3 Tree
!
Tree could degrade to O(n) operations
Balanced search trees
!
2-3 Trees
1
2-3 Tree
!
!
2
2 nodes have one data item and 2
children
O(lg n) operations
Each interior node has two or three
children
!
2-3 Trees
2 Node
Guaranteed to always be balanced
!
Operations maintain a balanced tree
S
Nodes with 2 children are called 2 nodes
Nodes with 3 children are called 3 nodes
NOT A BINARY TREE
Data is stored in both internal nodes
and leaves
2-3 Trees
Search keys < S
3
Search keys >= S
2-3 Trees
4
3 Node
2-3 Tree
3 nodes have two data items and 3
children (a left child, a middle child, and
a right child)
S
L
!
Search keys >= S
and < L
2-3 Trees
5
2-3 Tree
6
Inorder traversal 120 150
70
20
30 40
2-3 Trees
Traversing a 2-3 Tree
50 90
10
Operations take care of that for you
Node class
itemtype smallItem, largeItem
Node *left, *middle, *right, *parent
// you may find that you need other data members
// to make your life easier
// do not add a third item or a fourth child
Search keys >= L
Search keys < S
A leaf may contain 1 or 2 data items
2-3 trees are good because they are
easy to maintain as balanced
60
80
2-3 Trees
100 110 130 140 160
7
inorder (node* cur)
if current
inorder(cur->left)
visit small item if it exists
inorder(cur->middle)
visit large item if it exists
inorder(cur->right)
2-3 Trees
8
Searching a 2-3 Tree
Insert
// Assumes small and large exist. You will need to modify
// to account for nodes with only one value
search (Node* cur, itemtype key)
if (cur)
if (key is in cur)
return cur
else
if (key < cur->small)
search down left child
else if (key > cur->large)
search down right child
else
search down middle child
2-3 Trees
To insert an item, find a leaf to put the
item in then split nodes if necessary
9
Splitting a Leaf
2-3 Trees
Splitting an Internal Node
M P
P
SML
P
10
S
L
n1
n2
M P
P
n
S
a
c d
a b
P M
n1
e
SML
b
S
L
n1
n2
a
If splitting node causes the parent to have 3 items and 4
children, you will then split an internal node…
2-3 Trees
11
e
n2
d
c
P M
P
SML
L
n
a
SML
b c
n1
b
d e
2-3 Trees
S
L
c
d
n2
e
12
Splitting the Root
2-3 Tree
Start with this tree
50
New Root
M
Root
SML
a b
c d
70 90
d
b c
a
30
L
S
2-3 Trees
13
Insert 39
60
40
10 20
80
100
2-3 Trees
14
Insert 38
Locate leaf to insert 38
Locate leaf to insert 39
50
50
30
10 20
70 90
39 40
60
80
30
100
Leaf to insert only has 1 data item
!
10 20
70 90
39 40
60
80
100
Add 39 to the leaf
2-3 Trees
15
2-3 Trees
16
Insert 38
Insert 38
Conceptualize inserting 38 into this leaf
!
Do not actually add the item because the
node can only hold 2 data items
!
!
!
50
30
10 20
Determine
Smallest = 38
Middle = 39
Largest = 40
70 90
38 39 40
60
80
100
2-3 Trees
17
Insert 38
2-3 Trees
Insert 38
Move middle value up to parent p
Separate small and large values into two
separate nodes that will be children of p
50
30 39
50
30
10 20
18
10 20
70 90
38 39 40
2-3 Trees
60
80
38
70 90
40
60
80
100
100
19
2-3 Trees
20
Insert 37
Insert 36
Locate leaf to insert 36
Conceptualize inserting 36 into this leaf
Locate leaf to insert 37
Leaf contains 1 data value, just insert
value
!
50
50
30 39
10 20
37 38
30 39
70 90
40
60
100
80
2-3 Trees
10 20
40 60
36 37 38
80
100
2-3 Trees
22
Insert 36
Conceptualize moving middle value up to
parent p
Conceptualize attaching as children to p the
smallest and largest values
Do not actually move, node can’t have 3 data values
50
30 39
10 20
70 90
21
Insert 36
!
Determine small (36), middle (37), and large
(38)
!
Do not actually attach because a node can’t have 4
children
50
30 37 39
70 90
36 37 38
40 60
2-3 Trees
80
100
10 20
23
36
70 90
38
40 60
2-3 Trees
80
100
24
Insert 36
Insert 36
Parent p now has 3 data values and 4 children
Split - similar to leaf situation where leaf has 3 data
values
! You can generalize both situations into one
Split parent p
!
!
!
Divide to small (30), middle (37), and large (39)
Move middle value to nodes parent
Small and large become new children, s and l
50
50
30 37 39
10 20
36
38
s
70 90
40 60
100
80
10 20
2-3 Trees
l
30 37 39
36
40 60
38
25
Insert 36
70 90
80
100
2-3 Trees
26
Insert 35
Insert 35
Divide 4 children
! Two leftmost become children of s
! Two rightmost become children of l
!
Inserts into leaf
37 50
37 50
39
30
10 20
30
36
2-3 Trees
70 90
70 90
40
38
39
60
80
40
10 20 35 36 38
100
27
2-3 Trees
60
80
100
28
Insert 34
Insert 34
Insert 34
! Causes a split
Insert 34
! Causes a split
37 50
30
10 20
37 50
39
34 35 36
40
38
30
70 90
60
80
2-3 Trees
100
10 20
39
34 35 36
Insert 34
80
2-3 Trees
100
30
Insert 33
!
Inserts into leaf
37 50
37 50
39
30 35
34
60
Insert 33
Insert 34
! Causes a split
10 20
40
38
29
70 90
36
38
2-3 Trees
40
60
80
39
30 35
70 90
100
31
10 20
33 34
36
38
2-3 Trees
70 90
40
60
80
100
32
Insert
Insert (continued)
Insert into a tree without duplicates
insert (itemtype item)
leaf = leaf node to insert item (may be null or have 1 or 2 data items)
if (leaf is null - only happens when root is null)
add new root to tree with item
else if (# data items in leaf = 1)
add item to node
else // leaf has 2 data items
split ( leaf, item )
// Item is to be inserted into n. The insertion
// of item will cause n to have 3 items so n
// must be split
split ( Node* n, itemtype item, … // you may need more )
if ( n is the root )
create a new node p
else
let p be the parent of n
Replace node n with 2 nodes, n1 and n2, so that p is their parent
Give n1 the item in n with the smallest value
Give n2 the item in n with the largest value
// continued on next slide…
2-3 Trees
33
Insert (continued)
2-3 Trees
Insert 32
if (n is not a leaf)
n1 becomes the parent of n’s two leftmost children
n2 becomes the parent of n’s two rightmost children
In class exercise
!
Insert 32 into the tree below
37 50
x = the item in n that has the middle key value
if ( adding x to p would cause p to have 3 items )
split (p, x)
else
add x to p
39
30 35
10 20
2-3 Trees
34
35
33 34
36
38
2-3 Trees
70 90
40
60
80
100
36
Insert 32
Insert 32
37 50
39
30 35
10 20
32 33 34
37 50
36
60
40
38
80
2-3 Trees
39
30 33 35
70 90
100
10 20
37
Insert 32
60
40
38
36
34
32
70 90
80
2-3 Trees
100
38
Insert 32
37
33 37 50
50
33
30
10 20
32 34
39
35
36
38
70 90
40
60
80
30
100
10 20
2-3 Trees
39
32 34
39
35
36
38
2-3 Trees
70 90
40
60
80
100
40
Remove
Remove - Redistribute
With insertion, we split nodes. With
removing, we merge nodes
Deletion process needs to begin with a
leaf but you might be deleting a value
that is not a leaf
!
80 90
70 90
60
80
100
60
-
S
PS
S L
Take from right
X Y
2-3 Trees
S PL
X Y
PS
L
X Y
2-3 Trees
42
Remove - Merge
PL
L
S P
-
S
S L
PS S
PL
-
P
X S
S L
L
P
S
PS PL
41
PS PL
PS PL
-
100
70
Remove - Redistribute
X
L
L
-
2-3 Trees
X Y
S
Swap item to delete with inorder successor
Delete 70
-
P
-
L
43
A
L
B
S A
2-3 Trees
B
44
Remove - Merge
Remove - Redistribute
P
Merge to the
left
S L
L
L
S L
A
-
B
A S
B
a
2-3 Trees
45
a
S
b
c
PS
X Y
d
e
f
g
a
2-3 Trees
c
46
X Y
d
-
P
L
b
d
Remove - Merge
S PL
L
c
b
2-3 Trees
Remove - Redistribute
PS PL
a
d
c
b
P
S
-
e
g
f
47
a
S P
-
S
b
c
a
2-3 Trees
b
c
48
Remove - Merge
Deleting the Root
L
S L
-
New root
Empty root
a
A
b
c
d
a
e
b
S P
B
S A
B
c
d
S P
a
e
b
a
2-3 Trees
2-3 Trees
50
Remove 65
65 is an internal node - swap with
inorder successor
Start with this tree
50
!
30
10 20
c
c
49
2-3 Tree
b
Inorder successor will always be in a leaf
50
65 90
40
60 70 80 100
30
10 20
2-3 Trees
51
65 90
40
2-3 Trees
60 70 80 100
52
Remove 65
Remove 65
65 is now in an invalid location but that
is okay because we will remove it
Since there are 2 data values in the leaf,
just remove data value
50
50
30
10 20
30
70 90
60 65 80 100
40
2-3 Trees
10 20
60
40
53
Delete 70
80
100
2-3 Trees
54
Delete 70
70 is an internal node - swap with
inorder successor
!
70 90
70 is now in an invalid location but that
is okay - we will be removing that node
Inorder successor will always be in a leaf
50
50
30
10 20
30
70 90
40
60
2-3 Trees
80
100
10 20
55
80 90
40
60
2-3 Trees
70
100
56
Delete 70
Delete 70
Removing leaf leaves us with an invalid
2-3 tree
Merge nodes to fix tree
50
50
30
60
40
10 20
-
90
30
80 90
100
2-3 Trees
57
Delete 100
100
2-3 Trees
58
Delete 100
100 is already leaf, just remove leaf
Sibling has data item to spare,
redistribute
50
50
90
30
10 20
60 80
40
10 20
40
60 80
2-3 Trees
90
30
-
10 20
59
40
60 80
2-3 Trees
-
60
Delete 100
Delete 80
Sibling has data item to spare,
redistribute
Swap 80 with inorder successor
50
50
80
30
60
40
10 20
80
30
90
2-3 Trees
61
Delete 80
60
40
10 20
90
2-3 Trees
62
Delete 80
Can’t redistribute so merge nodes
Can’t redistribute so merge nodes
Invalid 2-3 tree, continue recursively up
the tree
50
50
90
30
-
30
10 20
60
40
10 20
2-3 Trees
63
60 90
40
2-3 Trees
64
Delete 80
Delete 80
Can’t redistribute so merge nodes
Can’t redistribute so merge nodes
50
30 50
-
30
10 20
60 90
40
10 20
-
2-3 Trees
65
60 90
2-3 Trees
66
Delete
Delete 80
deleteItem (itemtype item)
node = node where item exists (may be null if no item)
Root is now empty, set new root
pointer
if (node)
if (item is not in a leaf)
swap item with inorder successor (always leaf)
leafNode = new location of item to delete
else
leafNode = node
Root
30 50
10 20
40
40
delete item from leafNode
60 90
if (leafNode now contains no items)
fix (leafNode)
2-3 Trees
67
2-3 Trees
68
Delete
Delete
if ( some sibling of n has two items ) {
Distribute items appropriately among n, the
sibling and the parent (remember take from
right first)
// completes the deletion when node n is empty by either
// removing the root, redistributing values, or merging nodes.
// Note: if n is internal, it has only one child
fix (Node* n, …) //may need more parameters {
if (n is the root) {
remove the root
set new root pointer
}
else {
Let p be the parent of n
2-3 Trees
if ( n is internal ) {
Move the appropriate child from sibling n
(May have to move many children if
distributing across multiple siblings)
}
69
Delete
2-3 Trees
70
Delete
else { //merge nodes
Choose an adjacent sibling s of n (remember,
merge left first)
Bring the appropriate item down from p into s
In class exercise - remove the following
values from the tree in this order
!
37, 70
37 50
if ( n is internal )
move n’s child to s
Remove node n
if ( p is now empty )
fix (p)
} //end if
}//end if
2-3 Trees
39
30 35
10 20
71
33 34
36
38
2-3 Trees
70 90
40
60
80
100
72
Delete 37
39
30 35
33 34
10 20
Delete 37
38 50
36
70 90
60
40
-
80
38 70
100
38 50
10 20
-
30 35
10 20
33 34
36
60
39 40
80
10 20
33 34
73
39 40
39 40
10 20
33 34
90
60
-
30 35
100
-
10 20
33 34
39 40
2-3 Trees
74
36
39 40
80
60
90 100
35 50
50
36
100
80
38 50
38 80
30 35
60
2-3 Trees
Delete 70
50
36
36
100
38 80
30 35
33 34
90
70 90
2-3 Trees
Delete 70
50
30 35
60
30
90 100
10 20
75
33 34
80
38
36
2-3 Trees
39 40
60
90 100
76