159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1401
159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1401
201
ALB Internal
MASSEY UNIVERSITY
ALBANY CAMPUS
EXAMINATION FOR
159.201 Algorithms and Data Structures
Semester 1 - 2014
- Page 1 of 9 -
1401/159.201
ALB Internal
struct Node {
int data;
Node *next;
};
Node *A;
a) Write a new function to add a node after a certain element in the linkedlist.
The new function's prototype is:
AddNodeAfter(Node*& listpointer, int newthing, int searchedthing);
If the searched element does not exist in the linkedlist, then the newthing should be added to
the tail of the linkedlist.
[5 marks]
c) Modify the above function ReverseLL() in such a way that it avoids the memory leaking
problem.
[2 marks]
- Page 2 of 9 -
1401/159.201
ALB Internal
a) Write a method in C++ called containsOne() that returns true if the stack contains exactly
one node, and false otherwise (if the stack is empty, or contains two or more elements).
[5 marks]
b) Modify the Node above in such a way that it allows the traversal of elements in two
directions (from the head to tail and viceversa).
[1 marks]
c) The following main() creates a stack and uses Push(), Top() and Pop().
main() {
Stack S;
S = new Stack;
S.Push(5);
S.Top();
S.Push(4);
S.Top();
S.Push(3);
S.Pop();
S.Push(2);
S.Top();
S.Pop();
S.Push(1);
S.Pop();
//draw the diagram with the current contents at this point
}
Consider the contents of the nodes after carrying out all the statements above. Draw a
diagram of the entire linkedlist of the stack.
[2 marks]
- Page 3 of 9 -
1401/159.201
ALB Internal
public:
List();
~List();
bool FirstItem(T & item);
bool NextItem(T & item);
};
a) Write a function that traverses and prints the List. The function needs to rely on
FirstItem() and NextItem() methods to work. Assume that the data is integer, i.e., that the
instance of the List is: List<int> mylist;
[2 marks]
b) Write a new method called TraverseListForward() that traverses the list from front to tail.
Note: do not use FirstItem() or NextItem() methods, as the method can access the private
variables directly.
[2 marks]
c) Write a new method called TraverseListReverse() that traverses the list in reverse order.
Note: the class already contains a node with a previous pointer, and there is a tail pointer as
well.
[2 marks]
- Page 4 of 9 -
1401/159.201
ALB Internal
4. Consider the following binary tree for questions a), b) and c):
3 6
2 4 7
b) Is the tree above a Binary Search Tree (BST)? Why or why not?
[2 marks]
c) Write an algorithm (pseudocode) that returns the value of the smallest key in a Binary
Search Tree (BST). Demonstrate that your algorithm works using it on the tree above.
[2 marks]
d) AVL trees are trees that are always balanced. Explain why it is important to balance trees
when adding or deleting nodes from it.
[2 marks]
- Page 5 of 9 -
1401/159.201
ALB Internal
class Heap {
private:
unsigned int data[MAXSIZE];
int last;
public:
Heap(){ last=1; };
~Heap() { };
void InsertHeap(unsigned int newthing);
int DeleteRoot();
};
a) Write a method to return the value of the left child, given the index of its parent.
[2 marks]
b) Write a method to return the value of the parent, given the index of its right child.
[2 marks]
c) Consider that the InsertHeap() method is implemented correctly. Using the main()
function below, draw the array that contains the Heap considering that the MAXSIZE=10;
main(){
InsertHeap(2);
InsertHeap(3);
InsertHeap(5);
InsertHeap(4);
InsertHeap(1);
InsertHeap(7);
InsertHeap(6);
InsertHeap(8);
}
[4 marks]
- Page 6 of 9 -
1401/159.201
ALB Internal
a) Draw the threads to help the inorder traversal in the following tree:
Note: copy the tree on the blue answer book
B E
C D F
[2 marks]
b) Write a function for the inorder traversal of a threaded tree. In your implementation you
can use the function NextNode(Tree*N) below:
- Page 7 of 9 -
1401/159.201
ALB Internal
A
6 5
6 4
B D
2
7 2
5 C 1
a) Complete the adjacency matrix below (in the blue answer book).
A B C D
[2 marks]
b) Show two cycles in this graph (either draw them or just list the sequence of nodes for the
cycle's path).
[2 marks]
c) Using Dijkstra's algorithm, what is the shortest path from A to all other nodes? Show all
the work (permanent nodes, frontier nodes, possible paths, final path).
[4 marks]
- Page 8 of 9 -
1401/159.201
ALB Internal
8.
a) The Insertion sort algorithm is shown below:
int pass, i, temp;
int data[MAX];
...
InsertionSort(){
for (pass=0;pass<n1; pass++) {
temp=data[pass+1];
for (i=pass + 1; i > 0; i)
{
if(data[i1]>temp){
data[i] = data[i1];
}
else break;
}
data[i] = temp;
}
}
Suppose that two different input arrays are used, and the runtime compared. Array 1 has the
numbers already sorted, while array 2 has the numbers in reverse order. Which one runs
faster? Explain why based on the algorithm above.
[2 marks]
b) Draw (in the blue answer book) an approximate curve that shows the worstcase runtime
x set size for Bubble and for Merge sorting algorithms. Note: show the O(f(n)) trends only,
you do not need to show numerical data in either of the axis.
runtime
set size
[2 marks]
c) Write a C or C++ function that implements a Bubble Sort algorithm. Assume that the
original data is stored in an array.
[4 marks]
- Page 9 of 9 -