0% found this document useful (0 votes)
17 views

Dsa All Assign (5-10)

The document discusses the implementation of a graph data structure using both an adjacency matrix and adjacency list representation. It defines a Graph class that initializes the number of vertices and constructs empty adjacency matrix and list. Edges can be added by setting values in the matrix and adding vertices to lists. Methods print the matrix and list representations of a sample graph to demonstrate the structures.

Uploaded by

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

Dsa All Assign (5-10)

The document discusses the implementation of a graph data structure using both an adjacency matrix and adjacency list representation. It defines a Graph class that initializes the number of vertices and constructs empty adjacency matrix and list. Edges can be added by setting values in the matrix and adding vertices to lists. Methods print the matrix and list representations of a sample graph to demonstrate the structures.

Uploaded by

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

#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

Node* prev;

Node(int value) : data(value), next(nullptr), prev(nullptr) {}

class DoublyLinkedList {

private:

Node* head;

Node* tail;

public:

DoublyLinkedList() : head(nullptr), tail(nullptr) {}

void insertFront(int value) {

Node* newNode = new Node(value);

if (head == nullptr) {

head = tail = newNode;

} else {

newNode->next = head;

head->prev = newNode;

head = newNode;

void insertEnd(int value) {

Node* newNode = new Node(value);

if (tail == nullptr) {

head = tail = newNode;

} else {

newNode->prev = tail;
tail->next = newNode;

tail = newNode;

void deleteValue(int value) {

Node* current = head;

while (current != nullptr) {

if (current->data == value) {

if (current->prev != nullptr) {

current->prev->next = current->next;

} else {

head = current->next;

if (current->next != nullptr) {

current->next->prev = current->prev;

} else {

tail = current->prev;

delete current;

return;

current = current->next;

}}

void display() {

Node* current = head;

while (current != nullptr) {

cout << current->data << " ";

current = current->next;

cout << endl; }

};
int main() {

DoublyLinkedList dll;

dll.insertEnd(1);

dll.insertEnd(2);

dll.insertEnd(3);

dll.insertFront(0);

cout << "Doubly Linked List: ";

dll.display();

dll.deleteValue(2);

cout << "After deleting 2: ";

dll.display();

return 0;

OUTPUT –
Doubly Linked List: 0 1 2 3
After deleting 2: 0 1 3
Reversed List: 3 2 1 0
#include<iostream>
using namespace std;
class node{
public:
int data;
node*left,*right;
node(int val){
data = val;
left = NULL;
right = NULL;
}
};

node*insert_bst(node*root,int val){
if(root == NULL){
return new node(val);
}
if(val < root->data){
root->left = insert_bst(root->left, val);
}
else{
root->right = insert_bst(root->right,val);
}
return root;
}

void inorder(node*root){
if(root == NULL){
return;
}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);

int main(){
node*root = NULL;

root = insert_bst(root,5);
insert_bst(root,1);
insert_bst(root,3);
insert_bst(root,4);
insert_bst(root,2);
insert_bst(root,7);

inorder(root);
cout<<endl;
return 0;
}

OUTPUT –
123457
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* left;
Node* right;
bool isThreaded;

Node(int value) : data(value), left(nullptr), right(nullptr), isThreaded(false) {}


};

class ThreadedBinaryTree {
private:
Node* root;

public:
ThreadedBinaryTree() : root(nullptr) {}

void insert(int data) {


root = insertRecursive(root, data);
}

Node* insertRecursive(Node* current, int data) {


if (current == nullptr) {
return new Node(data);
}
if (data < current->data) {
current->left = insertRecursive(current->left, data);
current->isThreaded = false;
}
else if (data > current->data) {
current->right = insertRecursive(current->right, data);
current->isThreaded = false;
}

return current;
}

void createThreaded() {
Node* prev = nullptr;
createThreadedRecursive(root, prev);
}

void createThreadedRecursive(Node* current, Node*& prev) {


if (current == nullptr)
return;

createThreadedRecursive(current->left, prev);

if (prev != nullptr && prev->right == nullptr) {


prev->right = current;
prev->isThreaded = true;
}
prev = current;

createThreadedRecursive(current->right, prev);
}

void inOrderThreadedTraversal() {
Node* current = leftmost(root);

while (current != nullptr) {


cout << current->data << " ";
if (current->isThreaded)
current = current->right;
else
current = leftmost(current->right);
}
}

Node* leftmost(Node* node) {


if (node == nullptr)
return nullptr;

while (node->left != nullptr)


node = node->left;

return node;
}
};

int main() {
ThreadedBinaryTree threadedTree;
threadedTree.insert(4);
threadedTree.insert(2);
threadedTree.insert(6);
threadedTree.insert(1);
threadedTree.insert(3);
threadedTree.insert(5);
threadedTree.insert(7);

cout << "In-order threaded traversal: ";


threadedTree.createThreaded();
threadedTree.inOrderThreadedTraversal();
cout << endl;

return 0;
}

OUTPUT –
In-order threaded traversal: 1 2 3 4 5 6 7
#include <iostream>
#include <algorithm>
using namespace std;
class Node {
public:
int key;
int height;
Node* left;
Node* right;
Node(int k) : key(k), height(1), left(nullptr), right(nullptr) {}
};
class AVLTree {
private:
Node* root;
int getHeight(Node* node) {
if (node == nullptr) return 0;
return node->height;
}
int getBalanceFactor(Node* node) {
if (node == nullptr) return 0;
return getHeight(node->left) - getHeight(node->right);
}
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
y->height = 1 + max(getHeight(y->left), getHeight(y->right));
x->height = 1 + max(getHeight(x->left), getHeight(x->right));
return x;
}
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = 1 + max(getHeight(x->left), getHeight(x->right));
y->height = 1 + max(getHeight(y->left), getHeight(y->right));
return y;
}
Node* insert(Node* node, int key) {
if (node == nullptr) return new Node(key);
if (key < node->key) node->left = insert(node->left, key);
else if (key > node->key) node->right = insert(node->right, key);
else return node; // Duplicate keys are not allowed
node->height = 1 + max(getHeight(node->left), getHeight(node->right))
int balance = getBalanceFactor(node);
if (balance > 1) {
if (key < node->left->key)
return rightRotate(node);
else {
node->left = leftRotate(node->left);
return rightRotate(node);
}
}
if (balance < -1) {
if (key > node->right->key)
return leftRotate(node);
else {
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}
void preOrder(Node* node) {
if (node != nullptr) {
cout << node->key << " ";
preOrder(node->left);
preOrder(node->right);
}
}
public:
AVLTree() : root(nullptr) {}
void insert(int key) {
root = insert(root, key);
}
void displayPreOrder() {
preOrder(root);
cout << endl;
}
};
int main() {
AVLTree avl;
avl.insert(30);
avl.insert(20);
avl.insert(40);
avl.insert(10);
avl.insert(25);
avl.insert(5);

cout << "AVL Tree (Pre-order traversal): ";


avl.displayPreOrder();

return 0;
}

OUTPUT -

AVL Tree (Pre-order traversal): 20 9 7 6 10 15 40 30 35 45

AVL Tree (In-order traversal): 5 10 15 20 25 30 35 40 45

AVL Tree (Post-order traversal): 15 10 5 25 20 35 45 40 30


#include <iostream>
#include <vector>

using namespace std;

class Graph {
private:
int V; // Number of vertices
vector<vector<int>> adjacencyMatrix; // Adjacency matrix
vector<vector<int>> adjacencyList; // Adjacency list

public:
Graph(int vertices) : V(vertices) {
// Initialize the adjacency matrix with all zeros
adjacencyMatrix = vector<vector<int>>(V, vector<int>(V, 0));

// Initialize the adjacency list


adjacencyList = vector<vector<int>>(V, vector<int>());
}

void addEdge(int u, int v) {


// Add an edge in the adjacency matrix
adjacencyMatrix[u][v] = 1;
adjacencyMatrix[v][u] = 1;

// Add an edge in the adjacency list


adjacencyList[u].push_back(v);
adjacencyList[v].push_back(u);
}
void printAdjacencyMatrix() {
cout << "Adjacency Matrix:" << endl;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
cout << adjacencyMatrix[i][j] << " ";
}
cout << endl;
}
}

void printAdjacencyList() {
cout << "Adjacency List:" << endl;
for (int i = 0; i < V; i++) {
cout << "Vertex " << i << ": ";
for (int neighbor : adjacencyList[i]) {
cout << neighbor << " ";
}
cout << endl;
}
}
};

int main() {
Graph graph(5); // Create a graph with 5 vertices

// Add edges to the graph


graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(3, 4);
graph.addEdge(4, 2);

// Print the adjacency matrix and adjacency list


graph.printAdjacencyMatrix();
cout << endl;
graph.printAdjacencyList();

return 0;
}

OUPUT –
Adjacency Matrix:
01100
10110
11001
01001
00110

Adjacency List:
Vertex 0: 1 2
Vertex 1: 0 2 3
Vertex 2: 0 1 4
Vertex 3: 1 4
Vertex 4: 3 2
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
class set_operations{
public :
int pizza[1000];
int burger[1000];
int n,m;
void input(){
cout << "Enter the number of people who like burger : ";
cin >> n;
cout << "Enter the people who like burger :\n";
for(int i = 0;i < n;i++){
cin >> burger[i];
}
cout << "Enter the number of people who like pizza : ";
cin >> m;
cout << "Enter the people who like pizza : \n";
for(int i = 0;i < m;i++){
cin >> pizza[i];
}
sort(pizza,pizza+m);
sort(burger,burger+n);
}
void uni(){
vector<int> ans(2000);
auto it = set_union(pizza,pizza+m,burger,burger+n,ans.begin());
ans.resize(it-ans.begin());
cout << "People who eat pizza or burger or both are :\n";
for(auto i : ans){
cout << i << " ";
}
cout << endl;
}
void inter(){
vector<int>ans(2000);
auto it = set_intersection(pizza,pizza+m,burger,burger+n,ans.begin());
ans.resize(it-ans.begin());
cout << "People who eat pizza and burger both : \n";
for(auto i : ans){
cout << i << " ";
}
cout << endl;
}
void onlypizza(){
vector<int> ans(1000);
auto it = set_difference(pizza,pizza+m,burger,burger+n,ans.begin());
ans.resize(it-ans.begin());
cout << "People who eat only pizza are : \n";
for(auto i : ans){
cout << i << " ";
}
cout << endl;
}
void onlyburger(){
vector<int> ans(1000);
auto it = set_difference(burger,burger+n,pizza,pizza+m,ans.begin());
ans.resize(it-ans.begin());
cout << "People who eat only burger are : \n";
for(auto i : ans){
cout << i << " ";
}
cout << endl;
}
};
int main(){
set_operations s;
s.input();
while(1){
cout << "\nChoose operation to perform : ";
cout << "\n1.Set of people who like pizza or burger or both";
cout << "\n2.Set of people who like pizza and burger both";
cout << "\n3.Set of people who like only pizza";
cout << "\n4.Set of people who like only burger";
cout << "\n5.Exit\n";
int x;cin >> x;
switch (x) {
case 1:
s.uni();
break;
case 2 :
s.inter();
break;
case 3 :
s.onlypizza();
break;
case 4 :
s.onlyburger();
break;
case 5 :
return 0;
}
}
}

OUTPUT –
Enter the number of people who like burger :
4
Enter the people who like burger :
1
2
3
5
Enter the number of people who like pizza : 3
Enter the people who like pizza :
2
5
7
Choose operation to perform :
1.Set of people who like pizza or burger or both
2.Set of people who like pizza and burger both
3.Set of people who like only pizza
4.Set of people who like only burger
5.Exit
1
People who eat pizza or burger or both are :
12357

Choose operation to perform :


1.Set of people who like pizza or burger or both
2.Set of people who like pizza and burger both
3.Set of people who like only pizza
4.Set of people who like only burger
5.Exit
2
People who eat pizza and burger both :
25

Choose operation to perform :


1.Set of people who like pizza or burger or both
2.Set of people who like pizza and burger both
3.Set of people who like only pizza
4.Set of people who like only burger
5.Exit
3
People who eat only pizza are :
7

Choose operation to perform :


1.Set of people who like pizza or burger or both
2.Set of people who like pizza and burger both
3.Set of people who like only pizza
4.Set of people who like only burger
5.Exit
4
People who eat only burger are :
13

Choose operation to perform :


1.Set of people who like pizza or burger or both
2.Set of people who like pizza and burger both
3.Set of people who like only pizza
4.Set of people who like only burger
5.Exit
5
CODE:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Employee {
public:
string emp_id;
string name;
int age;
string department;
string post;
double salary;
void inputEmployeeData() {
cout << "Enter Employee ID: ";
cin >> emp_id;
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Employee Age: ";
cin >> age;
cout << "Enter Employee Department: ";
cin >> department;
cout << "Enter Employee Post: ";
cin >> post;
cout << "Enter Employee Salary: ";
cin >> salary;
}
void displayEmployeeData() {
cout << "Employee ID: " << emp_id << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << department << endl;
cout << "Post: " << post << endl;
cout << "Salary: " << salary << endl;
}
};
void addEmployeeRecord(const string& filename) {
Employee newEmployee;
newEmployee.inputEmployeeData();
ofstream file;
file.open("C:\\Users\\DELL\\Desktop\\C++ (DSA + OOPS)\\DSA\\FILEHANDLING.txt");
if (!file) {
cout << "Error opening the file for writing." << endl;
return;
}
file.write((char*)&newEmployee, sizeof(newEmployee));
file.close();
cout << "Employee record added successfully!" << endl;
}
void displayAllRecords(const string& filename) {
ifstream file;
file.open("C:\\Users\\DELL\\Desktop\\C++ (DSA + OOPS)\\DSA\\FILEHANDLING.txt");
if (!file) {
cout << "No records found." << endl;
return;
}
Employee employee;
while (file.read((char*)&employee, sizeof(employee))) {
employee.displayEmployeeData();
cout << endl;
}
file.close();
}
void searchEmployee(const string& filename, const string& empId) {
ifstream file;
file.open("C:\\Users\\DELL\\Desktop\\C++ (DSA + OOPS)\\DSA\\FILEHANDLING.txt");
if (!file) {
cout << "No records found." << endl;
return;
}
Employee employee;
bool found = false;
while (file.read((char*)&employee, sizeof(employee))) {
if (employee.emp_id == empId) {
cout << "Employee found:" << endl;
employee.displayEmployeeData();
found = true;
break;
}
}
if (!found) {
cout << "Employee not found." << endl;
}
file.close();
}
int main() {
string dbFilename = "FILEHANDLING.txt";
while (true) {
cout << "\nEmployee Database Management" << endl;
cout << "1. Add New Employee Record" << endl;
cout << "2. Display All Records" << endl;
cout << "3. Search for Employee" << endl;
cout << "4. Exit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addEmployeeRecord(dbFilename);
break;
case 2:
displayAllRecords(dbFilename);
break;
case 3:
{
string empId;
cout << "Enter Employee ID to search: ";
cin >> empId;
searchEmployee(dbFilename, empId);
}
break;
case 4:
return 0;
default: cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
OUTPUT:
Employee Database Management
1. Add New Employee Record
2. Display All Records
3. Search for Employee
4. Exit
Enter your choice: 1
Enter Employee ID: 123
Enter Employee Name: Madhur
Enter Employee Age: 19
Enter Employee Department: Comp
Enter Employee Post: student
Enter Employee Salary: 12345
Employee record added successfully!
Employee Database Management
1. Add New Employee Record
2. Display All Records
3. Search for Employee
4. Exit
Enter your choice: 2
Employee ID: 123
Name: Madhur
Age: 19
Department: Comp
Post: student
Salary: 12345

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