Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

CHINHOYI UNIVERSITY OF TECHNOLOGY

Name: David Machumi


Reg Number: C20141466L
Course Name: Data structures and Algorithm
Course Code: CUIT 205
PROGRAM: BSIT
Level: 2.2
Question 1

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:

(i) Depth First Traversal


(ii)Breadth First Traversal

PATH: S, A, B, C, D, E, F, G
[5]
empty

Question 4

Write a program which implements the Fibonacci series [6]


ANSWER:

Number = int(input("\nEnter the Range you want : "))


First = 0
Second = 1

for Num in range(0, Number):


if(Num <= 1):
Next = Num

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:

1) RDBMS= Array ( Array of structures)

Array of structures is used majorly in RDBMS so that the data is maintained properly.

2) Network data model = Graph


A database model that permits numerous records to be linked to the same owner file is known
as a network database model. The Data Model for Spatial and Graph Networks For vast,
complex networks, the graph feature can be used.

3) Hierarchical data model (Trees)

A hierarchical database model is one in which the information is structured in a tree-like


structure. The information is kept in the form of records that are linked together.

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.

You might also like