Homework Assignment #2

Homework 2

due March 25 at 11:59 pm to eCampus.

1. (15 points) Describe in pseudo code how to implement the stack ADT using two queues.

(a) Write a C++ function that implements your solution. You can use the C++ STL queue container.

(b) What is the running time of the push and pop functions in this case?

2. (15 points) Write a recursive function in C++ that counts the number of nodes in a singly linked list.

(a) Test your function using different singly linked lists. Include the code and screenshots with testing

cases.

(b) Write a recurrence relation that represents your algorithm.

(c) Solve the relation using the iterating or a recursive tree method to obtain the running time of the

algorithm in Big-O notation.

3. (15 points) Write a C++ recursive function that finds the maximum value in an array of integers

without using any loops.

(a) Test your function using different input arrays. Include the code and screenshots with testing

cases.

(b) Write a recurrence relation that represents your algorithm. Solve the relation and obtain the

running time of the algorithm in Big-O notation.

4. (15 points) What is the best, worst and average running time of quick sort algorithm? Provide arrangement of the input and the selection of the pivot point at every case. Provide a recursive relation

and solution for each case.

5. (10 points) What is the best, worst and average running time of merge sort algorithm? Use two

methods for solving a recurrence relation for the average case to justify your answer.

6. (10 points) R-10.17 p. 493

For the following statements about red-black trees, provide a justification for each true statement and

a counterexample for each false one.

(a) A subtree of a red-black tree is itself a red-black tree.

(b) The sibling of an external node is either external or it is red.

(c) There is a unique (2,4) tree associated with a given red-black tree.

(d) There is a unique red-black tree associated with a given (2,4) tree.

7. (10 points) R-10.19 p. 493

Consider a tree T storing 100,000 entries. What is the worst-case height of T in the following cases?

2

(a) T is an AVL tree.

(b) T is a (2,4) tree.

(c) T is a red-black tree.

(d) T is a binary search tree.

8. (10 points) R-9.16 p. 418

Draw an example skip list that results from performing the following series of operations on the skip

list shown in Figure 9.12: erase(38), insert(48,x), insert(24,y), erase(55). Record your coin

flips, as well.

3