Professional Documents
Culture Documents
S L e n1 S L n2 e
SML n SML
n1 n2
a b c d
a b c d
P P M
P P M
SML
S L
a
n1 n2 a n SML
n1 S L n2
Insert 39 Insert 38
Locate leaf to insert 39 Locate leaf to insert 38
50
50
30 70 90
30 70 90
10 20 39 40 60 80 100
30 70 90
10 20 38 39 40 60 80 100
Insert 38 Insert 38
Move middle value up to parent p
Separate small and large values into two 50
separate nodes that will be children of p
50 30 39 70 90
10 20 38 40 60 80 100
30 70 90
10 20 38 39 40 60 80 100
30 39 70 90 30 39 70 90
10 20 37 38 40 60 80 100 10 20 36 37 38 40 60 80 100
Insert 36 Insert 36
Conceptualize moving middle value up to Conceptualize attaching as children to p the
parent p smallest and largest values
! Do not actually move, node can’t have 3 data values ! Do not actually attach because a node can’t have 4
children
50 50
30 39 70 90 30 37 39 70 90
10 20 36 37 38 40 60 80 100 10 20 36 38 40 60 80 100
s l
30 37 39 70 90 30 37 39 70 90
10 20 36 38 40 60 80 100 10 20 36 38 40 60 80 100
Insert 36 Insert 35
Divide 4 children Insert 35
! Two leftmost become children of s ! Inserts into leaf
! Two rightmost become children of l
37 50
37 50
30 39 70 90
30 39 70 90
10 20 35 36 38 40 60 80 100
10 20 36 38 40 60 80 100
37 50 37 50
30 39 70 90 30 39 70 90
10 20 34 35 36 38 40 60 80 100 10 20 34 35 36 38 40 60 80 100
Insert 34 Insert 33
Insert 34 Insert 33
! Causes a split
! Inserts into leaf
37 50 37 50
30 35 39 70 90 30 35 39 70 90
10 20 34 36 38 40 60 80 100 10 20 33 34 36 38 40 60 80 100
37 50 37 50
30 35 39 70 90 30 33 35 39 70 90
10 20 32 33 34 36 38 40 60 80 100 10 20 32 34 36 38 40 60 80 100
Insert 32 Insert 32
37
33 37 50
33 50
30 35 39 70 90
30 35 39 70 90
10 20 32 34 36 38 40 60 80 100
10 20 32 34 36 38 40 60 80 100
60 80 100 60 70 100
2-3 Trees 41 2-3 Trees 42
- X S L PS PL L S - S P
X Y - S L - A B
X Y PL L S A B
P L
Merge to the
S L left L
S L - S P
A - B A S B
a b c d
a b c d
PS PL S PL P -
- S L X Y PS L X Y S - S P
a b c d e f g a b c d e f g a b c a b c
S L L -
New root
Empty root
S P
- A B S A B
S P
a b d e a b c
a b c d e c
a b c
30 65 90 50
10 20 40 60 70 80 100 30 65 90
10 20 40 60 70 80 100
2-3 Trees 51 2-3 Trees 52
Remove 65 Remove 65
65 is now in an invalid location but that Since there are 2 data values in the leaf,
is okay because we will remove it just remove data value
50 50
30 70 90 30 70 90
10 20 40 60 65 80 100 10 20 40 60 80 100
Delete 70 Delete 70
70 is an internal node - swap with 70 is now in an invalid location but that
inorder successor is okay - we will be removing that node
! Inorder successor will always be in a leaf
50 50
30 70 90 30 80 90
10 20 40 60 80 100 10 20 40 60 70 100
90
30 80 90 30
10 20 40 60 - 100 10 20 40 60 80 100
90 90
30 30
10 20 40 60 80 - 10 20 40 60 80 -
80 80
30 30
10 20 40 60 90 10 20 40 60 90
Delete 80 Delete 80
Can’t redistribute so merge nodes Can’t redistribute so merge nodes
Invalid 2-3 tree, continue recursively up
50 the tree
50
90
30
-
30
10 20 40 60 -
10 20 40 60 90
50 -
- 30 50
30
10 20 40 60 90 10 20 40 60 90
Delete
Delete 80 deleteItem (itemtype item)
Root is now empty, set new root node = node where item exists (may be null if no item)
pointer if (node)
if (item is not in a leaf)
swap item with inorder successor (always leaf)
Root leafNode = new location of item to delete
30 50 else
leafNode = node
Delete Delete
else { //merge nodes
Choose an adjacent sibling s of n (remember, In class exercise - remove the following
merge left first) values from the tree in this order
Bring the appropriate item down from p into s
! 37, 70
if ( n is internal ) 37 50
move n’s child to s
Remove node n 30 35 39 70 90
if ( p is now empty )
fix (p) 10 20 33 34 36 38 40 60 80 100
} //end if
}//end if 2-3 Trees 71 2-3 Trees 72
Delete 37 38 50
Delete 37
30 35 39 70 90 38 70
10 20 33 34 36 - 40 60 80 100
30 35 50 90
38 50
10 20 33 34 36 39 40 60 80 100
30 35 - 70 90
10 20 33 34 36 39 40 60 80 100
2-3 Trees 73 2-3 Trees 74
38 80 38 50
Delete 70 Delete 70
30 35 50 90 30 35 - 80
10 20 33 34 36 39 40 60 - 100 10 20 33 34 36 39 40 60 90 100
38 80 35 50
30 35 50 -
30 38 80
10 20 33 34 36 39 40 60 90 100 33 34 36
10 20 39 40 60 90 100
2-3 Trees 75 2-3 Trees 76