You are on page 1of 19

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

1
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

PART A OBJECTIVE QUESTIONS [BAHAGIAN A SOALAN OBJEKTIF]

[15 marks] [15 markah]

Part A consists of 15 objective questions. Choose the best answer and write your answer in the answer booklet provided. Each question carries 1 mark.
[ Bahagian A mengandungi 15 soalan objektif. Pilih jawapan yang paling tepat, dan jawab pada buku jawapan yang disediakan. Setiap soalan bernilai 1 markah. ]

1. Which of the statements below describe the WEAKNESS of linked-list compared to array?
[Yang mana di antara berikut merupakan KELEMAHAN senarai berpaut berbanding tatasusunan?]

A. A linked-list size is limited [Saiz senarai adalah terhad.] B. Random access on the data in the list cannot be done. [Capaian secara rawak ke atas data dalam senarai tidak boleh dilakukan.] C. Deletion will result an empty slot in the list [Operasi penghapusan akan menyebabkan satu ruang kosong ditinggalkan.] D. The operation that check whether a list is full or not has to be executed before any data insertion. [Operasi menyemak senarai penuh perlu dilakukan sebelum sebarang penambahan data boleh dilakukan.] Given Figure A-1 below, answer Question 2 and 3.
head item link item link item link
NULL

7 Figure A-1

2. What is the final value for count after the following code segment is executed on the list in Figure A-1? [Apakah nilai akhir count setelah keratan aturcara berikut dilaksanakan pada senarai dalam Rajah A-1 ?]
int count=0; while (head->link!=NULL) { count++; head = head->link; }

A. 0

B. 1

C. 2

D. 3

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

2
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

3. What is the result from the execution of the code segment below on Figure A-1? [Apakah kesan daripada perlaksanaan keratan aturcara berikut ke atas Rajah A-1?]
Node *current=head; while (current != NULL) current = current->link;

A. The first node has become the last node. [Nod pertama ditukarkan ke nod terakhir.] B. head is still pointing to the first node. [head masih kekal menuding ke nod pertama.] C. head is pointing to the last node. [head menuding ke nod terakhir.] D. head has NULL value. [head bernilai NULL.] 4. Which of the following ADT can correctly be used to store a group of short message systems (sms) waiting to be sent to the recipient? [Yang mana di antara Jenis Data Niskala berikut boleh digunakan dengan betul untuk menyimpan sekumpulan sistem pesanan ringkas yang sedang menunggu untuk dihantar kepada penerima?] A. B. C. D. List Stack Queue Tree [Senarai] [Tindanan] [Baris gilir] [Pepohon]

5. Which of the following statement is TRUE about inserting new item to a non empty stack implemented using linked list ? [ Yang mana di antara pernyataan berikut BENAR mengenai kemasukan item baru ke dalam tindanan bukan kosong perlaksanaan senarai berpaut?] A. Statement top = top + 1; will cause the top to point to the new node. [Pernyataan top = top + 1; menyebabkan top menuding ke nod baharu.] B. Statement head = newnode; will cause the head to point to the new node. [Pernyataan head = newnode; menyebabkan head menuding ke nod baharu.] C. Pointer next of the new node will has NULL value. [Penuding next bagi nod baharu akan bernilai NULL.] D. The new node will be inserted to the end of the list. [Nod baharu akan ditambah pada bahagian akhir senarai.]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

3
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

6. Which of the following statement is TRUE about operation_X() ? [ Yang mana di antara pernyataan berikut BENAR mengenai operation_X()? ]
bool stack::operation_X() { return (top == null); }

i) The stack is implemented using array. [Tindanan dilaksanakan secara tatasusunan.] ii) The stack is implememnted using linked list. [Tindanan dilaksanakan secara senarai berpaut.] iii) operation_X()will check whether the stack is empty or not. [operation_X()akan menyemak samada tindanan kosong atau tidak.] iv) operation_X()will check whether the stack is full or not. [operation_X()akan menyemak samada tindanan penuh atau tidak.]
A.

C.

I and IV II and III

B. D.

I and III II and IV

7. What is the task of the following statement in the pop(); operation for a stack

implemented using linked list? [ Apakah tugas pernyataan berikut di dalam operasi pop(); bagi tindanan yang dilaksanakan secara senarai berpaut? ]
head = head->next; A. Delete the node pointed by head.

or

head = delnode->next;

[Menghapus nod yang dituding oleh head.] B. head will point to the second node in the list. [head akan menuding ke nod kedua dalam senarai.] D. Make the delnode point to head. [Menjadikan delnode menuding kepada head. ] E. Delete the head pointer. [Menghapus penuding head. ] 8. What is the result of the evaluation of the following postfix expression? [Apakah hasil bagi ungkapan postfix berikut?] 2 2 5 2 * 2 / + A. infiniti C. 5 B. 0 D. 12

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

4
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

9. Which of the following operations leaves a queue unchanged? [Yang mana di antara operasi berikut menjadikan baris gilir tidak berubah?] A. B. C. D. enqueue() dequeue() createQueue() getFront()

10. What operation is being done in the following code fragments of a queue represented by linked list? [Apakah operasi yang dilaksanakan oleh keratan kod berikut terhadap barisgilir perlaksanaan senarai berpaut?]
P->next = Q->next; Q->next = P; Q = P;

A. Insert node P to empty linear queue. [Menambah nod P ke dalam baris gilir linear yang kosong.] B. Insert node P to non-empty circular queue. [Menambah nod P ke dalam baris gilir linear yang bukan kosong.] C. Insert node Q to non-empty circular queue. [Menambah nod Q ke dalam baris gilir membulat yang kosong.] D. Insert node Q to non-empty linear queue. [Menambah nod Q ke dalam baris gilir linear yang bukan kosong.] 11. Which of the following is NOT needed in the structure of a queue implementation circular linked list? [Yang mana di antara berikut TIDAK diperlukan bagi struktur baris gilir perlaksanaan senarai berpaut membulat?] A. Node. [Nod.] B. Pointer to front queue. [Penuding kepada bahagian hadapan baris gilir.] C. Pointer to rear queue. [Penuding kepada bahagian belakang baris gilir.] D. Pointer next for every node in the queue. [Penuding next bagi setiap nod dalam baris gilir.]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

5
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

12. Which of the following statement is TRUE regarding binary tree? [Kenyataan yang manakan antara yang berikut adalah BENAR berkenaan pepohon dedua?] A. B. C. A binary tree must be nonempty. [Pepohon dedua mesti tidak kosong.] The level of the root node is 0. [Aras nod akarnya adalah 0.] If a tree has only one node, the height of this tree is 0 because the number of level is 0. [Jika satu pepohon hanya mempunyai satu nod, tinggi pepohon adalah 0 kerana bilangan aras adalah 0.] The inorder traversal of a binary tree always outputs the data in ascending order. [Susuran dalam-tertib satu pepohon dedua sentiasa memberi output data dalam susunan menaik.]

D.

13. What is the result of the arithmetic expression generated by the binary tree in Figure A-2 below? [Apakah nilai bagi ungkapan aritmetik yang dihasilkan oleh pepohon dedua pada Rajah A-2 di bawah?] + / + 4 2 7 Figure A-2 2 3 3 + * 3

A. B. C. D.

1 6 17 18

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

6
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Given a tree with the content as in Figure A-3, answer Question 14 and 15. [Diberi pepohon dengan kandungan seperti dalam Rajah A-3, jawab Soalan 14 dan 15. ] E

C A D F H

K M I Figure A-3 O P

14. Which of the following set of inserted input sequence CANNOT produce the BINARY SEARCH TREE in Figure A-3? [Yang mana di antara set masukan jujukan input berikut TIDAK boleh menghasilkan PEPOHON CARIAN PERDUAAN seperti di Rajah A-3? ] A. B. C. D. ECADKHFIMOP ECDAMKHIFPO EKMHFIOPCAD EKMOPHIFCDA

15. What is the OUTPUT, if the following recursive function is executed on the tree in Figure A-3? [Apakah OUTPUT akhir sekiranya fungsi rekursif berikut dilaksanakan pada pepohon di Rajah A-3? ]
void Print(TreeNode* tree) { if (tree==NULL) cout<<"NULL"<<endl; else { cout<<tree->data; Print(tree->right); Print(tree->left); } }

A. B. C. D.

ECADKHFIMOP ECADKMOPHIF EKHIFMOPCPA EKMOPHIFCDA

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

7
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

PART B - STRUCTURED QUESTIONS


BAHAGIAN B SOALAN STRUKTUR

[85 marks]
[85 markah]

Part B consists of 4 structured questions. Answer all questions in this part.


[Bahagian B mengandungi 4 soalan struktur. Jawab semua soalan pada bahagian ini. ]

Question 1

[22 marks]

a) Figure B-1.1 is a singly linked list that stores the information for medicines. The list has a pointer head that points to the first node in the list. [Rajah B-1.1 mengandungi satu senarai berpaut tunggal yang menyimpan maklumat ubat. Senarai tersebut dituding oleh pembolehubah penuding head yang menuding kepada nod pertama dalam senarai.]
head Name Brand Price link 34 Name Brand Price link 30 Name Garlic Brand Price link Kordel 40 NULL

Batuk K_Laut

Calsium Bmore

Figure B-1.1 : Linked List for Medicines

i) Write a class definition of a node for the linked list above. Every node should have Name, Brand, Price and link as the attribute. Also, define a variable called head which will point to the first node in the list. [Tuliskan pengisytiharan nod sebagai satu kelas untuk senarai berpaut di atas. Setiap nod mempunyai medan Name, Brand, Price dan link. Ishtiharkan juga pembolehubah penuding head yang digunakan sebagai penunjuk kepada nod pertama dalam senarai tersebut.]
[3 marks]

ii) Write source codes that will display the output as shown in Figure B-1.2. You need to use the pointer head in order to print the value. [Tuliskan kod yang akan mencetak output seperti yang ditunjukkan dalam Rajah B1.2. Gunakan pembolehubah head bagi mencetak maklumat tersebut.]

Medicine Name: Garlic Brand : Kordel Price : RM 40

Figure B-1.2 : Output


[3 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

8
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

iii) Based on your previous class definition in a(i), write the code that can create a new node like shown in Figure B-1.3, and then assign the following data to the new node: Multi Vit, Bmore, 30 [Berdasarkan pengisytiharan kelas pada a(i), tulis arahan untuk mencipta satu nod baharu seperti yang ditunjukkan dalam Rajah B-1.3, dan seterusnya umpukkan data berikut kepada nod baru tersebut : Multi Vit, Bmore, 30]

newNode

Name
Multi Vit

Brand
Bmore

Price
30

link
NULL

Figure B-1.3 : newNode


[3 marks]

b) Figure B-1.4 shows a linked list that has 2 nodes which are node 30 and node 40. [Rajah B-1.4 menunjukkan sebuah senarai berpaut yang mempunyai 2 nod iaitu nod dengan nilai 30 dan 40.] item link prev
30

item
40

link n item link


35

head
Figure B-1.4 : Linked-list

current

Figure B-1.5 : A new node

i) A new node that is, a node with value 35 that is being pointed by n (Figure B-1.5) is going to be inserted into the list in Figure B-1.4. Write the code that inserts the new node, node n into the list. Please make sure that after the insertion operation is completed, the linked list should be sorted in an ascending way. [Satu nod baru iaitu nod yang mengandungi nilai 35 dan dituding oleh n (Rajah B1.5) hendak ditambah ke dalam senarai pada Rajah B-1.4. Tuliskan arahan-arahan yang membolehkan nod baru iaitu nod yang dituding oleh n ditambah ke dalam senarai berpaut tersebut. Pastikan setelah penambahan dilakukan, senarai berpaut berada dalam keadaan terisih secara susunan menaik.] [2 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

9
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

ii) Please draw the state of the linked list after the insertion of a new node has been done. Show what does the pointers prev, head and current are pointing to. [Lukiskan senarai berpaut ini selepas penambahan nod baru di dalam ruang kosong di bawah. Tunjukkan kedudukan pembolehubah penuding prev, head dan current yang terkini.] [2 marks]

c) Figure B-1.6 is a linked list that has 3 nodes and pointers head, prev, current and tail. [Rajah B-1.6 menunjukkan sebuah senarai berpaut yang mempunyai 3 nod serta penuding head, prev, current dan tail.] item link head prev 55 item link 66 current item link 77 tail item link 99

Figure B-1.6 : Linked-list with 3 nodes. The node with the value of 66 is going to be removed from the list. Write the code the will delete the node from the list using pointer head, prev, current and tail. [Nod dengan nilai 66 hendak dihapuskan daripada senarai. Tuliskan arahan-arahan untuk menghapus nod tersebut daripada senarai berpaut, dengan menggunakan penuding head, prev, current dan tail.] [2 marks] d) Given the code segment in Program 1, trace the execution, and redraw the linked list in Figure B-1.6 to show the changes. [Berdasarkan keratan aturcara pada Program 1, jejak perlaksanaan setiap penyataan yang diberikan dan lukiskan semula rajah senarai berpaut pada Rajah B1.6 untuk menunjukkan perubahan yang berlaku.]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

10
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

1 2 3 4 5 6 7 8 9 10 11

Node *newnode1; newnode1->item = 11; newNode1->link = head; head = newNode1; prev = prev->link; prev->item = 88; tail = tail->link; tail->link = head;

Program 1 : Codes in Linked list

i) Draw the state of the linked list in Figure B-1.6 after the execution of statement in line 1-2. [Lukiskan keadaan senarai berpaut dalam Rajah B-1.6 selepas penyataan dalam baris 1-2 dilaksanakan.] [1 mark] ii) Draw the state of the linked list in Figure B-1.6 after the execution of statement in line 4-5. [Lukiskan keadaan senarai berpaut dalam Rajah B-1.6 selepas penyataan dalam baris 4-5 dilaksanakan.] [2 marks] iii) Draw the state of the linked list in Figure B-1.6 after the execution of statement in line 7-8. [Lukiskan keadaan senarai berpaut dalam Rajah B-1.6 selepas penyataan dalam baris 7-8 dilaksanakan.] [2 marks] iv) Draw the state of the linked list in Figure B-1.6 after the execution of statement in line 10-11. [Lukiskan keadaan senarai berpaut dalam Rajah B-1.6 selepas penyataan dalam baris 10-11 dilaksanakan.] [2 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

11
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Question 2

[21 marks]

a) Referring to the algorithm to check for balanced parentheses, determine whether the following expressions has balance parentheses or not, then show the checking process by completing the table below. [Dengan merujuk kepada algoritma untuk menentukan keseimbangan kurungan, tentukan samada setiap penyataan di bawah mempunyai tanda kurungan yang seimbang atau tidak. Tunjukkan langkah demi langkah yang terlibat dengan melengkapkan jadual di bawah.]
(a * b) / c + ((d - e)

Input

Stack

[2 marks] b) Given the following infix expression, show the steps to convert the infix expression to prefix and postfix manually. [ Diberi ungkapan infiks berikut, tunjukkan langkah-langkah untuk menukar ungkapan infiks kepada ungkapan prefiks dan postfiks secara manual. ] (3 * 8) / 2 + 5 + 4 * (7 - 3) [4 marks]

c)

Using the algorithm to convert the following infix notation to postfix notation in Figure B-2.1, show the steps to convert the following infix notation to postfix notation in the table provided below : [ Dengan merujuk kepada algoritma untuk menukar notasi infix kepada postfix yang diberi dalam Rajah B-2.1, tunjukkan langkah-langkah perlaksanaan untuk menukar ungkapan tersebut di dalam ruang yang diberikan di jadual di bawah: ] Infix Notation Postfix Notation
6 * ( 3 + 4 ) / 2 3 * 5 6 3 4 + * 2 / 3 5 * -

[6 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

12
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Infix Notation

Stack

Postfix Notation

create(s); while (not end of infix input) { ch = getch(); // next input character if (ch is operand) add ch to postfix notation; if (ch = () push(ch) if (ch = )) { chpop = pop(); while (chpop != () { add chpop to postfix notation; chpop = pop(); } } if (ch is operator) { while (!isEmpty() && (precedence(stacktop()) >= precedence(ch))) { chpop = pop(); add chpop to postfix notation; } push(ch); } } while (!isEmpty()) { ch = pop(); add ch to postfix notation; }

Figure B-2.1 : Algorithm to convert infix notation to postfix. d) Answer this question by referring to the algorithm to evaluate the postfix expression explained in class. Show the steps of evaluation the following postfix expression.

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

13
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Write the solution in the space as shown below. Show the value of operan1 (op1), operan2 (op2) and operator (optr), if any. [Jawab soalan ini dengan merujuk kepada algoritma untuk menilai ungkapan postfix yang diterangkan dalam kelas. Tunjukkan langkah-langkah perlaksanaan untuk menilai ungkapan postfix berikut. Jalan penyelesaian perlu ditunjukkan di dalam ruang seperti yang disediakan di bawah. Tuliskan nilai operan1 (op1), operan2 (op2) dan operator (optr), jika ada.] Postfix Notation
6 3 4 + * 2 / 3 5 * -

[6 marks]

Postfix

Op1 Op2 Optr

Stack

e) Write an operation for the stack class that able to copy the elements of a stack (i.e., stack A) to another stack (i.e., stack B). [Tuliskan satu operasi tambahan bagi kelas tindanan yang boleh menyalin elemen daripada satu tindanan (iaitu, tindanan A) kepada tindanan yang lain (iaitu, tindanan B).]
void copyStack(stack&);

[5 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

14
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Question 3 a) View Program 2 to answer the following question. [Lihat Program 2 untuk menjawab soalan berikut.]
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: void main() { Queue q; X = 10 ; Y = 15 ; q.enqueue(x); q.enqueue(y); if (x % 5 == 0) q.enqueue(y-x); if (q.getFront()>= 10) item = q.dequeue(); q.enqueue(x+y); item = q.dequeue(); if (q.isFull()) item = q.deQueue(); q.enqueue(25 - 5); cout << q.getFront() << q.getRear(); }

[21 marks]

Program 2: Queue application. Assume that the size of the queue implementation linear array is 4, fill in the diagram with the content of the queue, value for front, rear and item for each execution of the statements in Program 2. Give the output of the program where applicable. [Anggapkan saiz baris gilir perlaksanaan tatasusunan linear adalah 4. Isi rajah yng diberi dengan kandungan terkini baris gilir, nilai bagi pembolehubah front, rear and item selepas perlaksanaan pernyataan dalam Program 2. Berikan output bagi pernyataan yang berkenaan .] i)
X = 10 ; Y = 15 ; q.enqueue(x); q.enqueue(y); if (x % 5 == 0) q.enqueue(y-x);

front = _______ rear = _______

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

15
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

ii) if (q.getFront()>= 10) item = q.dequeue();

front = _______ rear = _______ item = _______

iii) q.enqueue(x+y); item = q.dequeue();

front = _______ rear = _______ item = _______

iv)
if (q.isFull()) item = q.deQueue();

front = _______ rear = _______ item = _______ isFull() = ________

v)
q.enqueue(25 - 5); cout << Item at front: << q.getFront() << Item at back: << q.getRear(); front = _______ rear = _______ item = _______ isFull() = ______

OUTPUT

[8 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

16
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

b) State the problem that occur in the queue implementation linear array in question (a) and give solution for the problem. [Nyatakan masalah yang berlaku pada baris gilir perlaksanaan tatasusunan linear dalam soalan (a) dan berikan penyelesaian kepada masalah tersebut.] [3 marks] c) Based on the solution that you gave in (b), fill in the diagram with the current value in the queue and give the value for front, rear and isfull()after the execution of main() in Program 2. Give the output of the program. [Berdasarkan kepada penyelesaian yang diberi pada soalan (b), isi rajah baris gilir dengan kandungan terkini baris gilir, nilai bagi pembolehubah front, rear dan isfull() selepas selesai perlaksanaan main() dalam Program 2. Berikan output.]
front = ______ rear = ______ isFull()= _____

OUTPUT

[3 marks] d) Figure B-3.1 shows a queue implementation linear linked-list. Write C++ statements to delete a node from the queue. [Rajah B-3.1 menunjukkan sebuah baris gilir perlaksanaan senarai berpaut linear. Tulis kenyataan-kenyataan C++ untuk menghapuskan satu nod dari baris gilir.] [2 marks] front back

kenanga

orkid

teratai

Figure B-3.1 Queue implementation linear linked-list e) Figure B-3.2 shows queue implementation circular linked-list. Write C++ statements to delete a node from the queue. Draw a diagram to illustrate the changes of the pointers during the execution of the statements. Numbered the changes of pointers in your diagram according to the C++ statements sequence. [Rajah B-3.2 menunjukkan sebuah baris gilir senarai berpaut membulat. Tulis kenyataan-kenyataan C++ untuk menghapus satu nod daripada baris gilir. Lukiskan gambarajah untuk mengambarkan perubahan pada penuding semasa perlaksanaan kenyataan-kenyataan tersebut. Nomborkan perubahan punuding pada gambarajah anda mengikut jujukan kenyataan-kenyataan C++ tersebut. ] [3 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

17
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

theQueue

cempaka

melati

rose

Figure B-3.2 Queue implementation circular linked-list f) Deleting a node from a queue implementation circular linked list with only one node is a special case of queue condition that need to be considered. Explain the case. [Penghapusan nod daripada baris gilir perlaksanaan senarai berpaut membulat yang mempunyai hanya satu nod merupakan satu kes khas bagi keadaan baris gilir yang perlu dipertimbangkan. Nyatakan kes tersebut. ] [2 marks]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

18
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Question 4

[21 marks]

Refer to the tree in Figure B-4.1 to answer the following questions. [Rujuk kepada pepohon pada Rajah B-4.1 untuk menjawab soalan-soaln berikut.] 15

8 6 9 16 Figure B-4.1 : Binary Search Tree 18

24 40 21

a) Based on the binary search tree in Figure B-4.1, answer the following questions. [Berdasarkan rajah pepohon carian dedua dalam Rajah B-4.1, jawab soalan berikut.] [4 marks] i. List all the leaves. [Senaraikan semua nod daun.] ii. Find the height of the tree. [Cari ketinggian pepohon.] Is this binary tree complete? Explain your answer. [Adakah pepohon ini pepohon dedua lengkap? Jelaskan jawapan anda. ] Is this binary tree balanced? Explain your answer. [Adakah pepohon ini pepohon dedua seimbang? Jelaskan jawapan anda. ]

iii.

iv.

b) Based on the binary tree in Figure B-4.1, give the order of traversal based on the following traversal type. [Berdasarkan rajah pepohon dedua dalam Rajah B-4.1, berikan susunan susuran berdasarkan jenis susuran berikut berikut.] [4 marks] i. Postorder traversal. [Susuran pasca-tertib] ii. Preorder traversal. [Susuran pra-tertib]

w
w

PD

F -X C h a n ge

PD

F -X C h a n ge

O W !

bu

lic

lic

Final Exam Data Structure and Algorithm


o
.c

19
w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

c) Based on the binary search tree in Figure B-4.1, redraw the tree after the sequence of operations is performed on the tree. Each sequence of operations is independent. [Berdasarkan rajah pepohon carian dedua dalam Rajah B-4.1, lukis semula pepohon tersebut setelah jujukan operasi dilaksanakan pada pepohon tersebut. Setiap jujukan operasi adalah tidak berkait.] [5 marks] i. Insert 10, 20 and 30. [Memasukkan nilai 10,20 dan 30.]

ii. Delete 18 and 15. [Menghapuskan nilai 18 dan 15.] iii. Insert 32, delete 6, insert 28 and delete 24. [Memasukkan 32, menghapus 6, memasukkan 28 dan menghapus 24.] iv. Identify the output displayed by the following function, where the tree in Figure B-4.1 is used as the input. [Kenalpasti output yang dicetak oleh fungsi berikut, yang mana pepohon pada Rajah B-4.1 digunakan sebagai input.] [4 marks]

void Print(TreeNode* tree) { if(tree != NULL) { cout << "L"; Print(tree->left); cout << "/" << tree->info <<" "; cout << "R"; Print(tree->right); } }

d) Write a recursive function called leafCount() to count the leaves in a binary tree. [Tulis fungsi recursive bernama leafCount() untuk mengira bilangan daun pada satu pepohon dedua.] [4 marks]

w
w

You might also like