Presentation is loading. Please wait.

Presentation is loading. Please wait.

“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.

Similar presentations


Presentation on theme: "“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."— Presentation transcript:

1 “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

2 Binary Trees Important subcategory of trees: –maximum outdegree is two –“left subtree” and “right subtree” a bc def

3 A Java Class for Binary Trees Operations? –create new node –access subtrees –access data –add new subtrees

4 A Java Class for Binary Trees a bc def

5 Tree data, lt, rt getData, left, right, addLeft, addRight Class Diagram

6 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

7 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

8 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; }

9 Using Trees Many applications –decision trees –compilers We will consider a simple “knowledge base” (expert system) application –children’s guessing game

10 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

11 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

12 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

13 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; }

14 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"));

15 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

16 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.

17 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

18


Download ppt "“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."

Similar presentations


Ads by Google