This Site Uses Cookies
We use cookies to help you learn as much as you can this semester.
AGREE



Login
Tree Depth
Tree Node Count
Binary Search Tree
Homework: Binary Tree Search Path
Return to Index
Kotlin : 08/04/2021
Streams : 08/04/2021
Generics : 08/03/2021
Binary Search : 08/03/2021
Sorting Review : 07/30/2021
Quicksort : 07/30/2021
Merge Sort : 07/29/2021
Sorting Algorithms : 07/29/2021
Practice with Recursion : 07/28/2021
Trees and Recursion : 07/28/2021
Trees : 07/27/2021
Recursion : 07/26/2021
Working with Exceptions : 07/23/2021
Throwing Exceptions : 07/23/2021
Catching Exceptions : 07/23/2021
Maps : 07/22/2021
Hashing : 07/22/2021
Lists Review and Performance : 07/21/2021
Linked Lists : 07/21/2021
Array Lists : 07/20/2021
Algorithms and Lists : 07/20/2021
Testing 2 : 07/19/2021
Testing 1 : 07/19/2021
Lambda Expressions : 07/15/2021
Anonymous Classes : 07/14/2021
Practice with Interfaces : 07/14/2021
Implementing Interfaces : 07/13/2021
Using Interfaces : 07/13/2021
Reverse Engineering a Class : 07/12/2021
Reverse Engineering a Method : 07/12/2021
Serialization : 07/09/2021
Imports and Libraries : 07/09/2021
Java Memory Management : 07/08/2021
References and Polymorphism : 07/08/2021
References : 07/07/2021
Square Shape : 07/07/2021
Circle Shape : 07/06/2021
Data Modeling 2 : 07/06/2021
Equality and Object Copying : 07/03/2021
Polymorphism : 07/03/2021
Inheritance : 07/02/2021
Static : 07/02/2021
Course Object : 07/01/2021
Flip Flop Object : 07/01/2021
Data Modeling 1 : 06/30/2021
Encapsulation : 06/30/2021
Constructors : 06/29/2021
Objects, Continued : 06/29/2021
Introduction to Objects : 06/28/2021
Football CSV : 06/27/2021
Rot-13 Encryption : 06/26/2021
Asserting : 06/26/2021
Type Inference : 06/25/2021
Compilation : 06/25/2021
Multidimensional Arrays and Algorithms : 06/24/2021
String Equality : 06/24/2021
String Rotation : 06/23/2021
Multidimensional Arrays : 06/23/2021
null : 06/22/2021
Algorithms and Strings : 06/22/2021
Strings : 06/21/2021
More About Functions : 06/21/2021
Array Count Equal : 06/20/2021
Reverse Print : 06/20/2021
Staying Sane in CS : 06/19/2021
Functions : 06/19/2021
Algorithms I : 06/18/2021
Loops : 06/18/2021
Arrays : 06/17/2021
Time to Decade : 06/17/2021
Center of the Universe : 06/16/2021
Compound Conditionals : 06/16/2021
Conditional Expressions and Statements : 06/15/2021
Operations on Variables : 06/15/2021
Variables and Types : 06/14/2021
Welcome to CS 125 : 06/14/2021
Practice with Recursion
1
2
3
4
5
6
7
import cs125.trees.BinaryTree;
int treeDepth(BinaryTree tree) {
return 0;
}
assert treeDepth(new BinaryTree
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Guess what? In this lesson we’ll be doing more practice with binary trees! And recursion! What could be more fun?
Tree Depth
As a warm up let’s write a recursive function to determine the depth or height of a tree. As a reminder, the depth is defined as the distance from the root node to the farthest leaf node. (The depth is not defined for a empty tree, since it has no root.)
1
// Tree Depth
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Interactive Walkthrough
Click on an icon below to start!

Tree Node Count
Next, let’s look at an example of a recursive function that passes another data structure around. We’ll write a recursive method that returns a map that counts the number of times that each value appears in the tree. This will also prepare you for our next homework problem—which is a tricky one!
1
// Tree Value Count
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Interactive Walkthrough
Click on an icon below to start!


Binary Search Tree
Finally, 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 {
right.add(newValue);
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Interactive Walkthrough
Click on an icon below to start!

Homework: Binary Tree Search Path
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 as its first parameter and an Object as its second. It returns a List
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 125 Students
Login
WORKING
PREVIOUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cs125.trees.BinaryTree;
import java.util.ArrayList;
import java.util.List;
public class BinaryTreePath {
public static List
private static boolean pathToValue(BinaryTree tree, Object value, List
return false;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Learn Java Learn Kotlin MP Info Forum People Calendar
Contributors
Old Site
Innovations