Introduction to building a hafman algorithm p232

Source: Internet
Author: User

 

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Typedef struct bitnode
{
Char C;
Int weigh;
Struct bitnode * parent;
Struct bitnode * lchild;
Struct bitnode * rchild;
} Bitnode;
Bitnode * queue [101];
Bitnode * leaf [101];

Int CMP (const void * a, const void * B)
{
If (* (bitnode **) A)-> weweigh = (* (bitnode **) B)-> weweigh)
Return (* (bitnode **) A)-> C)-(* (bitnode **) B)-> C ));
Else
Return (* (bitnode **) B)-> weigh-(* (bitnode **) A)-> weigh );
}

Typedef struct
{
Char C;
Int bit [20];
Int count;
} Hcodetype;
Hcodetype huffcode [101];

Void insertsort (INT min2) // sort Inserts
{
Int I, J;
If (min2 = 1)
Queue [1] = queue [0];
Else
{
I = 1;
While (I <min2 & queue [0]-> weweigh <= queue [I]-> weweigh)
I ++;
 
For (j = min2; j> I; j --)
Queue [J] = queue [J-1];
Queue [I] = queue [0];
}
}

Void Huffman (int n) // create a user tree
{
Int min1, min2;
Int I;
Bitnode * P;
Min1 = N; // minimum
Min2 = n-1; // small
For (I = 1; I <n; I ++)
{
P = (bitnode *) malloc (sizeof (bitnode); // apply for a new node as the parent node
P-> lchild = queue [min1]; // left child whose minimum value is P
P-> rchild = queue [min2]; // left child whose size is P
Queue [min1]-> parent = queue [min2]-> parent = P; // the minimum number of child parent nodes is P.

P-> weigh = queue [min1]-> weigh + queue [min2]-> weigh; // the frequency of the parent node is the frequency of left and right children and

P-> parent = NULL; // The parent node of the parent node is null.
Queue [0] = P;

Insertsort (min2); // insert sorting
 
Min1 = min2; // minimum // reduce the number of queue members by 1
Min2 --; // small
}
}

Void haffmancode (int n) // evaluate the hafman encoding of the character
{
Bitnode * child, * parent;
Int count, bit [21];
Int I, J, K;
For (I = 1; I <= N; I ++)
{
Child = leaf [I];
Parent = Child-> parent;
Count = 21;

While (parent) // if the parent node is not empty, the root node is stopped.
{
If (child = parent-> lchild) // set the left child of the parent node to 0.
Bit [-- count] = 0;
Else // if the child is a parent node, set the right child to 1.
Bit [-- count] = 1;

Child = parent; // The Parent and Child Nodes are all retrieved from the top.
Parent = parent-> parent;
}

For (j = count, k = 1; j <= 20; j ++, K ++) // returns the hafman encoding of the reverse character.
Huffcode [I]. Bit [k] = bit [J];
Huffcode [I]. Count = K-1;
Huffcode [I]. c = leaf [I]-> C;
}
}

Void preorder (bitnode * BT) // traverse the Harman tree in ascending order
{
If (bt = NULL)
Return;
If (BT-> lchild)
Printf ("% d \ n", BT-> weigh );
Else
Printf ("% C % d \ n", BT-> C, BT-> weigh );
Preorder (BT-> lchild );
Preorder (BT-> rchild );
 
}

Void print (int n) // outputs the hafman encoding of each character
{
Int I, J;
For (I = 1; I <= N; I ++)
{

Printf ("% C", huffcode [I]. C );
For (j = 1; j <= huffcode [I]. Count; j ++)
Printf ("% d", huffcode [I]. Bit [J]);
Printf ("\ n ");
}
Printf ("\ n ");
}

Int main ()
{Freopen ("1.txt"," r ", stdin );
Int N, I, F, C;
Bitnode * P;
Printf ("Enter the number of characters N: \ n ");
Scanf ("% d", & N );
Printf ("Enter characters & Their frequency \ n ");

For (I = 1; I <= N; I ++) // enter each character and apply for a leaf node
{
P = (bitnode *) malloc (sizeof (bitnode ));
Scanf ("% C", & C );
P-> C = C;
P-> lchild = p-> rchild = NULL;
Queue [I] = P;
}
 
For (I = 1; I <= N; I ++) // enter the frequency of each character
{
Scanf ("% d", & F );
Queue [I]-> weweigh = F;
}

Qsort (queue + 1, n, sizeof (queue [1]), CMP); // sort all characters in lexicographically ascending order with the same frequency

For (I = 1; I <= N; I ++)
{
Leaf [I] = queue [I]; // the pointer leaf vector points to the hafman encoding that facilitates character search.
// Printf ("L = % C % d \ n", leaf [I]-> C, queue [I]-> weigh );
}
 
Huffman (n); // create a user-defined tree

// Preorder (queue [1]); // traverse the Harman tree in sequence

Haffmancode (n); // evaluate the hafman encoding of Characters

Print (n); // specifies the Haffman encoding of each character.
Return 0;
}
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.