Review of Huffman tree and Huffman coding (built from minimum heap)

Huffman tree

The weight of each node is multiplied by the tree with the smallest sum of the path lengths of the root nodes-the
characteristics of the optimal binary tree Huffman tree:
 There is no node with a degree of 1;
 Any arbitrary The left and right subtrees of the leaf nodes are still Huffman trees after exchange;  Huffman trees with n leaf nodes have a total of 2n-1 nodes;
 For the same set of weights {w1, w2, ……, wn} , There are different configurations of Huffman tree
Huffman tree The
binary tree is converted into the smallest heap according to the weight of the node, and each time the heap is taken to determine two elements to form a new binary tree node, which is inserted back into the heap;
when proceeding size-1 merger, the final result is the Huffman tree;

1. Tree node

#include <iostream>
#include <string>
using namespace std;
#define MaxNum 64

struct TreeNode{
    int Weight = 0;
    TreeNode *Left = nullptr;
    TreeNode *Right = nullptr;
};

2. Heap structure

struct HeapNode{// 堆结构
	TreeNode Data[MaxNum];      //data内放着树结构
	int Size =0;        //初始大小为0 
};

3. Heap creation function

HeapNode * CreateHeap(){    //建树——返回指向树根节点的指针 
	HeapNode *H = new(HeapNode);
	H->Data[0].Weight = -1;    //哨兵的weight设置为-1
	return H;       //建堆——一次即可 
}

4. Heap delete function

TreeNode *DeleteMin(HeapNode *H){ //返回堆定的指针 
	int Parent = 0 , Child=0;
	TreeNode temp;
	TreeNode *MinItem = new(TreeNode);       //新建一个minitem
	*MinItem = H->Data[1];   //保存堆顶
	
	temp = H->Data[(H->Size)--];   //拿到堆底的对象,并且size-1
	
	for(Parent=1;Parent *2 <= H->Size; Parent=Child)
	{
		Child= Parent*2;
		//Size就是为了作为边界 
		if(Child!=H->Size && (H->Data[Child].Weight>H->Data[Child+1].Weight))
			Child++;
		if(temp.Weight<=H->Data[Child].Weight)  break;
		else H->Data[Parent]=H->Data[Child];        //下沉 
	}
	//break出来说明找到合适位置 
	 H->Data[Parent] = temp;     //进行赋值
	 return MinItem;       //返回堆定成员指针 
} 

5. Insert heap operation

void Insert(HeapNode *H, TreeNode *item){     //将一个树成员插入堆中 
	int i=0;
	i= ++(H->Size);    //i为当前总容量
	//item为指向树成员的指针 
	for (;item ->Weight<H->Data[i/2].Weight;i/=2)  //插入最后,然后上浮 
	//当要插入的成员小于父节点,上浮 
	{
		H->Data[i]=H->Data[i/2];
	 } 
	 H->Data[i] = *item;       //将item所指成员放入堆中 
} 

6. Build a Huffman tree
Enter a pointer to the smallest heap object;

TreeNode *Huffman(HeapNode *H){    //把堆变为哈夫曼树 
	TreeNode *T = nullptr;     //初始空指针
	 int num = H->Size;      //一共有num个元素
	 //进行n-1次合并
	 for(int i=0;i<num-1;i++)
	 {
	 	T= new(TreeNode);
	 	T->Left = DeleteMin(H);
	 	T->Right = DeleteMin(H);
	 	T->Weight=T->Left->Weight + T->Right->Weight;
	 	Insert(H,T);    //T为指针——insert(H,T)
		 //要用动态内存创造空间时——用指针 
	  } 
	  T= DeleteMin(H);    
	  return T;      //返回指向堆头的指针 
	  // 
}

Summary The
Huffman tree is transformed from the smallest heap; it
can make the binary tree the optimal binary tree; it
can avoid ambiguity and construct the binary tree with the least coding cost;

Published 105 original articles · won praise 6 · views 4954

Guess you like

Origin blog.csdn.net/BLUEsang/article/details/105479833