Data Structure --- implementation of the Harman tree and coding in c language --- c fuman

Source: Internet
Author: User

Data Structure --- implementation of the Harman tree and coding in c language --- c fuman

// Harman tree // Yang Xin # include <stdio. h> # include <stdlib. h> typedef int ElemType; struct BTreeNode {ElemType data; struct BTreeNode * left; struct BTreeNode * right;}; // traverse the Harman 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 the left subtree if (BT-> right! = NULL) printf (","); PrintBTree_int (BT-> right); // outputs the right subtree printf (")");}}} // create a user 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 indicates the subscript of the root node with the minimum weight in the forest, and k2 indicates the minimum weight. Subscript int k1 =-1, k2; for (j = 0; j <n; j ++) // Let k1 point to the first tree in the forest, k2 points to the second {if (B [j]! = NULL & k1 =-1) {k1 = j; continue;} if (B [j]! = NULL) {k2 = j; break ;}for (j = k2; j <n; j ++) // construct the 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 ;} // calculate the permission path ElemType WeightPathLength (struct BTreeNode * FBT, int len) // The initial len value is 0 {if (FBT = NULL) // return 0 return 0 for the empty tree; else {if (FBT-> left = N ULL & FBT-> right = NULL) return FBT-> data * len; else return WeightPathLength (FBT-> left, len + 1) + WeightPathLength (FBT-> right, len + 1) ;}}// construct the user 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 ("encode the node value as % d: ", 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 ("Enter the number of weighted nodes n: \ n"); while (1) {scanf ("% d", & n); if (n> 1) break; else printf ("re-input n value:");} a = malloc (n * sizeof (ElemType); printf ("Enter % d integers as the weight: \ n ", n); for (I = 0; I <n; I ++) scanf (" % d ", & a [I]); fbt = CreateHuffman (a, n); printf ("the husky tree is as follows: \ n"); PrintBTree_int (fbt); printf ("\ n "); printf ("length of the weighted path of the Harman tree: \ n"); printf ("% d \ n", WeightPathLength (fbt, 0 )); printf ("each leaf node in the tree is encoded as \ n"); HuffManCoding (fbt, 0); return 0 ;}





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.