Shaurya Ds Assignment
Shaurya Ds Assignment
Assignment
Sahil Srivastava
9923103032
F2 Batch
Ans 1)
#include <iostream>
#include <queue>
#include <set>
using namespace std;
class MedianFinder {
private:
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
multiset<int> data;
public:
void insert(int num) {
data.insert(num);
if (maxHeap.empty() || num <= maxHeap.top()) {
maxHeap.push(num);
} else {
minHeap.push(num);
}
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.push(maxHeap.top());
maxHeap.pop();
} else if (minHeap.size() > maxHeap.size()) {
maxHeap.push(minHeap.top());
minHeap.pop();
}
}
double getMedian() {
if (maxHeap.size() > minHeap.size()) {
return maxHeap.top();
} else {
return (maxHeap.top() + minHeap.top()) / 2.0;
}
}
};
int main() {
MedianFinder mf;
int ch, num;
while (true) {
cout << "1. Insert\n2. Remove\n3. Get Median\n4. Exit\n";
cout << "Enter choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter number to insert: ";
cin >> num;
mf.insert(num);
break;
case 2:
cout << "Enter number to remove: ";
cin >> num;
mf.remove(num);
break;
case 3:
cout << "Median: " << mf.getMedian() << "\n";
break;
case 4:
return 0;
default:
cout << "Invalid choice. Try again.\n";
}
}
}
Ans 2)
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Node {
map<char, Node*>
next; bool endMarker =
false; int count = 0;
};
class SearchHelper {
private:
Node* base;
void gatherWords(Node* current, string buildPrefix,
vector<pair<string, int>>& results) {
if (!current) return;
if (current->endMarker)
results.push_back({buildPrefix, current->count});
for (auto& child : current->next)
gatherWords(child.second, buildPrefix +
child.first, results);
}
public:
SearchHelper() {base = new Node(); }
void addEntry(const string& term, int weight = 1) {
Node* pointer = base;
for (char symbol : term) {
if (!pointer->next[symbol]) pointer->next[symbol]
= new Node();
pointer = pointer->next[symbol];
}
pointer->endMarker =
true; pointer->count +=
weight;
}
vector<string> fetchMatches(const string& start) {
Node* pointer = base;
for (char symbol : start) {
if (!pointer->next[symbol]) return
{}; pointer = pointer->next[symbol];
}
vector<pair<string, int>> results;
gatherWords(pointer, start, results);
sort(results.begin(), results.end(), [](const pair<string,
int>& a, const pair<string, int>& b) {
if (a.second != b.second) return a.second > b.second;
return a.first < b.first;
});
vector<string> output;
for (auto& result : results)
output.push_back(result.first);
return output;}};
int main() {
SearchHelper helper;
int entries;
cout << "Number of terms to add: ";
cin >> entries;
for (int i = 0; i < entries; ++i) {
string term;
int weight;
cout << "Enter term and weight: ";
cin >> term >> weight;
helper.addEntry(term, weight);
}
while (true) {
string queryStart;
cout << "Enter query prefix (type 'stop' to exit):
";
cin >> queryStart;
if (queryStart == "stop") break;
vector<string> matches =
helper.fetchMatches(queryStart);
if (matches.empty()) {
cout << "No matches found.\n";
} else {
cout << "Matches:\n";
for (const string& match : matches)
cout << match << "\n";
}
}
return 0;
ANS 3)
CODE
#include <iostream>
using namespace std;
template <class T>
class queue {
private:
int size;
int front;
int rear;
T* arr;
public:
queue(int size) {
this->size = size;
front = rear = 0;
arr = new T[size];
}
bool isEmpty() { return (front == rear); }
bool isFull() { return ((rear + 1) % size == front); }
void enqueue(T val) {
if (isFull()) {
cout << "Queue is full! Cannot enqueue " <<
val << endl;
return;
}
rear = (rear + 1) % size;
arr[rear] = val;
}
T dequeue() {
if (isEmpty()) {
cout << "Queue is empty! Cannot dequeue."
<< endl;
return T();
}
front = (front + 1) % size;
return arr[front];
}
void display() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return;
}
cout << "Queue state: ";
for (int i = (front + 1) % size; i != (rear + 1) %
size; i = (i + 1) % size) {
cout << arr[i] << " ";
}
cout << endl;
}
int getSize() {
int count = (rear - front + size) % size;
return count;
}
};
int main() {
int n = 6;
queue<int> q(n + 1);
cout << "Enqueue tasks 1 to " << n << ":" << endl;
for (int i = 1; i <= n; i++)
q.enqueue(i);
q.display();
cout << "Processed tasks (dequeue 3): ";
for (int i = 0; i < 3; i++)
cout << q.dequeue() << " ";
cout << endl;
q.display();
cout << "Enqueue tasks " << n + 1 << " to " << n +
3 << ":" << endl;
for (int i = n + 1; i <= n + 3; i++)
q.enqueue(i);
q.display();
cout << "Processed tasks (dequeue 2): ";
for (int i = 0; i < 2; i++)
cout << q.dequeue() << " ";
cout << endl;
q.display();
cout << "Final queue state: ";
q.display();
cout << "Final queue size: " << q.getSize() << endl;
return 0;
}
Ans 4)
CODE
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
Node* list1 = new Node{10, new Node{20, new Node{30, new Node{40, new Node{50,
nullptr}}}}};
list1 = reverseList(list1);
insertAfter(list1, 20, 25);
list1 = deleteNode(list1, 40);
Node* list2 = new Node{100, new Node{200, new Node{300, nullptr}}};
Node* finalList = mergeLists(list1, list2);
int length = 0;
Node* current = finalList;
while (current) {
length++;
current = current->next;
}
3. Red nodes cannot have red children (no two consecutive red nodes).
4. Every path from a node to its descendant NULL pointers contains the
same number of black nodes (black-height property).
if (getColor(uncle) == RED) {
setColor(uncle, BLACK);
setColor(parent, BLACK);
setColor(grandparent, RED);
ptr = grandparent;
} else {
if (ptr == parent->left) {
rotateRight(parent);
ptr = parent;
parent = ptr->parent;
}
rotateLeft(grandparent);
swap(parent->color, grandparent->color);
ptr = parent;
}
}
}
setColor(root, BLACK);
}
inorderBST(ptr->left);
cout << ptr->data << " " << ptr->color <<
endl; inorderBST(ptr->right);
}
void RBTree::inorder() {
inorderBST(root);
}
void RBTree::insertValue(int n)
{ Node *node = new Node(n);
root = insertBST(root, node);
fixInsertRBTree(node);
}
AVL Tree
#include "bits/stdc++.h"
using namespace std;
struct AVLNode {
int value;
AVLNode *leftChild,
*rightChild; int height;
if (y->leftChild) {
y->leftChild = x;
}
x = y;
rotationCount++;
}
if (y->rightChild) {
y->rightChild = x;
}
x = y;
rotationCount++;
}
if (balance > 1) {
if (balanceFactor(node->leftChild) < 0) {
leftRotate(rootNode, node->leftChild);
}
rightRotate(rootNode, node);
} else if (balance < -1) {
if (balanceFactor(node->rightChild) > 0) {
rightRotate(rootNode, node->rightChild);
}
leftRotate(rootNode, node);
}
}
updateHeight(rootNode);
fixBalance(rootNode, rootNode);
}
public:
AVLTree() : rootNode(nullptr), rotationCount(0) {}
int getHeight() {
return height(rootNode);
}
int getRotationCount() {
return rotationCount;
}
AVLNode* getRoot()
{ return rootNode;
}
AVLNode* getLeftChild() {
return rootNode ? rootNode->leftChild : nullptr;
}
AVLNode* getRightChild() {
return rootNode ? rootNode->rightChild : nullptr;
}
};