December
1999 QUESTION 2 Total Marks: 15 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
(a) |
Describe what is meant by an ordered binary tree . In a ordered binary tree , •Left children of a parent node precede the parent node in sequence;(1 mark) •Right children of a parent node succeed the parent node in sequence.(1 mark)
|
[2] |
(b) |
Given the arithmetic expression (B ^ A )/(C + D ) (i)Draw a tree structure diagram to represent the expression;[2 marks ] The tree structure should appear as follows: And one mark should be deducted for each incorrectly represented subtree. Note particularly the positions of A and B . (ii)Convert the expression into Pre-fix notation;[2 marks ] The Pre-fix expression should read / ^ B A +C D And one mark should be deducted for each error. Note particularly the positions of A and B ,and the inclusion of the caret. (iii)Convert the expression into Post-fix notation.[2 marks ] The Post-fix expression should read: B A ^ C D + / And one mark should be deducted for each error. Note particularly the positions of A and B ,and the inclusion of the caret.
|
[6] |
(c) |
Given the following definition of a binary tree:
(i)Describe what is meant by an In-fix
(In-order)traversal of a binary •The left subtree is traversed, followed by the node,(1 mark) •and then the right subtree.(1 mark) (ii)Write a recursive procedure, the signature of which is given below,which takes in a binary tree of the type given above, and performs an In-fix traversal of the tree. The procedure should print out the value stored at each node as it is traversed, and sum the value stored at each node, returning the sum as a reference parameter [5 marks ] PROCEDURE InFixTraversalSum(root: BTreeNodePtr,
VAR Sum: A sample definition of InFixTraversalSum follows:
Write(root .Datum); IF(root
.Right<> NULL) THEN The following marking scheme should be used:
Recursing
with the left subtree before performing any action; Writing out the data after the left subtree recursion;(1 mark) And incrementing the Sum parameter;(1 mark) Then recursing with the right subtree.(1 mark) |
[7] |