05 Tree Maps
05 Tree Maps
Lesson 05:
Tree Maps
• Complexity O(n)
Search In Sorted Array
7 11 40 42 70 78 80 92 93 94 99
7 11 40 42 70 78 80 92 93 94 99
• As 78 < 92, check middle of
subarray between 80 and 99,
7 11 40 42 70 78 80 92 93 94 99 ie 93
7 11 40 42 70 78 80 92 93 94 99
• As 93 > 92, ignore the
rightmost values
• Finally find 92
Complexity
7 11 40 42 70 78 80 92 93 94 99
7 11 40 42 70 78 80 92 93 94 99
7 11 40 42 70 78 80 92 93 94 99
•
7 11 40 42 70 78 80 92 93 94 99
List/Array Based Map
• Each element is an Entry object, containing K and V
• Deletion: O(N)
4 4 4 4 4
2 2 2 5 2 5
3 3 1 3
Insertion Order
• Assume inserting same entries but in this order: 5, 4, 3, 2, 1
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Search
• Start from root 7
0 3 9
• Worst case: searched key is in a leaf node
7 7
4 11 4 11
2 8 14 2 8 14
0 3 9 0 3
Deletion With 1 Child
• Eg, delete 8 (or 4)
• Look at the parent of deleted node (ie 11)
• Change the child link of parent to the child of deleted node (ie 9)
7 7
4 11 4 11
2 8 14 2 9 14
0 3 9 0 3
Deletion With 2 Children
• Eg, delete 7 (or 2 or 11)
• Find minimum in right subtree (ie 8) and delete it
• Replace the content of right-minimum into deleted node
7 8
4 11 4 11
2 8 14 2 9 14
0 3 9 0 3
Runtime Complexity
• Extremely efficient
• Can guarantee search into Billions of data with just 30-50ish node lookups
2-3 Search Trees
• Before discussing RBT, let’s M
M M
E J R E J R
A C H L P S X A C H L S X
M M
E J R E J R
A C H L P S X A C H K L P S X
Insertion on 3-node, where
Parent is a 2-node
• Inserting Z
• 3-node (S,X) with 2-node parent (R)
• Split node, and transform parent into a 3-node
M M
E J R E J R X
A C H L P S X A C H L P S Z
Insertion on 3-node, where
Parent(s) are 3-nodes
• Inserting D M
• 3-node (A,C) with 3-node
parent (E,J) E J R
M E M
C E J R C J R
A D H L P S X A D H L P S X
Insertion on 3-node Root,
with No Children
• Inserting a new value, eg S
A E E
A S
Splitting The Root
• Adding D
• What happens when going bottom-up adding recursively to
parent we reach a 3-node root?
• Same as root with no children
• Note: this is the only case in which depth increases
E
E J C E J C J
A C H L A D H L A D H L
Cost
• Searching for position to insert is O(log N)
A B A A B A B
Black-links Balance
E S
Less E Greater
S
than E than S
M M
E J R E J R
A C H L P S X A C H K L P S X
Insertion into 2-node
• 2-node will need to transformed into a 3-node
• In RBT, need to have new node with RED link
• Need left rotation if new value is greater
B
B A B
A
B C
B B C
C B
Recall Insertion on 3-node
Root, with No Children
• 2-3 Trees
A E E
A S
Insertion in Single 3-node
• 3 cases, based on whether new value is larger, smaller or
between
D D D
B B F B F
Insertion in 3-node: Smaller
Case
• Smaller value, eg adding A
• After insertion, need right rotation
• Finally, need flipping of RED colors
D D B B
B B
A D A D
A
Insertion in 3-node:
Between Case
• Between value, eg adding C
• After insertion, need left rotation, followed by right rotation
• Finally, need flipping of RED colors
D D D C C
B B C
B D B D
C B
Cost of 3-node Insertion
• 3 cases, based on inserted key
A C R S A C R
H H S
A C R A C H S
H S
Left rotation: E and R
Recursion
Color flipping
• Do exercises in exercises/ex05