You are on page 1of 19

Balanced Trees

Binary search trees are not guaranteed


Balanced Trees - 2-3 Tree to be balanced given random inserts
and deletes
! Tree could degrade to O(n) operations
Balanced search trees
! Operations maintain a balanced tree

2-3 Trees 1 2-3 Trees 2

2-3 Tree 2 Node


Guaranteed to always be balanced 2 nodes have one data item and 2
! O(lg n) operations children
Each interior node has two or three
children 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 Search keys < S Search keys >= S
and leaves
2-3 Trees 3 2-3 Trees 4
3 Node 2-3 Tree
3 nodes have two data items and 3 A leaf may contain 1 or 2 data items
children (a left child, a middle child, and
2-3 trees are good because they are
a right child)
easy to maintain as balanced
S L ! Operations take care of that for you
Node class
itemtype smallItem, largeItem
Search keys >= L Node *left, *middle, *right, *parent
Search keys < S
// you may find that you need other data members
Search keys >= S // to make your life easier
and < L // do not add a third item or a fourth child
2-3 Trees 5 2-3 Trees 6

2-3 Tree Traversing a 2-3 Tree


50 90
Inorder traversal -
inorder (node* cur)
if current
70 120 150
20 inorder(cur->left)
visit small item if it exists
inorder(cur->middle)
10 30 40 60 80 100 110 130 140 160
visit large item if it exists
inorder(cur->right)

2-3 Trees 7 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 To insert an item, find a leaf to put the
search (Node* cur, itemtype key)
if (cur)
item in then split nodes if necessary
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 9 2-3 Trees 10

Splitting a Leaf Splitting an Internal Node


P M P P M P

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

If splitting node causes the parent to have 3 items and 4 b c d e


b c d e
children, you will then split an internal node…
2-3 Trees 11 2-3 Trees 12
Splitting the Root 2-3 Tree
Start with this tree
New Root 50
M
Root
SML
S L 30 70 90
a b c d
a b c d
10 20 40 60 80 100

2-3 Trees 13 2-3 Trees 14

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

Leaf to insert only has 1 data item 10 20 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 Determine
! Do not actually add the item because the ! Smallest = 38
node can only hold 2 data items ! Middle = 39
! Largest = 40
50

30 70 90

10 20 38 39 40 60 80 100

2-3 Trees 17 2-3 Trees 18

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

2-3 Trees 19 2-3 Trees 20


Insert 37 Insert 36
Locate leaf to insert 37 Locate leaf to insert 36
Leaf contains 1 data value, just insert Conceptualize inserting 36 into this leaf
value ! Determine small (36), middle (37), and large
(38)
50 50

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

2-3 Trees 21 2-3 Trees 22

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

2-3 Trees 23 2-3 Trees 24


Insert 36 Insert 36
Parent p now has 3 data values and 4 children Split parent p
Split - similar to leaf situation where leaf has 3 data ! Divide to small (30), middle (37), and large (39)
values ! Move middle value to nodes parent
! You can generalize both situations into one
! Small and large become new children, s and l
50 50

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

2-3 Trees 25 2-3 Trees 26

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

2-3 Trees 27 2-3 Trees 28


Insert 34 Insert 34
Insert 34 Insert 34
! Causes a split ! Causes a split

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

2-3 Trees 29 2-3 Trees 30

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

2-3 Trees 31 2-3 Trees 32


Insert Insert (continued)
// Item is to be inserted into n. The insertion
Insert into a tree without duplicates // of item will cause n to have 3 items so n
// must be split
split ( Node* n, itemtype item, … // you may need more )
insert (itemtype item) if ( n is the root )
leaf = leaf node to insert item (may be null or have 1 or 2 data items) create a new node p
else
if (leaf is null - only happens when root is null) let p be the parent of n
add new root to tree with item
else if (# data items in leaf = 1) Replace node n with 2 nodes, n1 and n2, so that p is their parent
add item to node
else // leaf has 2 data items
Give n1 the item in n with the smallest value
split ( leaf, item )
Give n2 the item in n with the largest value

// continued on next slide…


2-3 Trees 33 2-3 Trees 34

Insert (continued) Insert 32


In class exercise
if (n is not a leaf)
n1 becomes the parent of n’s two leftmost children ! Insert 32 into the tree below
n2 becomes the parent of n’s two rightmost children
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) 30 35 39
else
70 90
add x to p
10 20 33 34 36 38 40 60 80 100

2-3 Trees 35 2-3 Trees 36


Insert 32 Insert 32

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

2-3 Trees 37 2-3 Trees 38

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

2-3 Trees 39 2-3 Trees 40


Remove Remove - Redistribute
With insertion, we split nodes. With P L
removing, we merge nodes
Deletion process needs to begin with a S L - S P
leaf but you might be deleting a value
that is not a leaf
PS PL S PL
! Swap item to delete with inorder successor
70 90 80 90
Delete 70 - S L X Y PS L X Y

60 80 100 60 70 100
2-3 Trees 41 2-3 Trees 42

Remove - Redistribute Remove - Merge


PS PL X S P -

- X S L PS PL L S - S P

Take from right


PS PL S L
PS S L

X Y - S L - A B
X Y PL L S A B

2-3 Trees 43 2-3 Trees 44


Remove - Merge Remove - Redistribute

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

2-3 Trees 45 2-3 Trees 46

Remove - Redistribute Remove - Merge

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

2-3 Trees 47 2-3 Trees 48


Remove - Merge Deleting the Root

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

2-3 Trees 49 2-3 Trees 50

2-3 Tree Remove 65


Start with this tree 65 is an internal node - swap with
50 inorder successor
! Inorder successor will always be in a leaf

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

2-3 Trees 53 2-3 Trees 54

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

2-3 Trees 55 2-3 Trees 56


Delete 70 Delete 70
Removing leaf leaves us with an invalid Merge nodes to fix tree
2-3 tree
50 50

90
30 80 90 30

10 20 40 60 - 100 10 20 40 60 80 100

2-3 Trees 57 2-3 Trees 58

Delete 100 Delete 100


100 is already leaf, just remove leaf Sibling has data item to spare,
redistribute
50 50

90 90
30 30

10 20 40 60 80 - 10 20 40 60 80 -

2-3 Trees 59 2-3 Trees 60


Delete 100 Delete 80
Sibling has data item to spare, Swap 80 with inorder successor
redistribute
50 50

80 80
30 30

10 20 40 60 90 10 20 40 60 90

2-3 Trees 61 2-3 Trees 62

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

2-3 Trees 63 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 40 60 90 10 20 40 60 90

2-3 Trees 65 2-3 Trees 66

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 item from leafNode


10 20 40 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 ) {
// completes the deletion when node n is empty by either
Distribute items appropriately among n, the
// removing the root, redistributing values, or merging nodes.
sibling and the parent (remember take from
// Note: if n is internal, it has only one child
right first)
fix (Node* n, …) //may need more parameters {
if (n is the root) {
if ( n is internal ) {
remove the root
Move the appropriate child from sibling n
set new root pointer
(May have to move many children if
}
distributing across multiple siblings)
else {
}
Let p be the parent of n

2-3 Trees 69 2-3 Trees 70

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

You might also like