0% found this document useful (0 votes)
55 views

Data Structure

i) In-order traversal: 2, 1, 3, 14, 7, 10, 11, 30, 40. This visits the left subtree, then the root, then the right subtree. ii) Pre-order traversal: 14, 2, 1, 3, 7, 10, 11, 30, 40. This visits the root, then the left subtree, then the right subtree.

Uploaded by

Dennis Mutemi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Data Structure

i) In-order traversal: 2, 1, 3, 14, 7, 10, 11, 30, 40. This visits the left subtree, then the root, then the right subtree. ii) Pre-order traversal: 14, 2, 1, 3, 7, 10, 11, 30, 40. This visits the root, then the left subtree, then the right subtree.

Uploaded by

Dennis Mutemi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

NAME: MESHACK NYENZE

COURSE: BACHELOR OF SCIENCE IN INFORMATION


TECHNOLOGY

REG.NO. 14/02496

MODE OF STUDY: DISTANCE LEARNING

UNIT: BIT 3101DATA STRUCTURES AND ALGORITHMS

CAT 1
Discuss the steps involved in the development of an algorithm. (4 Marks)
i. Define the problem-clearly state what the algorithm will do.e.g problem could be: calculate
speed given time and didtance.

ii. Specify the inputs-declare input variables.e.g time, distance


iii. Specify the outputs-declare variables to store the rsults.e.g speed
iv. Specify the process-describe how inputs will be converted into outputs.one can use
expressions, formulas etc. e.g. speed=distance/time

v. Test the algorithm-using a pen and a paper go through the steps and plug in the inputs to
determine the output.

Distinguish between lists and arrays and provide a scenario where either is ideal in a
situation. (4 Marks)
Linked list is a complex data type that organize elements inform of a set of nodes where each
node has a pointer to some other node while an array is a built in data structure that contains a
set of elements of the same type stored in contiguous location.
Arrays are used to implement complex data structures e.g. hash tables
Linked list is used to create complex data structures e.g. binary tree

Discuss THREE operations performed in a stack. (3 Marks)


 Push-Add an element to the top of the stack
 Pop-Remove an element from the top of the stack
 Size-Returns the size of the stack
 Top-Returns the top element.
Write short notes on THREE Linear Data Structures. (3 Marks)

i. Linked lists
Linear complex data structure that stores values in nodes such that each node has data part
and a pointer to next node.

Applications:
 Used in memory management.
 Creating other complex data structures.

Implementation
Implemented using pointers and structures.

Operations-Insert, delete, traverse.

Types:-Singly-linked list, doubly linked list, circularly linked list.

ii. Stack ADT


Linear complex data structure that stores values to and removes values from the same end
called top. Follows last in, first out policy (LIFO).

Applications:
Used to store addresses, undo/redo, Convert infix expressions into postfix or prefix, stack of
cards.

Implementation: - Implemented using linked lists and arrays.

Operations: push, pop, size, is Empty, top.

iii. Queues
Linear complex data structure that stores values at one end called rear and removes values
from another end called front. Follows first in, first out policy (FIFO).

Applications: Used in memory management, job scheduling in OS, queue management


systems and creating other complex data structures.

Implementation: Implemented using linked lists and arrays.

Operations: Enqueue (push), dequeue (pop),size, isempty, front, rear/back.

Types: Deque, linear(simple) queue, priority queue, circular queue(ring buffer).


Given the following unsorted array [34, 56, 19, 41, 34, 51, 32, 14, 28, 33, 99, 109, 18].

a) Demonstrate how the following sorting algorithm will be used to sort the array in
ascending order.
i. Merge sort (3 Marks)
34 56 19 41 34 51 32

34 56 19 41 34 51 32

34 56 41 34 51 32

34 56 19 41 34 51 32

14 28 33 99 109 18

14 28 33 99 109 18

14 28 99 109

14 28 99 109 18
33

Sorted array: 14 18 19 28 32 33 34 34 41 51 56 99 109


ii. Quick sort ( 3 Marks)
P-pivot
L-left
R-right

P
34 56 19 41 34 51 32 14 28 33 99 109 18

L R
Case 1: 34<18 No
Swap
P
18 56 19 41 34 51 32 14 28 33 99 109 34

L R
CASE 2: 56<34 NO
Swap
P
18 34 19 41 34 51 32 14 28 33 99 109 56

L R
CASE 3: 99<56 NO
Swap P
18 34 19 41 34 51 32 14 28 33 56 109 99

L R

CASE 4: 109<99 NO
Swap P
18 34 19 41 34 51 32 14 28 33 56 99 109

L R
New array
P
18 34 19 41 34 51 32 14 28 33 56 99

LR
P
18 34 19 41 34 51 32 14 28 33 56

LR
P
18 34 19 41 34 51 32 14 28 33

L R
CASE: 18<14 NO
SWAP
P
14 18 19 28 32 33 34 34 41 51

LR

Sorted array: 14 18 19 28 32 33 34 34 41 51 56 99 109

b) Write a Program that implements each of the above algorithms. The program
accepts an array of n numbers ( such as the ones given above , outputs the sequence
of steps for each sorting step and prints the final output (6 Marks)

Marge sort
#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
//marge temp arrays to real array
while(i < nl && j<nr) {
if(larr[i] <= rarr[j]) {
array[k] = larr[i];
i++;
}else{
array[k] = rarr[j];
j++;
}
k++;
}
while(i<nl) { //extra element in left array
array[k] = larr[i];
i++; k++;
}
while(j<nr) { //extra element in right array
array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2;
// Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last index
cout << "Array after Sorting: ";
display(arr, n);
}
Consider the following binary tree below

Write the order of the above nodes visited in:


i) An in pre-order traversal (2 Marks)
1, 2,314,7,10,11,40,30

ii) A pre-order traversal (2 Mark)


14,2,1,3,11,10,7,30,40

You might also like