0% found this document useful (0 votes)
8 views9 pages

5th Daa

Uploaded by

mahesh kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

5th Daa

Uploaded by

mahesh kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment:- 5

5.1 WAP and algorithm for Binary search tree (Insertion ,deletion
,update,traversing).
Algorithm for Binary Search Tree Operations:
1. Insertion:
 If the tree is empty, the new node becomes the root.
 Otherwise, compare the value with the current node, and recursively insert it
into the left or right subtree based on comparison.
2. Deletion:
 If the node to be deleted has no children (leaf node), simply remove it.
 If the node has one child, replace it with its child.
 If the node has two children, find the in-order successor (smallest node in the
right subtree) or in-order predecessor (largest node in the left subtree) and
replace the node with it, then delete the successor/predecessor.
3. Update:
 Search for the node, and if found, simply modify its value.
4. Traversal:
 In-order: Visit the left subtree, then the current node, then the right subtree.
 Pre-order: Visit the current node, then the left subtree, then the right subtree.
 Post-order: Visit the left subtree, then the right subtree, then the current node.

Program:-
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) : data(value), left(nullptr), right(nullptr) {}
};

Cyber Security DAA Lab Manual July -Dec 2024


Node* insert(Node* root, int value) {
if (root == nullptr) {
return new Node(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
Node* search(Node* root, int value) {
if (root == nullptr || root->data == value) {
return root;
}
if (value < root->data) {
return search(root->left, value);
}
return search(root->right, value);
}
Node* deleteNode(Node* root, int value) {
if (root == nullptr) return root;

if (value < root->data) {


root->left = deleteNode(root->left, value);
} else if (value > root->data) {
root->right = deleteNode(root->right, value);
} else {
if (root->left == nullptr) {
Node* temp = root->right;

Cyber Security DAA Lab Manual July -Dec 2024


delete root;
return temp;
} else if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}

Node* temp = root->right;


while (temp && temp->left != nullptr) {
temp = temp->left;
}
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
Node* update(Node* root, int oldVal, int newVal) {
root = deleteNode(root, oldVal); // Delete the old value
return insert(root, newVal); // Insert the new value
}
void inorder(Node* root) {
if (root != nullptr) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
void preorder(Node* root) {
if (root != nullptr) {

Cyber Security DAA Lab Manual July -Dec 2024


cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node* root) {
if (root != nullptr) {
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
int main() {
cout<<"Prakhar Jain"<<endl;
cout<<"0827CY221043"<<endl;
Node* root = nullptr;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);

cout << "In-order traversal: ";


inorder(root); // Expected: 20 30 40 50 60 70 80
cout << endl;
cout << "Pre-order traversal: ";
preorder(root); // Expected: 50 30 20 40 70 60 80
cout << endl;

Cyber Security DAA Lab Manual July -Dec 2024


cout << "Post-order traversal: ";
postorder(root); // Expected: 20 40 30 60 80 70 50
cout << endl;
int searchVal = 40;
Node* result = search(root, searchVal);
if (result != nullptr) {
cout << "Found " << searchVal << " in the tree." << endl;
} else {
cout << "Value " << searchVal << " not found in the tree." << endl;
}
int deleteVal = 40;
root = deleteNode(root, deleteVal);
cout << "After deleting " << deleteVal << ", In-order traversal: ";
inorder(root); // Expected: 20 30 50 60 70 80
cout << endl;
int oldVal = 30, newVal = 25;
root = update(root, oldVal, newVal);
cout << "After updating " << oldVal << " to " << newVal << ", In-order traversal: ";
inorder(root); // Expected: 20 25 50 60 70 80
cout << endl;
return 0;
}
Output:-

Cyber Security DAA Lab Manual July -Dec 2024


5.2 WAP and algorithm for graph traversal(BFS and DFS).
1. Breadth-First Search (BFS)
BFS explores all the vertices of a graph level by level starting from the source vertex. It uses a
queue to keep track of vertices to be explored.
Algorithm for BFS:
1. Start from a source vertex and mark it as visited.
2. Enqueue the source vertex into a queue.
3. While the queue is not empty:
 Dequeue a vertex from the front of the queue.
 Visit all its unvisited adjacent vertices and enqueue them.
4. Repeat until all vertices are visited.
2. Depth-First Search (DFS)
DFS explores a graph as far as possible along each branch before backtracking. It uses a stack
(or recursion) to keep track of the vertices to be explored.
Algorithm for DFS:
1. Start from a source vertex and mark it as visited.
2. Visit each unvisited adjacent vertex and recursively perform DFS on them.
3. If all adjacent vertices are visited, backtrack to the previous vertex.
4. Repeat until all vertices are visited.
Program:-
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
class Graph {
public:
int V;
vector<vector<int>> adjList;
Graph(int V) {
this->V = V;

Cyber Security DAA Lab Manual July -Dec 2024


adjList.resize(V);
}
void addEdge(int u, int v) {
adjList[u].push_back(v);
adjList[v].push_back(u); // For undirected graph
}
void BFS(int start) {
vector<bool> visited(V, false);
queue<int> q;
visited[start] = true;
q.push(start);
cout << "BFS Traversal starting from vertex " << start << ": ";
while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << " ";
for (int v : adjList[u]) {
if (!visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
cout << endl;
}
void DFS(int start) {
vector<bool> visited(V, false);
cout << "DFS Traversal starting from vertex " << start << ": ";
DFSUtil(start, visited);
cout << endl;

Cyber Security DAA Lab Manual July -Dec 2024


}
void DFSUtil(int u, vector<bool>& visited) {
visited[u] = true;
cout << u << " ";
for (int v : adjList[u]) {
if (!visited[v]) {
DFSUtil(v, visited);
}
}
}
void DFSIterative(int start) {
vector<bool> visited(V, false);
stack<int> s;
visited[start] = true;
s.push(start);
cout << "DFS Iterative Traversal starting from vertex " << start << ": ";
while (!s.empty()) {
int u = s.top();
s.pop();
cout << u << " ";
for (int v : adjList[u]) {
if (!visited[v]) {
visited[v] = true;
s.push(v);
}
}
}
cout << endl;
}
};

Cyber Security DAA Lab Manual July -Dec 2024


int main() {
cout<<"Prakhar Jain"<<endl;
cout<<"0827CY221043"<<endl;
int V = 6; // Number of vertices
Graph g(V);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
g.BFS(0); // Expected BFS: 0 1 2 3 4 5
g.DFS(0); // Expected DFS (recursive): 0 1 3 4 2 5
g.DFSIterative(0); // Expected DFS (iterative): 0 1 3 4 2 5
return 0;
}
OUTPUT:-

Cyber Security DAA Lab Manual July -Dec 2024

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy