Professional Documents
Culture Documents
BSEM-F20-113-Assignment No #4-DSA
BSEM-F20-113-Assignment No #4-DSA
BSEM-F20-113-Assignment No #4-DSA
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);
}
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);
}
Node() {}
Node(int data): data(data) {}
};
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;
}
// 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;
}
// 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;
}
root = child;
int main()
{
int keys[] = { 15, 10, 20, 8, 12, 25 ,30,19,16};
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.
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 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.