In a linear structure, every element has unique successor.
In trees, may have many.
Simple path is series of distinct nodes such that there is an edge between successive nodes.
path length = number of nodes on path - 1 = # edges traversed in path,
height of node = length of longest path from that node to a leaf.
Height of tree = height of its root.
Depth of node is length of path from root to that node.
Degree of node is number of its direct descendents.
Level defined recursively:
Root is at level 0.
Level of any other node is one greater than level of its parent.
Level of a node also = length of path from root to that node.
We'll stick to binary trees for now! (All nodes of degree <= 2.)
Deal with oriented trees: Identify each subtree as being either left or right.
Lemma: If T is a binary tree, then at level k, T has <= 2k nodes.
Theorem: If T has height h then n = # nodes in T <= 2h+1 -1. Equivalently, if T has n nodes, then n - 1 >= h >= log(n+1) - 1.
Note that these bounds are tight.
A full binary tree of height h has all leaves on level h.
A complete binary tree of height h is obtained from a full binary tree of height h with 0 or more (but not all) of of the rightmost leaves at level h removed.
Say T is balanced if it has the minimum possible height for its # of nodes.
In this case, height = ceiling of log2(n+1) - 1 or O ( log2 n )
Note that a balanced tree may have lots of holes in it.
The structures package actually doesn't include an interface for binary trees (it just contains one class BinaryTree), but here is what it would look like if it existed:
public interface BinaryTreeInterface { public void clear(); // post: removes all nodes from tree public void insert(Object value); // pre: cursor is invalid // post: if tree empty, value is inserted at root, o'wise // value inserted where cursor last moved off tree public Object remove(); // pre: cursor is valid and has no children // post: leaf removed, cursor is moved to parent, if any public Object value(); // pre: cursor valid // post: returns value of object at cursor public void setValue(Object value); // pre: cursor valid // post: sets value found at cursor public void reset(); // post: moves the cursor to the root, if any public boolean valid(); // post: returns true if cursor points to a valid node. public boolean hasLeft(); // post: returns true iff cursor has left child public boolean hasRight(); // post: returns true iff cursor has right child public boolean hasParent(); // pre: cursor is valid // post: returns true iff cursor has parent public boolean isLeftChild(); // post: return true if cursor has parent & is left child public boolean isRightChild(); // post: return true if cursor has parent & is rt child public void moveLeft(); // pre: cursor is valid // post: cursor moves to left child of pre-cursor, // or off tree public void moveRight(); // pre: cursor is valid // post: cursor moves to right child of pre-cursor, // or off tree public void moveUp(); // pre: cursor is valid // post: cursor moves up to parent of pre-cursor public int height(); // post: returns height of cursor in tree // or -1 if tree is empty public int depth(); // post: returns depth of cursor in tree // or -1 if tree is empty public boolean isFull(); // post: return true iff subtree rooted at cursor is full public boolean isComplete(); // post: returns true iff subtree rooted at cursor is // complete public boolean isEmpty(); // post: returns true iff tree is emtpy public int size(); // post: returns number of nodes in tree public Iterator elements(); // post: returns inorder traversal of tree public Iterator inorderElements(); // post: returns inorder traversal of tree public Iterator preorderElements(); // post: returns preorder traversal of tree public Iterator postorderElements(); // post: returns postorder traversal of tree public String toString(); // post: returns string representation of tree }
Tree traversals - visit all nodes in a tree
root - left sub-tree - right sub-tree
left sub-tree - root - right sub-tree
left sub-tree - right sub-tree - root
Notice what happens when you do these traversals of expression trees: Get prefix, infix, or postfix notation.
Most algorithms involve two parts -
Why is recursion such an intuitive notion for trees?
Once we have an expression tree, how can we evaluate it?
Evaluate left subtree, evaluate right subtree, then perform operation at root:
What do we know about first possible characters of Factor, Term, or Expression?
Can use to help catch errors.
Look at how 3 * 7 + 6 / 2 - (3 + 7) would be understood as a tree.
3*7 is a term because both 3 and 7 are factors, similarly 6/2 is a term.
(3+7) is a factor because 3 + 7 is is an expression.
Because it is a factor, it is also a term.
Therefore 3 * 7 + 6 / 2 - (3 + 7) is of the form term1 + term2 - term3, and hence is an expression!
Exercise: Write out the corresponding tree (remember operations of the same precedence are done from left to right.
Once you have the tree, how do you evaluate it?
See Parser code on-line.