This lesson introduces a new data structure: trees. Trees are extremely useful, both for modeling certain types of data, and for enabling certain algorithms. They are also great for practice with recursion! Let’s do this…
But… you knew it! Let’s warm up with another debugging challenge!
Implement a method
factorial that accepts a single
Long and returns its factorial as a
You can reject negative arguments and ones greater than 20 by throwing an
You should submit a recursive solution. The factorial of 0 is 1, and this represents the base case. The factorial of n is n * the factorial of n - 1, and this represents the recursive step.
Wikipedia defines a (computer science) tree as:
In computer science, a tree is a widely used abstract data type that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes.
Let’s look at some diagrams that will help visualize this new data structure, and introduce some important terminology.
Trees can be used to model a variety of hierarchical data. Examples include:
.edubeing one of the root’s children (
.com) is another,
.illinoisbeing one of the children of
.purdue) being another, and so on
Let’s look at these example a bit more:
We’ll also see ways that trees can be used to store a collection of items in ways that enable certain efficient algorithms. For example, by storing data in a tree, we can enable more efficient search algorithms than the O(n) scans of arrays we’ve seen so far. We’ll get there!
Most of the work with trees that we’ll be doing is on a specific type of tree called a binary tree. Binary trees are so named because each node has up to two children, but no more.
Let’s develop the
BinaryTree class that we’ll use in class and that we’ll use on upcoming homework problems.
Note that this is available in the playground environments and homework as
As a way of reviewing object design and references, let’s walk through the process of designing this class:
Last time we introduced recursion, a problem solving technique that works by breaking down large problems into smaller pieces, solving them, and then combining the results. Binary trees are a great fit for recursion! Let’s see why:
In addition, compared to lists and arrays and
Strings, trees are hard to work with using iterative solutions!
Recursion is a much better approach…
Let’s get some practice with recursion on trees. We’ll take a tree filled with integer values and determine the sum of all the values it stores. Along the way, we’ll look at several recursive approaches, and begin to develop some of the patterns that we’ll use working recursively with binary trees.
To help you prepare for our next homework problem, let’s discuss the recursive approach to counting the number of nodes in a tree.
Create a method named
negativeSum that accepts a
that is a nullable
Return the sum of all the negative values in the tree.
cs125.trees.BinaryTree is defined like this:
Let’s pause for a moment to discuss how we evaluate the complexity of your homework submissions, and what you can do to improve your code when we indicate that it is too complicated. First, a practice problem to work on. Once you’ve solved this, the solution walkthrough will show what happens to an overly-complex submission, and discuss how to reduce the complexity of your code.
The practice problem below looks like a regular practice problem, but it introduces a new way in which we’ll begin scoring your homework solutions: evaluating their complexity. Starting now there is 1 point out of 10 you’ll earn for not submitting an overly-complex solution. Solve the problem and then watch a solution walkthrough to learn more!
Declare and implement a function called
sumIsOdd should accept two
Int argument and return
true if their sum is odd and
You will probably want to consider using the remainder operator (
%) to complete this problem.
Create a method
size that accepts a
cs125.trees.BinaryTree and returns the number of nodes it contains.
size accepts a
BinaryTree<*>?, that is a nullable
BinaryTree that can contain any kind of values.
You'll want to count recursively, identifying both a base case and a recursive step.
cs125.trees.BinaryTree is defined like this:
Don't overthink this! Like many recursive algorithms, the solution is elegant and simple: 4 lines total if you do
You'll also need to import
cs125.trees.BinaryTree for this and similar problems.
Need more practice? Head over to the practice page.