Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cosc 325 Exam

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

COSC 325

CHUKA UNIVERSITY

UNIVERSITY EXAMINATIONS
EXAMINATION FOR THE AWARD OF DEGREE OF BACHELOR OF SCIENCE IN
COMPUTER SCIENCE
COSC 325: DATA STRUCTURES
STREAMS: BSC (COMPUTER SCIENCE) TIME: 2 HOURS
DAY/DATE: FRIDAY 07/12/2018 11.30 A.M. – 1.30 P.M.

INSTRUCTIONS:

QUESTION 1 (30 MARKS)

a) Distinguish between a queue and a stack. [4 marks]

b) Asymptotic notations are commonly used to describe the complexity of an algorithm with
respect to the input size n.
i) For each function f(n) below, give an asymptotic upper bound using big-Oh notation
i. f(n) = 10n3 - 7n3 + 14n2 [1 Mark
ii. f(n) = 10n3 - 10n3 + 7n2 [1 Mark]
2
iii. f(n) = log(7n ) [1 Mark]

ii) Describe the worst case running time of the following code in terms of the variable n.
Explain your answer; [3 Marks]
void looper (int n) {
for(int i=0; i < n; i++)
for(int j=0; j < 10; j++)
for(int k=0; k < n; k++)
for(int m=0; m < 10; m++)
cout<<("m !");
}
c) Describe briefly [4 marks]
i. Any one sorting algorithm and

ii. Any one searching algorithm

Page 1 of 3
COSC 325

d) Illustrate the operation of the sorting algorithm described in part (c) i above on the following
list of integers [4 marks]

57, 23, 11, 74, 39, 40, 65


e) The diagram below shows an array representation of a binary tree. Draw the tree. [3 marks]

D A T A S T R U C T U R E

f) Suppose we begin with an empty stack, and perform the following operations: push 7, push
2, push 9, push 6, pop, pop, peek, push 1, push 3, peek, push 8, pop, peek, pop, pop, push 5,
push 4, pop, pop, pop, push 8. What is contained on the stack when we are done? Write out
the contents from top to bottom. [4 marks]

g) Given two scenarios: the first in which a problem solution involves a dynamic list (i.e. list in
which there are a lot of deletions and insertions) and the second in which a problem involves
many accesses to the interior values of a list. State with reasons which data structure will be
suitable for each of the two scenarios? [3 marks]

h) Outline any two applications of the stack data structure [2 marks]

QUESTION 2(20 marks)


a) What is the function of the variable head when used with a linked list? What is the data type of the
head variable? [2 marks]
b) Draw a diagram of a linked list that contains nodes with data items of type String that contains the
name of a county and type double that contains a population index. Include an instance variable
named head to indicate the beginning of the list. Insert the following nodes: Kisumu, 15.7, Kilifi,
23.2, Tharaka, 7.2. [3 marks]
c) Create a generic Node class to represent the linked list depicted in your diagrams above.
[10 marks]
d) Write a method called displayList that displays the data items in the Node class created in number
(c) above. [5 marks]

QUESTION 3 (20 marks)


a) Define a binary tree [2 marks]
b) Distinguish between a binary search tree and a binary tree [3 Marks]
c) Draw the resulting binary search tree inserting the following values in the given order:

7, 10, 5, 12, 1, 3, 9. [5 marks]

d) Name the three traversal orders for binary trees and show the result of each of these traversals on
the above binary search tree. [6 marks]

Page 2 of 3
COSC 325

e) Describe any two methods for storing binary trees in the computer [4 marks]

QUESTION 4(20 marks)


a) Explain how a stack can be used to determine if an infix expression is correctly
parenthesized. [5 marks]
b) Describe how a stack can be used to solve postfix expressions. [3 marks]
c) Evaluate the following postfix expression: [4 marks]
7 4 5 1 - * 8 / 9 4 - + /
d) Describe one implementation strategy for a stack and one for a queue. [8 marks]

QUESTION 5 (20 marks)


a) Distinguish between a graph and a tree [2 marks]

b) Draw the directed graph that is represented by the following: [4 marks]


Vertices: 1, 2, 3, 4, 5, 6, 7
Edges: (1, 2), (1, 4), (2, 3), (2, 4), (3, 7), (4, 7), (4, 6), (5, 6), (5, 7), (6, 7)

c) Describe two principal methods for representing graphs for computer algorithms
[6 marks]
d) If a graph is sparse which representation will you use and why? [2 marks]

e) Consider the weighted graph given below:

5
a b

1
7 1

c d
2

Represent the weighted graph using the two representation methods described in part (c) above.
[6 marks]
---------------------------------------------------------------------------------------------------------------------

Page 3 of 3

You might also like