/** * Removes the node with the value comp. * * @param comp Comparable being removed */ public void remove(Comparable comp) { splay(comp); if (comp.compareTo(rootNode.getValue()) != 0) // Node not found return; if (rootNode.getLeft() == null) rootNode = rootNode.getRight(); else { Node node = rootNode.getRight(); rootNode = rootNode.getLeft(); splay(comp); rootNode.setRight(node); } size--; }
/** * Inserts a node into the SplayTree, then splays around that node. * * @param comp Comparable value of new node being added */ public void insert(Comparable comp) { Node node = new Node(comp); if (rootNode == null && size == 0) { rootNode = node; return; } splay(comp); int temp = comp.compareTo(rootNode.getValue()); if (temp == 0) // Double checks for duplicates return; if (temp < 0) { node.setLeft(rootNode.getLeft()); node.setRight(rootNode); rootNode.setLeft(null); } else { node.setRight(rootNode.getRight()); node.setLeft(rootNode); rootNode.setRight(null); } rootNode = node; size++; }
/** * Gets the smallest item in the tree. * * @return Smallest item in the tree */ public Comparable getMin() { Node node = rootNode; if (node == null) return null; while (node.getLeft() != null) node = node.getLeft(); splay(node.getValue()); return node.getValue(); }
/** * Finds the item with the value comp. * * @param comp Comparable being searched for * @return item if found, null otherwise */ public Comparable find(Comparable comp) { if (rootNode == null) // Empty return null; splay(comp); if (rootNode.getValue().compareTo(comp) != 0) // Not found return null; return rootNode.getValue(); }