BSEM-F20-113-Assignment No #4-DSA

You might also like

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

NAME:

Muhammad Zafar Jahangir

ROLL NO:

BSEM-F20-113

SECTION:

BSSE-3B

ASSIGNMENT:

#04

SUBMITTED TO:

SIR ARIF
Data Structures & Algorithms
Task 1. Write the program to create the following binary search
tree in C++.

Solution

#include <iostream>
using namespace std;

struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->key << " -> ";
inorder(root->right);
}
}
struct node *insert(struct node *node, int key) {
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
int main() {
struct node *root = NULL;
root = insert(root, 15);
root = insert(root, 8);
root = insert(root, 18);
root = insert(root, 10);
root = insert(root, 19);
root = insert(root, 12);
root = insert(root, 25);
root = insert(root, 30);
cout << "Inorder traversal: ";
inorder(root);
}

Task 2. Write the function to insert “16” in the binary search tree
you have created in task 1.

Solution:
#include <iostream>
using namespace std;

struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->key << " -> ";
inorder(root->right);
}
}
struct node *insert(struct node *node, int key) {
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
int main() {
struct node *root = NULL;
root = insert(root, 15);
root = insert(root, 18);
root = insert(root, 10);
root = insert(root, 12);
root = insert(root, 8);
root = insert(root, 25);
root = insert(root, 30);
root = insert(root, 19);
cout << "Inorder traversal: ";
inorder(root);
root = insert(root, 16);
cout<<endl;
cout << "Inorder traversal after adding 16 : ";
inorder(root);
}

Task 3. Write the function to search “16” in the binary search


tree that you have inserted in task 2.

Solution:
#include <iostream>
using namespace std;

struct node {
int key;
struct node *left, *right;
void ifNodeExists( node* node, int k)
{
if(node->key=k)
cout<<"value found";
else if (k<node->key)
ifNodeExists(node->left,k);
else
ifNodeExists(node->right,k);
}
};
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
struct node *insert(struct node *node, int key) {
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}

int main() {
struct node *root = NULL;
root = insert(root, 15);
root = insert(root, 8);
root = insert(root, 18);
root = insert(root, 10);
root = insert(root, 19);
root = insert(root, 12);
root = insert(root, 25);
root = insert(root, 30);
root = insert(root, 16);
node n;
n.ifNodeExists(root,16);
}

Task 4. Write the function for deleting a node:


(a) Case 1: Deleting a node with no children: remove the
node from the tree
Delete “16” from the tree below.

(b) Case 2: Deleting a node with two children


Delete “20” from the tree below.

Solution of both “a” & “b”:


#include <iostream>
using namespace std;

// Data structure to store a BST node


struct Node
{
int data;
Node* left = nullptr, *right = nullptr;

Node() {}
Node(int data): data(data) {}
};

// Function to perform inorder traversal on the BST


void inorder(Node* root)
{
if (root == nullptr) {
return;
}

inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

// Helper function to find the maximum value node in the subtree rooted at `ptr`
Node* findMaximumKey(Node* ptr)
{
while (ptr->right != nullptr) {
ptr = ptr->right;
}
return ptr;
}

// Recursive function to insert a key into a BST


Node* insert(Node* root, int key)
{
// if the root is null, create a new node and return it
if (root == nullptr) {
return new Node(key);
}

// if the given key is less than the root node, recur for the left subtree
if (key < root->data) {
root->left = insert(root->left, key);
}

// if the given key is more than the root node, recur for the right subtree
else {
root->right = insert(root->right, key);
}

return root;
}

// Function to delete a node from a BST. Note that root is passed by


// reference to the function
void deleteNode(Node* &root, int key)
{
// base case: the key is not found in the tree
if (root == nullptr) {
return;
}

// if the given key is less than the root node, recur for the left subtree
if (key < root->data) {
deleteNode(root->left, key);
}

// if the given key is more than the root node, recur for the right subtree
else if (key > root->data) {
deleteNode(root->right, key);
}

// key found
else {
// Case 1: node to be deleted has no children (it is a leaf node)
if (root->left == nullptr && root->right == nullptr)
{
// deallocate the memory and update root to null
delete root;
root = nullptr;
}

// Case 2: node to be deleted has two children


else if (root->left && root->right)
{
// find its inorder predecessor node
Node* predecessor = findMaximumKey(root->left);

// copy value of the predecessor to the current node


root->data = predecessor->data;

// recursively delete the predecessor. Note that the


// predecessor will have at most one child (left child)
deleteNode(root->left, predecessor->data);
}

// Case 3: node to be deleted has only one child


else {
// choose a child node
Node* child = (root->left)? root->left: root->right;
Node* curr = root;

root = child;

// deallocate the memory


delete curr;
}
}
}

int main()
{
int keys[] = { 15, 10, 20, 8, 12, 25 ,30,19,16};

Node* root = nullptr;


for (int key: keys) {
root = insert(root, key);
}
inorder(root);

deleteNode(root, 16);
cout<<"\nafter deleting 16 "<<endl;
inorder(root);

return 0;
}

Task 5.
Prim's Algorithm?
Solution:
Prim's Algorithm:
Prim's Algorithm is used to find the minimum spanning tree from a graph. Prim's algorithm
finds the subset of edges that includes every vertex of the graph such that the sum of the
weights of the edges can be minimized.
Prim's algorithm starts with the single node and explore all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.

How Prim's algorithm works?


Solution:
The algorithm is given as follows:

Algorithm:
 Step 1: Select a starting vertex
 Step 2: Repeat Steps 3 and 4 until there are fringe vertices
 Step 3: Select an edge e connecting the tree vertex and fringe vertex that has
minimum weight
 Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]
 Step 5: EXIT

Example of Prim's algorithm?


Solution:
Example:
Construct a minimum spanning tree of the graph given in the following figure by using prim's
algorithm.

Example Solution:
 Step 1: Choose a starting vertex B.
 Step 2: Add the vertices that are adjacent to A. the edges that connecting the vertices
are shown by dotted lines.
 Step 3: Choose the edge with the minimum weight among all. i.e. BD and add it to
MST. Add the adjacent vertices of D i.e. C and E.
 Step 3: Choose the edge with the minimum weight among all. In this case, the edges
DE and CD are such edges. Add them to MST and explore the adjacent of C i.e. E and
A.
 Step 4: Choose the edge with the minimum weight i.e. CA. We can't choose CE as it
would cause cycle in the graph.
The graph produces in the step 4 is the minimum spanning tree of the graph shown in the
above figure.
The cost of MST will be calculated as;
Cost(MST) = 4 + 2 + 1 + 3 = 10 units.
Which one is better Prims or Kruskal?
Solution:
Which algorithm is better Prims or Kruskal can Prim's and Kruskal's algorithm yield different
minimum spanning trees?
In Computer Science. Both Kruskal and Prim's algorithms solve the Minimum Spanning Tree
problem. Both Kruskal and Prim's algorithms solve the Minimum Spanning Tree problem.
Prim's algorithm is based on a theorem of light edges of a cut of a graph.
Prim's algorithm is significantly faster in the limit when you've got a really dense graph with
many more edges than vertices. Kruskal performs better in typical situations (sparse graphs)
because it uses simpler data structures.

Task 6.
What is the concept of negative edge?
Solution:
Negative edge, the name for a concept in fighting game theory for games such as Street
Fighter II, wherein a special move is made easier to execute by allowing the player to execute
such a move by using a joystick command followed by the release of a previously depressed
pushbutton.

You might also like