// Textbook fragment 10.03 // Realization of a dictionary by means of a binary search tree public class BinarySearchTree extends LinkedBinaryTree> implements Dictionary { protected Comparator C; // comparator protected Position> actionPos; // insert node or removed node's parent protected int numEntries = 0; // number of entries /** Creates a BinarySearchTree with a default comparator. */ public BinarySearchTree() { C = new DefaultComparator(); addRoot(null); } public BinarySearchTree(Comparator c) { C = c; addRoot(null); } /** Nested class for location-aware binary search tree entries */ protected static class BSTEntry implements Entry { protected K key; protected V value; protected Position> pos; BSTEntry() { /* default constructor */ } BSTEntry(K k, V v, Position> p) { key = k; value = v; pos = p; } public K getKey() { return key; } public V getValue() { return value; } public Position> position() { return pos; } } /** Extracts the key of the entry at a given node of the tree. */ protected K key(Position> position) { return position.element().getKey(); } /** Extracts the value of the entry at a given node of the tree. */ protected V value(Position> position) { return position.element().getValue(); } /** Extracts the entry at a given node of the tree. */ protected Entry entry(Position> position) { return position.element(); } /** Replaces an entry with a new entry (and reset the entry's location) */ protected void replaceEntry(Position > pos, Entry ent) { ((BSTEntry) ent).pos = pos; replace(pos, ent); }