159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1603
159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1603
159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1603
201
ALB
Internal
MASSEY UNIVERSITY
ALBANY CAMPUS
EXAMINATION FOR
159.201 Algorithms and Data Structures
- Page 1 of 11 -
1603/159.201
ALB
Internal
This examination is Total Recovery: Students may not remove any part of this
question paper from the examination room.
struct Node {
int data;
Node *next;
};
Node *A;
If the deletion is completed, the function should return true. If the linked-list has less than N
elements, then the function should return false.
Make sure that your code covers all cases: when the list is empty, when the list only has one
element, or two elements etc.
[3 marks]
b) Write a function that searches for an item in a linked-list. If the search finds the item, then it
should return true. If the search does not find the item, then the function should return false.
The function's prototype should be:
bool SearchItem(Node * listpointer, int item);
[3 marks]
c) Draw a diagram of the linked-list mylist, produced after the following calls to the functions
AddNode_type1() and AddNode_type2() seen in the following code snippets:
...
- Page 2 of 11 -
1603/159.201
ALB
Internal
- Page 3 of 11 -
1603/159.201
ALB
Internal
Consider the contents of the Stack after carrying out all the statements above. Draw a diagram of
the Stack at the end of the main( ).
[2 marks]
- Page 4 of 11 -
1603/159.201
ALB
Internal
d) The following main() creates a queue and uses some of the methods above.
main() {
Queue Q;
Q = new Queue;
Q.Join(1);
Q.Join(2);
Q.Leave();
Q.Join(3);
Q.Front();
Q.Join(4);
Q.Leave();
Q.Join(5);
Q.Join(6);
Q.Leave();
if(Q.isEmpty()) Q.Join(7);
}
Consider the contents of the nodes after carrying out all the statements above. Draw a diagram of
the linked-list of the queue.
[2 marks]
- Page 5 of 11 -
1603/159.201
ALB
Internal
4. Consider the following binary tree (a BST) for questions a), b) and c)
81
64 90
32 72 85 93
a) Draw the threads for the tree above considering that they are for speeding up the in-order
traversals.
[1 marks]
b) Redraw the diagram of the BST above after deleting the node with key=90.
(copy the diagram in the blue answer book)
[1 marks]
c) Redraw the diagram of the BST above after inserting a node with key=47.
(copy the diagram in the blue answer book)
[1 marks]
d) Given the Tree class declaration written below, write the C++ method to search a node in a
BST. Your code can be either recursive or non-recursive. If the key is found, the method should
return true, if it fails to find that key value, than it should return false.
class Tree {
private:
int key;
Tree *leftptr, *rightptr;
public:
Tree(int newkey, Tree* L, Tree* R);
~Tree() { }
int RootData() { return key;}
Tree* Left() { return leftptr;}
Tree* Right() { return rightptr;}
//write the code for the method below
bool Search(int item);
};
[3 marks]
- Page 6 of 11 -
1603/159.201
ALB
Internal
50
30 20
15 10 5
0 1 2 3 4 5 6 7 8
b) For the Heap in the figure above draw the state of the Heap after inserting a node with
key = 64. Draw also the state of the array. (copy the diagrams in the blue answer book).
[2 marks]
c) For the Heap in the figure above draw the state of the Heap after deleting the root node. Draw
also the state of the array. (copy the diagrams in the blue answer book).
[2 marks]
- Page 7 of 11 -
1603/159.201
ALB
Internal
6. Given the Tree C++ class below, answer questions a) and b):
class Tree {
private:
char data;
Tree *leftptr, *rightptr;
public:
Tree(char newthing, Tree* L, Tree* R);
~Tree() { }
char RootData() { return data; }
Tree* Left() { return leftptr; }
Tree* Right() { return rightptr; }
};
[1 marks]
b) Using a Queue as a supporting ADT, write a C++ function to print a Binary Tree by level, root
first (aka Breadth-first traversal). The class declaration for the Binary Tree is listed below.
Note: the queue should contain pointers to the Tree nodes. Do not write the queue class itself, nor
its methods, just consider using the well-known methods Join(), Leave(), Front() and isEmpty().
[2 marks]
c) Draw a B-Tree with order 3, where the following elements were inserted in the following order:
21, 3, 4, 7, 23, 25, 6, 20, 1, 5, 2
(copy the diagram in the blue answer book).
[3 marks]
- Page 8 of 11 -
1603/159.201
ALB
Internal
C 3
1 1
5 3 A
1
B 5 2 2
4
2
2 1 D
E
1
a) Complete the adjacency matrix below (in the blue answer book).
A B C D E
[1 marks]
b) Using Kruskal algorithm, find the Minimum Cost Spanning Tree for the graph above.
[2 marks]
c) Write the pseudo-code for Dijkstra's algorithm. Using the algorithms, what is the shortest path
from A to all other nodes? Show all the work based on the pseudo-code (use the same variable
names).
[5 marks]
- Page 9 of 11 -
1603/159.201
ALB
Internal
8.
a) The code for a Bubble sort function is shown below:
swapping = true;
while (swapping) {
swapping = false;
for (i = 0; i < n-1; i++) {
if (data[i] > data[i+1]) {
swap(data[i], data[i + 1]);
swapping = true;
}
}
}
This algorithm works, but it can be improved. We know that part of the array data[] (at the end of
the array) is already sorted because at every iteration the 'bubble' drags the larger keys to the right
side of the array. Devise a modification to the algorithm that avoids computing steps where the
elements are already sorted. (tip: it is easier to modify the algorithm by drawing a small example
using 4 or 5 numbers).
[2 marks]
b) Use Cichelli's method to devise a perfect hash function for the following words:
TREE, STACK, QUEUE, GRAPH, HEAP, LINKEDLIST, VECTOR, SET
Tips: for the hash function, use: H() =( length + g(first letter) + g(last letter) ) % 8
Write the final hash table for the words above (do not forget to test if each index is unique).
[4 marks]
c) The following two C++ codes are implementations of two different sorting algorithms.
The code SortingA() has a runtime of about 2 miliseconds for a problem size N=100.
The code SortingB() has a runtime of about 5 miliseconds for a problem size N=100.
For a very large problem, e.g. N=1000000, which of the two algorithms is more likely to have the
shortest runtime?
Explain the reasons for your choice and write the average complexity of the two algorithms.
Note: the SortingB is a recursive function.
- Page 10 of 11 -
1603/159.201
ALB
Internal
SortingA( ){
for (pass = 0; pass < n – 1; pass++) {
min = pass;
for (i = pass + 1; i < n; i++) {
if (data[i] < data[min]) { min = i; }
}
swap(data[min], data[pass]);
}
}
[2 marks]
- Page 11 of 11 -