Haffman encoding algorithm (simple linked list method)

Source: Internet
Author: User

The algorithm code is as follows:

Public class huffmannode {public char {Get; set;} public int frequency {Get; set;} // used to construct the harfman tree internal huffmannode leftchild {Get; set ;} internal huffmannode rightchild {Get; Set ;}// used to construct the linked table internal huffmannode nextsibling {Get; Set ;}public string huffmancode {Get; Set ;} public huffmannode (char, int freq) {This. char = char; this. frequency = freq;} public class huffmanalgorit HMS {/// <summary> /// perform the hafman encoding. The nodes node is not changed during the encoding process, but only some fields are changed. /// </Summary> /// <Param name = "nodes"> </param> Public static void huffmancode (huffmannode [] nodes) {If (nodes = NULL | nodes. length = 0) {return ;}# region can be used instead of IF (nodes. length = 1) {nodes [0]. huffmancode = "0"; return;} If (nodes. length = 2) {nodes [0]. huffmancode = "0"; nodes [1]. huffmancode = "1"; return ;}# endregion huffmannode thehead = NULL; // create an ascending node linked list O (N ^ 2) for (INT I = 0; I <nodes. length; I ++) {insertnode (ref thehead, nodes [I]);} // creates a Huffman tree. The greedy method is used to obtain the smallest two (the first two) in the linked list each time ). huffmannode thetmp1 = NULL; huffmannode thetmp2 = NULL; thetmp1 = thehead; If (thehead! = NULL) {thetmp2 = thehead. nextsibling;} // if there are two operations in the linked list, the tree is created. Otherwise, the tree is created and the loop is exited. O (N * lgn) while (thetmp1! = NULL & thetmp2! = NULL) {// create an inner node that contains the two smallest elements in the current linked list. The frequency of this node is the sum of the frequencies of these two elements. huffmannode theparent = new huffmannode ('\ 0', thetmp1.frequency + thetmp2.frequency); theparent. leftchild = thetmp1; theparent. rightchild = thetmp2; // remove the current two smallest elements and insert the new node. thehead = thetmp2.nextsibling; // Insert the newly created inner node into the current linked list. insertnode (ref thehead, theparent); thetmp1 = thehead; If (thehead! = NULL) {thetmp2 = thehead. nextsibling;} // uses the first-order traversal of the tree for encoding. the result is stored in the encoding domain of the node. O (n) huffmancode (thehead, ""); // clear the temporary domain to ensure memory is released. O (n) foreach (VAR node in nodes) {node. leftchild = NULL; node. nextsibling = NULL; node. rightchild = NULL ;}//< summary> /// uses the first-order traversal of the tree for encoding. In fact, all the valid values are leaf nodes, and all the leaf nodes are in the nodes array. /// </Summary> /// <Param name = "Anode"> </param> /// <Param name = "strcode"> </param> Private Static void huffmancode (huffmannode anode, string strcode) {If (anode! = NULL) {anode. huffmancode = strcode; If (anode. leftchild! = NULL) {huffmancode (anode. leftchild, strcode + "0");} If (anode. rightchild! = NULL) {huffmancode (anode. rightchild, strcode + "1") ;}}/// <summary> // Insert the node into the linked list ahead and sort it in ascending order. /// </Summary> /// <Param name = "Ahead"> </param> /// <Param name = "Node"> </param> Private Static void insertnode (ref huffmannode ahead, huffmannode node) {huffmannode thetmp1 = ahead; huffmannode thetmp2 = NULL; while (thetmp1! = NULL & thetmp1.frequency <node. frequency) {thetmp2 = thetmp1; thetmp1 = thetmp1.nextsibling;} If (thetmp2 = NULL) {ahead = node;} else {thetmp2.nextsibling = node;} node. nextsibling = thetmp1 ;}}

PS: The minimum heap efficiency of this algorithm is higher. In O (nlgn), the algorithm here is O (n ^ 2), which is in line with the time complexity of inserting the sorting algorithm.

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.