Podcast Ch18d Title: Binary Search Tree Iterator

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Trees II CSC 172 SPRING 2002 LECTURE 15. Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node)
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Tree.
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.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Binary Search Tree Qamar Abbas.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Podcast Ch17b Title: Iterative Tree Traversal
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Podcast Ch17d Title: Drawing a Binary Tree
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Podcast Ch17a Title: Expression Trees
Binary Trees, Binary Search Trees
Binary Search Trees.
Data Structures Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Data Structures for Java William H. Ford William R. Topp
Podcast Ch18b Title: STree Class
CMSC 341 Binary Search Trees 1/3/2019.
Chapter 12: Binary Search Trees
Binary Search Trees.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Podcast Ch22c Title: Deleting from a Heap
Binary Trees, Binary Search Trees
Podcast Ch18c Title: BST delete operation
CSC 143 Binary Search Trees.
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Trees.
Podcast Ch21b Title: Collision Resolution
Binary Trees, Binary Search Trees
Chapter 10: Binary Trees.
Presentation transcript:

Podcast Ch18d Title: Binary Search Tree Iterator Description: Additional operations first and last; the BST iterator Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Additional STree Operations The method first() must return the smallest element in the tree. In a binary search tree, this element is in the leftmost node from root.

Additional STree Operations (continued) // returns the first (least) element in this // binary search tree public T first() { STNode<T> nextNode = root; // if the set is empty, return null if (nextNode == null) return null; // first node is the furthest left from root while (nextNode.left != null) nextNode = nextNode.left; return nextNode.nodeValue; }

Additional STree Operations (concluded) The method last() must return the largest element in the tree. In a binary search tree, this element is in the right-most node from root. Implement the methods by starting at the root and scan down the path of right children. The value of the last node on the path is the maximum value in the binary search tree. A binary search tree has best-case search time O(log2n), worst-case time O(n), and average time O(log2n).

Practice Coding Write the method last to find the largest item in a BST?

The STree Iterator Implement an iterative traversal of the elements using the STNode parent field. The figure illustrates the order of scan for the twenty elements in the search tree. If the current iterator position is node 18, the next two elements are found by moving up to the parent 20 and then down the right subtree to node 22 and node 23. From node 23 the next element is the root node 25. We locate the value by scanning up the path of parents four levels.

The STree Iterator (continued)

Student Question Given the tree Starting with #0, label the nodes in the order they will be visited by an iterator.

Practice Problem Given the tree Starting with #0, label the nodes in the order they will be visited by an iterator.

IteratorImpl Inner Class private class IteratorImpl implements Iterator<T> { // set expectedModCount to the number of list // changes at the time of iterator creation private int expectedModCount = modCount; // node of the last value returned by next() // if that value was deleted by the iterator // method remove() private STNode<T> lastReturned = null; // node whose value is returned a subsequent // call to next() private STNode<T> nextNode = null; ... }

IteratorImpl Inner Class (continued) The first node is the one far-left from the root. Subsequent nodes are visited in LNR order.

IteratorImpl Inner Class (cont) // constructor IteratorImpl() { nextNode = root; // if the tree is not empty, the first node // inorder is the farthest node left from root if (nextNode != null) while (nextNode.left != null) nextNode = nextNode.left; } // returns an iterator for the elements in the tree public Iterator<T> iterator() return new IteratorImpl();

Implementing next() If the right subtree is not empty, obtain the next node in order by moving to the right child and then moving left until encountering a null subtree. If the right subtree is empty, obtain the next node in order by following a chain of parent references until locating a parent, P, for which the current node, nodePtr, is a left child. Node P is the next node in order.

Implementing next() (continued) When invoking next() from the node with the tree's maximum value, the right subtree is empty. As we move up the tree, we are always a right child of our parent until we encounter the root node, whose parent is null. In this situation, the variable nextNode becomes null and we have completed an iteration.

Implementing next() (continued) // returns the next element in the iteration; // throws NoSuchElementException if iteration // has no more elements public T next() { // check that the iterator is in a consistent // state; throws ConcurrentModificationException // if it is not checkIteratorState(); // check if the iteration has another element; // if not, throw NoSuchElementException if (nextNode == null) throw new NoSuchElementException( "Iteration has no more elements"); // save current value of next in lastReturned lastReturned = nextNode; // set nextNode to the next node in order STNode<T> p;

Implementing next() (cont) // have already processed the left subtree, // and there is no right subtree; move up // the tree, looking for a parent for // which nextNode is a left child, stopping // if the parent becomes null; a non-null // parent is the successor; if parent is null, // the original node was the last node inorder p = nextNode.parent; if (nextNode.right != null) { // successor is the furthest left node of // right subtree nextNode = nextNode.right; while (nextNode.left != null) nextNode = nextNode.left; }

Implementing next() (concluded) else { while (p != null && nextNode == p.right) nextNode = p; p = p.parent; } // if we were previously at the right-most // node in the tree, nextNode = null return lastReturned.nodeValue;

Implementing hasNext() The iteration is complete when nextNode becomes null. The implementation of hasNext() simply verifies that nextNode is not equal to null. // returns true if the tree has more // unvisited elements public boolean hasNext() { // elements remain if nextNode is not null return nextNode != null; }

Student Question How would you write a non-recursive iterator if you did not have a parent pointer?

Practice Problem Write a non-recursive iterator assuming you do not have a parent pointer? The previous discussion by John and Tobie should be useful.