FinalExam 2006
FinalExam 2006
Bar Code
Good Luck!
Exercise 1 (6 Marks)
a) True or False? Justify your answer. Searching for a value x in a binary search tree is always faster
than searching for the same value in a binary tree.
Solution:
False: A binary search tree could be a linear tree with search in O(n) which is the same as search in
a binary tree.
b) Give one reason why it is helpful for a programmer to think in terms of Abstract Data Types (ADT).
Solution:
It is better to think in terms of ADT because this will eliminate the overhead of how a certain Data
structure is implemented, we do not care about its implementation, but instead we only care about
the Interface that we are going to use.
c) A List ADT can be implemented using either an array or a linked-list. Give one advantage for each
implementation.
Solution:
An advantage of using a linked-list is that, unlike an array, it is not restricted to a size that cannot
be changed after its creation, which means that a link-list is more useful when the size of the data
is not known at the time of creation of the data structure.
An advantage of using an array is having a random access time of O(1), which is useful when
frequent random access to individual elements using an index is needed.
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 2
a) Write an algorithm that prints the index i at which the numbers switch from 0’s to 1’s, i.e. A[i] = 0
and A[i + 1] = 1. In the example above, it should print "5", as A[5] = 0 and A[6] = 1.
Solution:
Solution:
O(n)
c) If the time complexity of your algorithm is O(n) or worse, describe in one short sentence a modifi-
cation to your algorithm that will perform in better that O(n).
Solution:
We can use the concept of binary search so that we check the middle of the array, if it is a zero and
the next value is 1 we print the current index, if it is a zero and the next value is zero too then we
binary search the right half of the array, otherwise we binary search the left half.
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 3
• 1 2 + 3 - is legal.
• * 5 4 is not legal.
• 6 7 - 3 / + is not legal.
Solution:
Exercise 4 (6 Marks)
while (!q.isEmpty()) {
for (int i = 2; i > q.peek(); i--)
q.enqueue(q.dequeue());
System.out.println(q.dequeue());
}
Solution:
0
0
0
Solution:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 5
a) Why is a Binary Search Tree called „Binary“? Why is it convenient for searching?
Solution:
A tree where each node has no more than 2 children is called binary. The search tree is convenient
because at each node you can select which of the two branches to search and completely eliminate
the other - the tree is organized so that one side is always greater, the other less, than the current
node.
If the tree is full and balanced, then you effectively halve the search area with each step, eliminating
one or the other subtree.
b) Given the following inputs sequence.
63 76 81 106 42 51 14 54 16 1 70
1. Construct the corresponding binary search tree by inserting the elements of the sequence
starting from left to right.
Solution:
n 63 PPPP
nnnnn PPP
n nn PPP
nn n PPP
vnnn PP(
42 A 76 A
}} AA }} AA
}} AA }} AA
}} AAA }} AA
~}} ~}} A
14 A 51 A 70 81 ?
{{ AA AA ??
{{ AAA AAA ??
{{ AA AA ??
}{{
1 16 54 106
nn 70 RRRRR
n nnnn RRR
RRR
nnnn RRR
nn RR(
vnn
42 A 76 A
}} AA AA
}} AA AA
}} AAA AA
~ }} A
14 A 51 A 81 ?
{{ AA AA ??
{{ AAA AAA ??
{{ AA AA ??
}{{
1 16 54 106
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 6
class Node
{
int item;
Node next;
}
class LinkedList{
public LinkedList()
{
first = null;
}
}
a) Write a method void organize(int i) that will modify this list, with the elements starting at
index i being split in half. The first half should be placed at the beginning of the list and the second
half at the end of the list. For example, given the list [1, 2, 3, 4, 5, 6, 7] and i = 3, the list
should be modified to [4, 5, 1, 2, 3, 6, 7].
Do not assume the existence of any methods.
Solution:
b) Re-implement the method organize given that you now supposed to return a new linked list with
the required organization and that you are allowed to use the methods insertFirst, insertLast,
removeFirst, and removeLast of the class LinkedList.
Solution:
{
size++;
temp.insertLast(l.deleteFirst().item);
}
for(int j = 0; j < i; j++)
l.insertLast(temp.deleteFirst().item);
LinkedList t = new LinkedList();
for(int j = 0; j < (size - i) / 2; j++)
t.insertLast(temp.deleteFirst().item);
for(int j = 0; j < (size - i) / 2; j++)
l.insertFirst(t.deleteLast().item);
while(!temp.isEmpty())
l.insertLast(temp.deleteFirst().item);
return l;
}
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 8
a) What is the worst case time complexity of searching for a book in a Quaternary Search Tree?
Solution:
O(n)
b) What is the worst case time complexity of searching for a book in a Quaternary Search Tree with an
ideal topology, which is defined similarly as the ideal topology of binary search trees presented in
class?
Solution:
O(log n)
c) Assuming that the root of the tree is at level 0 and its children are at level 1, what is the maximum
number of books that can be stored at level 3?
Solution:
64
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 9
Exercise 8 (8 Marks)
a) For a binary search tree (BST) with n items, what is the worse-case running time to find the
maximum difference between any two elements? Justify your answer.
Does the running time depend on whether the binary search tree is balanced? Say why or why not.
Solution:
To find the maximum difference, sufficient to find the minimum element in the BST, the maximum
element in the BST and compute their difference.
Running time depends on the worst-case depth of the minimum and maximum elements in the
BST.
Worst case running time for an unbalanced BST is O(n)
Worst case running time for a balanced BST is O(log(n))
b) Add an instance method to the BinarySearchTree class that swaps all nodes to the right of the
root with all nodes to the left of the root. Does the resulting tree satifies a similar property like the
binary search tree presented in the lecture.
Solution:
Exercise 9 (8 Marks)
Write a static recursive method
that takes two tree references as inputs and determines whether they have the same tree shape and
pointer structure.
Solution:
Exercise 10 (8 Marks)
In an attempt to describe a family tree we would like to modify the tree implementation presented in
class in order to make nodes represent persons. Each person should be allowed to have an arbitrary (not
predefined) number of children and also have a spouse (another person to which the person is married).
a) What are the instance variables needed to implement the family tree described above?
Solution:
We need two more instance variable:
• A reference to the spouse
• A reference to an array (or some other data structure) of children
class Node
{
Node spouse;
Node [] children;
}
b) Implement a boolean method that checks if every node in the tree and its spouse have the same
children.
Solution:
c) Implement a boolean method that checks if two given nodes are siblings, i.e. have the same parent.
Solution:
return null;
}
a) What is the strategy that you will use for resolving collisions? Explain the strategy in few words.
Solution:
Linear Probing
b) Insert the keys in the given order in the hash table below.
Solution:
0 444
1 6042
2 174
3 442
4 880
5 104
6 221
7 253
8
9
10 316
11 281
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 14
Extra Sheet
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 15
Extra Sheet
Data Structures and Algorithms, Final Exam, Winter 2006/2007 Page 16
Extra Sheet