Data Structures and Algorithms - Kaunda - James
Data Structures and Algorithms - Kaunda - James
INTAKE TWO 3
ONE/TWO YEAR
LECTURER: MR SINKALA
ASSIGNMENT NO. 1
DAY/EVE/DL DL
ASSIGNMENT BRIEF
STUDENT INSTRUCTIONS:
1. This form must be attached to the front of your assignment
2. The assignment must be handed in without fail by the submission date (see assessment schedule for your course)
3. Ensure that the submission form is date stamped by the reception staff when you hand it in
4. Late submission will not be entertained unless with prior agreement with the subject tutor
5. All assessable assignments must be word processed
ASSIGNMENT GUIDANCE
Knowledge of theories
MARKS
(Administration only*)
LECTURERS FEEDBACK:
PART 1
*
SORTING ARRAYS
Using a program of your choice, Develop a program that asks the user to enter a capital for a
Zambian Province. Upon receiving the user input, the program reports whether the user input is
correct. For this application, the 10 Provinces and their capital cities are stored in a two-
dimensional array in order by Province name. Display the current contents of the array then use a
bubble sort to sort the content by capital. Next, prompt the user to enter answers for all the
province capitals and then display the total correct count. The user's answer is not case-sensitive.
import java.util.Scanner;
// Create a Scanner
if (isEqual(provincesAndCapitals[i][1], capital)) {
else {
provincesAndCapitals[i][1]);
if (c.length() != a.length())
return false;
if (c.charAt(i) != a.charAt(i))
return false;
return true;
/** getData initializes the array with the 10 provinces and their capitals */
{"Lusaka", "Lusaka"},
{"Southern", "Livingstone"},
{"Western", "Mongu"},
{"Eastern", "Chipata"},
{"Luapula", "Mansa"},
{"Northern", "Kasama"},
{"Muchinga", "Mpika"},
{"Copperbelt", "Ndola"},
{"Central", "Kabwe"}},
return d;
Part 2
(i) Discuss the concepts of Tree Traversing and give examples.
Tree Traversing is a type of graph traversal in the data structure that refers to the process of
visiting, verifying, and updating each node in a tree data structure just once. The order in which
to examine the nodes of the tree is used to classify these traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal
A Stack is a linear data structure that holds a linear, ordered sequence of elements. It is an
abstract data type.
A Stack can be a fixed specific size, or it can be dynamic, i.e., the Stack size can be changed
dynamically. It can be represented by means of Pointer, Array, Structure, and Linked List.
(ii) Differentiate the different data structures one from the other specifically in the way
data is inserted and deleted. Use relevant properties for each data structure to
differentiate.
A Data structure is a storage that is used to store and organize data. It is a way of arranging data
on a computer so that it can be accessed and updated efficiently
PROGRAMIZ : https://www.programiz.com/dsa/data-structure-types
8 Linear data structures are useful for Non-linear data structures are useful for
. simple data storage and manipulation. representing complex relationships and data
Linear Data Structure Non-linear Data Structure
#include <iostream>
#include <queue>
// Node structure
struct Node {
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = NULL;
right = NULL;
};
// Function to traverse binary tree using post-order traversal
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
if (node == NULL)
return node;
if (node == NULL)
return node;
if (val < node->data)
else {
if (node->left == NULL) {
free(node);
return temp;
free(node);
return temp;
temp = temp->left;
node->data = temp->data;
return node;
}
int main() {
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
postorderTraversal(root);
deleteNode(root, 20);
cout << "Binary Tree Traversal (Post-Order) after deleting 20: ";
postorderTraversal(root);
return 0;
Reference
JAVA TRAINING: https://www.javatpoint.com/
PROGRAMIZ : https://www.programiz.com/dsa/data-structure-types