Dsa 18to20
Dsa 18to20
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
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;
q.push(start);
visited[start] = 1;
int main() {
int edges;
char u, v, start;
map<char, vector<char>> graph;
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;
s.push(start);
visited[start] = 1;
int main() {
int edges;
char u, v, start;
map<char, vector<char>> graph;
return 0;
}
OUTPUT: