Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.

Similar presentations


Presentation on theme: "© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary."— Presentation transcript:

1 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary Search Trees Bret Ford © 2005, Prentice Hall

2 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Tree The nodes in a binary search tree are on unique paths from the root according to an ordering principle. The nodes in a binary search tree are on unique paths from the root according to an ordering principle. The left subtree of a node contains smaller values and the right subtree contains larger values. This implies that duplicates are not allowed. The left subtree of a node contains smaller values and the right subtree contains larger values. This implies that duplicates are not allowed.

3 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Tree (continued)

4 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Binary Search Tree Insertion strategy: Insertion strategy: If the value of the new element is equal to the value of the current node, perform no action. If the value of the new element is equal to the value of the current node, perform no action. If the value of the new element is less than the value of the current node, proceed to the left subtree (left child) of the node. If null, insert the node as a left child of the parent. If the value of the new element is less than the value of the current node, proceed to the left subtree (left child) of the node. If null, insert the node as a left child of the parent. If the value of the new element is greater than the value of the current node, proceed to the right subtree (right child) of the node. If null, insert the node as a left child of the parent. If the value of the new element is greater than the value of the current node, proceed to the right subtree (right child) of the node. If null, insert the node as a left child of the parent.

5 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Binary Search Tree (continued)

6 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Locating an Object in a Binary Search Tree Search for an element the same way you inserted it. Search for an element the same way you inserted it.

7 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inorder Scan of a Binary Search Tree The first value visited in the minimum value in the tree. The first value visited in the minimum value in the tree. The LNR inorder scan visits the nodes in ascending order. The LNR inorder scan visits the nodes in ascending order. The RNL inorder scan visits the nodes in descending order. The RNL inorder scan visits the nodes in descending order.

8 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing a Binary Search Tree Node Note immediately that the root node cannot be removed from the following tree since we would be left with two orphaned subtrees. We need to find a replacement value for 25. Note immediately that the root node cannot be removed from the following tree since we would be left with two orphaned subtrees. We need to find a replacement value for 25.

9 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing a Binary Search Tree Node (continued) To delete a node from a binary search tree, you must find a replacement node, copy its value, and then splice it out of the tree. To delete a node from a binary search tree, you must find a replacement node, copy its value, and then splice it out of the tree.

10 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The STree Class The STree class implements the Collection interface. 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 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 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.

11 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The STree Class (continued) The constructor that creates an empty tree with size zero. 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. 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. 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. Methods first() and last() return the smallest and largest values in the tree.

12 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

13 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. STree Class UML

14 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 18.1 The program is a GUI application that allows a user to dynamically update a search tree by inserting and deleting elements. The program is a GUI application that allows a user to dynamically update a search tree by inserting and deleting elements.

15 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 18.1 (continued) public void actionPerformed(ActionEvent ae) { // ae.getSource() is a reference to the object // that caused the ActionEvent JButton buttonPressed = (JButton)ae.getSource(); int n; // convert the string in input to an int n = Integer.parseInt(itemField.getText()); if (n 99) { JOptionPane.showMessageDialog( appFrame, "Integer must be in range " + "from 0 to 99", "Data Error", JOptionPane.ERROR_MESSAGE); return; }

16 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 18.1 (concluded) if (buttonPressed == addButton) tree.add(n); else tree.remove(n); // display the tree in the text area currTreeDisplay += "\n\n" + tree.displayTree(2); textArea.setText(currTreeDisplay); itemField.setText(""); itemField.requestFocus(true); }

17 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

18 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing the STree Class (continued) The parent reference in STNode allows the implementation of an inorder tree iterator. 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. 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.

19 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing the STree Class (continued) 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. 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.

20 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. STree Class Private Members and Constructor Private methods findNode() and removeNode promote code reuse. Private methods findNode() and removeNode promote code reuse. public class STree implements Collection, Iterable { // reference to tree root private STNode 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;

21 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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; }

22 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. STree Class Private Members and Constructor (concluded) // iteratively traverse a path 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 findNode(Object item) {... } // private method used by remove() and the iterator // remove() to delete a node private void removeNode(STNode dNode) {... } }

23 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

24 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 = 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 )item).compareTo( t.nodeValue);

25 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 (item,parent);

26 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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; }

27 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. STree Method find() // 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 = findNode(item); T value = null; if (t != null) value = t.nodeValue; return value; } find() uses findNode() to seek a reference to a node containing value item. find() returns null or a reference only to the nodeValue component. find() uses findNode() to seek a reference to a node containing value item. find() returns null or a reference only to the nodeValue component.

28 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting a Node The private method removeNode() erases a node from the tree by finding a replacement node somewhere else in the tree and using it as a substitute for the deleted node. The private method removeNode() erases a node from the tree by finding a replacement node somewhere else in the tree and using it as a substitute for the deleted node. Choose the node so that, when it takes the place of the deleted node, its value maintains the structure of the tree. Choose the node so that, when it takes the place of the deleted node, its value maintains the structure of the tree. Subtrees for the deleted node and the replacement node must be reconnected in such a way that the new tree maintains search tree ordering. Subtrees for the deleted node and the replacement node must be reconnected in such a way that the new tree maintains search tree ordering.

29 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting a Node (continued) Notation: Notation: dNode identifies the deleted node D. pNode, identifies the parent P of the deleted node. When pNode is null, we are deleting the root. The removeNode() method sets out to find a replacement node R with reference rNode. dNode identifies the deleted node D. pNode, identifies the parent P of the deleted node. When pNode is null, we are deleting the root. The removeNode() method sets out to find a replacement node R with reference rNode.

30 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting a Node (continued) The algorithm for finding a replacement node considers two cases that depend on the number of children attached to node D. The algorithm for finding a replacement node considers two cases that depend on the number of children attached to node D.

31 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has an Empty Subtree The other child becomes the replacement node R. If the deleted node is a leaf node, it has two null children. In this case, the other node R is null. The other child becomes the replacement node R. If the deleted node is a leaf node, it has two null children. In this case, the other node R is null.

32 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has an Empty Subtree (continued)

33 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has an Empty Subtree (continued)

34 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has an Empty Subtree (continued) The algorithm must handle two special cases, one in which the deleted node is a leaf node and the other in which the deleted node is the root. The algorithm must handle two special cases, one in which the deleted node is a leaf node and the other in which the deleted node is the root.

35 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has an Empty Subtree (continued)

36 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (one null child) private void removeNode(STNode dNode) { if (dNode == null) return; // dNode = reference to node D that is deleted // pNode = reference to parent P of node D // rNode = reference to node R that replaces D STNode pNode, rNode; // assign pNode as a reference to P pNode = dNode.parent;

37 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (one null child continued) // if D has a null child, the // replacement node is the other child if (dNode.left == null || dNode.right == null) { if (dNode.right == null) rNode = dNode.left; else rNode = dNode.right; if (rNode != null) // the parent of R is now the parent of D rNode.parent = pNode;

38 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (one null child concluded) // deleting the root node; assign new root if (pNode == null) root = rNode; // attach R to the correct branch of P else if (((Comparable )dNode.nodeValue). compareTo(pNode.nodeValue) < 0) pNode.left = rNode; else pNode.right = rNode; }... }

39 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has Two Nonnull Children A node with two children has some elements in its subtrees that are less than and some elements that are greater than its value. A node with two children has some elements in its subtrees that are less than and some elements that are greater than its value. Find a replacement node that maintains the correct ordering among the items. Rather than removing the deleted node, update its value with that of the replacement node. Splice the replacement node out of the tree. Find a replacement node that maintains the correct ordering among the items. Rather than removing the deleted node, update its value with that of the replacement node. Splice the replacement node out of the tree.

40 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has Two Nonnull Children (continued)

41 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has Two Nonnull Children (continued) Select R as the node with the smallest value that is greater than the value of the deleted node. R is the leftmost node in the right subtree of D. Select R as the node with the smallest value that is greater than the value of the deleted node. R is the leftmost node in the right subtree of D.

42 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has Two Nonnull Children (continued) Move to the right child of D and then far- left to the replacement node R, maintaining a reference, PofR, to the parent of R. Move to the right child of D and then far- left to the replacement node R, maintaining a reference, PofR, to the parent of R.

43 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleted Node has Two Nonnull Children (continued) Splice out R from the tree, by copying its value in place of the the value of D and connect its right subtree to its parent. Splice out R from the tree, by copying its value in place of the the value of D and connect its right subtree to its parent.

44 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (two nonnull children) public void removeNode(STNode dNode) { if (dNode.left == null || dNode.right == null) {... } // case where deleted node has two nonnull children else { // pOfRNode is reference to parent of // replacement node STNode pOfRNode = dNode; // first possible replacement is right // child of D; the reference to PofR, // pOfRNode, is the deleted node rNode = dNode.right; pOfRNode = dNode;

45 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (two nonnull children continued) // descend down path of left children, keeping // a record of the current node and its parent; // stop at the replacement node while(rNode.left != null) { pOfRNode = rNode; rNode = rNode.left; }... }

46 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (two nonnull children continued) Complete the process by copying the value of the replacement node to D and then connect the right subtree of R to the tree. If PofR is the deleted node, then connect the right subtree of R as the rightchild of D; otherwise connect the right child of R as the left child of PofR. Assign the parent of a nonnull right subtree of R to be pOfRNode, the parent of R. Complete the process by copying the value of the replacement node to D and then connect the right subtree of R to the tree. If PofR is the deleted node, then connect the right subtree of R as the rightchild of D; otherwise connect the right child of R as the left child of PofR. Assign the parent of a nonnull right subtree of R to be pOfRNode, the parent of R.

47 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. removeNode() (two nonnull children concluded) // copy the value in R to D dNode.nodeValue = rNode.nodeValue; if (pOfRNode == dNode) dNode.right = rNode.right; else pOfRNode.left = rNode.right; // the parent of the right child of R is the parent of R if (rNode.right != null) rNode.right.parent = pOfRNode;

48 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The method remove() // if item is in the tree, remove it // and return true; otherwise, return // false public boolean remove(Object item) { // search tree for item STNode dNode = findNode(item); if (dNode == null) return false; removeNode(dNode); treeSize--; modCount++; return true; }

49 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

50 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Additional STree Operations (continued) // returns the first (least) element in this // binary search tree public T first() { STNode nextNode = root; // if the set is empty, return null if (nextNode == null) return null; // first node is the furthest node left from root while (nextNode.left != null) nextNode = nextNode.left; return nextNode.nodeValue; }

51 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

52 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Complexity of Binary Search Tree Operations A binary search tree has best-case search time O(log 2 n), worst-case time O(n), and average time O(log 2 n). A binary search tree has best-case search time O(log 2 n), worst-case time O(n), and average time O(log 2 n).

53 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

54 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The STree Iterator (continued)

55 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. IteratorImpl Inner Class private class IteratorImpl implements Iterator { // 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 lastReturned = null; // node whose value is returned a subsequent // call to next() private STNode nextNode = null;... }

56 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. IteratorImpl Inner Class (continued) The first node is the one far-left from the root. Subsequent nodes are visited in LNR order. The first node is the one far-left from the root. Subsequent nodes are visited in LNR order.

57 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. IteratorImpl Inner Class (continued) // 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; }

58 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. STree Method iterator() // returns an iterator for the elements in the tree public Iterator iterator() { return new IteratorImpl(); }

59 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 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. 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.

60 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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. 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.

61 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 p;

62 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing next() (continued) if (nextNode.right != null) { // successor is the furthest left node of // right subtree nextNode = nextNode.right; while (nextNode.left != null) nextNode = nextNode.left; } else { // 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;

63 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing next() (concluded) 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 nextNode = p; } return lastReturned.nodeValue; }

64 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing hasNext() // returns true if the tree has more // unvisited elements public boolean hasNext() { // elements remain if nextNode is not null return nextNode != null; } The iteration is complete when nextNode becomes null. The implementation of hasNext() simply verifies that nextNode is not equal to null. The iteration is complete when nextNode becomes null. The implementation of hasNext() simply verifies that nextNode is not equal to null.


Download ppt "© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary."

Similar presentations


Ads by Google