Ads Lab Manual
Ads Lab Manual
WEEK-1
Source Code:
A)
#include<iostream>
template<class t>
class bst
struct NODE
t data;
};
public:
createnewnode->data=data;
createnewnode->left=createnewnode->right=NULL;
return createnewnode;
if(root==NULL)
root=createnewnode(data);
}
else if(data<=root->data)
root->left=Insert(root->left,data);
else
root->right=Insert(root->right,data);
return root;
if(root!=NULL)
inorder(root->left);
cout<<root->data<<"";
inorder(root->right);
bst()
NODE* root=NULL;
t a,b,c,d,e,f;
cin>>a;
cin>>b;
cin>>c;
cin>>d;
cin>>e;
cin>>f;
root=Insert(root,a);
root=Insert(root,b);
root=Insert(root,c);
root=Insert(root,d);
root=Insert(root,e);
root=Insert(root,f);
cout<<"inorder:";
inorder(root);
cout<<endl;
insertion(root);
t n;
cin>>n;
root=Insert(root,n);
cout<<"inorder:";
inorder(root);
};
int main()
bst<int>a;
cout<<endl<<"-----------------"<<endl;
bst<char>b;
return 0;
Output:-
b)
#include <bits/stdc++.h>
struct Node
int data;
};
temp->data = data;
return temp;
temp1 = root;
if(root == NULL)
root = temp;
else
while(temp1 != NULL)
if(temp1->right == NULL)
temp1->right = temp;
break;
temp1 = temp1->right;
if(temp1->left == NULL)
temp1->left = temp;
break;
temp1 = temp1->left;
return root;
}
if (root != NULL)
display(root->left);
cout<<root->data<<" ";
display(root->right);
int pos = 0;
temp = root;
while(temp != NULL)
pos++;
if(temp->data == data)
return;
else
temp = temp->right;
return;
Node* Min(Node*root)
while(root->left!=NULL)
root=root->left;
return root;
if(root==NULL)
return root;
root->left= Delete(root->left,value);
root->right= Delete(root->right,value);
else
if(root->left==NULL&&root->right==NULL)
delete root;
root=NULL;
return root;
else if(root->left==NULL)
root=root->right;
delete temp;
return root;
else if(root->right==NULL)
root=root->left;
delete temp;
return root;
else
{
struct Node*temp=Min(root->right);
root->data=temp->data;
root->right=Delete(root->right,temp->data);
return root;
int main()
char ch;
int n, arr[20],size;
root = NULL;
cin>>size;
for(int i=0;i<size;i++)
cin>>arr[i];
Search(root, n);
Delete(root,n);
cout<<"\nElement Deleted"<<endl;
display(root);
cout<<endl;
return 0;
Output:-
c)
#include<iostream>
struct node {
int d;
node *left;
node *right;
};
node* CreateNode(int d) {
newnode->d = d;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
t = root;
if(root == NULL)
root = temp;
else {
while(t != NULL) {
if(t->d < d) {
if(t->right == NULL) {
t->right = temp;
break;
t = t->right;
if(t->left == NULL) {
t->left = temp;
break;
t = t->left;
}
return root;
int depth = 0;
temp = root;
while(temp != NULL) {
depth++;
if(temp->d == d) {
return;
temp = temp->left;
else
temp = temp->right;
return;
int main() {
char ch;
int n,c, i, a[10] = {93, 53, 45, 2, 7, 67, 32, 26, 71, 76};
root = NULL;
up:
cout<<"\nEnter the Element to be searched: ";
cin>>n;
Search(root, n);
cin>>ch;
goto up;
return 0;
Output:-
WEEK-2
a) Merge sort
b) Heap sort
c) Quick sort
SOURCE CODE:
a)
#include<iostream>
for(int i=0;i<subArrayOne;i++)
leftArray[i]=array[left+i];
rightArray[j]=array[mid+1+j];
int indexOfSubArrayOne=0;
int indexOfSubArrayTwo=0;
int indexOfMergedArray=left;
if(leftArray[indexOfSubArrayOne]<=rightArray[indexOfSubArrayTwo])
array[indexOfSubArrayOne]= rightArray[indexOfSubArrayTwo];
indexOfSubArrayOne++;
else
array[indexOfMergedArray]=rightArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
while(indexOfSubArrayOne<subArrayOne)
array[indexOfMergedArray]=leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
while(indexOfSubArrayTwo<subArrayTwo)
array[indexOfMergedArray]=rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
delete[] leftArray;
delete[] rightArray;
if(left<right)
int mid=left+(right-left)/2;
mergeSort(array,left,mid);
mergeSort(array,mid+1,right);
merge(array,left,mid,right);
cout<<array[i]<<"";
cout<<endl;
int main()
int array[]={38,27,43,3,9,82,10};
int size=sizeof(array) / sizeof(array[0]);
cout<<"Original array:";
printArray(array,size);
mergeSort(array,0,size-1);
cout<<"Sorted array:";
printArray(array,size);
return 0;
OUTPUT:-
b)
#include<iostream>
int largest=i;
int l=2*i+1;
int r=2*i+2;
largest=l;
largest=r;
if(largest!=i)
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
for(int i=n/2-1;i>=0;i--)
heapify(arr, n ,i);
swap(arr[0],arr[i]);
heapify(arr, i, 0);
for(int i=0;i<n;++i)
cout<<arr[i]<<"";
cout<<endl;
int main()
int arr[]={60,20,40,70,30,10};
int size=sizeof(arr)/sizeof(arr[0]);
cout<<"Original array:";
printArray(arr, size);
heapSort(arr, size);
cout<<"Sorted array:";
printArray(arr,size);
return 0;
OUTPUT:-
c)
#include <iostream>
int count = 0;
count++;
swap(arr[pivotIndex], arr[start]);
i++;
j--;
swap(arr[i++], arr[j--]);
return pivotIndex;
return;
quickSort(arr, p + 1, end);
}
int main()
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
return 0;
OUTPUT:-
WEEK-3
SOURCE CODE:
a)
#include <iostream>
class Node {
int *keys;
int t;
Node **C;
int n;
bool leaf;
public:
void traverse();
};
class BTree {
Node *root;
int t;
public:
BTree(int _t) {
root = NULL;
t = _t;
void traverse() {
if (root != NULL)
root->traverse();
t = t1;
leaf = leaf1;
n = 0;
void Node::traverse() {
int i;
if (leaf == false)
C[i]->traverse();
if (leaf == false)
C[i]->traverse();
void BTree::insert(int k) {
if (root == NULL) {
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2 * t - 1) {
Node *s = new Node(t, false);
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
root = s;
} else
root->insertNonFull(k);
void Node::insertNonFull(int k) {
int i = n - 1;
if (leaf == true) {
keys[i + 1] = keys[i];
i--;
keys[i + 1] = k;
n = n + 1;
} else {
i--;
if (C[i + 1]->n == 2 * t - 1) {
if (keys[i + 1] < k)
i++;
C[i + 1]->insertNonFull(k);
z->n = t - 1;
if (y->leaf == false) {
y->n = t - 1;
C[j + 1] = C[j];
C[i + 1] = z;
keys[j + 1] = keys[j];
keys[i] = y->keys[t - 1];
n = n + 1;
int main() {
BTree t(3);
t.insert(8);
t.insert(9);
t.insert(10);
t.insert(11);
t.insert(15);
t.insert(16);
t.insert(17);
t.insert(18);
t.insert(20);
t.insert(23);
t.traverse();
OUTPUT:-
b)
#include <iostream>
class BTreeNode {
int *keys;
int t;
BTreeNode **C;
int n;
bool leaf;
public:
void traverse();
};
class BTree {
BTreeNode *root;
int t;
public:
BTree(int _t) {
root = NULL;
t = _t;
void traverse() {
if (root != NULL)
root->traverse();
};
t = t1;
leaf = leaf1;
n = 0;
int BTreeNode::findKey(int k) {
int idx = 0;
++idx;
return idx;
}
void BTreeNode::deletion(int k) {
if (leaf)
removeFromLeaf(idx);
else
removeFromNonLeaf(idx);
} else {
if (leaf) {
cout << "The key " << k << " is does not exist in the tree\n";
return;
if (C[idx]->n < t)
fill(idx);
C[idx - 1]->deletion(k);
else
C[idx]->deletion(k);
return;
keys[i - 1] = keys[i];
n--;
return;
int k = keys[idx];
if (C[idx]->n >= t) {
keys[idx] = pred;
C[idx]->deletion(pred);
keys[idx] = succ;
C[idx + 1]->deletion(succ);
else {
merge(idx);
C[idx]->deletion(k);
return;
while (!cur->leaf)
cur = cur->C[cur->n];
while (!cur->leaf)
cur = cur->C[0];
return cur->keys[0];
borrowFromPrev(idx);
borrowFromNext(idx);
else {
if (idx != n)
merge(idx);
else
merge(idx - 1);
return;
child->keys[i + 1] = child->keys[i];
if (!child->leaf) {
child->C[i + 1] = child->C[i];
if (!child->leaf)
child->C[0] = sibling->C[sibling->n];
child->n += 1;
sibling->n -= 1;
return;
child->keys[(child->n)] = keys[idx];
if (!(child->leaf))
child->C[(child->n) + 1] = sibling->C[0];
keys[idx] = sibling->keys[0];
sibling->keys[i - 1] = sibling->keys[i];
if (!sibling->leaf) {
sibling->C[i - 1] = sibling->C[i];
child->n += 1;
sibling->n -= 1;
return;
child->keys[t - 1] = keys[idx];
child->keys[i + t] = sibling->keys[i];
if (!child->leaf) {
child->C[i + t] = sibling->C[i];
}
for (int i = idx + 1; i < n; ++i)
keys[i - 1] = keys[i];
C[i - 1] = C[i];
child->n += sibling->n + 1;
n--;
delete (sibling);
return;
void BTree::insertion(int k) {
if (root == NULL) {
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2 * t - 1) {
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
root = s;
} else
root->insertNonFull(k);
void BTreeNode::insertNonFull(int k) {
int i = n - 1;
if (leaf == true) {
keys[i + 1] = keys[i];
i--;
keys[i + 1] = k;
n = n + 1;
} else {
i--;
if (C[i + 1]->n == 2 * t - 1) {
if (keys[i + 1] < k)
i++;
C[i + 1]->insertNonFull(k);
}
void BTreeNode::splitChild(int i, BTreeNode *y) {
z->n = t - 1;
if (y->leaf == false) {
y->n = t - 1;
C[j + 1] = C[j];
C[i + 1] = z;
keys[j + 1] = keys[j];
n = n + 1;
void BTreeNode::traverse() {
int i;
if (leaf == false)
C[i]->traverse();
if (leaf == false)
C[i]->traverse();
void BTree::deletion(int k) {
if (!root) {
return;
root->deletion(k);
if (root->n == 0) {
if (root->leaf)
root = NULL;
else
root = root->C[0];
delete tmp;
return;
int main() {
BTree t(3);
t.insertion(8);
t.insertion(9);
t.insertion(10);
t.insertion(11);
t.insertion(15);
t.insertion(16);
t.insertion(17);
t.insertion(18);
t.insertion(20);
t.insertion(23);
t.traverse();
t.deletion(20);
t.traverse();
OUTPUT:-
c)
#include <iostream>
const int t = 3;
class BTreeNode {
public:
int t; // Minimum degree (defines the range for the number of keys)
BTreeNode** C; // Array of child pointers
BTreeNode(bool leaf);
void traverse();
};
class BTree {
public:
BTree() {
root = NULL;
this->t = ::t;
void traverse() {
if (root != NULL)
root->traverse();
}
BTreeNode* search(int k) {
};
BTreeNode::BTreeNode(bool leaf) {
this->leaf = leaf;
n = 0;
void BTreeNode::traverse() {
int i;
if (!leaf)
C[i]->traverse();
if (!leaf)
C[i]->traverse();
BTreeNode* BTreeNode::search(int k) {
int i = 0;
i++;
if (keys[i] == k)
return this;
if (leaf)
return NULL;
return C[i]->search(k);
void BTree::insert(int k) {
if (root == NULL) {
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2 * t - 1) {
s->C[0] = root;
s->keys[j - t] = root->keys[j];
s->n = t - 1;
root = s;
while (!cur->leaf) {
cur = cur->C[idx];
cur->keys[cur->n] = k;
cur->n++;
int main() {
BTree t;
t.insert(10);
t.insert(20);
t.insert(5);
t.insert(6);
t.insert(12);
t.insert(30);
t.insert(7);
t.insert(17);
t.traverse();
int key = 6;
if (t.search(key) != NULL)
cout << "Key " << key << " found in the B-Tree.\n";
else
cout << "Key " << key << " not found in the B-Tree.\n";
key = 15;
if (t.search(key) != NULL)
cout << "Key " << key << " found in the B-Tree.\n";
else
cout << "Key " << key << " not found in the B-Tree.\n";
return 0;
OUTPUT:-
WEEK-4:
SOURCE CODE:
a)
#include <iostream>
#include <vector>
#include <cmath>
class MinMaxHeap {
private:
vector<int> heap;
if (index == 0) return;
if (isMinLevel(index)) {
swap(heap[index], heap[parent]);
bubbleUpMax(parent);
} else {
bubbleUpMin(index);
} else {
swap(heap[index], heap[parent]);
bubbleUpMin(parent);
} else {
bubbleUpMax(index);
swap(heap[index], heap[grandparent]);
bubbleUpMin(grandparent);
}
swap(heap[index], heap[grandparent]);
bubbleUpMax(grandparent);
public:
heap.push_back(value);
bubbleUp(heap.size() - 1);
void printHeap() {
};
int main() {
MinMaxHeap heap;
heap.insert(10);
heap.insert(20);
heap.insert(5);
heap.insert(30);
heap.insert(15);
heap.insert(40);
heap.insert(1);
heap.printHeap();
return 0;
OUTPUT:-
b)
#include <iostream>
#include <vector>
#include <cmath>
class MinMaxHeap {
private:
vector<int> heap;
}
void trickleDownMin(int index) {
smallest = i;
smallest = i;
if (smallest != index) {
swap(heap[smallest], heap[index]);
swap(heap[smallest], heap[parent]);
trickleDownMin(smallest);
} else { // Child
swap(heap[smallest], heap[index]);
largest = i;
if (largest != index) {
swap(heap[largest], heap[index]);
swap(heap[largest], heap[parent]);
trickleDownMax(largest);
} else { // Child
swap(heap[largest], heap[index]);
if (isMinLevel(index))
trickleDownMin(index);
else
trickleDownMax(index);
public:
bubbleUp(heap.size() - 1);
if (index == 0) return;
if (isMinLevel(index)) {
swap(heap[index], heap[parent]);
bubbleUpMax(parent);
} else {
bubbleUpMin(index);
} else {
swap(heap[index], heap[parent]);
bubbleUpMin(parent);
} else {
bubbleUpMax(index);
bubbleUpMin(grandparent);
swap(heap[index], heap[grandparent]);
bubbleUpMax(grandparent);
if (heap[i] == value) {
index = i;
break;
if (index == -1) {
cout << "Element " << value << " not found in the heap.\n";
return;
heap[index] = heap.back();
heap.pop_back();
trickleDown(index);
void printHeap() {
};
int main() {
MinMaxHeap heap;
heap.insert(10);
heap.insert(20);
heap.insert(5);
heap.insert(30);
heap.insert(15);
heap.insert(40);
heap.insert(1);
heap.printHeap();
heap.deleteElement(15);
cout << "Heap after deletion: ";
heap.printHeap();
heap.deleteElement(1);
heap.printHeap();
heap.deleteElement(100);
return 0;
OUTPUT:-
c)
#include <iostream>
#include <vector>
#include <cmath>
class MinMaxHeap {
private:
vector<int> heap;
bool isMinLevel(int index) {
if (index == 0) return;
if (isMinLevel(index)) {
swap(heap[index], heap[parent]);
bubbleUpMax(parent);
} else {
bubbleUpMin(index);
} else {
swap(heap[index], heap[parent]);
bubbleUpMin(parent);
} else {
bubbleUpMax(index);
swap(heap[index], heap[grandparent]);
bubbleUpMin(grandparent);
swap(heap[index], heap[grandparent]);
bubbleUpMax(grandparent);
public:
heap.push_back(value);
bubbleUp(heap.size() - 1);
if (heap[i] == value) {
return true;
return false;
}
void printHeap() {
};
int main() {
MinMaxHeap heap;
heap.insert(10);
heap.insert(20);
heap.insert(5);
heap.insert(30);
heap.insert(15);
heap.insert(40);
heap.insert(1);
heap.printHeap();
cout << "Searching for " << search1 << ": ";
if (heap.searchElement(search1)) {
} else {
if (heap.searchElement(search2)) {
} else {
return 0;
OUTPUT:-
WEEK-5
SOURCE CODE:-
a)
#include <iostream>
struct Node {
int key;
Node* left;
Node* right;
};
class LeftistTree {
private:
Node* root;
swap(h1->left, h1->right);
return h1;
public:
LeftistTree() : root(NULL) {}
inOrder(node->left);
inOrder(node->right);
void printTree() {
inOrder(root);
Node* getRoot() {
return root;
};
int main() {
LeftistTree tree;
tree.insert(10);
tree.insert(20);
tree.insert(5);
tree.insert(30);
tree.insert(15);
tree.printTree();
return 0;
OUTPUT:-
b)
#include <iostream>
struct Node {
int key;
Node* left;
Node* right;
};
class LeftistTree {
private:
Node* root;
swap(h1->left, h1->right);
}
h1->rank = (h1->right ? h1->right->rank : 0) + 1;
return h1;
public:
LeftistTree() : root(NULL) {}
void deleteMin() {
if (!root) {
return;
delete oldRoot;
if (!node) return;
inOrder(node->left);
inOrder(node->right);
}
void printTree() {
if (!root) {
return;
inOrder(root);
};
int main() {
LeftistTree tree;
tree.insert(10);
tree.insert(20);
tree.insert(5);
tree.insert(30);
tree.insert(15);
tree.printTree();
tree.deleteMin();
tree.printTree();
tree.deleteMin();
tree.printTree();
return 0;
OUTPUT:-
c)
#include <iostream>
struct Node {
int key;
Node* left;
Node* right;
};
class LeftistTree {
private:
Node* root;
swap(h1->left, h1->right);
return h1;
public:
LeftistTree() : root(NULL) {}
}
void inOrder(Node* node) {
if (!node) return;
inOrder(node->left);
inOrder(node->right);
void printTree() {
if (!root) {
return;
inOrder(root);
};
int main() {
LeftistTree tree;
tree.insert(10);
tree.insert(20);
tree.insert(5);
tree.insert(30);
tree.insert(15);
tree.printTree();
int keyToSearch = 15;
cout << "Searching for key " << keyToSearch << ": ";
if (tree.searchKey(keyToSearch)) {
} else {
keyToSearch = 25;
cout << "Searching for key " << keyToSearch << ": ";
if (tree.searchKey(keyToSearch)) {
} else {
return 0;
OUTPUT:-
WEEK-6
SOURCE CODE:-
a)
#include <iostream>
#include <climits>
struct BinomialNode {
int key;
int degree;
BinomialNode* parent;
BinomialNode* child;
BinomialNode* sibling;
BinomialNode(int value) {
key = value;
degree = 0;
};
swap(tree1, tree2);
tree2->parent = tree1;
tree2->sibling = tree1->child;
tree1->child = tree2;
tree1->degree++;
return tree1;
BinomialNode* head;
head = heap1;
heap1 = heap1->sibling;
} else {
head = heap2;
heap2 = heap2->sibling;
tail->sibling = heap1;
heap1 = heap1->sibling;
} else {
tail->sibling = heap2;
heap2 = heap2->sibling;
tail = tail->sibling;
return head;
if (!head || !head->sibling)
return head;
while (next) {
if ((curr->degree != next->degree) ||
prev = curr;
curr = next;
} else {
curr->sibling = next->sibling;
} else {
if (prev)
prev->sibling = next;
else
head = next;
next = curr->sibling;
return head;
if (!node) return;
while (node) {
if (!node) return;
printTree(node->child);
printTree(node->sibling);
int main() {
printHeap(heap);
return 0;
OUTPUT:-
b)
#include <iostream>
#include <climits>
struct BinomialNode {
int key;
int degree;
BinomialNode* parent;
BinomialNode* child;
BinomialNode* sibling;
BinomialNode(int value) {
key = value;
degree = 0;
parent = NULL;
child = NULL;
sibling = NULL;
};
swap(b1, b2);
b2->parent = b1;
b2->sibling = b1->child;
b1->child = b2;
b1->degree++;
return b1;
h1 = h1->sibling;
} else {
h2 = h2->sibling;
tail->sibling = h1;
h1 = h1->sibling;
} else {
tail->sibling = h2;
h2 = h2->sibling;
tail = tail->sibling;
return head;
while (next) {
if (curr->degree != next->degree ||
prev = curr;
curr = next;
} else {
curr->sibling = next->sibling;
} else {
next = curr->sibling;
return merged;
BinomialNode* next;
while (curr) {
next = curr->sibling;
curr->sibling = prev;
prev = curr;
curr = next;
return prev;
while (curr->sibling) {
prevMin = curr;
minNode = curr->sibling;
curr = curr->sibling;
if (prevMin)
prevMin->sibling = minNode->sibling;
else
head = minNode->sibling;
BinomialNode* child = reverseHeap(minNode->child);
return minNode;
node->key = newKey;
swap(curr->key, parent->key);
curr = parent;
parent = curr->parent;
if (!node) return;
decreaseKey(node, INT_MIN);
delete minNode;
if (!head) return;
while (curr) {
curr = curr->child;
printHeap(head->sibling);
int main() {
printHeap(heap);
deleteNode(heap, nodeToDelete);
printHeap(heap);
return 0;
OUTPUT:-
c)
#include <iostream>
#include <climits>
struct BinomialNode {
int key;
int degree;
BinomialNode* parent;
BinomialNode* child;
BinomialNode* sibling;
swap(b1, b2);
b2->parent = b1;
b2->sibling = b1->child;
b1->child = b2;
b1->degree++;
return b1;
BinomialNode* res;
res = h1;
h1 = h1->sibling;
} else {
res = h2;
h2 = h2->sibling;
tail->sibling = h1;
h1 = h1->sibling;
} else {
tail->sibling = h2;
h2 = h2->sibling;
tail = tail->sibling;
return res;
while (next) {
if ((curr->degree != next->degree) ||
prev = curr;
curr = next;
} else {
curr->sibling = next->sibling;
} else {
next = curr->sibling;
return merged;
while (head) {
while (temp) {
temp = temp->sibling;
head = head->sibling;
}
int main() {
printBinomialHeap(head);
if (searchInBinomialHeap(head, key)) {
cout << "Element " << key << " found in the heap.\n";
} else {
cout << "Element " << key << " not found in the heap.\n";
return 0;
OUTPUT:-
WEEK-7
SOURCE CODE:-
a)
#include <iostream>
struct AVLNode {
int key;
int height;
AVLNode* left;
AVLNode* right;
};
AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
x->right = y;
y->left = T2;
return x;
AVLNode* leftRotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
return y;
else
return node;
return rightRotate(node);
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
}
void inorder(AVLNode* root) {
if (root) {
inorder(root->left);
inorder(root->right);
int main() {
inorder(root);
return 0;
OUTPUT:-
b)
#include <iostream>
struct Node {
int key;
Node* left;
Node* right;
int height;
};
if (!node) return 0;
if (node) {
Node* rotateRight(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
updateHeight(y);
updateHeight(x);
return x;
Node* rotateLeft(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
updateHeight(x);
updateHeight(y);
return y;
if (balance > 1) {
if (getBalanceFactor(node->left) < 0) {
node->left = rotateLeft(node->left);
return rotateRight(node);
if (getBalanceFactor(node->right) > 0) {
node->right = rotateRight(node->right);
return rotateLeft(node);
return node;
while (current->left) {
current = current->left;
}
return current;
if (!root) {
return root;
} else {
if (!root->left || !root->right) {
delete root;
return temp;
root->key = temp->key;
updateHeight(root);
return balanceNode(root);
if (!root) {
} else {
return root;
updateHeight(root);
return balanceNode(root);
if (root) {
inOrderTraversal(root->left);
inOrderTraversal(root->right);
int main() {
inOrderTraversal(root);
return 0;
OUTPUT:-
c)
#include <iostream>
struct AVLNode {
int key;
AVLNode* left;
AVLNode* right;
int height;
};
if (!node) return 0;
AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
x->right = y;
y->left = T2;
return x;
AVLNode* leftRotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
y->left = x;
x->right = T2;
return y;
} else {
return node;
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
if (key == root->key) {
return true;
} else {
if (root) {
inorderTraversal(root->left);
int main() {
inorderTraversal(root);
if (search(root, keyToSearch)) {
cout << "Element " << keyToSearch << " is found in the AVL tree.\n";
} else {
cout << "Element " << keyToSearch << " is not found in the AVL tree.\n";
keyToSearch = 15;
if (search(root, keyToSearch)) {
cout << "Element " << keyToSearch << " is found in the AVL tree.\n";
} else {
cout << "Element " << keyToSearch << " is not found in the AVL tree.\n";
return 0;
OUTPUT:-
WEEK-8
SOURCE CODE:-
a)
#include <iostream>
struct Node {
int data;
bool color;
Node* left;
Node* right;
Node* parent;
};
class RedBlackTree {
private:
Node* root;
node->right = nodeRight->left;
if (nodeRight->left != NULL) {
nodeRight->left->parent = node;
nodeRight->parent = node->parent;
if (node->parent == NULL) {
root = nodeRight;
node->parent->left = nodeRight;
} else {
node->parent->right = nodeRight;
nodeRight->left = node;
node->parent = nodeRight;
node->left = nodeLeft->right;
if (nodeLeft->right != NULL) {
nodeLeft->right->parent = node;
nodeLeft->parent = node->parent;
if (node->parent == NULL) {
root = nodeLeft;
node->parent->left = nodeLeft;
} else {
node->parent->right = nodeLeft;
nodeLeft->right = node;
node->parent = nodeLeft;
parent = node->parent;
grandparent = parent->parent;
if (parent == grandparent->left) {
grandparent->color = RED;
parent->color = BLACK;
uncle->color = BLACK;
node = grandparent;
} else {
if (node == parent->right) {
rotateLeft(root, parent);
node = parent;
parent = node->parent;
rotateRight(root, grandparent);
swap(parent->color, grandparent->color);
node = parent;
}
else {
grandparent->color = RED;
parent->color = BLACK;
uncle->color = BLACK;
node = grandparent;
} else {
if (node == parent->left) {
rotateRight(root, parent);
node = parent;
parent = node->parent;
rotateLeft(root, grandparent);
swap(parent->color, grandparent->color);
node = parent;
root->color = BLACK;
if (root == NULL) {
return node;
root->left->parent = root;
root->right->parent = root;
return root;
if (root == NULL) {
return;
inorderHelper(root->left);
cout << root->data << (root->color == RED ? " (R) " : " (B) ") << " ";
inorderHelper(root->right);
public:
RedBlackTree() : root(NULL) {}
fixViolation(root, node);
void inorder() {
inorderHelper(root);
};
int main() {
RedBlackTree tree;
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(15);
tree.insert(25);
tree.inorder();
return 0;
OUTPUT:-
b)
#include <iostream>
struct Node {
int data;
Color color;
};
class RedBlackTree {
private:
Node* root;
Node* TNULL; // Sentinel node to represent null leaves
void initializeTNULL() {
TNULL->color = BLACK;
TNULL->left = NULL;
TNULL->right = NULL;
void leftRotate(Node* x) {
Node* y = x->right;
x->right = y->left;
if (y->left != TNULL)
y->left->parent = x;
y->parent = x->parent;
if (x->parent == NULL)
root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
void rightRotate(Node* x) {
Node* y = x->left;
x->left = y->right;
if (y->right != TNULL)
y->right->parent = x;
y->parent = x->parent;
if (x->parent == NULL)
root = y;
else if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
y->right = x;
x->parent = y;
void fixDelete(Node* x) {
Node* s;
if (x == x->parent->left) {
s = x->parent->right;
if (s->color == RED) {
s->color = BLACK;
x->parent->color = RED;
leftRotate(x->parent);
s = x->parent->right;
s->color = RED;
x = x->parent;
} else {
if (s->right->color == BLACK) {
s->left->color = BLACK;
s->color = RED;
rightRotate(s);
s = x->parent->right;
}
s->color = x->parent->color;
x->parent->color = BLACK;
s->right->color = BLACK;
leftRotate(x->parent);
x = root;
} else {
s = x->parent->left;
if (s->color == RED) {
s->color = BLACK;
x->parent->color = RED;
rightRotate(x->parent);
s = x->parent->left;
s->color = RED;
x = x->parent;
} else {
if (s->left->color == BLACK) {
s->right->color = BLACK;
s->color = RED;
leftRotate(s);
s = x->parent->left;
s->color = x->parent->color;
x->parent->color = BLACK;
s->left->color = BLACK;
rightRotate(x->parent);
x = root;
}
}
x->color = BLACK;
if (u->parent == NULL)
root = v;
else if (u == u->parent->left)
u->parent->left = v;
else
u->parent->right = v;
v->parent = u->parent;
node = node->left;
return node;
Node* z = TNULL;
Node* x, * y;
if (node->data == key)
z = node;
node = node->right;
else
node = node->left;
}
if (z == TNULL) {
return;
y = z;
if (z->left == TNULL) {
x = z->right;
rbTransplant(z, z->right);
x = z->left;
rbTransplant(z, z->left);
} else {
y = minimum(z->right);
y_original_color = y->color;
x = y->right;
if (y->parent == z)
x->parent = y;
else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
rbTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
delete z;
if (y_original_color == BLACK)
fixDelete(x);
if (root != TNULL) {
if (last) {
} else {
cout << root->data << "(" << sColor << ")" << endl;
public:
RedBlackTree() {
initializeTNULL();
root = TNULL;
deleteNodeHelper(root, data);
node->parent = NULL;
node->data = key;
node->left = TNULL;
node->right = TNULL;
node->color = RED;
Node* y = NULL;
Node* x = root;
while (x != TNULL) {
y = x;
x = x->left;
else
x = x->right;
node->parent = y;
if (y == NULL)
root = node;
y->left = node;
else
y->right = node;
if (node->parent == NULL) {
node->color = BLACK;
return;
if (node->parent->parent == NULL)
return;
fixInsert(node);
void printTree() {
void fixInsert(Node* k) {
Node* u;
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
k = k->parent;
rightRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
leftRotate(k->parent->parent);
} else {
u = k->parent->parent->right;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
rightRotate(k->parent->parent);
if (k == root) break;
root->color = BLACK;
};
int main() {
RedBlackTree rbt;
rbt.insert(10);
rbt.insert(20);
rbt.insert(30);
rbt.insert(40);
rbt.insert(50);
rbt.printTree();
cout << "\nDeleting 30...\n";
rbt.deleteNode(30);
rbt.printTree();
rbt.deleteNode(10);
rbt.printTree();
return 0;
OUTPUT:-
c)
#include <iostream>
struct RBTreeNode {
int key;
Color color;
RBTreeNode* left;
RBTreeNode* right;
RBTreeNode* parent;
};
class RBTree {
private:
RBTreeNode* root;
RBTreeNode* TNULL;
public:
RBTree();
~RBTree();
};
RBTree::RBTree() {
TNULL->color = BLACK;
root = TNULL;
RBTree::~RBTree() {
delete TNULL;
}
void RBTree::leftRotate(RBTreeNode* x) {
RBTreeNode* y = x->right;
x->right = y->left;
if (y->left != TNULL) {
y->left->parent = x;
y->parent = x->parent;
if (x->parent == NULL) {
root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
y->left = x;
x->parent = y;
void RBTree::rightRotate(RBTreeNode* x) {
RBTreeNode* y = x->left;
x->left = y->right;
if (y->right != TNULL) {
y->right->parent = x;
y->parent = x->parent;
if (x->parent == NULL) {
root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
}
y->right = x;
x->parent = y;
void RBTree::fixInsert(RBTreeNode* k) {
RBTreeNode* u;
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
k = k->parent;
rightRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
leftRotate(k->parent->parent);
} else {
u = k->parent->parent->right;
if (u->color == RED) {
u->color = BLACK;
k->parent->color = BLACK;
k->parent->parent->color = RED;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
k->parent->color = BLACK;
k->parent->parent->color = RED;
rightRotate(k->parent->parent);
if (k == root) {
break;
root->color = BLACK;
node->parent = NULL;
node->key = key;
node->left = TNULL;
node->right = TNULL;
node->color = RED;
RBTreeNode* y = NULL;
RBTreeNode* x = root;
while (x != TNULL) {
y = x;
x = x->left;
} else {
x = x->right;
node->parent = y;
if (y == NULL) {
root = node;
y->left = node;
} else {
y->right = node;
if (node->parent == NULL) {
node->color = BLACK;
return;
if (node->parent->parent == NULL) {
return;
fixInsert(node);
if (key == current->key) {
return true;
current = current->left;
} else {
current = current->right;
}
}
return false;
if (node != TNULL) {
inOrderTraversal(node->left);
inOrderTraversal(node->right);
int main() {
RBTree tree;
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(40);
tree.insert(50);
tree.insert(25);
if (tree.search(key)) {
cout << "Element " << key << " found in the Red-Black Tree.\n";
} else {
cout << "Element " << key << " not found in the Red-Black Tree.\n";
tree.inOrderTraversal(tree.getRoot());
return 0;
}
OUTPUT:-
WEEK-9
SOURCE CODE:-
#include <iostream>
#include <cstring>
#include <cstdlib>
struct Node {
char* key;
char* value;
Node* next;
};
struct HashMap {
Node** arr;
};
mp->capacity = 100;
mp->numOfElements = 0;
mp->arr = (Node**)malloc(sizeof(Node*) * mp->capacity);
mp->arr[i] = NULL;
int bucketIndex;
bucketIndex = sum;
return bucketIndex;
node->next = NULL;
mp->arr[bucketIndex] = newNode;
} else {
newNode->next = mp->arr[bucketIndex];
mp->arr[bucketIndex] = newNode;
mp->numOfElements++;
if (strcmp(currNode->key, key) == 0) {
if (prevNode == NULL) {
mp->arr[bucketIndex] = currNode->next;
} else {
prevNode->next = currNode->next;
free(currNode->key);
free(currNode->value);
free(currNode);
mp->numOfElements--;
break;
prevNode = currNode;
currNode = currNode->next;
}
}
if (strcmp(bucketHead->key, key) == 0) {
return bucketHead->value;
bucketHead = bucketHead->next;
return errorMsg;
cout << "Key: " << node->key << ", Value: " << node->value << endl;
node = node->next;
int main() {
HashMap* mp = (HashMap*)malloc(sizeof(HashMap));
initializeHashMap(mp);
deleteKey(mp, "decentBoy");
node = node->next;
free(temp->key);
free(temp->value);
free(temp);
}
free(mp->arr);
free(mp);
return 0;
OUTPUT:-
SOURCE CODE:-
#include <bits/stdc++.h>
int M = strlen(pat);
int N = strlen(txt);
int lps[M];
// Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps);
if (pat[j] == txt[i]) {
j++;
i++;
if (j == M) {
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
// to search step.
if (len != 0) {
// i here
else // if (len == 0)
lps[i] = 0;
i++;
}
}
// Driver code
int main()
KMPSearch(pat, txt);
return 0;
OUTPUT:-
WEEK-10
SOURCE CODE:-
#include <iostream>
#include <string>
int n = text.length();
int m = pattern.length();
int j;
if (text[i + j] != pattern[j]) {
break;
}
}
if (j == m) {
int main() {
bruteForcePatternMatching(text, pattern);
return 0;
OUTPUT:-
SOURCE CODE:-
#include <bits/stdc++.h>
int badchar[NO_OF_CHARS])
{
int i;
badchar[i] = -1;
badchar[(int) str[i]] = i;
int m = pat.size();
int n = txt.size();
int badchar[NO_OF_CHARS];
badCharHeuristic(pat, m, badchar);
int s = 0;
int j = m - 1;
j--;
if (j < 0)
else
}
}
int main()
search(txt, pat);
return 0;
OUTPUT:-