[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]

Back to Project Presentations. On to Traversing Trees.

**Held** Wednesday, November 24, 1999

**Overview**

Today, we continue our discussion of trees by considering the design of a tree interface and another application, expression trees.

**Notes**

- Are there any questions on exam 3
- I haven't yet checked my email today, but I don't seem to have current files from all the groups.

**Contents**

**Handouts**

**Summary**

- A tree design
- Binary expression trees

- On Monday, we developed a basic outline of how we might represent trees.
- Here's my example
**/** * A simple way of looking a trees. Uses a cursor to support * retrieval of information in the tree. * * @author Samuel A. Rebelsky * @version 1.0 of November 1999 */**__public____interface__CursoredTree {**// +-----------+-----------------------------------------------****// | Modifiers |****// +-----------+****/** * Set the value stored in the root of the tree. * Pre: (none) * Post: The root of the tree now contains newRoot. * The tree is nonempty. */**__public____void__setRoot(Object newRoot);**/** * Set one of the children of the current node (given by the * cursor). Children are numbered from 0 to numChildren-1. * Pre: The cursor is at a known location. * Post: The number of children is at least childNum-1. * The childNum'th child contains newChild. */**__public____void__setChild(Object newChild,__int__childNum);**/** * Clear the subtree starting at the current node. * Pre: The cursor is at a known location. * Post: The cursor is at the parent of the current location * (as long as the cursor was not at the root). * There is now a "hole" where the subtree was. */**__public____void__clear();**// +-----------------+-----------------------------------------****// | Cursor Movement |****// +-----------------+****/** * Move the cursor to the root of the tree. * Pre: (none) * Post: The cursor is at the root of the tree. */**__public____void__root();**/** * Move the cursor up one level in the tree. * Pre: The cursor is not at the root of the tree. * Post: The cursor is at the parent of the current node. */**__public____void__parent();**/** * Move the cursor down to a child. * Pre: The current node has at least childNum-1 children. * Post: The cursor is on the node representing the specified child. */**__public____void__child(__int__childNum);**// +-----------+-----------------------------------------------****// | Accessors |****// +-----------+****/** * Get the value associated with the current node. * Pre: There is a current node. * Post: Returns the value associated with the current node. */**__public__Object getValue();**/** * Get the number of children of the current node. * Pre: There is a current node. * Post: Returns the number of children associated with the current node. */**__public____int__getNumChildren(); } // interface CursoredTree - This still leaves some design issues unconsidered
- What happens if we set the 4th child, but not the 3rd? How many children does the node then have?
- What really happens after you clear a subtree, particularly with regards to the parent?

- There are a number of ways that we can use trees.
- One common use of trees is to unambiguously represent arithmetic expressions.
- Consider the expression ``3+4*5-6''.
- What is the first operation you do? Is it ``add 3 and 4'' or ``multiply 4 and 5''?
- How do you know?

- If we draw it as a tree, we can make things clearer. An operation
is a node in the tree. Its operands are its children.
+ - / \ / \ 3 - * 6 / \ / \ * 6 + 5 / \ / \ 4 5 3 4

- This structure provides an easy evaluation strategy:
- Evaluate the left subtree
- Evaluate the right subtree
- Apply the operand

- Note that it has an interesting side-effect: the stuff to be done first appears near the bottom of the tree.

Tuesday, 10 August 1999

- Created as a blank outline.

Wednesday, 17 November 1999

- Filled in the details (as outline 45).

Wednesday, 24 November 1999

- Moved to outline 48.

Monday, 29 November 1999

- Moved uncovered material to outline 49.

Back to Project Discussion. On to Project Testing.

[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]

**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.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.48.html

Source text last modified Mon Nov 29 09:14:53 1999.

This page generated on Mon Nov 29 09:42:33 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu