06 30175 A1000989 LC Data Structure Algorithms Main v1
06 30175 A1000989 LC Data Structure Algorithms Main v1
06-30175
30175 LC Data Structure & Algorithms
Note
Answer ALL questions. Each question will be marked out of 20. The paper will be marked
Question 1
A non-circular, singly linked list Abstract Data Type has a private Node pointer, called
first , which points to the rst node of the list. This ADT does not maintain a header
pointer to the last node of the list, but it does maintain a variable size to keep track of
the number of nodes in the list.
Given a pointer to a node n , assume that values can be read or written to by using the
eld names n.val and n.next for the value and next elds of the node respectively.
Assume that a garbage collector is being used, so nodes do not need to be explicitly freed.
Use END for invalid or address pointer values.
null
(a) delete nth(int n) is a non-recursive method of the ADT that removes node
number n from the list and throws an IllegalArgumentException if no such
element exists or if n is less than 0. The rst node is numbered 0.
Fill in the missing part of the pseudocode for this function below:
1 void delete nth ( int n)
2 f
3 // WRITE THE CODE THAT SHOULD BE HERE
4 return
5 g
[10 marks]
(b) Assume that this ADT has two di erent di erent methods to delete all the elements
from the singly linked list as follows:
1 void d e l e t e a l l f r o m s t a r t ()
2 f
3 while ( size > 0)
4 d e l e t e n t h (0)
5 return
6 g
7
8 void d e l e t e a l l f r o m e n d ()
9 f
10 while ( size > 0)
11 delete nth ( size 1)
12 return
13 g
Give the complexities of these two methods in big O notation and explain how you
derived them. Write the pseudocode for a much simpler and more ecient method
to delete all elements from the list. [10 marks]
Question 2
(a) The following is the pseudocode for a recursive algorithm to print the values stored
in a Binary Tree (in no particular order):
1 p r i n t T r e e ( BTNode t)
2 f
3 if ( t != END )
4 f
5 print ( t . val )
6 printTree ( t . l e f t )
7 printTree ( t . right )
8 g
9 g
Hint: you may use a Stack Abstract Data Type in your answer. [10 marks]
(b) Consider an AVL-Tree of positive integers that does not allow duplicate values in the
tree. The insertion of the value 10 into a minimal such AVL-tree that is of height
3 triggers a double rotation.
Draw the diagram of this tree both before the insertion and after the insertion has
concluded. [10 marks]
Question 3
(a) A hash table is implemented using an array of size 8. The objects A to F are inserted,
in this order into the hash table. The Hash1 and Hash2 values for these objects are:
A B C D E F
Hash1: 11 1 1 9 0 1
Hash2: 1 3 7 1 3 5
[Note, if the list of array indexes probed for object X are 2, 5 and nally 0, then it
! ! !
can be written in the form: \X 2 5 0"]
(i) Assuming a Linear Probing strategy is employed using the Hash1 hash values,
list the sequence of addresses probed for each object and draw the nal state
of the resulting hash table array. [5 marks]
(ii) Assuming a Double Hashing strategy is employed, using the Hash1 hash values
as the primary hash values, and the Hash2 values as the secondary one, list the
sequence of addresses probed for each object and draw the nal state of the
resulting hash table array. [5 marks]
(b) Consider the following graph:
B
3 4
A 2 C
5
1 5
D E
3
Demonstrate the execution of the Jarnk-Prim algorithm for nding the minimal
spanning tree on this graph by writing out a table of the execution steps in the fol-
lowing format where the rst row, following initialisation, is already provided (initial
node to add is node A):
A B C D E Finished
0,A
..
1 ,B
..
1
,C
..
1 ,D
..
1 ,E
.. ..
. . . . . .
Each row should show the results of one iteration of the algorithm after a node is
added to the spanning tree, where the Finished column identi es the node that is
nished in that iteration, and the remaining columns show the current distance of
the node of that column from the tree, the node that connects it to the tree and a
tick mark if the node of the column is nished. [10 marks]
5