The method of realizing Huffman tree simple creation and traversal by C + + _c language

Source: Internet
Author: User

This article takes the example form to narrate the C + + Realization Huffman tree simple creation and the traversal method, compares the classical C + + algorithm.

The function of this example is: given n a weighted node, how to construct an n two-fork tree with a given weight of a leaf node, so that its full path length WPL minimum .

The optimal tree algorithm is constructed according to the following:

Huffman algorithm:

1. The nodes of n weights, which are w1,w2,w3,.... wn-1,wn, are sorted by weight increment, each weight as a binary tree. ,... of the forest f={t1,t2,t3,t4 of n-Tree binary trees Tn}, where each binary tree has only one weight, and its left and right words are empty

2. Select the root node weight in forest f The least binary tree, as the left and right words constitute a new two fork tree, and make the new two fork tree root node
The sum of the weights of the left and right words, where the leaves are the original trees

3. Delete the two trees in the forest f, and add the new two fork trees instead of the two trees to the forest F, so the number of binary trees in the forest is one less than before.

4. Repeat 2 and 3 for the New Forest, knowing that there is only one tree position in the forest, this tree is Huffman tree.

#include <iostream> using namespace std;  #define LEAFNUM 10//leaf node number, i.e. weight tree #define HUFNUM 2*leafnum #define Maxweight 999.9//********* storage Structure *********** class
Huftree;          node********** class NODE {Private:char Data;   The data field of the node is double Weight;   The weighted range int lchild,rchild,parent of the node;
 The left child of the node and the parent domain Public:node ()//constructor {Data = ';
 Weight = 0;
 Lchild =-1;
 Rchild =-1;        Parent =-1;
 Initialization of data to node {re_l () {return lchild}
 int Re_r () {return rchild;}
 Char Re_data () {return Data;}
 Double Re_weight () {return Weight;}     Friend class Huftree;
 DECLARE friend};//node//********huftree class ********** classes Huftree {private:int nodenum;
NODE Hufarry[hufnum];
 Public:huftree () {nodenum = 0;}   void Sethuf (Int,double,char);          Set weights and data field void Creathuf ();   Create Huffman tree void Selectmin (int,int&,int&);       Two tree void Find_root_and_print () with the smallest weight value of Huffman tree is found.          Finds the root node location void Printhuf (int); Traversal Huffman tree};//huftree void Huftree::seTHUF (int i,double Wei,char ch) {Hufarry[i].
 Data = ch; Hufarry[i].
Weight = Wei;
 } void Huftree::creathuf () {cout<< "location of two minimum trees per query:" <<endl;
 for (int i = Leafnum i < HUFNUM-1; i++) {int p1 = 0;
 int p2 = 0;           Selectmin (I,P1,P2);
 Find out the two trees with the smallest weight of tree cout<<p1<< "<->" <<p2<<endl; HUFARRY[P1].   Parent = i; Sets two parent hufarry[p2 of the smallest tree.
 Parent = i; Hufarry[i].   Lchild = p1; Set the root weight of the 3-node tree and the child hufarry[i].   
 Rchild = p2; Hufarry[i]. Weight = Hufarry[p1]. Weight + HUFARRY[P2].
 Weight;
} cout<< "************************" <<endl;
 } void huftree::selectmin (int i,int &p1,int &p2) {int least1 = Maxweight;
 int least2 = Maxweight; for (int j = 0; J < i; J +) {if (Hufarry[j]. Parent = = 1) {if (Hufarry[j].
  Weight < Least1) {least2 = Least1; Least1 = Hufarry[j].
  Weight;
  P2 = p1;
  P1 = j; } else {if (hufarry[j]. Weight < Least2) {least2 = Hufarry[j].
   Weight;
  P2 = j;
 }
  }
 }} void Huftree::find_root_and_print () {int Root; for (int i = 0; i < HUFNUM-1 i++) {if (hufarry[i).
  Parent = = 1) {root = i;
 Break
} printhuf (root);
 } void Huftree::P rinthuf (int position) {if (Nodenum = = Leafnum) {return; } else {if (hufarry[position]. Data!= '//If is the leaf node {cout<< ' weight: < 

Test results:

1 a
2 b
5 c
7 D
4 e
f
3 G
6 h
I
8 l

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.