Week - 11-Learn Dsa With C++
Week - 11-Learn Dsa With C++
LEARN DSA
WITH C++
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
There are more priority nodes to execute first.
Input : 1 2 3 4 5 6
Emergency : 10 13 7 8 9 20
⇒ ⇒ ⇒ ⇒
⇒ ⇒ ⇒
Delete Operation :-
⇒ del 20 ⇒
Insertion Operation :-
Index = 0 1 2 3 4 5 6 7
Array = 99 25 70 10 20 18 30 10
Insertion + Height;
LEARN DSA WITH C++
WEEK :: 11 DAY: 02 DATE: 29-06-2023
HEAP
Priority Queue
Heap
Max - Heap Min Heap
Ary:- 70 32 40 25 20 30 25 15
Idx : 0 1 2 3 4 5 6 7
// size of heap
cin>>n;
for(int i=0; i<n; i++)
{
cin>>element;
maxHeap.push_back(element);
insertHeap(maxHeap);
};
return 0;
};
// need to swap
if(largest != index)
{
swap(maxHeap[largest], maxHeap[index]);
Heapify(maxHeap, largest);
}
return;
};
int main()
{
vector<int>maxHeap;
int n, element;
// size of heap
cin>>n;
for(int i=0; i<n; i++)
{
cin>>element;
maxHeap.push_back(element);
insertHeap(maxHeap);
};
DeleteHeap(maxHeap);
DeleteHeap(maxHeap);
return 0;
};
Delete Operation :-
⇒ ⇒ ⇒
#include<iostream>
#include<vector>
using namespace std;
// need to swap
if(largest != index)
{
swap(maxHeap[largest], maxHeap[index]);
Heapify(maxHeap, largest);
}
return;
};
int main()
{
vector<int>maxHeap;
int n, element;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>element;
maxHeap.push_back(element);
}
for(int i=n/2-1; i>=0; i--)
{
Heapify(maxHeap, i);
};
return 0;
};
LEARN DSA WITH C++
WEEK :: 11 DAY: 03 DATE: 06-07-2023
HEAP USING SORTING
Shoring :-
Array
18 12 6 5 9 3 5
Idx : 0 1 2 3 5 4 6
// need to swap
if (largest != index)
{
swap(maxHeap[largest], maxHeap[index]);
heapify(maxHeap, size, largest);
}
}
public:
// Function to build a Heap from array.
void buildHeap(int arr[], int n)
{
// Your Code Here
}
public:
// Function to sort an array using Heap Sort.
void heapSort(int arr[], int n)
{
// code here
// First create max heap
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(arr, n, i);
};
// Heap sort
if(B==0)
return 0;
int sum=0;
priority_queue<int>p;
for(int i=0; i<A.size(); i++)
{
if(A[i])
p.push(A[i]);
}
while(B && p.size())
{
sum += p.top();
if(p.top() >1)
p.push(p.top()-1);
p.pop();
B--;
}
return sum;
}
return ans;
}
};
LEARN DSA WITH C++
WEEK :: 11 DAY: 03 DATE: 08-07-2023
HEAP :: PAIR
PAIR :- SLT
Nested - Pair :-
Create Pair :-
#include<iostream> #include<iostream>
using namespace std; using namespace std;
return 0; return 0;
}; };
int main()
{
vector<pair<int, int>>v;
v.push_back(make_pair(10, 20));
v.push_back(make_pair(12, 22));
v.push_back(make_pair(8, 15));
v.push_back(make_pair(11, 19));
v.push_back(make_pair(14, 5));
// print element
for(int i=0; i<5; i++)
cout<<v[i].first<<" "<<v[i].second<<endl;
return 0;
};
// sort element
sort(v.begin(), v.end(), sortbysecond);
// print element
for(int i=0; i<5; i++)
cout<<v[i].first<<" "<<v[i].second<<endl;
return 0;
};
// Min Heap
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int,
pair<int, int>>>> minheap;
Huffman Encoding
Huffman Coding is used because it reduces the size and improves the speed of transmission.
For data transmission using fax and text. It can be used to compress the file.
Bit allocated :-
A B C D E A=0
20 4 15 9 2 <— Freq C = 11
Small element D = 101
20 15 9 6 E = 1000
20 15 15 B = 1001
20 30
50
Step of Huffman :-
Node(int count) {
freq = count;
left = right = NULL;
}
};
class Compare {
public:
bool operator()(Node* a, Node* b) {
return a->freq > b->freq;
}
};
return median;
}
};