Rev 94 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
import java.util.ArrayList;// Generic BST implementationpublic class BSTree<K extends Comparable<? super K>, E> implements Dictionary<K, E> {private BSTreeNode<K,E> root; // Root of the BST// Constructorpublic BSTree() {this.root = null;}// Inserts a node into the BSTpublic void insert(K key, E element) {// Set the BST root as the returned nodethis.root = insertRecursive(root, key, element);}// Removes a node from the BST given the keypublic void remove(K key) {// Adds invalid coordinates if none are specifiedthis.remove(key, -1, -1);}public void remove(K key, int x, int y) {// Check if node existsE temp = findRecursive(root, key);if (temp != null) {// Remove the nodethis.root = removeRecursive(root, key, x, y);}}// Returns a node from the BST given the keypublic E find(K key) {return findRecursive(root, key);}// Returns a list of all nodes with the given keypublic ArrayList<E> findAll(K key) {ArrayList<E> elementList = new ArrayList<E>();findAllRecursive(root, key, elementList);return elementList;}// Removes all nodes from the BSTpublic void clear() {this.root = null;}// Recursively inserts a node into the BSTprivate BSTreeNode<K,E> insertRecursive(BSTreeNode<K,E> root, K key, E element) {if (root == null)// If there are no nodes in the BST, create and return a new nodereturn new BSTreeNode<K,E>(key, element);if (root.getKey().compareTo(key) > 0)// If passed key is smaller, insert into left childroot.setLeft(insertRecursive(root.getLeft(), key, element));else// If passed key is greater, insert into right childroot.setRight(insertRecursive(root.getRight(), key, element));return root;}// Recursively searches minimal nodes for matches to the given keyprivate E findRecursive(BSTreeNode<K,E> root, K key) {if (root == null)return null;if (root.getKey().compareTo(key) > 0)// If passed key is smaller, search the left childreturn findRecursive(root.getLeft(), key);else if (root.getKey().compareTo(key) == 0)// If key is found, return with the found elementreturn root.getElement();else// If key is not found, search the right childreturn findRecursive(root.getRight(), key);}// Recursively searches all nodes for matches to the given keyprivate void findAllRecursive(BSTreeNode<K,E> root, K key, ArrayList<E> elementList) {if (root == null)return;// If key matches, save to listif (root.getKey().compareTo(key) == 0)elementList.add(root.getElement());// Recursively call search on all possible nodesif (root.getKey().compareTo(key) > 0)findAllRecursive(root.getLeft(), key, elementList);elsefindAllRecursive(root.getRight(), key, elementList);}// Recursively removes a node from the BST given a keyprivate BSTreeNode<K,E> removeRecursive(BSTreeNode<K,E> root, K key, int x, int y) {if (root == null)return null;// Find the node to removeif (root.getKey().compareTo(key) > 0)root.setLeft(removeRecursive(root.getLeft(), key, x, y));else if (root.getKey().compareTo(key) < 0)root.setRight(removeRecursive(root.getRight(), key, x, y));// Node's key matcheselse {// If we want to narrow it down by coordinates as wellif (x >= 0 && y >= 0) {// If coordinates do not match, keep searching from the right childif (((Record)root.getElement()).getX() != x || ((Record)root.getElement()).getY() != y) {root.setRight(removeRecursive(root.getRight(), key, x, y));} else {// If one of the leaves is null, just return the other leafif (root.getLeft() == null)return root.getRight();else if (root.getRight() == null)return root.getLeft();else {// Otherwise create a new node with the smallest value on the right leafBSTreeNode<K,E> temp = getMin(root.getRight());root.setElement(temp.getElement());root.setKey(temp.getKey());root.setRight(deleteMin(root.getRight()));}}// Otherwise just remove the node} else {// If one of the leaves is null, just return the other leafif (root.getLeft() == null)return root.getRight();else if (root.getRight() == null)return root.getLeft();else {// Otherwise create a new node with the smallest value on the right leafBSTreeNode<K,E> temp = getMin(root.getRight());root.setElement(temp.getElement());root.setKey(temp.getKey());root.setRight(deleteMin(root.getRight()));}}}return root;}// Recursively returns the minimum key nodeprivate BSTreeNode<K,E> getMin(BSTreeNode<K,E> root) {if (root.getLeft() == null)return root;elsereturn getMin(root.getLeft());}// Recursively deletes the minimum key nodeprivate BSTreeNode<K,E> deleteMin(BSTreeNode<K,E> root) {if (root.getLeft() == null)return root.getRight();else {root.setLeft(deleteMin(root.getLeft()));return root;}}}