FinalExam 2009
FinalExam 2009
Bar Code
Good Luck!
a) Give the best data structure to store the courses and their prerequisites so that an administrator
can print a list of courses in the order of the courses that will be given, such that no course is
given before all its prerequisites are given. Store the courses given in the example above in the data
structure you selected. Note: Use one of the data structures you took in class.
b) Justify your answer by describing an efficient way to print all courses in the appropriate order.
Solution:
The courses and their prerequisites are stored in a tree, where each course is represented as a node in the
tree. If a course has prerequisites, they are represented as the children of the node. The tree needs to be
traversed bottom-up, where the leaves are visited first, and a parent is visited only when all its children
are visited. This can be achieved using the post-order traversal of the tree.
CSEN301
JJ
t
ttt JJ
JJ
tt JJ
tt J$
yt
CSEN201 MATH201
66
66
66
6
CSEN101 MATH101
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 2
a) Bubble sort
void bubbleSort(int[] a) {
for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++)
if (a[j+1] < a[j]) {
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
} } }
Solution:
Bubble sort is stable. The reason is that two elements with the same value will never be swapped,
thus the relative ordering of the elements with same value is maintained.
b) Selection sort
Solution:
Selection sort is not stable. The reason is that the smallest element is swapped with the first
unsorted element, which changes the position of the unsorted elements. This may cause elements
having the same value to lose their relative order.
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 3
c) Insertion sort
Solution:
Insertion sort is stable. The reason is that if there is a sorted element i and a non-sorted element j,
and that i and j have the same value, j will not be shifted to a position that is before i but to some
position after it.
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 4
a) Given a stack of integers, write an external method that sorts the stack in ascending order, so that
the smallest element appears on top of the stack. You can use the methods pop, top, push, and
size. Note: The initial stack should be sorted after the execution of the method. Only stacks can
be used!
Solution:
b) What is the order of complexity of this algorithm in terms of the size of the stack? Justify your
answer.
Solution:
The time complexity of the algorithm is O(n2 ), where n is the size of the stack. The outer loop is
executed for n times to sort n elements, and each loop takes n times to determine the maximum
element.
Solution:
The invariant is that after i passes, the smallest i elements are put in the bottom of the other stack,
i.e. the temp.
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 5
Exercise 4 (8 Marks)
Linked Lists
Implement an internal method public DoublyLinkedList weirdDiff() inside the class DoublyLinkedList
that returns a DoublyLinkedList having at each Link an int value equal to the difference between two
numbers such that:
• The first operand is equivalent to the summation of all numbers occurring after the link in the same
position in the initial list.
• The second operand is equivalent to the summation of all numbers occurring before the link in the
same position in the initial list.
Note that the element in the same position will not contribute to the calculated value.
The original list should remain preserved. You should use reference manipulation only.
• The first Link in the resulting DoublyLinkedList will have the value (3+2+5)-(0).
• The second Link in the resulting DoublyLinkedList will have the value (2+5)-(1).
• The last Link in the resulting list will have the value (0)-(2+3+1).
The resulting list, for the above one, will look as follows:
Solution:
curr = curr.next;
}
return res;
}
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 7
a) Let T1 and T2 be two binary search trees such that every element of T1 is less than every element
of T2. Let x be a value such that T1 < x < T2. Write a method join(T1,x,T2) that produces a
binary search tree containing the elements of T1, x, and T2. The method should be implemented
internally in the tree class, i.e. you can access the root of the tree.
Solution:
Solution:
The time complexity of the algorithm is O(1), because since T1 and T2 are both binary search trees,
the construction of the new binary search tree is just a matter of creating the new tree’s root and
performing two memory reference operations. The number of operations is constant independent
of the size of the trees.
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 8
a) Insert the following numbers into an initially empty binary search tree starting from left to right.
469 188 212 668 166 246 650 251 651 98 205 949
Solution:
b) Starting with tree from part a), delete 469. Redraw the tree.
Solution:
251 PP
iiii PPP
i ii iiii PPP
iii i PPP
ii PPP
tiiii (
188A 668 MM
qqq A AA }} MMM
qqq AA }} MMM
qqq A }} MMM
xqq A ~}} &
166 212A 650A 949
}} AA AA
}} AAA AAA
}} AA AA
~}}
98 205 246 651
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 9
nn 63 PPPPP
n nnnn PPP
nnn PPP
nn PPP
vnnn P(
42 A 76 A
}} AA }} AA
} AAA } AA
}}} A }}} AA
~}} A ~}} A
14 A 51 A 70 81 ?
{{ AA AA ??
{{ AAA AAA ??
{{ AA AA ??
} {{
1 16 54 106
Give the sequence of elements after traversing the tree in preorder traversal.
Solution:
63, 42, 14, 1, 16, 51, 54, 76, 70, 81, 106
b) Implement the preorder traversal in a nonrecursive method. Hint: Use stacks as a data structure
to store the nodes. Assume that the stack is a dynamic data structure, i.e. do not worry about the
size of the stack.
Solution:
int weightBalanceFactor();
Solution:
if(n==null)
return factor;
else {
int right = count(n.right);
int left = count(n.left);
int diff = Math.abs(left-right);
if(diff>factor)
factor = diff;
if(left>right)
return left;
else
return right;
}
}
15 8 22 1 12 19
into a hash table of size b = 7 and using the hash function h(x) = x%b.
a) Show where the numbers below end up in the hash table if we insert them in the order (left to
right).
Solution:
After applying the hash function:
x h(x)
15 1
8 1
22 1
1 1
12 5
19 5
2. Using Chaining:
Solution:
0
1 15 → 8 → 22 → 1
2
3
4
5 12 → 19
6
class HashTable {
private static int N = 11;
private int[] table = new int[N];
class Test {
public static void main(String[] args) {
HashTable h = new HashTable();
h.insert(...);
h.insert(...);
boolean f = h.find(...);
}
}
Assume that the hash function is a perfect one. Thus, searching and insertion can be done in O(1).
Solution:
Hash all values from A into a hash table. For every value b of B, check if it is already in the hash table.
If not then add it to the output. Now do the dual: hash all values from B into an empty hash table, and
process elements of A printing them whenever they are not in this table. We perform expected constant
time operations for the elements of A and B, obtaining expected linear execution time.
Implementation:
return diff;
Notice: that in the worst case, only sequential single loops are performed, thus the algorithm is insured
to have linear running time, provided that the methods insert and find run in O(1) time.
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 14
Extra Sheet
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 15
Extra Sheet
Data Structures and Algorithms, Final Exam, January 4, 2010 Page 16
Extra Sheet