“There is never enough time to do everything, but there is always enough time to do the most important thing.” – Brian Tracy Thought for the Day
Binary Trees Important subcategory of trees: –maximum outdegree is two –“left subtree” and “right subtree” a bc def
A Java Class for Binary Trees Operations? –create new node –access subtrees –access data –add new subtrees
A Java Class for Binary Trees a bc def
Tree data, lt, rt getData, left, right, addLeft, addRight Class Diagram
The Tree Class public class Tree { private T data; private Tree lt, // Ptr to left subtree rt; // Ptr to right subtree... } // class Tree Note: this really describes a single node
Constructors public Tree (T value, Tree left, Tree right) // Constructor: creates a new node with left // and right subtrees { lt = left; rt = right; data = value; } // Constructor public Tree (T value) // Constructor: creates a new node { this(value, null, null); } // Constructor Call first constructor
Other Methods Very simple public Tree left () // Return the left subtree of a tree { return lt; } public void addLeft (Tree left) // Add a left subtree to a node { if (lt != null) throw new …Exception(…); lt = left; } // addLeft public T getData () // Access the data value in this node { return data; }
Using Trees Many applications –decision trees –compilers We will consider a simple “knowledge base” (expert system) application –children’s guessing game
The Guessing Game Program asks the user questions, attempting to identify an animal The questions and answers (i.e. the program’s “knowledge”) are stored in a tree
The Knowledge Base Tree Does it live in water? Does it have webbed feet?Does it fly? birdDoes it bark? duckfish dogcat Note: This is a proper binary tree
The Algorithm Starting at the root: –if at a leaf node: give the answer –else ask the question if answer is positive, go to left subtree otherwise go to right subtree
The Java Code p pos = root; // Start at root w while (pos != null) { if (pos.left() != null) // Question { System.out.println(pos.getData()); if (answer()) pos = pos.left(); else pos = pos.right(); } else // Leaf: must be an answer { System.out.println("It's a " + pos.getData()); break; }
Initialising the Tree ree Tree p; // Temporary pointer oot root = new Tree ("Does it live in water?"); ot root.addLeft(new Tree ("...webbed feet?")); ot.a root.addRight(new Tree ("Does it fly?")); p = root.right(); p.addLeft(new Tree ("bird")); p.addRight(new Tree ("Does it bark?")); p = p.right(); p.addLeft(new Tree ("dog")); p.addRight(new Tree ("cat")); // Return to left subtree of root p = root.left(); p.addLeft(new Tree ("duck")); p.addRight(new Tree ("fish"));
Initialising the Tree: An Alternative Bottom up: Tree left, right, p; left = new Tree ("dog"); right = new Tree ("cat"); p = new Tree ("Does it bark?", left, right);... Uses the constructor rather than the addLeft and addRight methods
Traversing Trees We often need to work through a tree “visiting” each node Several different ways that we can do this: –in-order –pre-order –post-order –breadth-order –etc., etc.
Traversal Methods In-Order (LNR): – d b e a f c g Pre-Order (NLR): –a b d e c f g Post-Order (LRN): –d e b f g c a Breadth-Order: –a b c d e f g a bc defg