KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Binary Search Trees : 11/10/2026

  • Practice with Tree Recursion : 11/09/2026

  • Trees and Recursion : 11/06/2026

  • Trees : 11/05/2026

  • Recursion : 11/04/2026

  • Working With Agents: Say What You Saw : 11/03/2026

  • Working With Agents: Validate What It Built : 11/02/2026

  • Lists Review and Performance : 10/30/2026

  • Linked Lists : 10/29/2026

  • Algorithms and Lists : 10/28/2026

  • Working With Agents: Correct It, Out Loud, Early : 10/27/2026

  • Working With Agents: Discuss Ideas, Not Implementations : 10/26/2026

  • Lambda Expressions : 10/23/2026

  • Anonymous Classes : 10/22/2026

  • Practice with Interfaces : 10/21/2026

  • Implementing Interfaces : 10/20/2026

  • Using Interfaces : 10/19/2026

  • Working with Exceptions : 10/16/2026

  • Throwing Exceptions : 10/15/2026

  • Catching Exceptions : 10/14/2026

  • References and Polymorphism : 10/13/2026

  • References : 10/12/2026

  • Data Modeling 2 : 10/09/2026

  • Equality and Object Copying : 10/08/2026

  • Polymorphism : 10/07/2026

  • Inheritance : 10/06/2026

  • Data Modeling 1 : 10/05/2026

  • Static : 10/02/2026

  • Encapsulation : 10/01/2026

  • Constructors : 09/30/2026

  • Objects, Continued : 09/29/2026

  • Introduction to Objects : 09/28/2026

  • Testing and Compilation : 09/25/2026

  • Practice with Collections : 09/24/2026

  • Maps and Sets : 09/23/2026

  • Lists and Type Parameters : 09/22/2026

  • Imports and Libraries : 09/21/2026

  • Multidimensional Arrays : 09/18/2026

  • Practice with Strings : 09/17/2026

  • null : 09/16/2026

  • Algorithms and Strings : 09/15/2026

  • Strings : 09/14/2026

  • Functions and Algorithms : 09/11/2026

  • Practice with Functions : 09/10/2026

  • More About Functions : 09/09/2026

  • Errors and Debugging : 09/08/2026

  • Functions : 09/04/2026

  • Practice with Loops and Algorithms : 09/03/2026

  • Algorithms : 09/02/2026

  • Loops : 09/01/2026

  • Arrays : 08/31/2026

  • Compound Conditionals : 08/28/2026

  • Conditional Expressions and Statements : 08/27/2026

  • Operations on Variables : 08/26/2026

  • Variables and Types : 08/25/2026

  • Welcome to CS 124 : 08/24/2026

Binary Search Trees

We continue our practice with trees and recursion by returning to a problem we’ve seen before: finding a value in a binary tree. By changing how we add values to the tree, we can make searching it much more efficient. Let’s see how!

Binary Search Tree
Binary Search Tree

Let’s look again at the problem of locating a node in a binary tree. We’ll start with code from our previous answer, redesign it to be more efficient, and then analyze the performance of our new approach.

import java.util.Random;
// Binary Search Tree
public class BinaryTree {
private Object value;
private BinaryTree right;
private BinaryTree left;
private Random random = new Random();
public BinaryTree(Object setValue) {
value = setValue;
}
public BinaryTree(Object[] values) {
assert values.length > 0;
value = values[0];
for (int i = 1; i < values.length; i++) {
add(values[i]);
}
}
private void add(Object newValue) {
if (random.nextBoolean()) {
if (right == null) {
right = new BinaryTree(newValue);
} else {

Practice: Solve BST Contains

Created By: Geoffrey Challen
/ Version: 2026.11.0

Write a method bstContains that accepts a binary search tree as a BinaryTree<Integer> and an int value, and returns a boolean that is true if the tree contains the value and false otherwise. The tree passed to bstContains will not be null.

The tree is a binary search tree, so its values are kept in order: every value in a node's left subtree is smaller than that node's value, and every value in its right subtree is larger. No value appears in the tree more than once.

Use that ordering to make your search efficient! If the value you are looking for is smaller than the current node's value, it can only be in the left subtree, so only search there. If it is larger, only search the right subtree. Your solution must be recursive: search a subtree by calling bstContains on it. A solution that searches the entire tree will still find the right answer, but it throws away the whole point of keeping the tree in order!

For reference, cs125.trees.BinaryTree has the following public methods:

Practice: Solve Binary Tree Search Path

Created By: Geoffrey Challen
/ Version: 2026.11.0

Let's continue exploring recursion on binary trees. However, this problem takes a significant step forward in difficulty, so be prepared!

We've provided a public class BinaryTreePath with a single class method pathToValue. pathToValue accepts a BinaryTree<Object> as its first parameter and an Object as its second. It returns a List<Object> containing all the values in the tree on the way to the first node with a value equal to the passed Object, or null if the tree does not contain the passed Object. We've handled this case already for you in the starter code. However, you should fix pathToValue so that it throws an IllegalArgumentException if either the passed tree or the passed value is null.

Our wrapper method initializes the list properly and then calls a private helper method which performs the recursion. The helper should return true if the tree contains the value, and if it does also manipulate the list properly. If the tree does not contain the value it should return false. You will want to use add(int index, Object value) to add values to the front of the list as you work your way through the tree.

This problem is hard! Here's an outline of a solution to help get you started:

  • If you reach an empty tree, you can return false, since an empty tree does not contain the value
  • Otherwise, if this node contains the value, add yourself to the list, stop recursing, and return true.
  • Otherwise, first search your right subtree. If that succeeds, then this node is also part of the path and should be added. If not, try the left subtree.
  • If neither the right nor left subtree contains the node, you should return false and not modify the list, since this node is not on the path to the desired node.

Your solution must be recursive: the helper should search each subtree by calling itself on it. Good luck and have fun!

import cs125.trees.BinaryTree;
import java.util.ArrayList;
import java.util.List;
public class BinaryTreePath {
public static List<Object> pathToValue(BinaryTree<Object> tree, Object value) {
List<Object> path = new ArrayList<>();
if (pathToValue(tree, value, path)) {
return path;
} else {
return null;
}
}
private static boolean pathToValue(BinaryTree<Object> tree, Object value, List<Object> path) {
return false;
}
}

Homework: Solve BST Insert

Created By: Geoffrey Challen
/ Version: 2026.11.0

Write a method bstInsert that accepts a binary search tree as a BinaryTree<Integer> and an int value, and adds the value to the tree in the right place. bstInsert does not return anything. The tree passed to bstInsert will not be null. The value you are adding will never already be in the tree, so you don't need to handle duplicates.

The tree is a binary search tree, so its values are kept in order: every value in a node's left subtree is smaller than that node's value, and every value in its right subtree is larger. Your method needs to keep it that way!

Here's how to find the right place for the new value. Start at the root. If the new value is smaller than the current node's value, it belongs in the left subtree. If there is no left subtree, create a new BinaryTree<Integer> holding the value and make it the left child. Otherwise, insert the value into the left subtree. If the new value is larger, do the same thing on the right. So the new value always ends up in a new leaf node. Your solution must be recursive: when the value belongs in a subtree that already exists, call bstInsert on that subtree. A loop that walks down the tree finds the same place, but it won't pass.

For reference, cs125.trees.BinaryTree has the following public constructor and methods:

More Practice

Need more practice? Head over to the practice page.