Professional Documents
Culture Documents
Edited from:
https://cs.mtsu.edu/~jhankins
/files/3110/presentations/2-
3Trees.ppt
Motivation
• Binary search trees need O(n) time for search,
insertion and deletion.
• Would like to have a tree which self balances
itself with every deletion and insertion to
maintain as much a balanced tree as possible
wherein all leaves are at the same level
CSCI 2720
Slide 2
Properties
• Each node has either one value or two value
• A node with one value is either a leaf node or has exactly two
children (non-null).
Val(left subtree) < val(node) < val(right subtree)
CSCI 2720
Slide 3
Insertion
CSCI 2720
Slide 4
Inserting into full node
CSCI 2720
Slide 5
Inserting so far
CSCI 2720
Slide 6
Inserting Items
How do we insert 32?
CSCI 2720
Slide 7
Inserting Items
creating a new root if necessary
tree grows at the root
CSCI 2720
Slide 8
Inserting Items
How do we insert 32?
CSCI 2720
Slide 9
Inserting Items
Final Result
CSCI 2720
Slide 10
Notes
The height of the 23 tree increases upwards
CSCI 2720
Slide 11
Deletion
CSCI 2720
Slide 12
Step 1
Deletion happens at the leaf node.
Delete 20
70
80
CSCI 2720
Slide 14
Case 2: Redistribution
CSCI 2720
Slide 15
Redistribution
A sibling has 2 items:
redistribute item
between siblings
and parent
CSCI 2720
Slide 16
Case 3: Merging(might propogate
upwards)
Merging
move item from parent
to sibling
adopt child of n
CSCI 2720
Slide 17
Merging till root
CSCI 2720
Slide 18
Merge with: double key node
Deleting 70: ... get rid of 70
CSCI 2720
Slide 19
Merge with: sibbling with
two keys.
50
30,45
10,20 40 47 60,90
45
30 50
10,20 40 47 60,90
CSCI 2720
Slide 20
Merge with single key
sibbling -1
Deleting 80 ...
CSCI 2720
Slide 21
Example: Merge with single
key sibbling -2
Deleting 80 ...
CSCI 2720
Slide 22
Conclusion
CSCI 2720
Slide 23