Professional Documents
Culture Documents
Q1
a) Define the following terms; [4 marks]
i. Data structure-a data structure is a way of organizing data that considers not only
the items stored, but also their relationship to each other. Advance knowledge
about the relationship between data items allows designing of efficient algorithms
for the manipulation of data
ii. Balanced binary tree –binary tree that is also balanced
iii. Graph-Graph is a collection of nodes or vertices connected together through edges
or arcs. Graphs are used to model electrical circuits, chemical compounds,
highway maps, and so on. They are also used in the analysis of electrical circuits,
finding the shortest route, project planning, linguistics, genetics, social science,
and so forth.
i. Heap-the parent always greater than both kids or vice versa
b) If you are requested to write an application to simulate the inbox of your mobile phone in
reference to arrival of new massages
i) Suggest the appropriate data structure you will use and justify answer [2 marks]
-stack
ii) What are the factors that will guide you to choose the particular data structure in
(i) above [2 marks]
-efficiency
-accuracy
-cost
else
top=top+1;
arr[top]=item;
c) Cite any three areas where queues can be applied in information systems
[3 marks]
-buffers
-storage mapping
-any other
Q2
a) Define a list ADT [2 marks]
b) Draw a flow chart to
[5 marks]
i) Add a new item X at position i
ii) Represent the array below in a binary tree. Perform a heap sort on it [5 marks]
[0] 19
[1] 60
- [2] 23 Draw the binary tree
- [3] 66 show all the steps of swapping and re heaping
[4] 30
[5] 8 d) Trace the steps of binary search for the key field 20 in the array
[6] 10 below
[4 marks]
70 63 52 8 10 12 60 56 32 30 14
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
M=(L+H)/2
M=(1+11)/2=6
Compare the key=20
Since 20<32 search on the left side that is
H=M-1
REAPAET THE ABOVE
Q3
a) i) Define a stack ADT [2 marks]
-FIFO
ii) Define the sequential search
[2 marks]
-The search in Linear Search starts at the beginning of an array and move to the
end, testing for a match at each item.
c) Trace the behavior of the following operation on (b) above PUSH (70) PUSH (80)
PUSH(90) POP() PUSH(77) POP() [3 marks]
B C
D E F G
H I J
Inorder : DHBEAFCIGJ
Preorder: ABDHECFGIJ
Postorder: HDEBFIJGCA
e) Write a pseudo code for a binary search tree. Assume the array is already sorted
[5 marks]
bool Binary_Search ( int *list, int size, int key, int* rec )
{
bool found = false;
int low = 0, high = size - 1;
while ( high >= low )
{
int mid = ( low + high ) / 2;
if ( key < list[mid] )
high = mid - 1;
else
if ( key > list[mid] )
low = mid + 1;
else
{
found = true;
rec = &list[mid];
break;
}
}
return found;
}
Q4
a) Given the equation (A+B)*C/F construct a binary tree and come up wit prefix,
infix and post fix notation
[6 marks]
*
+ /
A B C F
INFIX A+B*C/F
PREFIX *+AB/CF
POST FIX AB+CF/*
b) Write a C code to implement the POP and PUSH functions of stack ADT
[6 marks]
c) Define a binary search tree and outline four of its properties [6 marks]
[0] 36
[1] 24
[2] 10
[3] 6
[4] 12
d) The cost of sequential search id On and that of the binary search id Olog2n
Which faster between the two. Justify your answer [2 marks]
Q5
a) What are the advantages of linked lists? [4 marks]
Overflow can never occur unless the memory is actually full.
Insertions and deletions are easier than for contiguous (array) lists.
With large records, moving pointers is easier and faster than moving the items themselves
b) Explain any three desirable traits of a good algorithm [6 marks]
c) Suppose you delete items from an array based on implementation of list but do not
close the gaps. State any two problems that may arise [4 marks]
d) Given the tree below perform a down-heap followed by up-heap of value 10 and 3
4
[6 marks]
6 5
9
8
e) Define the following term in relation to trees using examples
i. Siblings [3 marks]
ii. Depth
iii. Leaf