CH 18
CH 18
Trees
Binary Trees
• Binary trees should have been covered in CS1720, but we will quickly review them here.
• Here is some code that represents the way I like to code binary trees. It represents the best
of the ideas I've gleaned from others.
Trees Page 1
// TreeNode: Contains an individual node of an expression tree
class TreeNode
{
public:
int value; // integer value of node, if a leaf
char operator; // operator (+-*/) if internal node
TreeNode *left; // left child
TreeNode *right; // right child
TreeNode( int Val, char Op, TreeNode *L = NULL,
TreeNode *R = NULL ) : value(Val), operator(Op),
left( L ), right( R ) { }
};
class BinaryTree
{ protected:
void makeEmpty( TreeNode * & T );
void printTree( TreeNode * T ,int indent) ;
TreeNode *root; // Root of expression tree
void getSubtree(TreeNode *& root, string exp,int& beg);
public:
BinaryTree( ) : root( NULL ) { }
BinaryTree(string exp);
virtual ~BinaryTree( ) { makeEmpty( root ); }
void makeEmpty( ) { makeEmpty( root ); }
void printTree( ) { printTree( root,1 ); }
};
Trees Page 2
{ TreeNode * left, *right;
if (exp[beg]=='('){ // You fill in
}
else{ // expecting a single digit operand
root = new TreeNode(exp[beg]-'0',' ');
beg++;
}
}
Expression Trees
• Tree whose interior nodes represent and operation (e.g., addition, multiply) and leaf node
represent operands.
• For example, see Figure 1 below.
Traversal
• Sometimes we need to visit each node exactly once (imposed linearity).
Trees Page 3
• Typically, we visit the left branch before the right one.
• There are three traversal methods:
1. Preorder
Order of visit – node, left, right
For an example, consider nodes are chapters or subsections and arcs are
containment relationship, preorder gives table of contents.
This is the prefix form of an expression
2. Inorder
Order of visit – left, node, right
If tree represents expression tree, inorder gives algebraic expression.
Pay attention to precedence. The higher the precedence, the lower in the
tree.
A-C/D + 5*D template <class Etype>
3. Postorder void BinarySearchTree<Etype>::
Order of visit – left, right, Preorder( TreeNode<Etype> *T) const
node { if (T==NULL) return;
Postfix form of an expression cout << T->Element << endl;
Preorder(T->Left);
• The procedure to print the contents of a tree
Preorder(T->Right);
in preorder is shown in Figure 2. }
• Discuss by level traversal (breadth first). Use
queue as an auxiliary structure.
Figure 2 Preorder Traversal
Efficiency Issues
inorder(node * root)
Auxiliary Stack { node * p;
STACKCLASS Stack;
for (p = root; p != NULL; p = p->Left) Stack.push(p);
• Since recursion is while (!Stack.isempty())
expensive, it is sometimes { p = Stack.pop();
better to code iteratively. cout << p->Element;
Then a stack is used as an for(p = p->Right; p != NULL; p = p->nleft)
auxiliary data structure. Stack.push(p);
• See the code in Figure 3. }
}
General Trees
Figure 3 Using a Stack with a Tree
Trees Page 4
• Fixed number of children for a given parent.
• If there are a fixed number of children (say MAX), the definition in Figure 4 works well.
Figure 5 Parent
Pointer
Trees Page 5