TCB 1063 Algorithm and Data Structure
TCB 1063 Algorithm and Data Structure
i
i
R: A
N&'iUt;
COURSE
DATE TIME
: :
INSTRUCTIONS TO CANDIDATES
I. 2. 3. 4. Answer ALL questions from the Questions Booklet. Begin EACH answer on a new page in the Answer Booklet. Indicate clearly answers that are cancelled, if any. Where applicable, show clearly steps taken in arriving at the solutions and indicate ALL assumptions, 5. Do not open this Question Booklet until instructed. There are ELEVEN (11) pages in this Question Booklet including the
Note
cover page.
Universit
i.. Teknoloq
PETRONAS
TBB1 063I1CB1 063 I. and vector2 in FIGURE QIa are vector objects of the
Vector
vectorl
FIGURE QIa
a.
I.
ADT
[2 marks]
IL
vector
[2 marks]
b.
Write the Java code which calls a predefined method of the Vector ADT to:
IL
[3 marks]
iiL display the index where an integer 3 is stored in vectorl. [2 marks)
TBBI O631TCB1063 Write a new method named extend Vector be added to the which will
c.
the vector object that calls the the vector's content to appends FIGURE QIb shows the result of a sample call to the method. method extend.
13
(vector2)
1 vectorl
vector2
r-iij LIIi
12 32
10
12
d.
is a vector containing several objects of Score. for is an ADT that has four public components: a string Score The first vector is three parallel vectors. storing course code and IDs, the second is for student marks and for storing student used Suppose vector3 the third is for student grades. Draw with sample data how acell of vector3 look like. will [3 marks}
i.
II.
TBB1 063/TCBI 063 2. The ADT for a singly linked list node is defined as in FIGURE Q2a.
{ ListNode class public mnt data; public ListNode public next; )
FIGURE Q2a
Trace the code segment in FIGURE Q2b which creates a singly linked list of Lis tod. e. Draw the list, including the external insertion of each node. pointers, after
ListNodo (mnt for ListNodo aNode aNoae data next curr = null; = null, i<::: 5; j++){ (); ListNode aNode = new =i; = null;
aw
aList 1=1;
if
C else
(aList
aList
== null)
= aNode; CurT = No e; ?
i. f { else
ourr. eurr
b.
by pointed
has at least 3 nodes, write an algorithm that deletes its aLl s t: middle node. [NOTE: If the number of nodes is even, both middles will be deleted. ] [5 marks] 4
c.
Rewrite the code in FIGURE Q2a so that it defines an ADT for e doubly linked list node.
[2 marks]
d. Rewrite the code in FIGURE Q2b so that it creates a doubly linked list of similar sequence data items. [4 marks] a State ONE (1) advantage and ONE (1) disadvantage of choosing a doubly linked list over a singly linked list in a program. [4 marks]
a.
Write an algorithm for converting the infix expression above to its postfix equivalent using suck. [6 marks]
b.
C.
FIGURE Q3a
I.
ii.
queue . System out println . . insertQueue queue . System. system: out . println out . print
deleteQueue ()
rear .
public
static
void
{ //this
function
removes
operators
all
(+
data
-
other *) from
than queue .
FIGURE
Q3c
[5 marks]
063 TBB1 D63i1`CB1 4. a. State with explanation the running time complexity in Big-O notation for each of the program excerpts below. Assume the value for n is during runtime. entered
i_
I (i=O;
(j=O; Systemout
[3 marks]
I1. Ir M jilt r 11 . :
for for
(i=0; (j-O;
System.
1<n; j<n;
aut i<n; out.
i++) J++)
(n)
for
(i=O; Syem.
(i );
[3 marks] FIGURE Q4 shows two lists of numbers, lis tA and li, s tB which to be combined into an ascending order single list. are
b,
FIGURE Q4
L Provide step by step explanation of how insertion sort is applied to sort 1i stB into ascending order. method
[4 marks]
..
II.
Suggest how 1is to and the sorted 1is tB in part (b)(i) can be merged to give a complete, sorted set of numbers.
[4 marks]
C81 063 1"BB10631"f c. A hash function is needed in order to calculate the address at which data sto be stored in a hash table of size I oo. certain Given TABLE Q4, determine the method used (i.e., folding, or modular arithmetic) by hash functions A, B mid-square c based on the data and the address they hashed to. and TABLE Q4
Hash
Data Hash Function 11211 13745 16600 4 49 36 A
Address
Hash Function 33 92 76
Hash Function
ll 45 0
[6 marks]
tiJ
a,
Insert the integers into a Binary Search Tree (BST) according to the they are given. order [4 marks]
b.
Transform the BST in part (a) into an AVL tree by showing the tree after each rotation performed. resulting [6 marks] Using the BST of FIGURE Q5a, state:
C.
FIGURE Q5a
the node that will replace the node containing 10 if deleted. [1 mark]
IL
iL
10
TBBI 053fTCB1053 iv. the node which is the sibling of node containing 18. [1 mark] the sequence of the nodes if traversed in post order. [2 marks] d. Trace and give the value returned by the recursive function in FIGURE Q5b using the BST in FIGURE Q5a as the initial parameter.
int null) 0; (root. left! =null left) =null) && root. Q5d(root. + right! right); =null) Q5d (BST {
V.
public if
root)
else
if
return else if
return else if
root. (root.
root.
data right!
data
Q5d(root. + =null)
Q5d(root. +
left);
right);
0;
}
FIGURE Q5b [4 marks]
OF PAPER-END
11