1 / 20

Podcast Ch18b

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.

cbret
Download Presentation

Podcast Ch18b

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

  2. The STree Class • The STree class implements theCollection 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.

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

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

  5. STree Class UML

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

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

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

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

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

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

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

  13. 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) { . . . } }

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

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

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

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

  18. STree Method find() find() uses findNode() to seek areference 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; }

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

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

More Related