BSTMap (Binary Search Trees)
Lab 6
CS61B Spring 2025
Announcements
CS61B Spring 2025
Binary Search Trees
CS61B Spring 2025
Binary Search Trees
Given a sorted array, we’re able to achieve a runtime of log(N) when searching for a specific element in the array (why?).
CS61B Spring 2025
Binary Search Trees
With binary search trees, we can take advantage of a tree structure while adding in some additional conditions to maintain the elements in a “sorted” order.
CS61B Spring 2025
Binary Search Tree Def.
As the name implies, binary search trees are just binary trees, so the properties of a binary tree still applies (each node has at most 2 children).
CS61B Spring 2025
Binary Search Tree Def.
As the name implies, binary search trees are just binary trees, so the properties of a binary tree still applies (each node has at most 2 children).
The specific BST properties are as the following:
CS61B Spring 2025
Insertion
To maintain the properties of the BST, let’s consider how we insert an element T (assume no duplicates)
CS61B Spring 2025
Insertion
To maintain the properties of the BST, let’s consider how we insert an element T (assume no duplicates)
CS61B Spring 2025
Insertion Demo
CS61B Spring 2025
BST Insertion
14
Starting out, let’s say we’ve inserted 14 into our binary search tree. So 14 is the root of our tree.
CS61B Spring 2025
14
10
Next to insert.
10
BST Insertion
Now let’s insert a number into the binary search tree. We’ll go with 10.
At this point, we need to figure how where 10 goes in the binary search tree. To do so, we want to make a comparison between 10 and 14.
CS61B Spring 2025
14
14
10
20
Next to insert.
BST Insertion
Since 10 is less than 14, we go to the left. Since 14 has no children so far, 10 can be inserted as leaf node to the left.
Now let’s insert 20 into our tree and repeat the same process.
CS61B Spring 2025
14
14
20
10
23
5
4
Next to insert.
Since 20 is greater than 14, it will be inserted into the tree to the right of 14.
What will our tree look like if we inserted these numbers in this order: 4, 5, 23?
BST Insertion
CS61B Spring 2025
14
14
20
10
23
5
Next to insert.
Since 20 is greater than 14, it will be inserted into the tree to the right of 14.
What will our tree look like if we inserted these numbers in this order: 4, 5, 23?
BST Insertion
4
CS61B Spring 2025
14
14
20
10
23
Next to insert.
Since 20 is greater than 14, it will be inserted into the tree to the right of 14.
What will our tree look like if we inserted these numbers in this order: 4, 5, 23?
BST Insertion
4
5
CS61B Spring 2025
14
14
20
23
5
4
10
It’ll look something like this!
When inserting an item, we make comparisons with the current node we’re on to determine which way we traverse down the binary search tree.
Once we hit a leaf node, we place our item there.
BST Insertion
CS61B Spring 2025
Quick Tip
Recursion is your best friend!
CS61B Spring 2025
Quick Tip
Recursion is your best friend!
Avoid “arms-length” recursion!
CS61B Spring 2025
One Last Important Thing
How does the order of elements inserted into the BST affect the runtime of it (think about the structure that can be created)?
CS61B Spring 2025
One Last Important Thing
How does the order of elements inserted into the BST affect the runtime of it (think about the structure that can be created)?
Nope! The insertion of elements can affect our worst case runtime for lookup (i.e. a bushy tree structure is O(log(N)) while a spindly tree is O(N)).
CS61B Spring 2025
One Last Important Thing
14
14
20
23
27
34
Suppose we had the following spindly tree:
What would be the runtime of containsKey(34) be?
CS61B Spring 2025
One Last Important Thing
Suppose we had the following spindly tree:
What would be the runtime of containsKey(34) be?
Worst case, O(N). Why?
14
14
20
23
27
34
CS61B Spring 2025
Lab Overview
CS61B Spring 2025
An Overview
Lab 06 is due Friday, 03/07 at 11:59 pm.
Deliverables:
CS61B Spring 2025
Lab Notes
CS61B Spring 2025
Lab Notes
printInorder(): Here’s a reminder of the pseudocode for in-order traversal of a binary tree
public void inOrder(Node node) {
if (node == null) {
return;
}
inOrder(node.left);
processNode(node);
inOrder(node.right);
}
CS61B Spring 2025
Lab Notes
Another thing to note in this lab is that in your implementation, you should ensure that generic keys K in BSTMap<K, V> extends Comparable.
This is to make sure that you’re able to use the compareTo method in your implementation, specifically with K (why might that be useful?).
CS61B Spring 2025
public class BSTSet<K extends Comparable<K>> implements Set61B<K> {...}
In this lab, your generic key K needs to implement Comparable. The above shows an example of what that may look like - the main difference being that your BSTMap takes <K, V> instead.
CS61B Spring 2025
Attendance
CS61B Spring 2025