0% found this document useful (0 votes)
72 views9 pages

159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1201

The document contains questions about algorithms and data structures. It asks questions about stacks, queues, lists, trees, graphs, and sorting algorithms. It also includes questions about binary search, algorithm complexity, and choosing the best algorithm based on problem size.

Uploaded by

Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views9 pages

159.201 Algorithms and Data Structures - Massey - Exam - I10 - 1201

The document contains questions about algorithms and data structures. It asks questions about stacks, queues, lists, trees, graphs, and sorting algorithms. It also includes questions about binary search, algorithm complexity, and choosing the best algorithm based on problem size.

Uploaded by

Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1201/159.

201
ALB Internal

MASSEY UNIVERSITY
ALBANY CAMPUS

EXAMINATION FOR
159.201 Algorithms and Data Structures
Semester One - 2012

Time allowed: THREE (3) hours

Attempt ALL 10 (ten) questions.

This examination contributes 70% to the final assessment.


Programs, algorithms and pseudo-code may be written in pencil.

- Page 1 of 9 -
1201/159.201
ALB Internal

1. a) Give a name of a commonly used Abstract Data Type. Give an example of a problem
where this particular ADT can be utilised in an effective, efficient and easy-to-program
way. Explain how the ADT is helping to solve the problem.
[4 marks]
b) Show how one would implement Stacks using arrays. Describe all the control variables
one would need in order to control a Stack using the four basic functions (Push, Pop, Top
and isEmpty).
[3 marks]

2. The following code is used to implement a class for the ADT List. This list is
implemented as a linked-list.

class List {
private:
struct Node {
char data;
Node *next;
};
Node *listpointer;
public:
//methods to be implemented here...
}

a) Write a C++ method or a function that searches for a given item. You need to pass at
least the item as a parameter, or the list pointer and the item if using a function. Return the
pointer of the node instead of the value. E.g., call your method or function Search(item).
[2 marks]

b) Write a C++ method called InsertAfterItem() to insert a node in the middle of the list,
after a certain item is found.
You can assume that the search method/function above works, and use it to support the
writing of the InsertAfterItem() method.
The method requires that two items are passed as a parameter, the first is the new item,
the second being the item to search. If the item is found, insert the new node after it.
Otherwise, insert the node to the front of the list. E.g., suppose a new node containing '4'
is inserted after node containing '1'. In main the statement would look like: InsertAfterItem(
4, 1).
4

3 1 5

- Page 2 of 9 -
1201/159.201
ALB Internal

[5 marks]

3. Consider the following class for a Stack:


class Stack {
private:
struct Node {
float data;
Node *next;
};
Node *listpointer;
public:
Stack();
~Stack();
//other methods ...
};

a) Write a new C++ method for this class called DeleteAll(). The method deletes every
node , leaving an empty stack. You can use any of the four standard methods to support
the new method.
[4 marks]

b) Explain why the following code fails to copy Stack A onto Stack B (hint: if we delete A,
B is also deleted).
Briefly explain (without actually writing the C++ code) how you would create a method that
would implement a proper copy of the stack onto another new stack, such as B=A.Copy();

Stack A, B;
main(){
//push several elements to stack A
B = A;
//do something else ...
}
}
[3 marks]

- Page 3 of 9 -
1201/159.201
ALB Internal

4. The two following classes are possible implementations of a Queue:


class Queue {
private:
struct Node {
char data;
Node *next;
};
Node *front, *rear;
public:
Queue();
//other methods...
}

class Queue {
private:
int data[200];
int first,last,count;
public:
Queue();
//other methods...
}

a) Considering the “creeping problem”, which one of these two classes could be affected
by it, if not properly implemented?
[2 marks]

b) Write the full code for a C++ method called Invert() for the Queue class implemented
with a linked-list. The method Invert() inverts the order of all items.
For example, if the Queue consists of A,B,C,D (A is at the front), then after using this
method the queue would consist of D,C,B,A.
Important: Write the method in such a way that it does not need to create nor delete
nodes.
[5 marks]

- Page 4 of 9 -
1201/159.201
ALB Internal

5. Binary Trees:

a) Write the pre-order, in-order and post-order traversals for the following binary tree:

[3 marks]

b) Write a recursive function to traverse an arithmetic tree in-order, with brackets in


place (a fully parenthesised expression), given the following class below. For example, if
traversing the tree in the figure below, the function would have to print: ( 2 + 3 )

2 3

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; }
};

[4 marks]

- Page 5 of 9 -
1201/159.201
ALB Internal

6. Starting from the following BST, answer the following questions:

a) From the BST in the figure, delete node '25' from the BST and draw the new BST.
[1 marks]

b) From the BST in the figure, delete node '32' from the BST and draw the new BST.
[1 marks]

c) From the BST in the figure, delete node '55' from the BST and draw the new BST, in a
way that achieves the most balanced tree.
[2 marks]

d) Suppose that two BSTs contain the same nodes: 2,18,20,22,36,87. It is possible to draw
lots of different binary trees with these nodes, but are they all BSTs?
Draw at least two different BSTs containing the same nodes.
[3 marks]

- Page 6 of 9 -
1201/159.201
ALB Internal

7. Graphs

A B C D

A 0 1 1 6

B 1 0 3 4

C 4 5 0 6

D 6 7 5 0

a) Draw this graph.


[2 marks]
b) Show two cycles in this graph.
[2 marks]
c) Explain how you would implement this graph using an adjancency list using linked-lists.
[3 marks]

8. Sorting algorithms.

a) Given the following sorting algorithms, which one should be used for generic sorting
problems involving arrays of integers? Give a brief explanation for your choice.
• Selection Sort
• Insertion Sort
• Bubble Sort
• Merge Sort
• Quicksort
[2 marks]

b) Explain how Heap sorting works, and how the Heap properties help with the algorithm.
(You don't need to write any code nor any pseudo-code.)
[2 marks]

c) Write a pseudo-code for Quicksort. (you don't need to write code in C/C++).

[3 marks]

- Page 7 of 9 -
1201/159.201
ALB Internal

9. a) Write a pseudo-code for a Binary search algorithm. (you don't need to write code in
C/C++).
[4 marks]

b) Consider the average time for searching something within a set. Using a Binary Search
implementation, draw the line that illustrates the runtime for different problem sizes.

runtime

set size
[3 marks]

10. a) Assume that there are three algorithms, A1, A2 and A3. They can solve a certain
(same) computation problem using the data from a database. The complexity of the three
algorithms is O(N), O(N log(N)) and O(N2) respectively.
A computer scientist measures the runtime for the three algorithms, finding:
A1 – 10 seconds
A2 – 5 seconds
A3 – 6 seconds

Then the company using the algorithm realises that the measurements were done with a
very small version of the database that will run in production.
Despite what the measurements seem to indicate, the scientist argues that algorithm A1
will be the best solution when the problem size grows (uses a larger database). Is he right?
Explain why or why not, using a simple figure or table to strengthen your arguments.
[4 marks]

b) Discuss how you would estimate the average time complexity for Quicksort. Also,
discuss the worst case complexity (the maximum possible time complexity).
[3 marks]

- Page 8 of 9 -
1201/159.201
ALB Internal

++++++++

- Page 9 of 9 -

You might also like