A Recursively-Defined Tree Class1
Jeffrey D. Oldham
2000 Apr 05
We explain how to use the recursively defined Tree class. The
implementation file and
example use code are available.
Using the templatized Tree class requires specifying the type
of the items stored in the tree. In this document, we will use trees
of ints.
A tree is
- either an empty tree
- or an item with two subtrees.
To create an empty tree, use
Tree<int>()
To check if a tree T is empty, use
T.empty()
which returns the boolean value true if the tree has no items and
otherwise false. Using T in a place where a boolean is
expected yields true if the tree is not empty and otherwise false.
For example, if (T) cout << "tree is not empty
n";.
To construct a tree with one item, e.g., 3, use
Tree<int>(3,Tree<int>(),Tree<int>())
To construct a tree with one item, e.g., -17, and two tree children
L and R, use
Tree<int>(-17,L,R)
To obtain a tree T's item, which has int type, use
T.item()
To obtain the left subtree, which has Tree<int> type, use
T.left()
To obtain the right subtree, which has Tree<int> type, use
T.right()
To check if a tree is nonempty, negate the result of checking for an
empty tree.
The example use code illustrates
using Trees.
To use the linked tree class with a program you wrote, copy the
implementation file to the directory
containing the program's C++ code. One way to do this is to use the
``Save As...'' item on a WWW browser's file menu.
In your C++ program, add the line
#include "tree.h"
near the other header inclusions. See also the
sample program.
Footnotes
- ... Class1
- ©2000
Jeffrey D.
Oldham .
All rights reserved. This document may not be redistributed in any form
without the express permission of the author.
2000-04-12