170 likes | 325 Views
Thought for the Day. “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu. 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
E N D
Thought for the Day “To become truly great, one has to stand with people, not above them.”– Charles de Montesquieu
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.
a b c d e f g 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
Writing Traversal Methods • We can use the existing methods in our Tree class • Recursion is the easiest way! Challenge (for those who don’t believe me!): write a non-recursive traversal method.
Recursive calls Printing the Contents of a Tree public void LNRPrint (Tree<Character> root) // Recursive in-order traversal of tree // printing out the nodes' data { if (root != null) { LNRPrint(root.left()); System.out.println(root.getData()); LNRPrint(root.right()); } } // LNRPrint
Iterators • Tree traversals are common operations • Useful to provide methods in tree classes • But, we may want to do different things • print • add to total • compare with a “search” value • etc.
Solution: • Provide methods that supply iterators • objects • have methods to move through the data structure, and access the data values • See how it’s done later...
Ordered Binary TreesBinary Search Trees • Trees so far: • No requirement for ordering nodes • Binary Search Tree • Values less than the node’s value: in left subtree • Otherwise in right subtree
m b t a k m Example • LNR (in-order) traversal visits nodes in ascending order a b k m m t
BinarySearchTree root insert, remove, contains, getLNRIterator, getNLRIterator, getLRNIterator Implementation • To prevent clients destroying the ordering we need a different design: • remove addLeft and addRight methods • hide the structure (inner “node” class) • Class diagram:
Three pointers The BinarySearchTree Class public class BinarySearchTree <T extends Comparable> { private class BSTreeNode { public T data; public BSTreeNode lt, // Left subtree rt, // Right subtree parent; // Parent node } // inner class BSTreeNode private BSTreeNode root; ...
Bounded Generic Type Parameters public class X<T extends Comparable> ... • T can be any type that extends (implements) the Comparable interface • Restricts the type of objects that can be used • Here, Comparable is required for the ordering of the nodes
public int compareTo (Object o); /* Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. */ The Comparable Interface • Standard Java interface • Specifies the class must provide a method called compareTo:
Is (comparable) object x < object y? if (x.compareTo(y) < 0)... The Comparable Interface • Many classes implement this interface • wrapper classes, String, Date, etc. • Allows us to insert items into the correct position in a binary search tree
Overloaded, private method The insert method public void insert (T newValue) // Add a new node to the tree { if (root == null) root = new BSTreeNode(newValue); else insert(newValue, root); } // insert
Recursive calls The Private insert Method private void insert (T value, BSTreeNode root) { assert root != null; if (root.data.compareTo(value) > 0) // Add to left subtree if (root.lt != null) insert(value, root.lt); else root.lt = new BSTreeNode(value, root); else // Add to right subtree if (root.rt != null) insert(value, root.rt); else root.rt = new BSTreeNode(value, root); } // insert