Problème 1.
Consider the following sequence of values
Note
We can represent tree textually via the following representation
Where we use as a placeholder for a missing child for those nodes that only have a single child.
P1.1
Draw the min heap (as a tree) obtained by adding the values in in sequence. Show each step
-
. The root of the heap.
-
. Added to the left of the root.
- . Added to the right of the root.
- . Added to the left of the left child of the root. (42 < 86)
- . Added to the right of 42.
- . Added to the left of 39.
- . Added to the right of 39.
- . 20 becomes left child of 86. (20 < 86) then swap. (20 < 42) then swap.
- . 88 becomes right of 42
- . 51 becomes right of 49
- . 64 becomes right of 49
P1.2
Draw the max heap (as a tree) obtained by adding the values in in sequence. Show each step
- . The root of the heap.
- . 42 becomes the root, 3 becomes left child.
- . 39 becomes right child.
- . 86 becomes root. 42 becomes left child, 3 becomes left child of 42.
- . 49 becomes left child of 86, swap 42, 42 becomes right child of 49.
- . 89 become routes, swap 86, 49.
- . 99 becomes root, swap 89, 49.
- . 20 swap with 3, 3 becomes left child of 20.
- . 88 becomes left child of 99, swap 49, 20.
- . 51 becomes right child of 88, swap 42, 20.
- . 64, pushes 49 down.
P1.3
Draw the binary search tree obtained by adding the values in in sequence. Show each step
-
. The root of the tree.
-
. 42 becomes the right child of 3.
- . 39 becomes the left child of 42.
- . 86 becomes the right child of 42.
- . 49 becomes the left child of 86.
- . 89 becomes the right child of 86.
- . 99 becomes the right child of 89.
- . 20 becomes the left child of 39.
- . 88 becomes the right child of 86.
- . 51 becomes the right child of 49.
- . 64 becomes the left child of 51.
Problème 2.
Given an ordered list and value , the LowerBound
algorithm provide the position in list such that is the first offset in of a value larger-equal to . Hence, (or, if no such offset exists, ). The LowerBound
algorithm does so in comparisons. Argue that LowerBound
is worst-case optimal: any algorithm that finds the correct position for any inputs and using only comparisons will require comparisons.
Solution
For a list of size there are possible outcomes for the position in the list. The minimum height of a binary tree needed for outcomes is (at most leaves or
From Stirling’s approximation, comparison-based sorting algorithm lower bound is . Given that the algorithm operates in comparisons, it matches with the theoretical lower bound for the search algorithm. Therefore, no comparison-based algorithm can guarantee a better worst-case performance for position , making LowerBound
the worst-case optimal.
Problème 3.
Min heaps and max heaps allow one to efficiently store values and efficiently look up and remove the smallest values and largest values, respectively. One cannot easily remove the largest value from a min heap or the smallest value from a max heap, however.
P3.1
Assume a value is a part of a min heap of at-most values and that we know v is stored at position in that heap. Provide an algorithm that can remove from the heap in worst-case
P3.2
Provide a data structure that allows one to efficiently store values and efficiently look up and remove both the smallest and the largest values: all three of these operations should be supported in
We will implement a Double-ended Priority Queue (DEPQ), which is a min-max heap.