Data Structures Flashcards
Linked List
Points to first [and last] linked nodes. If a Doubly Linked List, it can iterate in either direction.
Pros:
- insert/remove from start/end/middle—if you have a node reference—is always O(1)
Cons:
- arrays iterate faster (though not much different in JS since they’re hash tables)
- random read/writes are O(n) whereas arrays are O(1)
Node
Simple object that contains data and a reference to the next Node (or null if there isn’t one).
Hash Table
Arrays & Objects in JS are Hash Tables, so our desire is only to understand their pros/cons:
Pros:
Cons:
Queue
FIFO.
Operations: Add to the end of a list and extract the first from the list (like a line) w/ O(1) performance. LinkedList are great for this (whereas arrays have shift as O(n))
Stack
LIFO.
Operations: Add to end of list and extract the last from list w/ O(1) performance. JS Arrays are great for this.
Deque
Same as Queue but can add/remove from either direction.
Operations: Add to start/end of list and extract from start/end of list w/ O(1) performance. LinkedLists are great for this (whereas arrays have shift/unshift as O(n))
Trees & Balanced Trees
Unlike linear data structures like Array or LinkedList, Trees allow you to find a value in less steps than iterating a linear data structure. Balanced trees ensure that the max number of steps stays balanced across the tree.
Pros:
- faster searching
Cons:
- more complex to implement
- removing is no trivial matter, and can be slower than linked lists
Graphs
Graphs are a set of vertices (points on the graph; singular word is vertex) and edges (connections between vertices). A path on a graph is a chain of vertices connected by edges, equivalent to a route on a map.
By traversing the graph, we can determine the path.
BFS—breadth first search—visits neighbors of each vertex before moving on to the next vertex. This is done using a queue containing the next vertex.