Podcast Ch18b Title: STree Class

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
1 Binary Trees Binary Trees Binary Search Trees Binary Search Trees CSE Lecture 13 –Trees.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
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,
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
© 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.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
© 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.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
© 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
Chapter 16: Linked Lists.
Podcast Ch23e Title: Implementing Huffman Compression
Data Structures for Java William H. Ford William R. Topp
Podcast Ch17b Title: Iterative Tree Traversal
Chapter 25 Binary Search Trees
Binary Trees and Binary Search Trees
Podcast Ch26a Title: Representing Graphs
Podcast Ch17d Title: Drawing a Binary Tree
Trees.
COMP 103 Binary Search Trees.
Podcast Ch17a Title: Expression Trees
Tree Traversals – reminder
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 20: Binary Trees.
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.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Tree Traversals – reminder
Chapter 21: Binary Trees.
Data Structures Binary Trees.
Data Structures for Java William H. Ford William R. Topp
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Lecture 12 CS203 1.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Podcast Ch22c Title: Deleting from a Heap
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 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 Ch21b Title: Collision Resolution
Chapter 10: Binary Trees.
Presentation transcript:

Podcast Ch18b Title: STree Class Description: Overview; private members and constructor; adding and finding nodes Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

The STree Class The STree class implements the Collection interface. The add() method inserts a new element if a duplicate value is not already present. The boolean return value indicates whether the item was added to the tree. The method toArray() returns an Object array that mirrors the inorder scan and so the elements are referenced in ascending order. An Iterator object scans the elements in ascending order.

The STree Class (continued) The constructor that creates an empty tree with size zero. toString() returns a string that describes the elements in a comma separated list enclosed in square brackets. The elements are listed in ascending order. The methods displayTree(), drawTree() and drawTrees() give a "tree-view" of the elements. Methods first() and last() return the smallest and largest values in the tree.

The STree Class (continued) The method find() takes item as an argument and searches the tree looking for an element whose value matches item. The return value is a reference to the value field of the node or null if no match occurs. The find() method allows an applications programmer to update an element in the tree without having to remove it and reinsert it back in the tree. Use it to change a field not related to the key used for ordering.

STree Class UML

Student Question Doesn’t the find method and the contains method basically do the same thing? How are they different?

Implementing the STree Class For the STree class, extend the structure of a tree node to include a fourth variable that provides a link to the parent of the node. The STNode class defines the modified tree node object.

Implementing STree (cont) The parent reference in STNode allows the implementation of an inorder tree iterator. The STNode class is defined as a private inner class in the STree class. The arrow from the class to itself indicates that an STNode references other STNodes.

Implementing STree (cont) The example gives a tree view and a node view of an STree object. Dotted lines represent the parent links. Note that the parent of the root node is null.

Student Question Are parent pointers required to implement a BST? Would it be possible to have an implementation without parent pointers?

STree Class Private Members and Constructor Private methods findNode() and removeNode promote code reuse. public class STree<T> implements Collection<T>, Iterable<T> { // reference to tree root private STNode<T> root; // number of elements in the tree private int treeSize; // increases whenever the tree changes; used // by an iterator to verify that it is in a // consistent state private int modCount;

STree Class Private Members and Constructor (continued) // create an instance representing an empty // tree; the root is null and the variables // treeSize and modCount are initially 0 public STree() { root = null; modCount = 0; treeSize = 0; }

STree Class Private Members and Constructor (concluded) // iteratively traverse from the root to the // node whose value is item; return a reference // to the node containing item or null if // the search fails private STNode<T> findNode(Object item) { . . . } // private method used by remove() and the // iterator remove() to delete a node private void removeNode(STNode<T> dNode) }

Inserting and Locating a Node Method add() iteratively moves on a path from the root until it locates an empty subtree. It inserts the new node and returns true. If it finds a duplicate, it returns false.

STree Method add() // if item is not in the tree, insert it and // return true; if item is a duplicate, do not // insert it and return false public boolean add(T item){ // t is current node in traversal, parent the // previous node STNode<T> t = root, parent = null, newNode; int orderValue = 0; // terminate on an empty subtree while(t != null){ // update the parent reference parent = t; // compare item and the current node value orderValue = ((Comparable<T>)item).compareTo( t.nodeValue);

STree Method add() (continued) // if a match occurs, return false; // otherwise, go left or go right // following search tree order if (orderValue == 0) return false; // exit, item not added else if (orderValue < 0) t = t.left; else t = t.right; } // create the new node newNode = new STNode<T>(item,parent);

STree Method add() (concluded) if (parent == null) // this is the first node added; make it root root = newNode; else if (orderValue < 0) // attach newNode as the left child of parent parent.left = newNode; else // attach newNode as the right child of parent parent.right = newNode; // increment the tree size and modCount treeSize++; modCount++; // we added a node to the tree return true; }

STree Method find() find() uses findNode() to seek a reference to a node containing value item. find() returns null or a reference only to the nodeValue component. // search for item in the tree and return a // reference to its value or null if item // is not in the tree public T find(T item) { STNode<T> t = findNode(item); T value = null; if (t != null) value = t.nodeValue; return value; }

Student Question If you did not have a parent pointer, how would it be possible to update the parent node when adding a new value to the BST?

Practice Coding Rewrite the add method to work correctly without the use of parent pointers. The previous discussion by John and Tobie should be useful here.