Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.

Similar presentations


Presentation on theme: "CSC 205 Java Programming II Lecture 26 Traversing Binary Tree."— Presentation transcript:

1 CSC 205 Java Programming II Lecture 26 Traversing Binary Tree

2 TreeNode & BinaryTree root

3 Reference-Based Repres’n Needs a TreeNode class UML class diagram TreeNode -item:Object -left:TreeNode -right:TreeNode +TreeNode(item:Object) +TreeNode(item:Object,left:TreeNode, right:TreeNode) +setLeft(left:TreeNode) +getLeft():TreeNode +isLeaf():boolean

4 The BinaryTree Class BinaryTreeBasis #root:TreeNode +BinaryTreeBasis() +BinaryTreeBasis(root:TreeNode) +isEmpty():boolean +makeEmpty() +getRoot():TreeNode BinaryTree +BinaryTree () +BinaryTree (root:TreeNode) +BinaryTree (root:TreeNode, leftTree:BinaryTree, rightTree:BinaryTree) +setRoot(root:TreeNode) +attachLeft(left:TreeNode) +detachLeft() +attachLeftSubtree(lTree:BinaryTree) +detachLeftSubtree(lTree:BinaryTree)

5 Typical BinaryTree Methods public void attachLeft(Object newItem) { if (!isEmpty() && root.getLeft() == null) { // assertion: nonempty tree; no left child root.setLeft(new TreeNode(newItem, null, null)); } // end if } // end attachLeft

6 public void attachLeftSubtree(BinaryTree leftTree) throws TreeException { if (isEmpty()) { throw new TreeException("TreeException: Empty tree"); } else if (root.getLeft() != null) { // a left subtree already exists; it should have been // deleted first throw new TreeException("TreeException: " + "Cannot overwrite left subtree"); } else { // assertion: nonempty tree; no left child root.setLeft(leftTree.root); // don't want to leave multiple entry points into // our tree leftTree.makeEmpty(); } // end if } // end attachLeftSubtree

7 Example – Animal Guess Are you a mammal? Are you bigger than a catDo you live underwater? KangarooMouseTroutRobin Yes No root

8 Tree Traversal Traversal: visit each node in a tree Visit root Visit every node in the left subtree Visit every node in the right subtree Three different traversal algorithms Preorder: visit root before all other nodes Postorder: visit root after all other nodes Inorder: visit nodes in left subtree, then the root, and then nodes in the right tree

9 Examples Related concepts Preorder Postorder Inorder Jane Bob Tom AlanErikNile

10 Tree Traversal Using Iterator An Iterator class needs to implement hasNext() next() and optionally, remove() To enforce the ordering, items in a tree will be put in a queue One of the three orders should be chosen before the iterator may be used Changes will not be reflected in the iterator, since the remove method is not implemented here

11 Typical TreeIterator Methods public class TreeIterator implements java.util.Iterator { private BinaryTreeBasis binTree; private TreeNode currentNode; private QueueInterface queue; public TreeIterator(BinaryTreeBasis bTree) { binTree = bTree; currentNode = null; // empty queue indicates no traversal type // currently selected or end of current // traversal has been reached queue = new QueueReferenceBased(); } // end constructor

12 Typical TreeIterator Methods – cont’d public boolean hasNext() { return !queue.isEmpty(); } // end hasNext public Object next() throws java.util.NoSuchElementException { try { currentNode = (TreeNode)queue.dequeue(); return currentNode.getItem(); } // end try catch (QueueException e) { throw new java.util.NoSuchElementException(); } // end catch } // end next

13 Typical TreeIterator Methods – cont’d public void setPreorder() { queue.dequeueAll(); preorder(binTree.root); } // end setPostorder private void preorder(TreeNode treeNode) { if (treeNode != null) { queue.enqueue(treeNode); preorder(treeNode.getLeft()); preorder(treeNode.getRight()); } // end if } // end preorder

14 Binary Search Tree Three properties for any node n in a BST n’s value is greater than all values in its left subtree n’s value is less than all values in its right sub tree Both left and right subtrees are BSTs


Download ppt "CSC 205 Java Programming II Lecture 26 Traversing Binary Tree."

Similar presentations


Ads by Google