Data Structures Flashcards
Linked List
(def., positioning, what are they used for, how to invoke)
a collection of nodes where each node contains a data field and a reference (link) to the next node in the list
unlike normal list (arrays) elements do not have objective positions in the list, instead they have a relational postition based on surrounding nodes
used to create graphs and trees, data strucutres that require frequent addition/deletion
invoked in python by creating a Node class
Hash Table
(def, use case)
(object in js, dictionary in python)
data structure of key(index), value pairs
lookup up values by the key, not their location in the data structure
good for optimizing a problem, nested loop
Big O for Arrays (List)
lookup: O(1)
push: O(1)
pop: O(1)
insert: O(n) - iterate through
delete: O(n) - iterate through
prepend: O(n) - iterate through
Pros & Cons of Array (List)
Pros
- fast lookup
- fast push/pop
- ordered
Cons
- slow insert, delete, append at beginning
- static arrays: fixed size
Big O for Hash Table (Dictionary)
insert: O(1)
lookup: O(1) : sometimes O(n) when collision occurs
delete: O(1) : not ordered so no shifting
search: O(1)
Pros & Cons for Hash Tables
Pros
- fast access O(1), but more memory required O(n)
- fast lookups and inserts
Cons
- unordered
- collision: key has more than one value (data element)
- slows down insert & delete O(n)
- for loops, loop the entire memory space
- arrays only loop through the size of the data strucutre
- slow key insertion
Common Approach to Handling Hash Table Collision
what is the big O notation?
create a linked list for the additional value (data element)
O(n) insert and delete
Big O for Linked List
prepend: O(1)
append: O(1)
lookup: O(n)
insert: O(n)
delete: O(n)
Doubly Linked List
(definition)
pointers to the next a previous nodes
traversal forwards a backwards
Pros (4) & Cons (3) Linked List
(Single & Double)
Pros
- inserts and deletes are faster than arrays
- size is more flexible than an array (arrays double in memory once size hits a certain threshold)
- although not indexed, items are sorted unlike hash tables
- can be used to handle hash table collision
Cons
- iterating through linked list is sometimes slower than arrays becuase nodes are scattered over memory
- in single linked list, you can only traverse in one direction
- for doubly linked list, it requires more memory becuase of the two way traversal
Array (List)
(def., types of data it can hold, memory footprint)
most straight forward type of data structure
a data structure that contains a group of elements
can hold almost any type of data
smallest memory footprint
Pros (2) & Cons (3) of Doubly Linked List
Pros
- traverse forwards and backwards, unlike single linked list
- lookup faster
Cons
- delete and insert, slower than singly linked list
- requires more memory than singly linked list
- more complex to implement
Stacks & Queues
(def., ex.)
Stacks
LIFO: last item in is the only element you can access
Examples:
- undo button on phone/ms word
- function calls: inner most function (last written) first called
- browser history: backwards to the previous page
Queues
FIFO: first item in is the only elment you can access
Examples:
- uber ride: first person requesting ride is the first serviced
- printing: first request, first printed
- waiting in line: first in line, first serviced
Big O Stacks & Queues
Stack: Big O
lookup: O(n) - available although not often used
pop: O(1)
push: O(1)
peek: O(1) - view top item in the stack
Queue: Big O
lookup: O(n) - available although not often used
enqueue (similar to prepend): O(1)
dequeue (pop from the beginning): O(1)
peek: O(1) - view bottom item in queue
Trees
def. & nodes (4)
- hierarchical relational data structure
- nodes, pointers, & data
- linked list, except nodes only point to children
nodes
parent: root node
child: descends from parent
sibling: child node belonging to same parent
leaf: ending node that has no children