Professional Documents
Culture Documents
David Machumi
David Machumi
i) Construct a balanced binary max-heap using the following elements, pushing them onto the
heap in the given order: [6]
7, 2, 1, 9, 12, 3, 14
step 1
2 1
3 14
9 12
step 2
12 1
9 2 3 14
Step 3
7
12
14
2 3 1
9
Final step
14
12 7
9 2 3 1
ii) Now pop the two largest elements off the heap. Draw the heap after each such
extraction. [2]
pop 14
14
12 7
9 2 3 1
step 1 to max-heap
1
12 7
9 2 3
step 2 to max-heap
12
1 7
9 2 3
Pop 12
12
9 7
1 2 3
step 1 to max-heap
9 7
1 2
9
final max-heap
3 7
1 2
Question 2
Give the pre-order, in-order, and post-order traversal of the following tree in figure 2.
[6]
Figure 2
Pre-order(path) : 3 1 4 5 8 6 2 20 12 10 11 30 21 30
In-order(path): 1 3 4 5 6 8 9 11 10 12 20 21 30 31
Post-order(path): 1 4 3 6 8 5 11 10 12 21 31 30 20 9
Question 3
Traverse the graph below in figure 3 using:
PATH: S, A, B, C, D, E, F, G
[5]
empty
Question 4
else:
Next = First + Second
First = Second
econd = Next
print(Next)
Question 5
Rewrite the code below correctly
if a>=b:
return a
else:
return b
def min(a,b):
if a<=b:
return a
else:
return b
a=input(“Enter a number: “)
b=input(“Enter a number: “)
print “The maximum number is: “
print (max)
print (“The minimum number is: “)
print (min) [10]
ANSWER:
def min(a,b):
if a<=b:
return a
else:
return b
a=input(“Enter a number: “)
b=input(“Enter a number: “)
print “The maximum number is: “
print (max)
print (“The minimum number is: “)
print (min)
if a>=b:
return a
else:
return b
Question 6
a) Describe the major data structures used in the following areas: RDBMS, Network data model
and Hierarchical data model (6)
ANSWER:
Array of structures is used majorly in RDBMS so that the data is maintained properly.
b) Compare the quick-sort and merge-sort algorithms in terms of their time and space
complexity.
[4]
In quick sort the pivot element is used for the sorting. In terms of speed of execution Quick sort
work faster for small data set like selection sort whereas merge sort does not use pivot element
for performing the sorting therefor merge sort has a consistent speed on any size of data.