CSC 205 Java Programming II Lecture 26 Traversing Binary Tree
TreeNode & BinaryTree root
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
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)
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
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
Example – Animal Guess Are you a mammal? Are you bigger than a catDo you live underwater? KangarooMouseTroutRobin Yes No root
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
Examples Related concepts Preorder Postorder Inorder Jane Bob Tom AlanErikNile
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
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
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
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
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