0% found this document useful (0 votes)
3 views31 pages

Assignment Graphics

The document contains multiple C++ programs that implement various graphics algorithms, including DDA, Mid Point, Bresenham for line drawing, and Mid Point for circle and ellipse drawing. It also includes programs for performing transformations such as translation, rotation, scaling, and shearing on graphical objects. Each section provides code snippets along with explanations for user inputs and outputs.

Uploaded by

leomessisaha69
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)
3 views31 pages

Assignment Graphics

The document contains multiple C++ programs that implement various graphics algorithms, including DDA, Mid Point, Bresenham for line drawing, and Mid Point for circle and ellipse drawing. It also includes programs for performing transformations such as translation, rotation, scaling, and shearing on graphical objects. Each section provides code snippets along with explanations for user inputs and outputs.

Uploaded by

leomessisaha69
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

1. Write a program in C++ to draw a line using DDA line Algorithm.

Code:
#include <iostream>
#include <graphics.h>
#include <cmath>

using namespace std;


void DDA(int x0, int y0, int x1, int y1) {
int dx = x1 - x0;
int dy = y1 - y0;
int steps = max(abs(dx), abs(dy));
float xIncrement = (float)dx / steps;
float yIncrement = (float)dy / steps;
float x = x0;
float y = y0;
for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += xIncrement;
y += yIncrement;
delay(10);
}
}
int main() {
int x0, y0, x1, y1;
cout << "--- DDA Line Drawing Algorithm ---\n";
cout << "Enter starting coordinates (x0, y0): ";
cin >> x0 >> y0;
cout << "Enter ending coordinates (x1, y1): ";
cin >> x1 >> y1;
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)" ");
DDA(x0, y0, x1, y1);
getch();
closegraph();
return 0;
}
Output:

1
2
2. Write a program in C++ to draw a line using Mid Point Line Drawing algorithm.

Code:-

#include <graphics.h>
#include <iostream>
using namespace std;
class LineDrawer {
public:
void midPoint(int X1, int Y1, int X2, int Y2) {
int dx = X2 - X1;
int dy = Y2 - Y1;
int d, x, y;
if (dy <= dx) {
d = dy - (dx / 2);
x = X1;
y = Y1;
while (x < X2) {
x++;
if (d < 0) {
d = d + dy;
} else {
d += (dy - dx);
y++;
}
putpixel(x, y, BLUE);
}
} else if (dx < dy) {
d = dx - (dy / 2);
x = X1;
y = Y1;
while (y < Y2) {
y++;
if (d < 0) {
d = d + dx;
} else {
d += (dx - dy);
x++;
}
putpixel(x, y, BLUE);
}
}
}
};
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int x1, y1, x2, y2;
int xmax = getmaxx();
int ymax = getmaxy();
setbkcolor(6);
cleardevice();
cout << "Xmax of screen = " << xmax << "\nYmax of screen = " << ymax << "\n";
cout << "Enter line end coordinates according to Xmax and Ymax of Screen!\n";
cout << "Enter endpoints A(x1, y1): ";
cin >> x1 >> y1;

3
cout << "Enter endpoints B(x2, y2): ";
cin >> x2 >> y2;
LineDrawer drawer;
[Link](x1, y1, x2, y2);
getch();
closegraph();
return 0;
}

Output:-

4
5
3. Write a program in C++ to draw a line using Bresenham line drawing algorithm.
Code:
#include <iostream>
#include <graphics.h>
#include <cmath>
using namespace std;

void drawBresenham(int x1, int y1, int x2, int y2) {


int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int x = x1;
int y = y1;
if (dx > dy) {
int p = 2 * dy - dx;
for (int i = 0; i <= dx; i++) {
putpixel(x, y, WHITE);
x += sx;
if (p < 0) {
p += 2 * dy;
} else {
y += sy;
p += 2 * dy - 2 * dx;
}
}
}
else {
int p = 2 * dx - dy;
for (int i = 0; i <= dy; i++) {
putpixel(x, y, WHITE);
y += sy;
if (p < 0) {
p += 2 * dx;
} else {
x += sx;
p += 2 * dx - 2 * dy;
}
}
}
}

int main() {
int x1, y1, x2, y2;
cout << "--- Bresenham Algorithm (Two Cases) ---" << endl;
cout << "Enter the starting coordinates (x1, y1): ";
cin >> x1 >> y1;
cout << "Enter the ending coordinates (x2, y2): ";
cin >> x2 >> y2;
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
drawBresenham(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}

6
Output:

7
[Link] a program in C++ to draw a circle using Mid Point Circle drawing algorithm.
Code:
#include <iostream>
#include <graphics.h>
using namespace std;

class circle_ {
int x, y, p;
public:
circle_ (int r): x(0), y(r), p(1 - r) {};

void midPoint(int xc, int yc) {


while(x <= y) {
putpixel(xc + x, yc + y, 15);
putpixel(xc - x, yc + y, 15);
putpixel(xc - x, yc - y, 15);
putpixel(xc + x, yc - y, 15);
putpixel(xc + y, yc + x, 15);
putpixel(xc - y, yc + x, 15);
putpixel(xc - y, yc - x, 15);
putpixel(xc + y, yc - x, 15);

x++;
if (p < 0)
p = p + 2 * x + 3;
else {
y--;
p = p + 2 * (x - y) + 5;
}
}
}
};
int main() {
int x, y, r, gd = DETECT, gm;
initgraph(&gd,&gm,"");
cout <<"Enter the center coordinates: ";
cin>> x>> y;
cout<<endl<<"Enter the radius: ";
cin>> r;
circle_ obj (r);
[Link](x,y);
getch();
closegraph();
return 0;
}

8
Output:

9
5. Write a program in C++ to Ellipsc drawing algorithm.
Code:
#include <iostream>
#include <graphics.h>
using namespace std;

void plotPoints(int xc, int yc, int x, int y) {


putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
}
void midpointEllipse(int xc, int yc, int rx, int ry) {
float dx, dy, d1, d2, x, y;
x = 0;
y = ry;
d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
while (dx < dy) {
plotPoints(xc, yc, x, y);
x++;
dx = dx + (2 * ry * ry);
if (d1 < 0) {
d1 = d1 + dx + (ry * ry);
} else {
y--;
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
delay(10);
}
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);
while (y >= 0) {
plotPoints(xc, yc, x, y);
y--;
dy = dy - (2 * rx * rx);
if (d2 > 0) {
d2 = d2 - dy + (rx * rx);
} else {
x++;
dx = dx + (2 * ry * ry);
d2 = d2 + dx - dy + (rx * rx);
}
delay(10);
}
}
int main() {
int xc, yc, rx, ry;
cout << "--- Midpoint Ellipse Algorithm ---\n";
cout << "Enter Center coordinates (xc yc): ";
cin >> xc >> yc;
cout << "Enter X-radius (rx): ";

10
cin >> rx;
cout << "Enter Y-radius (ry): ";
cin >> ry;
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
midpointEllipse(xc, yc, rx, ry);
getch();
closegraph();
return 0;
}
Output:

11
6. Write a program in cpp to perform translation and rotation.
Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <graphics.h>
#include <conio.h>
using namespace std;
const float PI = 3.14f;

class Point {
public:
float x;
float y;

Point(float x = 0, float y = 0) : x(x), y(y) {}


};
class obj {
private:
vector<Point> vertices;
public:
void addVertex(float x, float y) {
vertices.push_back(Point(x, y));
}
void translate(float tx, float ty) {
for (size_t i = 0; i < [Link](); ++i) {
vertices[i].x += tx;
vertices[i].y += ty;
}
}
void rotate(float angle_degrees) {
float radians = angle_degrees * PI / 180.0f;
float cos_theta = cos(radians);
float sin_theta = sin(radians);
for (size_t i = 0; i < [Link](); ++i) {
float new_x = vertices[i].x * cos_theta - vertices[i].y * sin_theta;
float new_y = vertices[i].x * sin_theta + vertices[i].y * cos_theta;
vertices[i].x = new_x;
vertices[i].y = new_y;
}
}

void draw(int color){


setcolor(color);
int n = [Link]();
for (int i = 0; i < n; ++i) {
int next = (i + 1) % n;
int x1 = vertices[i].x;
int y1 = vertices[i].y;
int x2 = vertices[next].x;
int y2 = vertices[next].y;
line(x1, y1, x2, y2);
}
}
};
int main() {

12
obj myObject;
float x, y, tx, ty, angle;
for (int i = 1; i <= 3; ++i) {
cout << "Enter coordinates for Vertex " << i << " (x y): ";
cin >> x >> y;
[Link](x, y);
}
cout << "Enter translation distances (tx ty): ";
cin >> tx >> ty;
cout << "Enter rotation angle (in degrees): ";
cin >> angle;
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
[Link](WHITE);
[Link](tx, ty);
[Link](angle);
[Link](YELLOW);
getch();
closegraph();
return 0;
}
Output:

13
7. Write a program in C++ to perform Translation and Scaling. ( Translation and
Scaling in one code).
Code:
#include <iostream>
#include <graphics.h>
using namespace std;

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, const char* label) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
outtextxy(x1, y1 - 15, const_cast<char*>(label));
}
void translateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int tx, int ty) {
// Apply translation formula: x' = x + tx, y' = y + ty
int tx1 = x1 + tx, ty1 = y1 + ty;
int tx2 = x2 + tx, ty2 = y2 + ty;
int tx3 = x3 + tx, ty3 = y3 + ty;

setcolor(YELLOW);
drawTriangle(tx1, ty1, tx2, ty2, tx3, ty3, "Translated");
}
void scaleTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float sx, float sy) {
// Apply scaling formula relative to the first vertex (x1, y1)
int sx1 = x1;
int sy1 = y1;
int sx2 = x1 + (x2 - x1) * sx;
int sy2 = y1 + (y2 - y1) * sy;
int sx3 = x1 + (x3 - x1) * sx;
int sy3 = y1 + (y3 - y1) * sy;

setcolor(LIGHTRED);
drawTriangle(sx1, sy1, sx2, sy2, sx3, sy3, "Scaled");
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, const_cast<char*>(""));
int x1, y1, x2, y2, x3, y3;
cout << "Enter the coordinates of the triangle (x1 y1 x2 y2 x3 y3): ";
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
setcolor(WHITE);
drawTriangle(x1, y1, x2, y2, x3, y3, "Original");
int tx, ty;
cout << "Enter translation factors (tx ty): ";
cin >> tx >> ty;
translateTriangle(x1, y1, x2, y2, x3, y3, tx, ty);
float sx, sy;
cout << "Enter scaling factors (sx sy): ";
cin >> sx >> sy;
scaleTriangle(x1, y1, x2, y2, x3, y3, sx, sy);
getch();
closegraph();
return 0;
}

14
Output:

15
8. Write a program in C++ to perform Shearing.
Code:
#include <iostream>
#include <graphics.h>
#include <cmath>
using namespace std;
class shear {
float sx, sy;
public:
void drawRectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x4, y4);
line(x4, y4, x1, y1);
}
void display(double obj[3][3]) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++)
cout << obj[i][j] << " ";
cout << endl;
}}
void display_rectangle(double obj[3][4]) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 4; j++)
cout << obj[i][j] << " ";
cout << endl;
}}
void matrix(double obj[3][4], int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
obj[0][0] = x1;
obj[0][1] = x2;
obj[0][2] = x3;
obj[0][3] = x4;
obj[1][0] = y1;
obj[1][1] = y2;
obj[1][2] = y3;
obj[1][3] = y4;
obj[2][0] = obj[2][1] = obj[2][2] = obj[2][3] = 1;
}
void matrix_multiply(double obj1[3][3], double obj2[3][4]) {
double r[3][4] = {0};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 3; k++)
r[i][j] += obj1[i][k] * obj2[k][j];
} }
cout << "Result of multiplication: " << endl;
display_rectangle(r);
drawRectangle(r[0][0], r[1][0], r[0][1], r[1][1], r[0][2], r[1][2], r[0][3], r[1][3]);
}
void applyShear(char c, double obj[3][3]) {
if(c == 'x') {
cout << "Enter shearing factor in X-direction: ";

16
cin >> sx;
obj[0][0] = 1;
obj[0][1] = sx;
obj[1][1] = 1;
obj[2][2] = 1;
}
else if(c == 'y') {
cout << "Enter shearing factor in Y-direction: ";
cin >> sy;
obj[0][0] = 1;
obj[1][0] = sy;
obj[1][1] = 1;
obj[2][2] = 1;
}
else {
cout << "Error: Invalid Axis.";
exit(1);
} } };
int main() {
int x1, y1, x2, y2, x3, y3, x4, y4, b, gd = DETECT, gm;
char c;
initgraph(&gd, &gm, (char*)"");
shear obj;
cout << "Enter the 1st coordinate of rectangle: ";
cin >> x1 >> y1;
cout << "Enter the 2nd coordinate of rectangle: ";
cin >> x2 >> y2;
cout << "Enter the 3rd coordinate of rectangle: ";
cin >> x3 >> y3;
cout << "Enter the 4th coordinate of rectangle: ";
cin >> x4 >> y4;
[Link](x1, y1, x2, y2, x3, y3, x4, y4);
cout << "Matrix for rectangle: " << endl;
double rect[3][4] = {0};
double shearing[3][3] = {0};
[Link](rect, x1, y1, x2, y2, x3, y3, x4, y4);
obj.display_rectangle(rect);
cout << "Enter shearing type (x or y) in lowercase: ";
cin >> c;
[Link](c, shearing);
cout << "Shearing Matrix: " << endl;
[Link](shearing);
obj.matrix_multiply(shearing, rect);
getch();
closegraph();
return 0;
}

17
Output:

18
9. Write a program in C++ to perform Cohen-Sutherland algorithm.
Code:-
#include <iostream>
#include <graphics.h>
using namespace std;
class CohenSutherland {
private:
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
int x_min, y_min, x_max, y_max;
int computeCode(int x, int y) {
int code = INSIDE;
if (x < x_min) {
code |= LEFT;
} else if (x > x_max) {
code |= RIGHT;
}
if (y < y_min) {
code |= BOTTOM;
} else if (y > y_max) {
code |= TOP;
}
return code;
}
public:
CohenSutherland(int xmin, int ymin, int xmax, int ymax) {
x_min = xmin;
y_min = ymin;
x_max = xmax;
y_max = ymax;
}
void clip(int x1, int y1, int x2, int y2) {
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
int accept = 0;
while (1) {
if (code1 == 0 && code2 == 0) {
accept = 1;
break;
} else if (code1 & code2) {
break;
} else {
int code_out;
int x, y;
if (code1 != 0) {
code_out = code1;
} else {
code_out = code2;
}

if (code_out & TOP) {


x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;

19
} else if (code_out & BOTTOM) {
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
} else if (code_out & RIGHT) {
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
} else if (code_out & LEFT) {
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
cout << "Line accepted from (" << x1 << ", " << y1 << ") to (" << x2 << ", " <<
y2 << ")\n";
setcolor(GREEN);
line(x1, y1, x2, y2);
} else {
cout << "Line rejected\n";
}
}
};
int main() {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, (char*)"");
int x_min, y_min, x_max, y_max;
cout << "Enter the clipping window coordinates (x_min y_min x_max y_max): ";
cin >> x_min >> y_min >> x_max >> y_max;
int x1, y1, x2, y2;
cout << "Enter the line endpoints (x1 y1 x2 y2): ";
cin >> x1 >> y1 >> x2 >> y2;
cleardevice();
setcolor(WHITE);
rectangle(x_min, y_min, x_max, y_max);
setcolor(RED);
line(x1, y1, x2, y2);
CohenSutherland clipper(x_min, y_min, x_max, y_max);
[Link](x1, y1, x2, y2);
getch();
closegraph();
return 0;
}

20
Output:-

21
10. Write a program in C++ to perform Boundary fill algorithm.

Code:-

#include <graphics.h>

#include <iostream>

using namespace std;

class FillAlgorithm {

public:

void boundaryFill(int x, int y, int fillColor, int boundaryColor) {

if (getpixel(x, y) != boundaryColor && getpixel(x, y) != fillColor) {

putpixel(x, y, fillColor);

boundaryFill(x + 1, y, fillColor, boundaryColor);

boundaryFill(x - 1, y, fillColor, boundaryColor);

boundaryFill(x, y + 1, fillColor, boundaryColor);

boundaryFill(x, y - 1, fillColor, boundaryColor);

boundaryFill(x + 1, y + 1, fillColor, boundaryColor);

boundaryFill(x - 1, y - 1, fillColor, boundaryColor);

boundaryFill(x + 1, y - 1, fillColor, boundaryColor);

boundaryFill(x - 1, y + 1, fillColor, boundaryColor);

};

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)"");

int left, top, right, bottom;

cout << "Enter top-left coordinates of rectangle (left top): ";

22
cin >> left >> top;

cout << "Enter bottom-right coordinates of rectangle (right bottom): ";

cin >> right >> bottom;

rectangle(left, top, right, bottom);

int x, y;

cout << "Enter starting coordinates for boundary fill (x y): ";

cin >> x >> y;

int fillColor, boundaryColor;

cout << "Enter fill color: ";

cin >> fillColor;

cout << "Enter boundary color: ";

cin >> boundaryColor;

FillAlgorithm filler;

[Link](x, y, fillColor, boundaryColor);

getch();

closegraph();

return 0;

23
Output:-

24
[Link] a cpp program to perform flood fill algoritm
Code:
#include <iostream>
#include <graphics.h>
using namespace std;

void floodFill(int x, int y, int fillColor, int oldColor) {


if (getpixel(x, y) == oldColor && oldColor != fillColor) {
putpixel(x, y, fillColor);
delay(1);
floodFill(x + 1, y, fillColor, oldColor);
floodFill(x - 1, y, fillColor, oldColor);
floodFill(x, y + 1, fillColor, oldColor);
floodFill(x, y - 1, fillColor, oldColor);
}
}
int main() {
int tr_x, tr_y, bl_x, bl_y, seed_x, seed_y;
cout << "--- Flood Fill Algorithm ---\n";
cout << "Enter Top-Right coordinate (x y): ";
cin >> tr_x >> tr_y;
cout << "Enter Bottom-Left coordinate (x y): ";
cin >> bl_x >> bl_y;
cout << "Enter Seed Point (x y) inside the rectangle: ";
cin >> seed_x >> seed_y;
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int oldColor = getpixel(seed_x, seed_y);
int fillColor = YELLOW;
floodFill(seed_x, seed_y, fillColor, oldColor);
getch();
closegraph();
return 0;
}
Output:

25
26
[Link] a program in C++ to perform Arithmetic Encoding.
Code:
#include <iostream>
#include <cstring>
using namespace std;
class ArithmeticEncoder {
char symbols[50];
double probabilities[50];
double low_range[50];
double high_range[50];
int num_symbols;
public:
void input() {
cout << "Enter number of symbols: ";
cin >> num_symbols;
for (int i = 0; i < num_symbols; i++) {
cout << "Enter symbol " << i + 1 << ": ";
cin >> symbols[i];
cout << "Enter probability of " << symbols[i] << ": ";
cin >> probabilities[i];
} }
void calculateRanges() {
double current_low = 0.0;
for (int i = 0; i < num_symbols; i++) {
low_range[i] = current_low;
high_range[i] = current_low + probabilities[i];
current_low = high_range[i];
} }
int getIndex(char c) {
for (int i = 0; i < num_symbols; i++) {
if (symbols[i] == c) {
return i;
} }
return -1;
}
double encode(char* str) {
double low = 0.0;
double high = 1.0;
double range = 1.0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
int idx = getIndex(str[i]);
if (idx == -1) {
cout << "Error: Unrecognized character in input string." << endl;
return -1;
}
range = high - low;
high = low + range * high_range[idx];

27
low = low + range * low_range[idx];
}
return low;
} };
int main() {
ArithmeticEncoder encoder;
[Link]();
[Link]();
char str[100];
cout << "Enter the string to encode: ";
cin >> str;
double tag = [Link](str);
if (tag != -1) {
cout << "The final tag is: " << tag << endl;
}
return 0;
}
Output:

28
13. Write a program in C++ to perform Haffman Encoding.
Code:
#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

class Node {
public:
char ch;
int freq;
Node *left, *right;
Node(char ch, int freq) {
left = right = NULL;
this->ch = ch;
this->freq = freq;
}
};
class Compare {
public:
bool operator()(Node* l, Node* r) {
return l->freq > r->freq;
}
};
void encode(Node* root, string str, map<char, string> &huffmanCode) {
if (root == NULL) {
return;
}
if (!root->left && !root->right) {
huffmanCode[root->ch] = str;
}
encode(root->left, str + "0", huffmanCode);
encode(root->right, str + "1", huffmanCode);
}
void buildHuffmanTree(string text) {
map<char, int> freq;
for (size_t i = 0; i < [Link](); i++) {
freq[text[i]]++;
}
priority_queue<Node*, vector<Node*>, Compare> pq;

// Create a leaf node for each character (Old style iterator loop)
for (map<char, int>::iterator it = [Link](); it != [Link](); ++it) {
[Link](new Node(it->first, it->second));
}
while ([Link]() != 1) {
Node *left = [Link](); [Link]();
Node *right = [Link](); [Link]();
int sum = left->freq + right->freq;
Node *node = new Node('\0', sum);
node->left = left;
node->right = right;
[Link](node);
}
Node* root = [Link]();

29
map<char, string> huffmanCode;
encode(root, "", huffmanCode);
cout << "Huffman Character Codes: \n";
for (map<char, string>::iterator it = [Link](); it != [Link](); ++it)
{
cout << "'" << it->first << "' : " << it->second << "\n";
}
cout << "\nOriginal string: " << text << "\n";
string encodedString = "";
for (size_t i = 0; i < [Link](); i++) {
encodedString += huffmanCode[text[i]];
}
cout << "\nEncoded string: " << encodedString << "\n";
}
int main() {
string text;
cout << "Enter a string to compress using Huffman Encoding: ";
getline(cin, text);
if ([Link]()) {
cout << "String is empty. Nothing to encode." << endl;
return 0;
}
buildHuffmanTree(text);
return 0;
}
Output:

30
31

You might also like