Introduction to Algorithms, 2020-1
Week 11: Exercises

1. Two-Stack Queue

We have seen how to implement a queue using a linked list or a circular array. Another possible implemention will use two stacks, one for elements at the head of the queue and one for elements at the tail. Impelement a two-stack queue in Python and investigate its performance.

2. Tree Values

  1. Write a function that prints out all values in a binary search tree in order, separated by spaces.

  2. Modify your function to print out the tree values in reverse order.

3. Rebalancing a Tree

Write a function that takes a binary tree and constructs a second tree that contains all the value from the first tree and is as balanced as possible.

4. Non-Recursive Tree Sum

Write a function that computes the sum of all values in a binary tree without using recursion.

5. Tree Check

Write a function that takes a binary tree and returns True if the tree satisfies the ordering requirements of a binary search tree.

6. Complete Tree

Write a function that takes a binary tree and returns True if the tree is complete.

7. Biggest Gap

Write a function that returns the greatest difference between any two consecutive values in a binary search tree.

8. Heap Check

Write a function that takes a binary tree and returns True if the tree satisfies the ordering requirements of a binary heap.

9. Heapify Running Time

Prove that we can convert an unordered array of N integers into a binary heap in time O(N) in the worst case. You may assume that N = 2k – 1 for some integer k.

10. Adjacency Matrix to Adjacency List

Write a function that takes a undirected graph in adjacency matrix representation, and returns the same graph in adjacency list representation. Assume that the graph's vertices are numbered from 0 to (N – 1).

11. Adjacency List to Adjacency Matrix

Write a function that takes a undirected graph in adjacency list representation, and returns the same graph in adjacency matrix representation. Assume that the graph's vertices are numbered from 0 to (N – 1).