# Class 45: Introduction to Trees

Back to Project Discussion. On to Implementing Trees.

Held Monday, April 24, 2000

Overview

Today, we generalize the heap and the search tree into a generic tree data structure.

Notes

• I was hoping to grade assignment 5 over the weekend. Unfortunately, as in all holiday weekends, the time evaporated.
• Are there project questions?

Contents

Summary

• Heaps and search trees, revisited
• Representing arithmetic expressions
• Evaluating expressions
• Generalizing: a binary tree ADT
• Generalizing further: a tree ADT

## Nonlinear Structures

• Until recently, all of the ``ordered'' structures we considered (lists, arrays, vectors, stacks, and queues) were somewhat linear (in an informal rather than a formal sesnse).
• What makes a structure linear?
• Exactly one first element.
• Exactly one last element.
• Each element (except the first) has exactly one predecessor.
• Each element (except the last) has exactly one successor.
• Search trees and heaps had a somewhat different structure. How are search trees Different?
• There is still exactly one first element.
• There are now many last elements.
• Each element (except the first) still has exactly one predecessor.
• Each element (except the last) may have many (okay, two) successors.
• By careful consideration of such restrictions, we can develop a number of different interesting structures.

## An Introduction to Trees

• We can generalize search trees to develop a data structure commonly called trees.
• Trees are a collection of values,
• stored in nodes,
• with a designated root (first element)
• and a number of leaves (``last'' elements).
• Every non-leaf node has one or more children (successors)
• and the leaves have zero children,
• Every nonroot node has exactly one parent (previous element)
• and the root has no parents.
• In binary trees, each node can have at most two children, often designated as left and right.

### Applications of Trees

• There are many ways in which trees are used, including,
• search trees, which are used to store information for retrieval;
• decision trees, which are used to describe decision processes;
• class hierarchies, which show the relationships between classes in a single-inheritance language like Java;
• representing arithmetic expressions, using operators for internal nodes and values for leaves; and even
• program trees, which show the structure of a program.
• You've already seen binary search trees:
• A node stores a value.
• All smaller values are in the left subtree.
• All larger values are in the right subtree.
• You've also seen heaps:
• The root stores the smallest value.
• The left and right subtrees are also heaps.
• The tree is nearly-complete.
• Trees are good for hierarchical structures, like advising relationships in graduate school.

• Purposefully left open. I'm going to ask you to design this one.
• The basics so far:
```public interface Tree
{
/**
* Add another child to a vertex in the tree.
* Pre: The vertex (parent) is in the current tree.
* Post: The vertex (parent) has another child.  The new child
*       contains newThing.
*/
public void add(Object newThing, Vertex parent)
throws Exception;

/**
* Get the root of the tree.
*/
public _______ getRoot();
}
```

## History

Tuesday, 18 January 2000

• Created as a blank outline.

Monday, 24 April 2000

Tuesday, 25 April 2000

• Updated to better represent what happened.

Back to Project Discussion. On to Implementing Trees.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.