Codes to remember CICS 210 Data Structures

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

15 Terms

1
New cards
1. UNION: Get everything from both Sets


Set<E> result = new HashSet<>();
result.addAll(s);
result.addAll(t);
return result;


2
New cards
2. INTERSECTION: Keeps only values that appear in both Sets


Set<E> result = new HashSet<>();
result.addAll(s);
result.retainAll(t);
return result;


3
New cards
3. MAP OF SETS: Adds a value to a Set inside a Map


if (!m.containsKey(k)) {
    m.put(k, new HashSet<V>());
}
m.get(k).add(v);


4
New cards
4. MAP OF LISTS: Adds a value to a List inside a Map


List<V> lst = m.get(k);
if (lst == null) {
    lst = new LinkedList<>();
    m.put(k, lst);
}
lst.add(v);


5
New cards
5. COUNT OCCURRENCES: Counts how many times each value appears


Map<E, Integer> counts = new HashMap<>();
for (E element : list) {
    counts.put(element, counts.getOrDefault(element, 0) + 1);
}
return counts;


6
New cards
6. RECURSION BASE CASE: Stops when there is no node


if (node == null) {
    return false;
}


7
New cards
7. BINARY TREE CONTAINS: Searches both sides for a value


if (node == null) {
    return false;
}
if (node.data.equals(key)) {
    return true;
}
return contains(node.left, key) || contains(node.right, key);


8
New cards
8. BST FIND: Searches left or right based on the value


if (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
New cards
9. POSTORDER: Visits left, right, then root


if (root == null) {
    return;
}
postOrder(root.left);
postOrder(root.right);
System.out.println(root.data);


10
New cards
10. INSERT LESSER: Finds the leftmost node and adds a left child


Node<E> current = root;
while (current.left != null) {
    current = current.left;
}
current.left = new Node<E>(e);


11
New cards
11. ADD MINIMUM VALUE: Starts at the given node and moves left


while (root.left != null) {
    root = root.left;
}
root.left = new Node<>(e);


12
New cards
12. ADD LEAST PREDECESSOR: Goes right once, then all the way left


Node<E> current = n;
current = n.right;
while (current.left != null) {
    current = current.left;
}
current.left = new Node<E>(e);


13
New cards
13. SPLICE: Removes a node with zero or one child


if (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);


14
New cards
FIND LEAST SUCCESSOR: Goes right once, then all the way left


Node<E> successor = node.right;
while (successor.left != null) {
    successor = successor.left;
}


15
New cards
15. REMOVE NODE WITH TWO CHILDREN: Replaces node with its successor


Node<E> successor = node.right;
while (successor.left != null) {
    successor = successor.left;
}
node.data = successor.data;