1 / 17

“To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu

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

calix
Download Presentation

“To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thought for the Day “To become truly great, one has to stand with people, not above them.”– Charles de Montesquieu

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

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

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

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

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

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

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

  9. m b t a k m Example • LNR (in-order) traversal visits nodes in ascending order a b k m m t

  10. 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:

  11. 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; ...

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

  13. 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:

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

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

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

More Related