|
| 1 | +# Data Structures |
| 2 | + |
| 3 | +## What is a data structure? |
| 4 | + |
| 5 | +A data structure is a particular way of organizing data in a computer so that it can be used efficiently. |
| 6 | + |
| 7 | +When a problem becomes very complicated, it becomes useful to have some structure to store data in memory. |
| 8 | + |
| 9 | +## Array |
| 10 | + |
| 11 | +A collection of elements, each identitifed by an array index or key |
| 12 | + |
| 13 | +### Example |
| 14 | + |
| 15 | +```java |
| 16 | +int[] array = new int[5]; |
| 17 | + |
| 18 | +int array = {2, 3, 5, 7, 11}; |
| 19 | + |
| 20 | +System.out.print(array[2]); // output is '5' |
| 21 | + |
| 22 | +array[2] = 13; // changes the '5' to '13' |
| 23 | +``` |
| 24 | + |
| 25 | +## Linked Lists |
| 26 | + |
| 27 | +A list can hold an arbitrary number of items, and can easily change size to add or remove items. |
| 28 | + |
| 29 | +Each element in the list is called a *node*, and contains two pieces of information: |
| 30 | +- **data** - information that the node is holding |
| 31 | +- **nextNode** - a reference to the node that follows the current one |
| 32 | + |
| 33 | +An example class for the **nodes** would look like the following: |
| 34 | + |
| 35 | +```java |
| 36 | +class Node { |
| 37 | + String data; |
| 38 | + Node nextNode; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### Example |
| 43 | + |
| 44 | +The following code shows how to create a basic linked list (no data) |
| 45 | + |
| 46 | +```java |
| 47 | +Node start = new Node(); |
| 48 | + |
| 49 | +for (int i = 1; i <= 10; i++) { |
| 50 | + start.nextNode = new Node(); |
| 51 | + start = start.nextNode; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +The following code shows how to iterate over a linked list |
| 56 | + |
| 57 | +```java |
| 58 | +Node start = ... |
| 59 | + |
| 60 | +while (start != null) { |
| 61 | + // do something with data from the node |
| 62 | + start = start.nextNode; |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Lists |
| 67 | + |
| 68 | +If you don't want to worry about creating your own data structure to utilize a list with an unbounded size, use *ArrayLists*. |
| 69 | + |
| 70 | +Very similar to an array, but does not need initial size (resizes itself). |
| 71 | + |
| 72 | +Primitive types (*int, double, char*, etc.) must be changed to their **object** counterpart (*Integer, Double, Character*, etc.). |
| 73 | + |
| 74 | +### Example |
| 75 | + |
| 76 | +```java |
| 77 | +ArrayList<Integer> list = new ArrayList<Integer>(); |
| 78 | +list.add(2); |
| 79 | +list.add(3); |
| 80 | +System.out.print(list.get(1)); // outputs '3' |
| 81 | +list.set(0, 5); // changes '2' to '5' |
| 82 | +``` |
| 83 | + |
| 84 | +## Sets |
| 85 | + |
| 86 | +A list of elements that **does not contain duplicates**. |
| 87 | + |
| 88 | +Does not preserve the ordering of the elements as you add them, and doesn't have a `get(x)` method like *ArrayList*. |
| 89 | + |
| 90 | +Usually sets are used just to iterate over once you add and remove elements, and can check for uniqure elements in one pass. |
| 91 | + |
| 92 | +`Set` is an interface in Java, so you will need to instantiate it using the `HashSet` class. |
| 93 | + |
| 94 | +### Example |
| 95 | + |
| 96 | +```java |
| 97 | +Set<Integer> set = new HashSet<Integer>(); |
| 98 | +set.add(7); // {7} |
| 99 | +set.add(3); // {7, 3} |
| 100 | +set.add(3); // {7, 3} |
| 101 | +set.add(4); // {7, 3, 4} |
| 102 | +``` |
| 103 | + |
| 104 | +The following is how you can iterate over each element in a set: |
| 105 | + |
| 106 | +```java |
| 107 | +for (Integer i : set) { |
| 108 | + // do something with the element |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Queue |
| 113 | + |
| 114 | +Imagine a movie queue where the first one in line is the first one out. In data structures, a queue operates the same one: the first element pushed into the queue is the first element popped out. |
| 115 | + |
| 116 | +In Java, `Queue` is an interface, so it cannot be instantiated; instead, you need to instantiate a `LinkedList` object (which already implements `Queue`)] |
| 117 | + |
| 118 | +A queue has three main operations: |
| 119 | +- **Push** - add an element to the queue |
| 120 | +- **Pop** - remove the first element of the queue |
| 121 | +- **Peek** - see what the first element of the queue is without removing it |
| 122 | + |
| 123 | +```java |
| 124 | +Queue<Integer> qyeye = new LinkedList<Integer>(); |
| 125 | +queue.add(2); // {2} |
| 126 | +queue.add(4); // {2. 4} |
| 127 | +queue.add(8); // {2, 4, 8} |
| 128 | +System.out.print(queue.peek()); // output is '2' |
| 129 | +queue.remove(); // {4, 8} |
| 130 | +``` |
| 131 | + |
| 132 | +## Priority Queue |
| 133 | + |
| 134 | +First in, first out (FIFO) like a queue, but stores element in a *heap* (specialized tree-based structure). |
| 135 | + |
| 136 | +As elements are pushed to the `PriorityQueue`, they are add to the heap, which sorts the elements based on a pre-defined priority. |
| 137 | + |
| 138 | +### Example |
| 139 | + |
| 140 | +```java |
| 141 | +PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); |
| 142 | +pq.add(6); // {6} |
| 143 | +pq.add(3); // {3, 6}, '3' has a higher priority than '6' |
| 144 | +pq.add(10); // {3, 6, 10} |
| 145 | +pq.remove(); // removes '3' |
| 146 | +``` |
| 147 | + |
| 148 | +## Stack |
| 149 | + |
| 150 | +Imagine a stack of plates, the last plate that you put on the stack is the first plate that you take off. |
| 151 | + |
| 152 | +A stack has the same operations as a queue: push, pop, peek. |
| 153 | + |
| 154 | +### Example |
| 155 | + |
| 156 | +```java |
| 157 | +Stack<Integer> stack = new Stack<Integer>(); |
| 158 | +stack.push(2); // {2} |
| 159 | +stack.push(3); // {2, 3} |
| 160 | +stack.push(5); // {2, 3, 5} |
| 161 | +System.out.print(stack.peek()); // output is '5' |
| 162 | +stack.pop(); // {2, 3} |
| 163 | +``` |
| 164 | + |
| 165 | +## Map |
| 166 | + |
| 167 | +An object that maps keys to values; a map cannot contain duplicate keys. |
| 168 | + |
| 169 | +The keys of the map are sent through a hash function, which tells us which bucket to place the corresponding value in. |
| 170 | + |
| 171 | +We can map any `Object` to any other `Object`: |
| 172 | +- number --> color (`int` to `String`) |
| 173 | +- phone number --> address (`String` to `String`) |
| 174 | +- name --> friends (`String` to `ArrayList<String>`) |
| 175 | + |
| 176 | +Like `Queue`, `Map` is an interface, so it cannot be instantiated; instead, you need to instantiate a `HashMap` object (which already implements `Map`). |
| 177 | + |
| 178 | +### Example |
| 179 | + |
| 180 | +```java |
| 181 | +Map<Integer, String> map = new HashMap<Integer, String>(); |
| 182 | +map.put(2, "blue"); |
| 183 | +map.put(5, "red"); |
| 184 | +System.out.print(map.get(2)); // output is 'blue' |
| 185 | +System.out.print(map.get(4)); // output is null |
| 186 | +``` |
| 187 | + |
| 188 | +## TreeMap |
| 189 | + |
| 190 | +Same functionality as a map, but the map is sorted according to the natural ordering of its keys. |
| 191 | + |
| 192 | +### Example |
| 193 | + |
| 194 | +```java |
| 195 | +TreeMap<Integer, String> map = new TreeMap<Integer, String>(); |
| 196 | +map.put(17, "red"); |
| 197 | +map.put(28, "blue"); |
| 198 | +map.put(14, "green"); |
| 199 | +for (Integer key : map.keySet()) { |
| 200 | + System.out.print(key); // output is '14, 17, 28' |
| 201 | +} |
| 202 | +``` |
0 commit comments