AlgExerciseSet4
AlgExerciseSet4
These exercises are for your own benefit. Feel free to collaborate and share your answers
with other students. There are many problems on this set, solve as many as you can and ask
for help if you get stuck for too long. Problems marked * are more difficult but also more
fun :).
2 (Exercise 6.1-4 in the book) Where in a max-heap might the smallest element reside, assuming
that all elements are distinct?
3 (half a *, Problem 6-1 in the book) We can build a heap by repeatedly calling Max-Heap-Insert
to insert the elements into the heap. Consider the following variation on the Build-Max-Heap
procedure:
Build-Max-Heap’(A)
1. A.heap-size = 1
2. for i = 2 to A.length
3. Max-Heap-Insert(A, A[i])
3b Show that in the worst case, Build-Max-Heap’ requires Θ(n lg n) time to build an n-
element heap (in comparison to Build-Max-Heap that we saw in class only needs time
Θ(n)).
4 (*, Exercise 6.5-9 in the book) Give an O(n lg k)-time algorithm to merge k sorted arrays into
one sorted array, where n is the total number of elements in all the input arrays.
Hint: Use a min-heap of size k for k-way merging.
Page 1 (of 7)
Build-Max-Heap(A,6)
1
2 3
4 5 7
Page 2 (of 7)
Design and analyze an efficient algorithm for the above problem. Ideally your algorithm should
run in time O(k log k) but the worse running time of O(min{k log n, k 2 }) is also acceptable.
7 (Exercise 10.1-2 in the book) Explain how to implement two stacks in one array A[1 . . . n] in such
a way that neither stack overflows unless the total number of elements in both stacks together
is n. The Push and Pop operations should run in O(1) time.
8a Show how to implement a stack by a singly linked list (the operations Push and Pop
should still take O(1) time).
8b Show how to implement a queue by a singly linked list (the operations Enqueue and
Dequeue should still take O(1) time).
Page 3 (of 7)
L.head A B B A /
L.head O L A /
Design and analyze an algorithm that, given a pointer to the head of a single-linked list
which represents a word, outputs YES if the word is a palindrome and NO otherwise.
For full score your algorithm should run in linear time and should not use any other
data structures than single-linked lists, i.e., no arrays, stacks, queues, etc..
10 (*, Exercise 10.2-7 in the book) Give a Θ(n)-time nonrecursive procedure that reverses a singly
linked list of n elements. The procedure should use no more than constant storage beyond that
needed for the list itself.
12 What is the maximum and minimum height of a binary search tree of n elements? Which tree
is better? Motivate your answers.
13 20
Draw the resulting trees obtained after executing each of the following operations (each operation
is executed starting from the tree T above, i.e., they are not executed in a sequence):
Page 4 (of 7)
14 (Exercise 12.1-3)
Give a nonrecursive algorithm that performs an inorder tree walk.
Hint: An easy solution uses a stack as an auxiliary data structure. A more complicated, but
elegant, solution uses no stack but assumes that we can test two pointers for equality.
15 (Exercise 12.3-3) We can sort a given set of n numbers by first building a binary search tree
containing these numbers (using Tree-Insert repeatedly to insert the numbers one by one)
and then printing the numbers by an inorder tree walk. What are the worst-case and best-case
running times for this sorting algorithm?
1. there exists an integer j, where 0 ≤ j ≤ min(p, q), such that ai = bi for all i = 0, 1, ..., j − 1
and aj < bj , or
For example, if a and b are bit strings, then 10100 < 10110 by rule 1 (letting j = 3) and
10100 < 101000 by rule 2. This ordering is similar to that used in English-language dictionaries.
The radix tree data structure shown in Figure 1 stores the bit strings 1011, 10, 011, 100, and
0. When searching for a key a = a0 a1 ...ap , we go left at a node of depth i if ai = 0 and right if
ai = 1. Let S be a set of distinct bit strings whose lengths sum to n. Show how to use a radix
tree to sort S lexicographically in Θ(n) time. For the example in Figure 1, the output of the sort
should be the sequence 0, 011, 10, 100, 1011.
Page 5 (of 7)
Neuchâtel NE
NW Fribourg
Yverdon SE
Lausanne
SW
Each node v in the quadtree will store the name (v.name) and the coordinates (v.x and v.y)
of the cities. In addition, the node v will contain a pointer v.p to its parent (or NIL if it’s the
root) and pointers v.N W, v.N E, v.SE, v.SW to its four children. Similarly as with binary search
trees, the key properties that make quadtrees useful are the following:
• if u is in the quadtree rooted by v.N W then u.x < v.x and u.y ≥ v.y;
• if u is in the quadtree rooted by v.N E then u.x ≥ v.x and u.y > v.y;
• if u is in the quadtree rooted by v.SE then u.x > v.x and u.y ≤ v.y;
• if u is in the quadtree rooted by v.SW then u.x ≤ v.x and u.y < v.y.
Figure 3 shows a possible quadtree representation of the cities in Figure 2. The root is
the node corresponding to Fribourg that has x and y coordinates equal to 578461 and 183802,
respectively.
Fribourg
(578461,183802)
NW NE SE SW
Yverdon
(539316,181385)
Page 6 (of 7)
Search(Q.root, x, y) — prints the name of the city located at (x, y) if such a city exists
in the quadtree rooted at Q.root.
PrintSouthMost(Q.root) — prints the name of the southmost city (the city with the
smallest y coordinate) in the quadtree rooted at Q.root.
Your runtime analyses should be tight for the typical case when the height of the quadtree is
logarithmic in the number of cities that it contains. In addition, for full score, the running times
of your implementations should not be unnecessarily large.
Page 7 (of 7)