Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

EXAMINATION FOR

159.201  Algorithms and Data Structures

Semester   1 - 2014

1. The following code implements a linked­list with a function AddNode():

struct Node {

int data;

Node *next;

};

Node *A;

void AddNode(Node * & listpointer, int newthing) {

Node *temp;

temp = new Node;

temp­>data = newthing;

temp­>next = listpointer;

listpointer = temp;

}

a) Write a new function to add a node after a certain element in the linked­list. The new function's prototype is:

AddNodeAfter(Node*& listpointer, int newthing, int searchedthing);

If the searched element does not exist in the linked­list, then the newthing should be added to the tail of the linked­list. [5 marks]

b) The following code implements a reverse function for the linked­list:

void ReverseLL(Node * &listpointer) {

Node *current;

Node *reversedcopy=NULL;

Node *temp;

current = listpointer;

while (current!=NULL) {

AddNode(reversedcopy,current­>data);

current = current­>next;

}

listpointer=reversedcopy;

}

Based on the code, explain the problem called “memory leaking” . [1 marks]

c) Modify the above function ReverseLL() in such a way that it avoids the memory leaking problem. [2 marks]


2. Consider the following class for a Stack:

class Stack {

private:

struct Node {

float data;

Node *next;

};

Node *listpointer;

public:

Stack();

~Stack();

void Push(int newthing);

void Pop();

int Top();

bool isEmpty();

bool containsOne();

};

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 vice­versa). [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 linked­list of the stack. [2 marks]


3. The following class implements a List using a linked­list.

template <class T>

class List {

private:

struct Node {

T data;

Node *next, *previous;

};

Node *front, *current,  *tail;

public:

List();

~List();

bool FirstItem(T & item);

bool NextItem(T & item);

};

template <class T>

bool List<T>::FirstItem(T & item) {

if (front == NULL) { return false; }

item = front­>data;

current = front;

return true;

}

template <class T>

bool List<T>::NextItem(T & item) {

current = current­>next;

if (current == NULL) { return false; }

item = current­>data;

return true;

}

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]

4. Consider the following binary tree for questions a), b) and c):

 

a) Write the pre­order and post­order traversals. [2 marks]

b) Is the tree above a Binary Search Tree (BST)? Why or why not? [2 marks]

c) Write an algorithm (pseudo­code) 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]

5. Considering that a Heap class is implemented with an array:

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]

6. The following code implements a Threaded Binary Tree class:

class Tree {

private:

char

Tree

bool

public:

data;

*leftptr, *rightptr;

lthread, rthread;  // two new variables


Tree(char newthing, Tree* L, Tree* R);

~Tree() { }

char RootData() { return data; }  // inline functions

Tree* Left() { return leftptr; }

Tree* Right() { return rightptr; }

bool IsEmpty();

bool LeftThread() { return lthread; }  // two new methods

bool RightThread() { return rthread; }

};

a) Draw the threads to help the in­order traversal in the following tree:

Note: copy the tree on the blue answer book

 

 [2 marks]

b) Write a function for the in­order traversal of a threaded tree. In your implementation you can use the function NextNode(Tree*N) below:

Tree *NextNode(Tree *N) {

Tree *temp;

if (N­>RightThread()) { return N­>Right(); }

temp = N­>Right();

while (temp­>LeftThread() == false) {

temp = temp­>Left();

}

return temp;

}

[4 marks]


7. Consider the following representation of a graph:

 

a) Complete the adjacency matrix below (in the blue answer book).

 

A

B

C

D

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 ­


8.

a) The Insertion sort algorithm is shown below:

int pass, i, temp;

int data[MAX];

...

InsertionSort(){

for (pass=0;pass<n­1; pass++) {

temp=data[pass+1];

for (i=pass + 1; i > 0; i­­)

{

if(data[i­1]>temp){

data[i] = data[i­1];

}

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 worst­case 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.

[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]