exam prep Flashcards

(39 cards)

1
Q

data structure: linked list

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

data structure: array

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

data structure: stack

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

data structure: hash table

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

insert: O(n)
- avg: O(1)

remove: O(n)
- avg O(1)

search: O(n)
- avg O(1)

access: N/A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

data structure: binary search tree

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

insert: O(n)

remove: O(n)

search: O(n)

access: O(n)

(averages are O(logn) for all)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

data structure: avl tree

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

O(logn) for everything

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

data structure: 2-3 tree / b-tree

list the worst case time complexity for:
insertion, removal, search, access/peak

(what about averages?)

A

O(logn) for everything

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what does a compression map do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To minimize collisions, patterns in the keys should NOT…

A

result in patterns in the hash values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

there are two parts of a hash function…

A
  1. hash code
    - maps key to an integer (if it isn’t one already)
  2. compression map
    - maps hash code to fit in the range of our table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

2 properties of a good hash function:

A
  • minimizes collisions
  • fast to compute
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

the load factor of a hash table is:

A

number of entries (n)

—– divided by —–

size of array (N)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

collision resolution

what’s the first step?

then what?

A
  1. 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
  2. implement an effective collision strategy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are the 2 collision strategies that we learned?

A
  1. 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 )

  1. separate chaining
  • linked list that corresponds to each index in the table

this strategy allows for load factors > 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

in order to achieve the favourable performance a hash table can have, a hash table needs what 3 properties?

A

a hash table requires:

  • a good hash function,
  • a good collision resolution strategy, and
  • a reasonable load factor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

in a hash table ordered traversal takes _________ time, although this is rarely used for hash tables

A

O(n logn)

  1. traverse entire table and copy keys into separate array - O(n)
  2. sort - O(n logn)
17
Q

what is a leaf or terminal node?

A

a node with no children

18
Q

sibling nodes all have the same _______ node

19
Q

In a post-order traversal, all ______ nodes are acted on before their _______ node

A

child, parent

20
Q

A pre-order traversal is also called a ___________ search

21
Q

A _____ binary tree is one in which every row has the maximum number of nodes

A

perfect (i think)

22
Q

a perfect binary tree of height h contains _________ nodes

(what’s the formula)

A

2^(h+1) − 1

23
Q

how do you access the parent of a node in a binary heap?

24
Q

how do you access the left and right child of a node in a binary heap?

A

left: k * 2 + 1
right: k * 2 + 2

25
T or F: an empty tree is still technically a tree
True
26
The running time of getMedianValue() in a full BST
O(1)
27
After inserting the following values, in order, into a BST, this will be the height of tree. [10,8,20,2,9,30,22,27]
4
28
This node is *promoted* when 8 is deleted. 10 8 20 2 9 30
9
29
The new root after deleting 10 10 8 20 2 9 30
20
30
for sweaty tryhards: To increase the likelihood of a balanced BST, one could preprocess an array of values using this O(nlog(n)) operation/algorithm to allow for more strategic insertions
merge sort processing before middle-outward inserts
31
This operation is performed when a node with 2 keys receives a new key, causing a promotion of a key up to its parent
a (node) split
32
what does this node method do? boolean ___?_____() { return children[0] == null; }
check's if it's a leaf node
33
A structure that consists of nodes (or vertices) connected by edges, where each edge links a pair of nodes.
a graph or tree
34
The node at the top of a tree data structure
root
35
A single node, or a structure consisting of a root node with at most two children, where each child is itself the root of this kind of structure
binary tree
36
Given a BST with n nodes, what is the worst-case running time for the getHeight() method? (asymptotic notation)
O(n)
37
The addition of these references can help reduce stack overflows when searching/inserting into particularly deep trees
parent node references
38
Although it maintains an O(h) search/insert runtime, when comparing full tree types, this tree grows quickly in height compared to others, resulting in a slightly less efficient runtime
bst
39
what is the time complexity for calling retrieveMax() on a heap?
O(logn) O(1) to get value. then O(logn) to adjust tree