KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Trees

public class BinaryTree {
private Object value;
private BinaryTree left;
private BinaryTree right;
public BinaryTree(Object setValue) {
value = setValue;
}
public Object getValue() {
return value;
}
public BinaryTree getLeft() {
return left;
}
public Object getRight() {

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…

Warm Up Debugging Challenge
Warm Up Debugging Challenge

But… you knew it! Let’s warm up with another graded debugging challenge!

What is a Tree?
What is a Tree?

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.

What are Trees For?
What are Trees For?

Trees can be used to model a variety of hierarchical data. Examples include:

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!

Binary Trees
Binary Trees

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 cs125.trees.BinaryTree. As a way of reviewing object design and references, let’s walk through the process of designing this class:

// BinaryTree class

Recursion on Trees
Recursion on Trees

Yesterday 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…

Recursive Tree Sum
Recursive Tree Sum

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.

// BinaryTree Sum

Recursive Tree Size
Recursive Tree Size

To help you prepare for our next homework problem, let’s discuss the recursive approach to counting the number of nodes in a tree.

Practice: BinaryTree Negative Sum

Created By: Geoffrey Challen
/ Version: 2021.10.0

Create a public class named BinaryTreeNegativeSum with a single class method named negativeSum. negativeSum accepts a BinaryTree<Integer>, that is a BinaryTree containing Integer values. Return the sum of all the negative values in the tree.

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

Computational Complexity
Computational Complexity

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 in this lesson 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!

Created By: Geoffrey Challen
/ Version: 2021.7.0

Declare and implement a function called sumIsOdd. sumIsOdd should accept two int arguments and return true if their sum is odd and false otherwise. You will probably want to consider using the remainder operator (%) to complete this problem.

Homework: Binary Tree Size

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create a public class BinaryTreeSize that provides a single static method size. size accepts a cs125.trees.BinaryTree<?>, a BinaryTree that can contain any value, and returns the number of nodes it contains. You'll want to count recursively, identifying both a base case and a recursive step.

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

Don't overthink this! Like many recursive algorithms, the solution is elegant and simple: 4 lines total if you do it right. You'll also need to import cs125.trees.BinaryTree for this and similar problems.

More Practice

Need more practice? Head over to the practice page.