Muhammad Asaad 22F-3879 3D Mam Rabia Anwar: Data Structure Lab 12 Name: Roll No: Section: Teacher
Muhammad Asaad 22F-3879 3D Mam Rabia Anwar: Data Structure Lab 12 Name: Roll No: Section: Teacher
Task 1:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
int height;
};
Node* rotateRight(Node* y)
{
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
Node* rotateLeft(Node* x)
{
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
// Left Left
if (balance > 1 && val < node->left->data)
return rotateRight(node);
// Right Right
if (balance < -1 && val > node->right->data)
return rotateLeft(node);
// Left Right
if (balance > 1 && val > node->left->data) {
node->left = rotateLeft(node->left);
return rotateRight(node);
}
// Right Left
if (balance < -1 && val < node->right->data) {
node->right = rotateRight(node->right);
return rotateLeft(node);
}
return node;
}
if (temp == nullptr) {
temp = root;
root = nullptr;
}
else // One child
*root = *temp;
delete temp;
}
else {
// Node with two children.. Getting inorder
successor (smallest in right subtree)
Node* temp = root->right;
while (temp->left != nullptr)
temp = temp->left;
root->data = temp->data;
// Deleting inorder successor
root->right = deleteNode(root->right, temp-
>data);
}
}
// One node
if (root == nullptr) return root;
// Left Left
if (balance > 1 && balanceFactor(root->left) >= 0)
return rotateRight(root);
// Left Right
if (balance > 1 && balanceFactor(root->left) < 0) {
root->left = rotateLeft(root->left);
return rotateRight(root);
}
// Right Right
if (balance < -1 && balanceFactor(root->right) <= 0)
return rotateLeft(root);
// Right Left
if (balance < -1 && balanceFactor(root->right) > 0) {
root->right = rotateRight(root->right);
return rotateLeft(root);
}
return root;
}
int main() {
Node* root = nullptr;
int comparisons = 0;
Node* found = search(root, 30, &comparisons);
if (found != nullptr)
{
cout << "Searching for 30.." << endl;
cout << "Element found after " << comparisons << "
comparisons." << endl<<endl;
}
else cout << "Element not found." << endl<<endl;
if (isAVL(root))
cout << "The tree is AVL." << endl;
else
cout << "The tree is not AVL." << endl;
system("pause;");
return 0;
}
Output:
Task 2:
#include <iostream>
class MinHeap {
private:
int* heapArray;
int capacity;
int heapSize;
void percolateUp(int i) {
int parentIndex = parent(i);
while (i > 0 && heapArray[parentIndex] > heapArray[i])
{
// Swap elements
int temp = heapArray[i];
heapArray[i] = heapArray[parentIndex];
heapArray[parentIndex] = temp;
i = parentIndex;
parentIndex = parent(i);
}
}
if (smallest != i) {
// Swapping
int temp = heapArray[i];
heapArray[i] = heapArray[smallest];
heapArray[smallest] = temp;
percolateDown(smallest);
}
}
public:
MinHeap(int cap) {
capacity = cap;
heapSize = 0;
heapArray = new int[capacity];
}
~MinHeap() {
delete[] heapArray;
}
int getMin() {
if (heapSize == 0) {
cout << "Heap is empty\n";
return -1;
}
return heapArray[0];
}
int extractMin()
{
if (heapSize <= 0) {
cout << "Heap is empty\n";
return -1;
}
if (heapSize == 1) {
heapSize--;
return heapArray[0];
}
return root;
}
heapSize++;
int i = heapSize - 1;
heapArray[i] = newVal;
percolateUp(i);
}
void remove(int i)
{
if (i < 0 || i >= heapSize) {
cout << "Index out of range\n";
return;
}
int* getHeapArray() {
return heapArray;
}
int getHeapSize() {
return heapSize;
}
};
int main() {
int capacity;
cout << "Enter the capacity of the min heap: ";
cin >> capacity;
MinHeap minHeap(capacity);
int choice;
do {
cout << "\nMin Heap Operations:" << endl;
cout << "1. Insert" << endl;
cout << "2. Extract Minimum" << endl;
cout << "3. Decrease Key" << endl;
cout << "4. Remove" << endl;
cout << "5. Get Minimum" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
cout << "Enter " << capacity << " values to
insert into the heap:" << endl;
for (int i = 0; i < capacity; ++i) {
int newVal;
cin >> newVal;
minHeap.insert(newVal);
}
break;
}
case 2: {
int min = minHeap.extractMin();
cout << "Minimum element extracted: " << min <<
endl;
case 3: {
int index, newVal;
cout << "Enter the index and new value to
decrease key: ";
cin >> index >> newVal;
minHeap.decreaseKey(index, newVal);
system("pause");
return 0;
}
Output:
Task 3:
#include <iostream>
using namespace std;
if (largest != i) {
swap(arr[i], arr[largest]);
// Recursively heapifying affected sub-tree..
MaxHeapify(arr, n, largest);
}
}
if (smallest != i) {
swap(arr[i], arr[smallest]);
MinHeapify(arr, n, smallest);
}
}
// Extracting elements
for (int i = n - 1; i > 0; i--) {
swap(arr[0], arr[i]);
MaxHeapify(arr, i, 0);
}
}
HeapSort(arr, n);
Output:
Task 4:
#include <iostream>
using namespace std;
if (smallest != i) {
swap(arr[i], arr[smallest]);
minHeapify(arr, n, smallest);
}
}
cout << "The " << k << " largest elements in the array are: ";
for (int i = 0; i < k; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = { 1, 23, 12, 9, 30, 2, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int k;
cout << "The array is: " << "1 6 12 9 35 2 50" << endl << endl;
cout << "Enter the number of largest elements you want to find: ";
cin >> k;
Output:
Task 5:
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
void inorderTraversal(TreeNode* root, int arr[], int& index)
{
if (root == nullptr) return;
inorderTraversal(root->left, arr, index);
arr[index++] = root->val;
inorderTraversal(root->right, arr, index);
}
TreeNode* makeBST()
{
TreeNode* root = nullptr;
int n, val;
cout << "Enter the number of nodes in BST: ";
cin >> n;
cout << "Enter the values of nodes: " << endl;
for (int i = 0; i < n; ++i) {
cin >> val;
root = buildBST(root, val);
}
return root;
}
int main() {
TreeNode* root = makeBST();
convertBSTtoMinHeap(root);
system("pause");
return 0;
}
Output:
Task 6:
#include <iostream>
using namespace std;
if (largest != i) {
swap(arr[i], arr[largest]);
maxHeapify(arr, n, largest);
}
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
convertMinHeapToMaxHeap(arr, n);
delete[] arr;
return 0;
}
Output:
Task 7:
#include <iostream>
using namespace std;
struct Task {
int taskID;
int priority;
int executionTime;
class PriorityQueue {
private:
Task* heap;
int capacity;
int size;
if (smallest != i) {
swap(heap[i], heap[smallest]);
minHeapify(smallest);
}
}
public:
PriorityQueue(int capacity) : capacity(capacity), size(0)
{
heap = new Task[capacity];
}
~PriorityQueue() {
delete[] heap;
}
// Removing task with highest priority (i.e., with lowest priority level)
Task extractMin() {
if (size <= 0) {
cerr << "Priority queue is empty. No task to extract." << endl;
return Task(-1, -1, -1); // dummy task
}
Task peek() {
if (size <= 0) {
cout << "Priority queue is empty. No task to peek." << endl;
return Task(-1, -1, -1);
}
return heap[0];
}
};
int main() {
PriorityQueue pq(10);
pq.insertTask(1, 5, 10);
pq.insertTask(2, 3, 8);
pq.insertTask(3, 7, 12);
pq.insertTask(4, 3, 9);
return 0;
}
Output: