exam prep Flashcards
(39 cards)
data structure: linked list
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
insert: O(1)
- O(n) if sorted
remove: O(n) at position
- O(1) if removing front (maybe a queue)
search: O(n)
access: O(n)
(averages are the same)
data structure: array
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
insert: depends
- O(1) if unsorted (and know the index where inserting)
- O(n) if unsorted. if resizing
remove: O(n)
search: O(n)
- O(logn) if sorted
access: O(1)
(averages are all the same)
data structure: stack
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
insert: O(1)
remove: O(1)
search: O(n) (but not really applicable)
access: O(n)
- O(1) from the top
(averages are all the same)
data structure: hash table
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
insert: O(n)
- avg: O(1)
remove: O(n)
- avg O(1)
search: O(n)
- avg O(1)
access: N/A
data structure: binary search tree
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
insert: O(n)
remove: O(n)
search: O(n)
access: O(n)
(averages are O(logn) for all)
data structure: avl tree
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
O(logn) for everything
data structure: 2-3 tree / b-tree
list the worst case time complexity for:
insertion, removal, search, access/peak
(what about averages?)
O(logn) for everything
what does a compression map do?
a compression map takes a hash code and maps it into a specific range (to fit in your table)
- using modulo is a simple form of a compression map
To minimize collisions, patterns in the keys should NOT…
result in patterns in the hash values
there are two parts of a hash function…
- hash code
- maps key to an integer (if it isn’t one already) - compression map
- maps hash code to fit in the range of our table
2 properties of a good hash function:
- minimizes collisions
- fast to compute
the load factor of a hash table is:
number of entries (n)
—– divided by —–
size of array (N)
collision resolution
what’s the first step?
then what?
- minimize collisions by creating a good hash function
- the polynomial hash map is good at turning words into uniformly distributed integers
- To break up patterns, use an array whose length is prime - implement an effective collision strategy
what are the 2 collision strategies that we learned?
- open addressing
- simple search pattern:
- if h(k) already occupied, check the next spot
- treat array as circular if necessary
- variant search pattern:
- check h(k) + d, h(k) + 2d, h(k) + 3d, . . . for some step size d that is relatively prime to N
(two numbers are relatively prime if they share no common divisors other than 1)
acceptable for low load factors ( < 0.75 )
- separate chaining
- linked list that corresponds to each index in the table
this strategy allows for load factors > 1
in order to achieve the favourable performance a hash table can have, a hash table needs what 3 properties?
a hash table requires:
- a good hash function,
- a good collision resolution strategy, and
- a reasonable load factor
in a hash table ordered traversal takes _________ time, although this is rarely used for hash tables
O(n logn)
- traverse entire table and copy keys into separate array - O(n)
- sort - O(n logn)
what is a leaf or terminal node?
a node with no children
sibling nodes all have the same _______ node
parent
In a post-order traversal, all ______ nodes are acted on before their _______ node
child, parent
A pre-order traversal is also called a ___________ search
depth-first
A _____ binary tree is one in which every row has the maximum number of nodes
perfect (i think)
a perfect binary tree of height h contains _________ nodes
(what’s the formula)
2^(h+1) − 1
how do you access the parent of a node in a binary heap?
(k-1) / 2
how do you access the left and right child of a node in a binary heap?
left: k * 2 + 1
right: k * 2 + 2