Sets, Maps, and BSTs
Applications of total order and the binary search invariant.
Kevin Lin, with thanks to many others.
1
Total Order
Based on the ordering given by the binary search tree to the left,fill in the tree to the right with valid symbols.
2
Q
?: Binary search trees are related to OrderedLinkedSets. What do we know about the relationship between the square symbol and the triangle symbol?
Q1: Based on the ordering given by the binary search tree to the left, fill in the tree to the right with valid symbols.
Total Order
Based on the ordering given by the binary search tree to the left,fill in the tree to the right with valid symbols.
3
A
Total Order
Based on the ordering given by the binary search tree to the left,fill in the tree to the right with valid symbols.
4
A
1
2
3
Applying Total Order
Say we have the following total order.
Assume that there are several other symbols not shown above.
Where can the pentagon symbol reside?
5
Q
A
C
D
E
B
Q1: Where can the pentagon symbol reside?
Applying Total Order
Say we have the following total order.
Assume that there are several other symbols not shown above.
Where can the pentagon symbol reside?
6
A
A
C
D
E
B
Binary Search Invariant
Say we have the following total order.
Binary Search Invariant.For every node X in the tree:
All keys in the left subtree ≺ X’s key.
All keys in the right subtree ≻ X’s key.
This invariant is recursive!
7
lower
upper
lower
upper
?: If we search a left subtree, how does that change the lower limit on the keys? The upper limit?
?: If we search a right subtree, how does that change the lower limit on the keys? The upper limit?
Minimum Height Trees
Draw a valid BST of minimum height containing the keys 1, 2, 3, 7, 8, 9, 5.
Give a valid insertion order for this tree.
8
Q
Q1: Draw a valid BST of minimum height containing the keys 1, 2, 3, 7, 8, 9, 5.
Q2: Give a valid insertion order for this tree.
Minimum Height Trees
Draw a valid BST of minimum height containing the keys 1, 2, 3, 7, 8, 9, 5.
Give a valid insertion order for this tree.
9
A
Minimum Height Trees
Draw a valid BST of minimum height containing the keys 1, 2, 3, 7, 8, 9, 5.
Give a valid insertion order for this tree.
5, 2, 8, 1, 3, 7, 9.
10
A
2
1
3
8
7
9
5
Fill in order
Hibbard Deletion
Draw the result of removing 6.
11
Q
1
0
3
2
9
7
10
8
6
5
4
Q1: Draw the result of removing 6.
Hibbard Deletion
Draw the result of removing 6.
12
A
1
0
3
2
9
7
10
8
6
5
4
Hibbard Deletion
Draw the result of removing 6.
13
A
1
0
3
2
9
7
10
8
6
5
4
Hibbard Deletion
Draw the result of removing 6.
14
A
1
0
3
2
9
7
10
8
5
5
4