Hansung Flashcards

1
Q

What is a heap?

A

A binary tree with two constraints
- It is complete
- Each child has a value “greater than or equal to” it sparent

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

How do you add to a heap?

A
  • add the element to the next available space in the tree
  • Percolate the value up the tree to maintain the correct ordering
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What data structures use heaps?

A

Priority Queues

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

How does removeMin work on priority queues?

A
  • Pop the root
  • Replace it with the last element in the heap
  • Percolate this element down to the bottom of the heap choosing the minimum child
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do we navigate a heap?

A
  • The root of the tree is at array location 0
  • The last element is size()-1
  • The parent of a node k is at floor((k-1)/2)
  • The children of node k are at array locations 2k + 1 and 2k + 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the time complexity of perlocating in a heap?

A

Ɵ(1)

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

What is the time complexity of the height of the tree?

A

Ɵ(log(n))

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

What is the time complexity of add/removeMin on a heap?

A

Ɵ(log(n)) in worst case
Ɵ(1) normally

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

What is the worst case time complexity of Heap sort?

A

O(n log(n))

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

What is the space complexity of heap sort?

A

O(1)

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

What is a B-tree?

A

Balanced multiway trees made for fast search, finding successors/predecessors, insert, delete, maximum, minimum

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

What are M-way trees?

A

Trees where each non-leaf node has M children

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

What is the access time of a M-way tree?

A

Logm(n) = Log2(n)/Log2(m)

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

What makes B+ trees different to B-Trees

A
  • Only leaf nodes contain data pointers, while internal nodes contain keys only
  • Sequential access is possible like a linked list
  • Searching is faster
  • Insertion and Deletion are easier and faster
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What makes B-Trees different to B+ Trees?

A
  • All internal nodes and leaf nodes contain data pointers along with keys
  • Sequential access of nodes is not possible
  • Searching is slower
  • Insertion and Deletion take more time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What values should you use for M in B+ trees?

A

Odd numbers

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

What is a Trie?

A

A multiway tree often used for storing large sets of words

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

How do tries work?

A
  • They are trees with a possible branch for every letter of an alphabet
  • All words end with a special letter “$”
  • They compactify the edges in the tree in order to remove: internal and external one-way branching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the uses of Tries?

A
  • Way of implementing sets
  • Provide quick insertion, deletion and find
  • Good for spell checkers, completion, algorithm
20
Q

What are the disadvantages of Tries?

A
  • Waste large amounts of memory
21
Q

How are Binary Tries different from Normal Tries?

A

They speeds up finds but loses the advantage of multiway trees by reducing the depth

22
Q

Why use Tries?

A
  • Classic memory, time complexity trade-off
  • Specialist
  • Not too complicated to implement
23
Q

What is the time complexity of a quick-find find?

24
Q

What is the time complexity of a quick-union find?

25
Q

What is the time complexity of a quick find union?

26
Q

What is the time complexity of a quick-union union?

27
Q

What is the time complexity of a Sequential (Linear) Search in each case?

A

Worse Case: Θ(n)
Best Case: Θ(1)
Average Case: Θ(n)

28
Q

What is the time complexity of a binary search?

A

O(log2(n)).

29
Q

What is the time complexity of an Insertion Sort?

A

Average Case:Θ(n^2).
Best Case: Θ(n)

30
Q

What are the properties of Binary Search?

A
  • Query the middle items
  • Worst case number of queries = depth of the tree
  • Balanced trees have O(log2(n)) levles
31
Q

What are the properties of insertion sorts?

A
  • It is stable (We only swap the ordering of two elements if one is strictly less than the other)
  • It is in-place
32
Q

What are the properties of Selection Sort?

A
  • It is in-place
  • It is not stable
  • Always requires Θ(n^2) comparisons.
33
Q

What is the time complexity of Selection Sort?

34
Q

What are the properties of bubble sort?

A
  • In-place
  • Stable
  • Space complexity of O(1)
35
Q

What is the time complexity of bubble sort?

36
Q

What is a loop invariant?

A
  • A property of a loop that helps us understand why an algorithm is correct
37
Q

What is a decision tree?

A

A way to visualise many algorithms

38
Q

What does each case time complexity of a decision tree tell us?

A

Worst case: Depth of the deepest leaf
Best case: Depth of shallowest leaf
Average case: Average depth of leaves

39
Q

What are the properties of Merge Sort?

A
  • Stable
  • Not in-place
  • Merging sub-arrays is quick
  • Worst case time complexity: O(nlog(n))
  • Asymptotically optimal
40
Q

How could you improve merge sort?

A

If sub-arrays fall below a certain threshold, we switch to insertion sort

41
Q

What is the time complexity of quicksort?

A
  • Partitioning an array is Θ(n)
  • Worst case: O(n^2)
  • On average O(nlog(n))
  • In practice, much faster 39% more comparisons than merge sort closer to O(n)
42
Q

How does Knuth/Fisher Yates Shuffle work?

A
  • Loop backwards through the array
  • At every iteration, generate a random index in the unshuffled part of the array
  • Exchange the item under that index with the the current item
  • Time complexity of Θ(n), is in-place
43
Q

What are the benefits of Multi-pivoting?

A

It improves performance as there is fewer cache misses

44
Q

What are the properties of Counting Sort?

A
  • No comparisons
  • Stable
  • Not in-place
  • Time Complexity: Θ(n + k)
  • Space Complexity: Θ(n + k)
  • Of nis much larger than k, it is linear time sorting algorithm
45
Q

What are the properties of Radix Sort?

A
  • Θ(n)
  • Stable
  • Not in-place