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!
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.
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:
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:
false, since an empty tree does not contain the valuetrue.Your solution must be recursive: the helper should search each subtree by calling itself on it. Good luck and have fun!
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:
Need more practice? Head over to the practice page.