0% found this document useful (0 votes)
18 views10 pages

Dsa 18to20

Uploaded by

Kavita Rani
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)
18 views10 pages

Dsa 18to20

Uploaded by

Kavita Rani
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/ 10

PROGRAM :18

AIM: Program to traverse a binary search tree in pre-order, in-


order and post-order;
Source code:

#include <iostream>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;
};

// Insert function that creates and inserts nodes as needed


Node* insert(Node* root, int data) {
if (root == nullptr) {
root = new Node; // Allocate memory for a new node directly
root->data = data; // Assign data
root->left = root->right = nullptr; // Initialize children to nullptr
} else if (data < root->data) {
root->left = insert(root->left, data); // Insert in the left subtree
} else if (data > root->data) {
root->right = insert(root->right, data); // Insert in the right subtree
}
return root;
}
// Inorder traversal: Left -> Root -> Right
void inorder(node *root)
{
if (root == nullptr)
{

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

// Preorder traversal: Root -> Left -> Right


void preorder(node *root)
{
if (root == nullptr)
{

return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
// Postorder traversal: Left -> Right -> Root
void postorder(node *root)
{
if (root == nullptr)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
int main()
{
node *root = nullptr;
char choice;
int value;
cout << "\n :: Binary search tree ::\n 1.INSERT A NODE \n 2.INORDER
TRAVERSAL \n 3. PREORDER TRAVERSAL \n 4. POSTORDER TRAVERSAL \n
5.EXIT \n";
while (choice != 5)
{
cout << "ENter your choice";
cin >> choice;
switch (choice)
{
case '1':
cout << "Enter value to insert: ";
cin >> value;
root = insert(root, value);
cout << "Node inserted successfully!" << endl;
break;

case '2':
cout<<"\nINorder traversal :: ";
inorder(root);
break;
case '3':
cout<<"\nPreorder traversal ::";
preorder(root);
break;
case '4':
cout<<"\n Post-order traversal::";
postorder(root);
break;
case '5':
cout<<"\nEXIT THE PROGRAM\n";
return 0;
}
}
return 0;
}
OUTPUT:
PROGRAM :19
AIM: Program to traverse a Graph using BFS.
Source code:
#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;

// Add edge to the graph


void addEdge(map<char, vector<char>> &graph, char u, char v) {
graph[u].push_back(v);
graph[v].push_back(u); // For undirected graph
}

// Perform BFS traversal


void BFS(map<char, vector<char>> &graph, char start) {
map<char, int> visited; // Track visited vertices
queue<char> q;

q.push(start);
visited[start] = 1;

cout << "BFS Traversal: ";


while (!q.empty()) {
char node = q.front();
q.pop();
cout << node << " ";

for (char neighbor : graph[node]) {


if (!visited[neighbor]) {
q.push(neighbor);
visited[neighbor] = 1;
}
}
}
cout << endl;
}

int main() {
int edges;
char u, v, start;
map<char, vector<char>> graph;

cout << "Enter the number of edges: ";


cin >> edges;
for (int i = 0; i < edges; i++) {
cout << "Enter edge (u v): ";
cin >> u >> v;
addEdge(graph, u, v);
}

cout << "Enter starting vertex for BFS: ";


cin >> start;
BFS(graph, start);

return 0;
}OUTPUT:

PROGRAM :20
AIM: Program to traverse a Graph using DFS.
Source code:
#include <iostream>
#include <stack>
#include <map>
#include <vector>
using namespace std;

// Add edge to the graph


void addEdge(map<char, vector<char>> &graph, char u, char v) {
graph[u].push_back(v);
graph[v].push_back(u); // For undirected graph
}

// Perform DFS traversal


void DFS(map<char, vector<char>> &graph, char start) {
map<char, int> visited; // Track visited vertices
stack<char> s;

s.push(start);
visited[start] = 1;

cout << "DFS Traversal: ";


while (!s.empty()) {
char node = s.top();
s.pop();
cout << node << " ";

for (char neighbor : graph[node]) {


if (!visited[neighbor]) {
s.push(neighbor);
visited[neighbor] = 1;
}
}
}
cout << endl;
}

int main() {
int edges;
char u, v, start;
map<char, vector<char>> graph;

cout << "Enter the number of edges: ";


cin >> edges;
for (int i = 0; i < edges; i++) {
cout << "Enter edge (u v): ";
cin >> u >> v;
addEdge(graph, u, v);
}

cout << "Enter starting vertex for DFS: ";


cin >> start;
DFS(graph, start);

return 0;
}

OUTPUT:

You might also like