






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







public class BinarySrchTree implements Bag, Sorted, FindMin { protected final Comparator test; protected BSTNode root = new BSTSentinel(); protected int count = 0; public BinarySrchTree(Comparator t) { test = t; } public BinarySrchTree() { test = DefaultComparator; } // Implementation of the Collection interface. public boolean isEmpty() { return root.isEmpty(); } public int size() { return count; } public Enumeration elements() { return new InorderTreeTraversal(root); } // Implementation of the Bag interface. ... // Implementation of the FindMin interface. ... // Private, inner classes. private class BSTNode extends BinaryNode() { ... } private class BSTSentinel extends BSTNode() { ... } }
public class BinarySrchTree implements Bag, Sorted, FindMin { ... // Implementation of the Bag interface. ... public boolean containsElement(Object val) { return root.contains(val); } public boolean findElement(Object val) { return root.find(val); } public synchronized void addElement(Object val) { root = root.add(val); count++; } public synchronized void removeElement(Object val) { root = root.remove(val); count--; } // Implementation of the FindMin interface. ... public Object getFirst() { return root.getFirst(); } public void removeFirst() { root = root.removeFirst(); count--; } }
public class BinarySrchTree implements Bag, Sorted, FindMin { ... public boolean isEmpty() { return root.isEmpty(); } ... private class BSTNode extends BinaryNode { ... public boolean isEmpty() { return false; } ... } private class BSTSentinel extends BSTNode { ... public boolean isEmpty() { return true; } ... } }
// Binary search tree. public Object getFirst() { return root.getFirst(); } public void removeFirst() { root = root.removeFirst(); count--; } ...
// BSTNode. public Object getFirst() { // “First” object is always if (leftBST().isEmpty()) return obj; // at the leftmost node. return leftBST().getFirst(); } public BSTNode removeFirst() { if (leftBST().isEmpty()) return rightBST(); left = leftBST().removeFirst(); return this; } ...
// BSTSentinel: these methods only called when the tree is empty. public Object getFirst() { throw new NoSuchElementException(); } public BSTNode removeFirst() { throw new NoSuchElementException(); }
// Binary search tree. public boolean containsElement(Object val) { return root.contains(val); } ...
// BSTNode. public boolean contains(Object val) { int result = test.compare(val, obj); if (result == 0) return true; if (result < 0) return leftBST().contains(val); return rghtBST().contains(val); } ...
// BSTSentinel. public boolean contains(Object val) { return false; }
// Binary search tree. public Object findElement(Object val) { return root.find(val); } ...
// BSTNode. public Object find(Object val) { int result = test.compare(val, obj); if (result == 0) return obj; if (result < 0) return leftBST().contains(val); return rghtBST().contains(val); } ...
// BSTSentinel. public Object find(Object val) { return null; }
// Binary search tree. public synchronized void addElement(Object val) { root = root.add(val); count++; } ...
// BSTNode. public BSTNode add(Object val) { // Find position (sentinel) to insert. if (test.compare(val, obj) < 0) left = leftBST().add(val); else rght = rghtBST().add(val); return this; } ...
// BSTSentinel. public BSTNode add(Object val) { // Reached sentinel node. BSTNode node = new BSTNode(val); // Create new BSTNode. node.left = node.rght = this; // Both children are sentinels. return node; }
// Binary search tree. public synchronized void removeElement(Object val) { root = root.remove(val); count--; }
// BSTNode. public BSTNode remove(Object val) { int result = test.compare(val, obj); if (result == 0) { // Found element. if (rghtBST().isEmpty()) return leftBST(); obj = rghtBST().getFirst(); rght = rghtBST().removeFirst(); } else if (result < 0) left = leftBST().remove(val); else rght = rghtBST().remove(val); return this; }
// BSTSentinel. public BSTNode remove(Object val) // Should not have { throw new NoSuchElementException(); } // reached sentinel.
Replace with: rghtBST().getFirst()