Data Structures and Algorithms
Data Structures and Algorithms
(5 points) Check the ADT or data structure that is most appropriate for each of
the following problems, for each answer give a short explanation of your choice.
(a) You want to build an address book with entries in alphabetical order by last
name.
Unsorted List
Sorted List
Stack
Balanced Search Tree
Directed Graph
(b) You want to build a meeting reminder for a PDA that keeps track of events
you schedule and periodically checks the next event to sound an alarm to
remind you of the next thing you need to do.
List
Hashtable
Stack
Queue
Priority Queue
(c) You want to build a table of contents for a textbook. The textbook consists
of chapters, chapters consist of sections, and sections consist of subsections.
List
Hashtable
Tree
Binary Tree
Balanced Tree
(d) You want to build an email address miner that scans a hard drive looking for
email addresses, then sends them to a remote host.
List
Hashtable
HashSet
Array
Balanced Tree
(6 points) List the sequence of nodes visited by preorder, inorder, and postorder
traversals of the following tree:
Given the following heap, draw the heap that would result after deleting
the minimum element.
(5 points) Say the following tree was obtained by inserting the element 42 into an
AVL tree. The tree no longer satisfies the AVL invariant, but the invariant can be
reestablished by performing two rotate operations. Show the resulting tree after
this is done.
Suppose we use mergesort to sort an array containing the following sequence of elements:
1, 5, 6, 3, 2, 4, 9, 0. For this example input, illustrate how merge sort works by drawing the
array states that result after each merge.
Suppose we have a hash table with an table size of 10, into which we are inserting the
integers from 1 to 10. Our hash function h(x) is the square of the element, modulo the table
size (i.e., the remainder when the square is divided by the table size. For example, h(9) = 1.)
Draw the hash table that results, assuming it uses chaining.
Problem B1
A 2-coloring of an undirected graph with n vertices and m edges is the assignment of one of
two colors (say, black or white) to each vertex of the graph, so that no two adjacent nodes
have the same color. So, if there is an edge (u; v) in the graph, either node u is black and v is
white or vice versa. Give (pseudocode!) an O(n+m) time algorithm to 2-color a graph or
determine that no such coloring exists, and justify the running time. For this problem you
are not allowed to assume the existence of any “black-box” algorithms, i.e. give pseudocode
for everything you use.
The following shows examples of graphs that are and aren't 2-colorable:
Problem B2
In this problem you will design a linear-time algorithm that clones a binary tree T . This
algorithm should construct an exact copy of T . You are only allowed to use the methods of
the Tree and Binary Tree ADTs (i.e. isEmpty, root, parent, children,
isInternal, isExternal, isRoot, size, elements, positions,
swapElements, replaceElement, leftChild, rightChild, sibling,
expandExternal, removeAboveExternal).
1. Describe (in pseudo-code) your algorithm.
2. Analyze its worst-case running time.
Problem B3
Let S1, S2, . . ., Sk be k different sequences whose elements have integer keys in the range
[0; N − 1], for some N > 1. Describe an algorithm (in pseudocode) to sort all of the sequences
in O(k + n + N) time, where n is the total size of all of the sequences. Note that the
sequences should remain separate, so your algorithm should rearrange the elements in
each sequence into the sorted order, not output one big sorted sequence containing all of
the elements.