Trees Study Guide
Author: Josh Hug


Tree Representations. Before today, trees were an abstract idea. Two common representations are a linked data structure using a node class (representation 1) or an array of key/value pairs (representation 2).

Traversals. When we iterate over a tree, we call this a "tree traversal".

Depth First Traversals. We have three depth first traversals: Pre-order, in-order and post-order. In a pre-order traversal, we visit a node, then traverse its children. In an in-order traversal, we traverse the left child, visit a node, then traverse the right child. In a post-order traversal, we traverse both children before visiting. These are very natural to implement recursively. Pre-order and post-order generalize naturally to trees with arbtirary numbers of children. In-order only makes sense for binary trees.

Level Order Traversal. A level-order traversal visits every item at level 0, then level 1, then level 2, and so forth. One typical implementation for a level order traversal is an iterative-deepening strategy.

Visitor Pattern. To avoid rewriting tree traversal code for every single task, we can create traversal methods that take a function as an argument (wrapped up inside a Java object, as required by Java syntax).

Asymptotics of Traversals. Our depth-first traversals take linear time. Iterative deepening takes between N time and N^2 time. These facts are much less important than their derivations.

C level

  1. Question 1 from the Fall 2014 discussion worksheet.
  2. Question 1 and 2 from Algs4Part1.

B level

  1. Question 3 from Algs4Part1. Some randomly generated problems may be slightly out of the scope of our course.
  2. Question 1 and 2 from Algs4Part1.
  3. Question 4 from the Fall 2014 midterm.

A level

  1. Question 7 from the Guerrilla section worksheet.