0% found this document useful (0 votes)
15 views13 pages

Huffman Decoding

Uploaded by

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

Huffman Decoding

Uploaded by

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

HUFFMAN DECODING

PROBLEM STATEMENT

• Huffman coding assigns variable length codewords to fixed length input


characters based on their frequencies. More frequent characters are assigned
shorter codewords and less frequent characters are assigned longer codewords.
All edges along the path to a character contain a code digit. If they are on the
left side of the tree, they will be a 0(zero). If on the right, they'll be a 1 (one).
Only the leaves will contain a letter and its frequency count. All other nodes
will contain a null instead of a character, and the count of the frequency of all
of it and its descendant characters.
SOLUTION

• To solve this problem we make use of ‘Trees’(Non-linear data structure).


TREES
Tree Data Structure is a hierarchical data structure in which a collection of
elements known as nodes are connected to each other via edges such that there
exists exactly one path between any two nodes.
Basic terminologies of trees:
• Root Node: The topmost node of a tree or the node which does not have any parent node is called the root
node. {A} is the root node of the tree. A non-empty tree must contain exactly one root node and exactly one
path from the root to all other nodes of the tree.
• Parent Node: The node which is a predecessor of a node is called the parent node of that node. {B} is the
parent node of {D, E}.
• Child Node: The node which is the immediate successor of a node is called the child node of that node.
Examples: {D, E} are the child nodes of {B}.
• Leaf Node or External Node: The nodes which do not have any child nodes are called leaf nodes. {K, L,
M, N, O, P, G} are the leaf nodes of the tree.
• Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
• Level of a node: The count of edges on the path from the root node to that node. The root node has level 0.
• Internal node: A node with at least one child is called Internal Node.
• Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
• Subtree: Any node of the tree along with its descendant.
• Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called Ancestors of that
node. {A,B} are the ancestor nodes of the node {E}
• Descendant: Any successor node on the path from the leaf node to that node. {E,I} are the descendants of
the node {B}.
TYPES OF TREES

Binary tree:
In a binary tree, each node can have a maximum of two
children linked to it. Some common types of binary trees
include full binary trees, complete binary trees, balanced
binary trees, and degenerate or pathological binary trees.
Ternary Tree:
A Ternary Tree is a tree data structure in which each node
has at most three child nodes, usually distinguished as “left”,
“mid” and “right”.
N-ary Tree or Generic Tree:
Generic trees are a collection of nodes where each node
is a data structure that consists of records and a list of
references to its children(duplicate references are not allowed).
Unlike the linked list, each node stores the address of multiple
nodes.
BINARY TREE

Binary Tree is defined as a tree data structure where each


node has at most 2 children. Since each element in a
binary tree can have only 2 children, we typically name
them the left and right child.

Binary Tree Representation:

A Binary tree is represented by a pointer to the


topmost node (commonly known as the “root”) of the
tree. If the tree is empty, then the value of the root is
NULL. Each node of a Binary Tree contains the
following parts:
1.Data
2.Pointer to left child
3.Pointer to right child
Applications of binary tree:

File Systems: Binary trees are used in file systems to organize and store files.
Search Engines: Binary trees are used in search engines to organize and index web pages.
Searching and Sorting Algorithms.
• Expression Trees.
• Decision Trees: Binary trees can be used to implement decision trees, a type of machine learning
algorithm used for classification and regression analysis.

• Huffman Coding:

It is a data compression algorithm that uses binary trees to encode characters. The binary tree used in
Huffman coding is called a Huffman tree, and it is a binary tree in which the nodes represent the
characters to be encoded. The algorithm assigns variable-length characters to each character, and the
most frequent characters receive short codes.
HUFFMAN DECODING
SOURCE CODE:

//Header files
#include <stdio.h>
#include <stdlib.h>

// structure definition of binary tree


typedef struct Node {
char data;
struct Node *left;
struct Node *right;
} Node;

//Creating a node of binary tree


Node* create_node(char data) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
// Huffman decoding function definition

char decode_huff(Node* root, char* s)


{
Node* current_node = root;
int I = 0;
while (s[i] != ‘\0’) {
if (s[i] == ‘0’) {
current_node = current_node->left;
} else {
current_node = current_node->right;
}
if (current_node->data != ‘\0’) {
putchar(current_node->data);
current_node = root;
}
i++;
}
}
// main function

int main()
{
// Example Huffman tree creation
Node* root = create_node(‘\0’);
root->left = create_node(‘\0’);
root->right = create_node(‘A’);
root->left->left = create_node(‘B’);
root->left->right = create_node(‘C’);

// Prompt the user to enter the encoded string


printf(“Enter the encoded string: “);
char encoded_string[100]; // assuming a maximum length of 100 characters
scanf(“%s”, encoded_string);

// Decode the string and print the result


decode_huff(root, encoded_string);

return 0;
}
Now let’s see how the code works

The values of characters(from the constructed tree) are:


A-1
B-00
C-01

If the encoded string is “ 1001101


Then ,
The decoded string would be “ABAAC”

S=“1001011”
Processing the string from left to right.

S[0]=‘1’: we move to the right child of the root. We encounter a leaf node with value ‘A’. We add ‘A’ to the decoded
string. We move back to the root.
S[1]=‘0’ : we move to the left child.
S[2]=‘0’:we move to the left child. We encounter a leaf node with value ‘B’. We add ‘B’ to the decoded string. We move
back to the root.
S[3] =‘1’: we move to the right child of the root. We encounter a leaf node with value ‘A’. We add ‘A’ to the
decoded string. We move back to the root.

S[4]=‘1’: we move to the right child of the root. We encounter a leaf node with value ‘A’. We add ‘A’ to the
decoded string. We move back to the root.

S[5]=‘0’: we move to the left child.

S[6] = ‘1’: we move to the right child of the root. We encounter a leaf node with value ‘C’. We add ‘C’ to the
decoded string. We move back to the root.

Decoded String = “ABAAC”

You might also like