1/14
Looks like no tags are added yet.
1. UNION: Get everything from both SetsSet<E> result = new HashSet<>();
result.addAll(s);
result.addAll(t);
return result;2. INTERSECTION: Keeps only values that appear in both SetsSet<E> result = new HashSet<>();
result.addAll(s);
result.retainAll(t);
return result;3. MAP OF SETS: Adds a value to a Set inside a Mapif (!m.containsKey(k)) {
m.put(k, new HashSet<V>());
}
m.get(k).add(v);4. MAP OF LISTS: Adds a value to a List inside a MapList<V> lst = m.get(k);
if (lst == null) {
lst = new LinkedList<>();
m.put(k, lst);
}
lst.add(v);5. COUNT OCCURRENCES: Counts how many times each value appearsMap<E, Integer> counts = new HashMap<>();
for (E element : list) {
counts.put(element, counts.getOrDefault(element, 0) + 1);
}
return counts;6. RECURSION BASE CASE: Stops when there is no nodeif (node == null) {
return false;
}7. BINARY TREE CONTAINS: Searches both sides for a valueif (node == null) {
return false;
}
if (node.data.equals(key)) {
return true;
}
return contains(node.left, key) || contains(node.right, key);
8. BST FIND: Searches left or right based on the valueif (node == null) {
return null;
}
if (node.data.equals(key)) {
return node;
}
if (key.compareTo(node.data) < 0) {
return find(node.left, key);
}
return find(node.right, key);9. POSTORDER: Visits left, right, then rootif (root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.println(root.data);
10. INSERT LESSER: Finds the leftmost node and adds a left childNode<E> current = root;
while (current.left != null) {
current = current.left;
}
current.left = new Node<E>(e);11. ADD MINIMUM VALUE: Starts at the given node and moves leftwhile (root.left != null) {
root = root.left;
}
root.left = new Node<>(e);
12. ADD LEAST PREDECESSOR: Goes right once, then all the way leftNode<E> current = n;
current = n.right;
while (current.left != null) {
current = current.left;
}
current.left = new Node<E>(e);13. SPLICE: Removes a node with zero or one childif (n.left == null || n.right == null) {
splice(n);
} else {
Node<E> successor = n.right;
while (successor.left != null) {
successor = successor.left;
}
n.data = successor.data;
splice(successor);FIND LEAST SUCCESSOR: Goes right once, then all the way leftNode<E> successor = node.right;
while (successor.left != null) {
successor = successor.left;
}15. REMOVE NODE WITH TWO CHILDREN: Replaces node with its successorNode<E> successor = node.right;
while (successor.left != null) {
successor = successor.left;
}
node.data = successor.data;