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

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

Upload: calix

Post on 05-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

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

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

Thought for the Day

Page 2: “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.

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

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

b c

d e f g

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

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.

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

Printing the Contents of a Tree

pupublic 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

Recursivecalls

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

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.

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

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

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

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

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

Example

m

b t

a k m

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

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

BinarySearchTree

rootrootinsert, 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:

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

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

Three pointers

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

Bounded Generic Type Parameters

• 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 class X<T extends Comparable> ...

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

The Comparable Interface

• Standard Java interface

• Specifies the class must provide a method called compareTo:

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

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

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

• Is (comparable) object x < object y?

if (x.compareTo(y) < 0)...

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

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

Overloaded, privatemethod

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

The Private insert Methodprivate 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

Recursivecalls

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