20BCS7552 Exp6
20BCS7552 Exp6
20BCS7552 Exp6
Experiment-6
CODE:
a. Tree Huffman Decoding:
#include<bits/stdc++.h>
using namespace std;
node * huffman_hidden(string s) {
spq pq;
vector<int>count(256,0);
if( count[i] != 0 )
pq.push(n_node);
while( pq.size() != 1 ) {
return pq.top();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if(root == NULL)
return;
if(root->data != '\0') {
mp[root->data] = code;
}
/*
The structure of the node is
int freq;
char data;
node * left;
node * right;
} node;
*/
}
if(next -> data == '\0'){
n = next;
}
else{
ans += next -> data;
n = root;
}
}
cout << ans << endl;
}
int main() {
string s;
std::cin >> s;
string coded;
decode_huff(tree,coded);
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
b. Balanced forest:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <cassert>
#include <stdlib.h>
vector<int> g[N];
ll c[N];
ll f[N];
ll res = INF;
ll tot = 0;
bool was[N];
void upd(ll a, ll b, ll c) {
if (a == b && c <= a)
res = min(res, a - c);
if (a == c && b <= a)
res = min(res, a - b);
if (b == c && a <= b)
res = min(res, b - a);
}
set<ll>* dfs(int v) {
was[v] = true;
f[v] = c[v];
set<ll>* sv = new set<ll>();
for (int to : g[v])
if (!was[to]) {
set<ll>* sto = dfs(to);
f[v] += f[to];
sv = unite(sv, sto);
}
if (f[v] % 2 == 0 && sv->count(f[v] / 2))
upd(f[v] / 2, f[v] / 2, tot - f[v]);
if (sv->count(tot - f[v]))
upd(tot - f[v], 2 * f[v] - tot, tot - f[v]);
if (sv->count(2 * f[v] - tot))
upd(2 * f[v] - tot, tot - f[v], tot - f[v]);
sv->insert(f[v]);
return sv;
}
void solve() {
int n;
cin >> n;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main() {
ios_base::sync_with_stdio(0);
int p;
cin >> p;
while (p--) {
solve();
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
OUTPUT:
a. Tree Huffman Decoding:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
b. Balanced Forest: