December
1998 QUESTION 1 (Compulsory) Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS
|
(a) | Give two distinguishing characteristics (e.g., size, lookup and update policy/efficiency) for each of the following data-structures: | |
(i) linked-list | [2] | |
1 mark for each of the points above, to a maximum of 2 marks.
|
||
(ii) array | [2] | |
1 mark for each of the points above, to a maximum of 2 marks
|
||
(iii) FIFO queue | [2] | |
1 mark for each of the points above, to a maximum of 2 marks
|
||
(iv) LIFO queue | [2] | |
1 mark for each of the points above, to a maximum of 2 marks
|
||
(b) | Given the following tree:
|
|
(i) Write a Pascal type definition, called TreeType that models the tree
data-structure shown above.
|
[3] | |
Type TreeType =
^NodeType; NodeType = Record Left : TreeType; Right : TreeType; Up : TreeType; Data : TreeType; End; One mark should be awarded for having the correct structure to the record; one mark should be awarded for incorporating Data as an integer; one mark should be awarded for having Left, Right, and Up as correct pointers to tree types. The students should not be penalised for using different names for Data, Left, Right, Up, Down or NodeType. If the students obtain half marks for this part of the question, please round their mark up.
|
||
(ii) What will be printed when the tree shown above is passed to the procedure anonymous below? | [2] | |
PROCEDURE anonymous(VAR root:TreeType); VAR ptr,copy : TreeTypes; BEGIN ptr := root; WHILE (ptr <> Nil) DO BEGIN copy := ptr; IF (ptr^.Left <> Nil) THEN BEGIN ptr := ptr^.Left; copy^.Left := Nil; END ELSE BEGIN IF (ptrv.Right <> Nil) THEN BEGIN ptr := ptr^.Right; copy^.Right := Nil; END ELSE BEGIN WRITELN(ptr^.Data); ptr := ptr^.Up; END END END END; |
||
One mark for two lines of
numbers. Please award no half marks: 1916
|
||
(iii) Does the procedure anonymous print out the integers held in the tree in a prefix, infix, or postfix style? | [1] | |
Postfix style
|
||
(iv) What effect does the procedure have on the tree root? | [1] | |
The procedure destroys
the tree by overwriting the left and right fields of each node.
|
||
(v) Write a reursive procedure that performs a preorder traversal of the tree, printing out the integers held within the tree during the traversal. | [5] | |
A sample definition of
the pre-order recursive printing procedure is shown below: Procedure preOrder(root: Tree); and the following marking scheme should be used:
do not deduct marks for trivial syntactic errors when marking the above points. |