In a linear structure, every element has unique successor.
In trees, may have many.
DEFINITION: A
tree is either empty or consists of a node, called the root node,
together with a collection of (disjoint) trees, called its subtrees.
Once we have an expression tree, how can we evaluate it?
Evaluate left subtree, evaluate right subtree, then perform operation at root.
Asking questions corresponds to path from root to a leaf, where leaf holds animal which is best guess.
If guess wrong, then add new animal and corresponding question (w/old animal, too) in tree in place of old animal.
Level defined recursively:
Note. 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.
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.
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 the rightmost leaves at level h removed.
Say T is balanced if it has the minimum possible height for its # of nodes.
The structures package doesn't actually 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
}