0% found this document useful (0 votes)
7 views14 pages

Min Heap

The document contains implementations of three algorithms: a Min Heap, Huffman Algorithm, and Graph traversal using Depth-First Search (DFS) and Breadth-First Search (BFS). Each algorithm is encapsulated in classes and includes methods for insertion, deletion, searching, and traversal. The main function demonstrates the usage of these algorithms with sample data.

Uploaded by

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

Min Heap

The document contains implementations of three algorithms: a Min Heap, Huffman Algorithm, and Graph traversal using Depth-First Search (DFS) and Breadth-First Search (BFS). Each algorithm is encapsulated in classes and includes methods for insertion, deletion, searching, and traversal. The main function demonstrates the usage of these algorithms with sample data.

Uploaded by

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

MIN HEAP

------------------------------

#include<bits/stdc++.h>

using namespace std;

using ll = long long;

class Node{

public:

int data;

Node* left;

Node* right;

Node* parent;

Node(int val){

data=val;

left=NULL;

right=NULL;

parent=NULL;

};

class MinHeap{

public:

Node* root;

MinHeap(){

root=nullptr;

void insert(int val){

Node* newNode = new Node(val);


if(root==nullptr){

root=newNode;

return;

queue<Node*>q;

q.push(root);

Node* temp;

while(!q.empty()){

Node* curr = q.front();

q.pop();

if(curr->left==nullptr){

newNode->parent=curr;

curr->left=newNode;

temp = curr->left;

break;

else{

q.push(curr->left);

if(curr->right==nullptr){

newNode->parent=curr;

curr->right=newNode;

temp = curr->right;

break;

else{
q.push(curr->right);

while(temp->parent!=nullptr && temp->data<temp->parent->data){

swap(temp->data,temp->parent->data);

temp=temp->parent;

void search(int val){

if(root==nullptr){

cout<<"Heap is empty\n";

return;

queue<Node*>q;

q.push(root);

while(!q.empty()){

Node* curr = q.front();

q.pop();

if(curr->data==val){

cout<<"Element found\n";

return;

if(curr->left!=nullptr){

q.push(curr->left);

if(curr->right!=nullptr){
q.push(curr->right);

void del() {

if (root == nullptr) return;

if (root->left == nullptr && root->right == nullptr) {

delete root;

root = nullptr;

return;

queue<Node*> q;

q.push(root);

Node* lastNode = nullptr;

Node* parentOfLast = nullptr;

while (!q.empty()) {

Node* curr = q.front();

q.pop();

if(curr->left) {

q.push(curr->left);

if(curr->right) {

q.push(curr->right);

lastNode = curr;
}

parentOfLast = lastNode->parent;

swap(root->data, lastNode->data);

if(parentOfLast->right == lastNode) {

parentOfLast->right = nullptr;

}else{

parentOfLast->left = nullptr;

delete lastNode;

Node* temp = root;

while(true){

Node* temp2 = temp;

if(temp->left && temp->left->data < temp2->data)

temp2 = temp->left;

if(temp->right && temp->right->data < temp2->data)

temp2 = temp->right;

if(temp2 != temp) {

swap(temp->data, temp2->data);

temp = temp2;

}else{

break;

void traverse(){

queue<Node*>q;
q.push(root);

while(!q.empty()){

Node* curr = q.front();

q.pop();

cout<<curr->data<<" ";

if(curr->left!=nullptr){

q.push(curr->left);

if(curr->right!=nullptr){

q.push(curr->right);

cout<<"\n";

};

void dedsec(){

MinHeap minHeap;

minHeap.insert(10);

minHeap.insert(20);

minHeap.insert(5);

minHeap.insert(30);

minHeap.insert(15);

minHeap.insert(25);

minHeap.insert(35);

minHeap.insert(40);
minHeap.insert(50);

minHeap.insert(45);

minHeap.insert(55);

minHeap.traverse();

minHeap.del();

minHeap.traverse();

minHeap.search(20);

minHeap.search(100);

int main()

cin.tie(0)->sync_with_stdio(0);

ll t=1;

//cin >> t;

while(t--){

dedsec();

Output:
Huffman Algorithm

--------------------------------------

#include<bits/stdc++.h>

using namespace std;

using ll = long long;

class Node {

public:

char data;

int freq;

Node* left;

Node* right;

Node* parent;

Node(char data, int freq) {

this->data = data;

this->freq = freq;

left = right = parent = nullptr;

struct freqComp{

bool operator()(Node* l, Node* r) {

return l->freq > r->freq;

};

};

class MinHeap {
public:

priority_queue<Node*, vector<Node*>, Node::freqComp> pq;

void insert(Node* node) {

pq.push(node);

Node* Min() {

Node* minNode = pq.top();

pq.pop();

return minNode;

int size() {

return pq.size();

};

void generateCodes(Node* root, string str, map<char, string>& huffmanCodes) {

if (root == nullptr) return;

if (root->left == nullptr && root->right == nullptr) {

huffmanCodes[root->data] = str;

generateCodes(root->left, str + "0", huffmanCodes);

generateCodes(root->right, str + "1", huffmanCodes);

Node* buildHuffmanTree(string& text) {

map<char, int> freq;

for (char ch:text) {


freq[ch]++;

MinHeap minHeap;

for (auto &[x,y]: freq) {

Node* newNode = new Node(x, y);

minHeap.insert(newNode);

while (minHeap.size() > 1) {

Node* left = minHeap.Min();

Node* right = minHeap.Min();

Node* interNode = new Node('\0', left->freq + right->freq);

interNode->left = left;

interNode->right = right;

minHeap.insert(interNode);

return minHeap.Min();

void dedsec(){

string text = "This";

Node* root = buildHuffmanTree(text);

map<char, string> huffmanCodes;

generateCodes(root, "", huffmanCodes);

for (auto &[x,y]: huffmanCodes) {

cout << x << ": " << y << endl;

}
int main() {

cin.tie(0)->sync_with_stdio(0);

ll t=1;

//cin >> t;

while(t--){

dedsec();

Output:

Graph

-------------------------

#include<bits/stdc++.h>

using namespace std;

using ll = long long;

void dfs(ll u, vector<vector<ll>>&adj, vector<bool>&visited){


visited[u]=true;

cout << u << " ";

for(auto v:adj[u]){

if(!visited[v]){

dfs(v, adj, visited);

void dedsec(){

ll n,m;

cin >> n >> m;

vector<vector<ll>>adj(n+1);

for(ll i=0;i<m;i++){

ll u,v;

cin >> u >> v;

adj[u].push_back(v);

adj[v].push_back(u);

vector<bool>visited(n+1, false);

dfs(1, adj, visited);

cout << '\n';

queue<ll>q;

vector<bool>visited2(n+1,false);

q.push(1);

visited2[1]=true;

while(!q.empty()){
ll u=q.front();

cout << u << " ";

q.pop();

for(auto v:adj[u]){

if(!visited2[v]){

visited2[v]=true;

q.push(v);

cout << '\n';

int main()

cin.tie(0)->sync_with_stdio(0);

ll t=1;

cin >> t;

while(t--){

dedsec();

}
Output:

You might also like