Podcast Ch17b Title: Iterative Tree Traversal

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Advertisements

Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 17 Binary.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 16 Binary.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Podcast Ch24c Title: Breadth First Search
Podcast Ch23e Title: Implementing Huffman Compression
The Tree ADT.
Chapter 12 – Data Structures
Trees Chapter 15.
Lecture No.14 Data Structures Dr. Sohail Aslam
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Recursive Objects (Part 4)
Chapter 22 Custom Generic Data Structures
Podcast Ch17d Title: Drawing a Binary Tree
Trees.
Podcast Ch17a Title: Expression Trees
Tree Traversals – reminder
Chapter 17 Object-Oriented Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Tree Traversals – reminder
Data Structures for Java William H. Ford William R. Topp
CMSC 341 Binary Search Trees.
Podcast Ch18b Title: STree Class
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
2018, Fall Pusan National University Ki-Joune Li
Podcast Ch22c Title: Deleting from a Heap
Podcast Ch18c Title: BST delete operation
Podcast Ch23f Title: Serialization
CMSC 341 Binary Search Trees.
CSC 143 Java Trees.
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch26b Title: Digraph Class Implementation
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21f Title: HashSet Class
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Trees.
Podcast Ch24b Title: Strongly Connected Components
Podcast Ch21b Title: Collision Resolution
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Podcast Ch17b Title: Iterative Tree Traversal Description: Iterative tree traversal; the InorderIterator Class; program 17.2 Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Iterative Tree Traversal Traversing the nodes in a binary tree is more difficult than traversing a LinkedList or ArrayList collection since a tree is a nonlinear structure and there is no one traversal order (e.g. preorder, inorder, postorder). The problem with the recursive algorithms is that there is no escape from the recursive process until it completes. We need to simulate a recursive traversal with an iterative algorithm.

Iterative Tree Traversal (continued) Our binary tree iterator implements the Iterator interface by using a stack to hold the nodes which have been visited. The scan can halt at any time, the programmer can deal with a node, and then continue the scan.

Iterative Tree Traversal (cont) The figure gives a UML diagram for the class InorderIterator that includes the instance variables and the private method goFarLeft() which is critical to finding the "next" node.

Iterative Tree Traversal (continued) Inorder Iterator Algorithm Find the leftmost node in the tree by starting at the root and following the chain of left children until locating a node with an empty left subtree. During this process, the root and all intermediate nodes on the chain of left children are pushed on the stack.

Iterative Tree Traversal (continued) Inorder Iterator Algorithm (continued) Capture the value in the node. If the right branch of the node is not empty, move to the right child and then traverse the chain of left children until locating a node with a null left subtree. This is the next node. During the traversal, push on the stack a reference to each node on the path.

Iterative Tree Traversal (continued) Inorder Iterator Algorithm (concluded) If the right branch of the node is empty, the scan of the node's left branch is complete, and the node itself has been visited. The next node to visit is on the stack. If the stack is not empty, pop it to determine the next node in the scan. If the stack is empty, all nodes have been visited and the scan concludes.

Iterative Tree Traversal (continued)

Iterative Tree Traversal (continued)

Iterative Tree Traversal (continued)

Student Question Step by step show the stack for the inorder iterative traversal of the following tree

Practice Problem Step by step show the stack for the inorder iterative traversal of the following tree

Implementing the InorderIterator The InorderIterator class provides instances that execute an iterative inorder scan of a binary tree. An InorderIterator object scans a binary tree and access the value of the elements. The remove() method throws an UnsupportedOperationException. The instance variables include a stack for TNode references and the variable curr which references the next node. The end of a traversal occurs when curr becomes null.

Implementing the InorderIterator (cont) The class uses the private method goFarLeft() to locate the first element and to execute rule 2. The method begins at node t and stacks all of the nodes until it locates one with a null left subtree. A reference to this node is the return value. public class InorderIterator<T> implements Iterator<T> { private ALStack<TNode<T>> s = null; private TNode<T> curr = null; . . . private TNode<T> goFarLeft(TNode<T> t) {...} }

Implementing the InorderIterator Class (continued) // go far left from t, pushing all the nodes with // left children on stack s private TNode<T> goFarLeft(TNode<T> t) { if (t == null) return null; while (t.left != null) s.push(t); t = t.left; } return t;

Implementing the InorderIterator Class (continued) The constructor allocates the stack and calls goFarLeft() to position curr at the first node inorder. Since InorderIterator is not included in a collection class, a user must pass the root of the binary tree as an argument. public InorderIterator(TNode<T> root) { s = new ALStack<TNode<T>>(); curr = goFarLeft(root); }

Implementing the InorderIterator Class (continued) The method next() implements Steps 1 through 3. In keeping with the requirements of the Iterator interface, next() throws NoSuchElementException if the tree traversal is complete.

The InorderIterator (concluded) public T next() { if (curr == null) throw new NoSuchElementException( "InorderScan: no elements remaining"); // capture the value in the node T returnValue = curr.nodeValue; if (curr.right != null) // have a right subtree // stack nodes on left subtree curr = goFarLeft(curr.right); else if (!s.isEmpty()) // no right subtree; there are other nodes // to visit; pop the stack curr = (TNode<T>)s.pop(); else // end of tree; set curr to null curr = null; return returnValue; }

Student Question Can you think of some practical applications for the inorder iterator?

Program 17.2 The static method, buildTime24Tree() in the BinaryTree class builds the following binary tree of Time24 objects.

Program 17.2 (continued) The program calls buildTime24Tree() to create the tree and then uses an inorder tree iterator to traverses the nodes. After each call to next(), the program updates the time value of the node by adding 60 minutes ( 1 hour). A call to displayTree() outputs the updated tree.

Program 17.2 (Run) Original tree 3:15 18:35 20:55 10:45 12:00 15:30 18:35 20:55 10:45 12:00 15:30 5:15 7:30 9:15 Modified tree 4:15 19:35 21:55 11:45 13:00 16:30 6:15 8:30 10:15

Program 17.2 (continued) import ds.util.TNode; import ds.util.BinaryTree; import ds.util.InorderIterator; import ds.time.Time24; public class Program17_2 { public static void main(String[] args) { // roots for the tree TNode<Time24> root; // build a tree of Time24 data root = BinaryTree.buildTime24Tree(); // display the tree System.out.println("Original tree"); System.out.println(BinaryTree.displayTree( root, 5) + "\n");

Program 17.2 (concluded) // declare an inorder tree iterator InorderIterator<Time24> iter = new InorderIterator<Time24>(root); // go through the tree and add 1 hour while (iter.hasNext()){ // obtain the value in a tree node Time24 t = iter.next(); // add 1 hour to the time t.addTime(60); } System.out.println("Modified tree"); System.out.println (BinaryTree.displayTree(root, 5)); // delete the nodes in the tree BinaryTree.clearTree(root);

Student Question How could you write an preorder traversal of a binary tree that does not use recursion?

Practice Coding Now that you have heard the discuss by John and Tobie, write a method that will print a preorder traversal without using recursion