private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val, 1); int cmp = key.compareTo(x.key); if (cmp < 0) x.left = put(x.left, key, val); else if (cmp > 0) x.right = put(x.right, key, val); else x.val = val; x.N = 1 + size(x.left) + size(x.right); return x; }
private Node put(Node x, String key, Value val, int d) { if (x == null) x = new Node(); if (d == key.length()) { x.val = val; return x; } char c = key.charAt(d); x.next[c] = put(x.next[c], key, val, d + 1); return x; }
public void put(Key key, Value val) { for (Node tmp = first; tmp != null; tmp = tmp.next) { if (tmp.key.equals(key)) { tmp.val = val; return; } } N++; first = new Node(key, val, first); }
private static Node createNode(int[] arr, int start, int end) { if (start > end) { return null; } int mid = (start + end) / 2; Node n = new dot43().new Node(); n.val = arr[mid]; n.left = createNode(arr, start, mid - 1); n.right = createNode(arr, mid + 1, end); return n; }
private Node delete(Node x, String key, int d) { if (x == null) return null; if (d == key.length()) x.val = null; else { char c = key.charAt(d); x.next[c] = delete(x.next[c], key, d + 1); } if (x.val != null) return x; for (int c = 0; c < R; c++) if (x.next[c] != null) return x; return null; }
public int canCompleteCircuit(int[] gas, int[] cost) { if (null == gas || null == cost || 0 == gas.length || 0 == cost.length || gas.length != cost.length) return -1; int val = 0; for (int idx = 0; idx < gas.length; ++idx) { val += gas[idx] - cost[idx]; } if (val < 0) return -1; Node node = new Node(0, gas[0] - cost[0]), p; node.next = node; int v1; for (int idx = 1; idx < gas.length; ++idx) { v1 = gas[idx] - cost[idx]; if (node.val >= 0 && node.val + v1 >= 0 || node.val < 0 && v1 < 0) { node.val += v1; } else { p = new Node(idx, v1); p.next = node.next; node.next = p; node = p; } } p = node; while (node.next != node) { if (node.val >= 0 && node.val + node.next.val >= 0 || node.val < 0 && node.next.val < 0) { node.val += node.next.val; node.next = node.next.next; } } return node.idx; }
/** * Inserts the key-value pair into the symbol table, overwriting the old value with the new value * if the key is already in the symbol table. If the value is <tt>null</tt>, this effectively * deletes the key from the symbol table. * * @param key the key * @param val the value */ public void put(Key key, Value val) { if (val == null) { delete(key); return; } for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); N++; }
/** * Inserts the specified key-value pair into the symbol table, overwriting the old value with the * new value if the symbol table already contains the specified key. Deletes the specified key * (and its associated value) from this symbol table if the specified value is <tt>null</tt>. * * @param key the key * @param val the value * @throws NullPointerException if <tt>key</tt> is <tt>null</tt> */ public void put(Key key, Value val) { if (key == null) throw new NullPointerException("first argument to put() is null"); if (val == null) { delete(key); return; } for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); N++; }
// insert the key-value pair in the subtree rooted at h private Node put(Node h, Key key, Value val) { if (h == null) return new Node(key, val, RED, 1); int cmp = key.compareTo(h.key); if (cmp < 0) h.left = put(h.left, key, val); else if (cmp > 0) h.right = put(h.right, key, val); else h.val = val; // fix-up any right-leaning links if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h); h.N = size(h.left) + size(h.right) + 1; return h; }
// 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); }
public void deleteNode(Node node) { node.val = node.next.val; node.next = node.next.next; }