Huffman Tree//Xin Yang # include <stdio.h> #include <stdlib.h>typedef int elemtype;struct btreenode{elemtype data; struct btreenode* left; struct btreenode* right;};/ /Traverse Huffman tree void Printbtree_int (struct btreenode* bt) {if (BT! = NULL) {printf ("%d", bt->data); if (bt->left! = NULL | | Bt->right! = NULL) {printf ("); Printbtree_int (Bt->left); Output left subtree if (bt->right! = NULL) printf (","); Printbtree_int (Bt->right); Output right sub-tree printf (")"); }}}//Create Huffman tree struct btreenode* createhuffman (elemtype a[], int n) {int i, J; struct Btreenode **b, *q; b = malloc (n*sizeof (struct btreenode)); for (i = 0; i < n; i++)//dynamic memory allocation {B[i] = malloc (sizeof (struct btreenode)); B[i]->data = A[i]; B[i]->left = B[i]->right = NULL; } for (i = 1; i < n; i++) {//K1 represents the subscript of the tree root node with the least weight in the forest, K2 is the sub-minimum subscript int K1 =-1, K2; for (j = 0; J < N; j + +)//Let K1 initially point to the first tree in the forest, K2 point to the second tree {if (b[j]! = NULL && K1 = = 1) {K1 = j; Continue } if (B[j]! = NULL) {K2 = j; Break }} for (j = K2; J < N; j + +)//construct optimal solution {if (b[j]! = NULL) {if (B[j]->data < B[k1]->data) {K2 = K1; K1 = j; } else if (B[j]->data < b[k2]->data) K2 = j; }} q = malloc (sizeof (struct btreenode)); Q->data = B[k1]->data + b[k2]->data; Q->left = B[k1]; Q->right = B[k2]; B[K1] = q; B[K2] = NULL; } free (b); return q; }//calculates the weighted path elemtype weightpathlength (struct btreenode* FBT, int len)//len initially 0{if (FBT = = null)//Empty tree returns 0 return 0; else { if (Fbt->left = = NULL && Fbt->right = = null) return fbt->data * len; else return Weightpathlength (fbt->left,len+1) +weightpathlength (fbt->right,len+1); }}//constructs Huffman code void huffmancoding (struct btreenode* FBT, int len) {static int a[10]; if (FBT = null) {if (Fbt->left = = NULL && Fbt->right = = null) {int i; printf ("The value of the node is%d encoding:", fbt->data); for (i = 0; i < len; i++) printf ("%d", a[i]); printf ("\ n"); } else {A[len] = 0; Huffmancoding (Fbt->left, Len + 1); A[len] = 1; Huffmancoding (fbt->right, Len + 1); }}}int Main () {int n, I; Elemtype* A; struct btreenode* FBT; printf ("Please enter the weighted node n:\n"); while (1) {scanf ("%d", &n); if (n > 1) break; else printf ("Re-enter N value:"); } A = malloc (n * sizeof(Elemtype)); printf ("Enter%d integers as the weights: \ n", "N."); for (i = 0; i < n; i++) scanf ("%d", &a[i]); FBT = Createhuffman (A, n); printf ("Huffman tree as follows: \ n"); Printbtree_int (FBT); printf ("\ n"); printf ("Huffman tree with the right path length: \ n"); printf ("%d\n", Weightpathlength (FBT, 0)); printf ("Huffman code for each leaf node in the tree: \ n"); Huffmancoding (FBT, 0); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure---C language Implementation huffman tree and encoding