DSA Reviewer: Heaps ~ is a complete binary tree where the value of each of each parent node is either higher or lower than the value of its child nodes. Two types of Heaps: • • Min Heap – The value of each parent node is less than or equal to the values of its child nodes. Max Heap – The value of each parent node is greater than or equal to the values of its child nodes. Notes: Heaps can be Java using ArrayList from the java.util package. implemented in Example: ArrayList<Integer> heap = new ArrayList<>(); addAll() method - used to create heap with initial values. Example (based on the given min heap above): ArrayList<Integer> minHeap = new ArrayList<>(); Collections.addAll(minHeap, 1, 7, 6, 8, 9); Explanation: The root node is always at index 0. Its child nodes are always at index 1 and index 2. Indices 3, 4, 5, and 6 can store the child nodes of those two (2) nodes and so on. get() method – used to determine the root node. Example: System.out.print(minHeap.get(0)); Priority Queues - is a special type of queue where elements are processed based on their order (natural or custom). Notes: Can be implemented in Java using the PriorityQueue class from the java.util package. Example: PriorityQueue<Integer> printer = new PriorityQueue<>(); add() or offer() – use to enqueue. poll() or remove() – used to dequeue. Notes: The dequeue operation starts with the minimum element. Comparator - is used to create a specific ordering for a collection of objects. Notes: To create a comparator in Java, use the Comparator interface and its methods such as comparing(), comparingInt(), and compare(). Sets - is a collection of elements where each element is unique. Common set operations: Union: The union of two (2) sets (A ∪ B) is the set that contains all the elements in either set. Example: {1, 3, 5, 7} ∪ {2, 3, 4, 5} = {1, 2, 3, 4, 5, 7} Intersection: The intersection of two (2) sets (A ∩ B) is the set that contains only the elements common to both sets. Example: {1, 3, 5, 7} ∩ {2, 3, 4, 5} = {3, 5} Difference: The difference of sets A and B (A – B) is the set that contains the elements that are in A but not in B. Example: {1, 3, 5, 7} – {2, 3, 4, 5} = {1, 7} Subset: Set A is a subset of set B (A ⊂ B) if every element of set A is also an element of set B. Example: {1, 3, 5, 7} ⊂ {1, 2, 3, 4, 5, 7} = true Three (3) general-purpose set implementations. All are included in the java.util package. HashSet – This stores its elements in a hash table without a guaranteed order upon iteration. This is the best-performing implementation. TreeSet – This stores its elements in a special type of tree where elements are sorted (natural or custom) during iteration. LinkedHashSet – This stores its elements in a hash table with a linked list running through it. The order of the elements during the iteration is the same as the order they were inserted into the set. Maps - is a set of ordered pairs where elements are known as keys (identifiers) and values (content). Three (3) general-purpose map implementations. TreeMap - is a data structure for storing and organizing objects according to key-value pairs. LinkedHashMap - is an extension of the HashMap class and it implements the Map interface. HashMap - stores the data in (Key, Value) pairs, and you can access them by an index of another type (e.g. an Integer). Notes: A map cannot contain duplicate keys. Each key can map to only one (1) value. Example: Key Values: Map of first Names K Kae M1 Mark M2 Mairo Dictionaries – known as maps in python. Note: To create a dictionary, use the colon symbol (:) for key-value pairs within the curly braces.