Sunteți pe pagina 1din 1

Data Structure and Algorithm . . SETS AND MAPS .

Maps
**Created and Prepared by John Mark Padilla October 22, 2019: 5:00AM Sets • Set of orderedpairs where elements are known as keys(identifiers) & values(content)
• A collection of elements where each element is unique. Common set operations: o A map cannot contain duplicate keys.
. HEAPS AND PRIORITY QUEUES . o Union: The union of two (2) sets (A ∪ B) is the set that contains all the elements in o Each key can map to only one (1) value.
Heaps either set. Example: {1, 3, 5, 7} ∪ {2, 3, 4, 5} = {1, 2, 3, 4, 5, 7} Example (A map of first names):
• A complete binary tree where the value of each of each parent node is either higher o Intersection: The intersection of two (2) sets (A ∩ B) is the set that contains only
or lower than the value of its child nodes. the elements common to both sets. Example: {1, 3, 5, 7} ∩ {2, 3, 4, 5} = {3, 5}
• These are the two (2) types of heap: o Difference: The difference of sets A and B (A – B) is the set that contains the
o Min Heap – The value of each parent node is < / = to the values of its child nodes elements that are in A but not in B. Example: {1, 3, 5, 7} – { 2, 3, 4, 5} = {1, 7}
o Max Heap – The value of each parent node is > / = to the values of its child nodes o 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 • Same with sets, there are also three (3) general-purpose map implementations in
• Java has (3) general-purpose set implementations & all are included in the java.util Java: HashMap, TreeMap, and LinkedHashMap.
o HashSet – This stores its elements in a hash table without a guaranteed order upon • Sample codes for maps in Java:
iteration. This is the best-performing implementation. 1. To create an empty map:
o TreeSet – This stores its elements in a special type of tree where elements are sorted Map <String, String> nameMap = new HashMap<>();
(natural or custom) during iteration. 2. To insert a mapping:
• Can be implemented in Java using ArrayList from the java.util package. o LinkedHashSet – This stores its elements in a hash table with a linked list running nameMap.put("M1", "Mark");
Example: ArrayList<Integer> heap = new ArrayList<>(); through it. The order of the elements during the iteration is the same as the order nameMap.put("M2", "Mairo");
• To create a heap with initial values, use the addAll() method of the Collections class they were inserted into the set. 3. To delete the mapping for a key:
Example: ArrayList<Integer> minHeap = new ArrayList<>(); • Sample codes for sets in Java: nameMap.remove("M2");
Collections.addAll(minHeap, 1, 7, 6, 8, 9); 1. To create an empty set: 4. To replace a key’s value:
Explanation: The root node is always at index 0. Its child nodes are always at index 1 and nameMap.replace("M2", "Marco");
Set a = new HashSet();
index 2. Indices 3, 4, 5, and 6 can store the child nodes of those two (2) nodes and so on. 5. To retrieve the value based on the key:
Set b = new TreeSet ();
• To determine the root node, use the get() method. Set c = new LinkedHashSet(); System.out.println(nameMap.get("M1"));
Example: System.out.print(minHeap.get(0)); 2. To add items to the set: 6. To check whether a map contains a mapping for the specified key or value:
Priority Queues Collections.addAll(a, "Mark", "Nika", "Mairo", "Kae"); System.out.println(nameMap.containsKey("M2"));
• A special type of queue where elements are processed based on order (nat or cus) 3. To determine the union, intersection, and difference: System.out.println(nameMap.containsValue("Mark"));
• Can be implemented in Java using the PriorityQueue class from the java.util package 7. To retrieve all the keys and values of a map:
Set a = new HashSet();
Example: PriorityQueue<Integer> printer = new PriorityQueue<>(); System.out.println(nameMap.keySet());
Set b = new TreeSet ();
• To enqueue, use add() or offer(). System.out.println(nameMap.values());
Collections.addAll(a, “Mark”, “Nika”, “Mairo”, “Kae”);
8. To display entries in separate lines:
• To dequeue, use poll() or remove(). operation starts with the minimum element. Collections.addAll(b, “John”, “Marco”, “Mark”);
for (Map.Entry e : nameMap.entrySet()) {
• Sample code to dequeue in Java based on natural order: Set union = new HashSet(a);
Set inter = new HashSet(a); System.out.println(e.getKey() + ": " + e.getValue()); }
PriorityQueue<Integer> printer = new PriorityQueue<>();
Set diff = new HashSet(a); //Map.Entry is an interface to retrieve entries
printer.add(9);
union.addAll(b); • Maps in Python are known as dictionaries.
printer.add(7);
inter.retainAll(b); • To create a dictionary, use the colon (:) for key-value pairs within the curly braces
printer.add(1);
printer.add(3); diff.removeAll(b); • Sample codes for dictionaries in Python:
while (!printer.isEmpty()) { System.out.println(“Union: “ + union); 1. To create an empty dictionary:
System.out.print(printer.remove() + " "); } System.out.println(“Intersection: “ + inter); name_map = { }
Output: 1 3 7 9 System.out.println(“Difference: “ + diff); 2. To initialize a dictionary:
• Sample code to dequeue in Python based on natural order: Output: name_map = {"M1": "Mark", "M2": "Mairo"}
from queue import PriorityQueue Union: [Marco, Nika, Mairo, John, Mark, Kae] 3. To insert a mapping:
prio = PriorityQueue() Intersection: [Mark] name_map["N"] = "Nika"
prio.put("dog") Difference: [Nika, Mairo, Kae] 4. To delete the mapping for a key:
prio.put("d") 4. To determine whether a set is a subset of another set: del name_map["M2"]
prio.put("dogs") System.out.println(a.containsAll(b)); 5. To replace a key’s value:
while not prio.empty(): • Curly braces or the set() function can be used to implement sets in Python. name_map["M2"] = "Marco"
next_item = prio.get() • Sample codes for sets in Python: 6. To retrieve the value based on the key:
print(next_item) 1. To create an empty set: print(name_map["M1"])
7. To check whether a dictionary contains a mapping for the specified key:
• A comparator is used to create a specific ordering for a collection of objects. a = set()
print("M2" in name_map)
• To create a comparator in Java, use the Comparator interface and its methods such 2. To initialize a set:
a = set(["Mark", "Nika", "Mairo", "Kae"]) 8. To retrieve all the keys and values of a dictionary:
as comparing(), comparingInt(), and compare().
print(list(name_map))
• Sample code to dequeue in Java based on a custom order: b = {"John", "Marco", "Mark"}
3. To determine the union, intersection, and difference print(list(name_map.values()))
Comparator<String> comp = Comparator.comparing(String::length);
a = set(["Mark", "Nika", "Mairo", "Kae"]) 9. To display entries in separate lines:
//:: indicates a method reference, ContainingType::methodName
b = set(["John", "Marco", "Mark"]) for x, y in name_map.items():
PriorityQueue<String> prio = new PriorityQueue<>(comp);
print(“Union: “ + str(a | b)) print(x, y)
prio.add("cat");
prio.add("c"); print(“Intersection: “ + str(a & b))
prio.add("cats"); print(“Difference: “ + str(a - b))
while (!prio.isEmpty()) { 4. To determine whether a set is a subset of another set:
System.out.println(prio.remove()); } print(a.issubset(b))

S-ar putea să vă placă și