public void put(long key, T t) { int index = getIndex(key); Node<T> top = nodes[index]; if (top == null) { Node newNode = new Node(); newNode.key = key; newNode.data = t; nodes[index] = newNode; return; } if (top.key == key) { // replace top top.data = t; return; } // traverse until null and add it while (top.next != null) { top = top.next; if (top.key == key) { top.data = t; return; } } Node newNode = new Node(); newNode.key = key; newNode.data = t; top.next = newNode; }
private Node deleteKey(Node root, int key) { if (null == root) { return root; } if (key < root.key) { root.left = deleteKey(root.left, key); } else if (key > root.key) { root.right = deleteKey(root.right, key); } else { // This is the node to be deleted, as it has its // key equal to the root // If the node has only 1 child, return the other present chlid. // If the node has no children, null will be returned, which will destory the relation // of this node with its parent, thereby deleting it from the tree if (null == root.left) { return root.right; } else if (null == root.right) { return root.left; } // A child with 2 elements // Find the inorder successor of this node, (which is the minimum value in the right // sub-tree), set this node to the min value, and delete the inorder successor root.key = minValue(root.right); root.right = deleteKey(root.right, root.key); } return root; }
public void add(int key) { if (elemCount == 0) { root = new Node(key, Color.BLACK); root.left = new Node(-1, null, null, root, Color.BLACK); numOfLeaves++; root.right = new Node(-1, null, null, root, Color.BLACK); numOfLeaves++; } else if (lookup(key)) { System.out.println("Cannot accept duplicate values"); } else { do { if (key <= root.key) { root = root.left; } else { root = root.right; } } while (root.key != -1); root.key = key; root.color = Color.RED; root.left = new Node(-1, null, null, root, Color.BLACK); root.right = new Node(-1, null, null, root, Color.BLACK); numOfLeaves++; fixTree(root); } elemCount++; resetRoot(); }
/** * Deletes the node specified by the parameter toDelete. parent specifies the parent of the node * to be deleted. */ private void deleteNode(Node toDelete, Node parent) { if (toDelete.left != null && toDelete.right != null) { // Case 3: toDelete has two children. // Find a replacement for the item we're deleting -- as well as // the replacement's parent. // We use the smallest item in toDelete's right subtree as // the replacement. Node replaceParent = toDelete; Node replace = toDelete.right; while (replace.left != null) { replaceParent = replace; replace = replace.left; } // Replace toDelete's key and data with those of the // replacement item. toDelete.key = replace.key; toDelete.data = replace.data; // Recursively delete the replacement item's old node. // It has at most one child, so we don't have to // worry about infinite recursion. deleteNode(replace, replaceParent); } else { // Cases 1 and 2: toDelete has 0 or 1 child Node toDeleteChild; if (toDelete.left != null) toDeleteChild = toDelete.left; else toDeleteChild = toDelete.right; // null if it has no children if (toDelete == root) root = toDeleteChild; else if (toDelete.key < parent.key) parent.left = toDeleteChild; else parent.right = toDeleteChild; } }
// delete the key-value pair with the given key rooted at h private Node delete(Node h, Key key) { // assert get(h, key) != null; if (key.compareTo(h.key) < 0) { if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h); h.left = delete(h.left, key); } else { if (isRed(h.left)) h = rotateRight(h); if (key.compareTo(h.key) == 0 && (h.right == null)) return null; if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h); if (key.compareTo(h.key) == 0) { Node x = min(h.right); h.key = x.key; h.val = x.val; // h.val = get(h.right, min(h.right).key); // h.key = min(h.right).key; h.right = deleteMin(h.right); } else h.right = delete(h.right, key); } return balance(h); }
// auxiliary method for move private Node remove(Node n, Integer k) { if (n == null) return n; int cmp = k.compareTo(n.key); if (cmp < 0) n.left = remove(n.left, k); else if (cmp > 0) n.right = remove(n.right, k); else { if (n.left == null) { n = n.right; return n; } else if (n.right == null) { n = n.left; return n; } else { Node replace = minV(n.right); n.key = replace.key; n.value = replace.value; n.right = remove(n.right, n.key); return n; } } return balance(n); }