Professional Documents
Culture Documents
class Node {
public:
int data;
Node* next;
Node* prev;
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
if (head == nullptr) {
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
if (tail == nullptr) {
} else {
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
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() {
current = current->next;
};
int main() {
DoublyLinkedList dll;
dll.insertEnd(1);
dll.insertEnd(2);
dll.insertEnd(3);
dll.insertFront(0);
dll.display();
dll.deleteValue(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;
class ThreadedBinaryTree {
private:
Node* root;
public:
ThreadedBinaryTree() : root(nullptr) {}
return current;
}
void createThreaded() {
Node* prev = nullptr;
createThreadedRecursive(root, prev);
}
createThreadedRecursive(current->left, prev);
createThreadedRecursive(current->right, prev);
}
void inOrderThreadedTraversal() {
Node* current = leftmost(root);
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);
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);
return 0;
}
OUTPUT -
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));
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
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