159.201 Algorithms and Data Structures Semester 3 - 2013
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 3 - 2013
1. The following code implements a linkedlist 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) Modify the AddNode() function to add a node to the tail of a circular linkedlist. [4 marks]
b) The following code implements a reverse function for the linkedlist:
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”. [2 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 viceversa). [1 marks]
c) The following main() creates a stack and uses Push(), Top() and Pop().
main() {
Stack S;
S = new Stack;
S.Push(1);
S.Top();
S.Push(2);
S.Top();
S.Push(3);
S.Pop();
S.Push(4);
S.Top();
S.Pop();
S.Push(5);
S.Pop();
}
Write the printout of a traversal of the linkedlist of the stack from head to tail. [2 marks]
3. The following class implements a List using a linkedlist.
template
class List {
private:
struct Node {
T data;
Node *next, *previous;
};
Node *front, *current, *tail;
public:
List();
~List();
//other methods ...
void UnknownMethod();
};
template
void List
Node * temp;
temp=front;
while(temp!=NULL){
L.AddtoTail(temp>data);
temp=temp>next;
}
}
a) Describe briefly what the UnknownMethod() does. [2 marks]
b) Write a new method called PreviousItem(T & item) to recover the previous item when the current pointer is pointing to a certain element in the List. [2 marks]
c) Write a method that traverses the list in reverse order. [2 marks]
4.
a) Consider the following binary tree:
Write the inorder and postorder traversals. [2 marks]
b) Write an algorithm that returns the height of a binary tree. [2 marks]
c) Draw the resulting BST after numbers are entered in the following order: 5, 3, 4, 1, 2, 6, 8 [2 marks]
d) Write a function that returns the value of the largest key in a Binary Search Tree (BST). [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 simple equation to find the left child, given the index of its parent. [1 marks]
b) Write a simple equation to find the right child, given the index of its parent. [1 marks]
c) Write a simple equation to find the parent, given the index of its right child. [1 marks]
d) 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);
}
[5 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) The threaded tree above had its treads written for inorder traversal. The same threaded tree has now to be traversed, although inefficiently, in postorder traversal. Modify the following recursive function so that it works with the Tree class implemented above.
void PostOrder(Tree *T) {
if (T == NULL) { return; }
PostOrder(T>Left());
PostOrder(T>Right());
printf("%c ", T>RootData());
}
[2 marks]
b) Draw the threads to help the inorder traversal in the following tree:
Note: copy the tree on the blue answer book
[2 marks]
c) Explain what are AVL trees and what are the advantages of using them. [2 marks]
7. Consider the following representation of a graph:
5
2
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]
8.
a) The Bubble sort algorithm is shown below:
swapping=true;
while (swapping) {
swapping=false;
for (i=0; i< n1; i++ ) {
if( data[i] > data[i+1]);
swap(data[i], data[i+1]);
swapping = true;
}
}
}
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 an Insertion Sort algorithm using a linkedlist. Assume that the original data sits on an array. [4 marks]
2023-06-13