# Class 46: Implementing Trees

Back to Introduction to Trees. On to Lab: Traversing and Iterating Trees.

Held Tuesday, April 25, 2000

Overview

Today we continue our discussion of trees, developing an interface and beginning to investigate implementations.

Notes

• Those of you looking into study abroad in the coming years might want to check out the article on online resources at `http://chronicle.com/free/2000/04/2000042501t.htm`
• New project files are available online at `http://www.math.grin.edu/~rebelsky/CS152/Project/Phase5/`
• Your goal for next Monday:
• Stuff that really works (or appears to).
• 3-5 page essays on your part of the project (enough that your colleagues could then write your part).
• Preparation for presentations.
• Some extensions (?), such as three-color Othello.
• We'll devote Friday's and Monday's classes to getting you ready.
• I need a volunteer to collect pizza orders. (Matt, Shiva?)
• The geneaology of theoritical computer science can be found at `http://sigact.acm.org/genealogy/`
• A few of you asked what I'm teaching next year.
• In the fall, I'll be teaching CSC151 and CSC152.
• In the spring, I'll be teaching CSC151 and CSC362 (Compilers).
• If you want to take CSC362 in the spring, you'll need to take CSC213 in the fall.

Contents

Summary

• Implementation techniques
• Arrays
• Nodes
• Parent/child lists.

## More Tree Applications

• As we continue to discuss a tree ADT, it may be helpful to consider other applications of trees.
• Decision trees can formalize or describe decision processes, such as twenty questions or the animals game.
```        Does it fly?
yes  /           \  no
/             \
Is it a bird?   Does it swim?
yes /    \ no       ...
/      \
...    Is it nocternal?
yes /     \
/     ...
Bat
```
• Expression trees provide an unambiguous way to represent arithmetical expressions. For example, we might write 3+4*5-6 as
```      +
/ \
3   -
/ \
*   6
/ \
4   5
```
• These have a simple evaluation strategy: evaluate both subtrees and then apply the operator.

• 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.
* @exception Exception
*   when the parent permits no more children
*/
public void add(Object newThing, Vertex parent)
throws Exception;

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

/**
* Get the vertex that corresponds to an instance of
* a particular object.
* @exception Exception
*   If it's not there.
*/
public Vertex find(Object findMe)
throws Exception;

}
```
• Note that we also need to think about the Vertex interface.
```public class Vertex {
} // class Vertex
```
• We'd (okay, I'd) decided that the `find` operation might be better described in terms of more primitive operations, just as `find` and `member` in lists are implemented through the core list operations, `car`, `cdr`, and `empty`.
• We'll continue the ``you're stuck at the board designing this'' method of learning.
• Base operations for find etc?
• How do we create trees?
• What modifiers should we support?
• What accessors should we support?

## Design Issues

• Can a node have an ith child and not an i-1st child?
• Can trees be empty? How do we handle that special case?

## Implementing Trees

• In an array (not as easy for non-binary trees).
• Using Node-like objects (hmmm ... are these vertices?)
• As a list of parent/child pairs.
• ...

### Tree Nodes

• The implementation class.
```public class TreeNode
implements Vertex
{
// +--------+----------------------------------------
// | Fields |
// +--------+

/** The contents of the current vertex. */
protected Object contents;

/** The children of the current vertex. */
protected SequencedList children;

// +-----------------+-------------------------------
// | Everything Else |
// +-----------------+

...

} // class TreeNode
```
• We wrap this in a Tree class.
```public class NodeBasedTree
implements Tree
{
// +--------+----------------------------------------
// | Fields |
// +--------+

/** The root of the tree. */
protected TreeNode root;

/** The number of elmements in the tree. */
protected int size;

// +-----------------+-------------------------------
// | Everything Else |
// +-----------------+

...
} // class NodeBasedTree
```

## History

Tuesday, 18 January 2000

• Created as a blank outline.

Tuesday, 25 April 2000

• Filled in some details.

Back to Introduction to Trees. On to Lab: Traversing and Iterating 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.