哈夫曼樹C 實現

2021-10-07 20:19:32 字數 3997 閱讀 5379

給定一組具有確定權值的葉子結點,可以造出不同的二叉樹,將其中帶權路徑長度最小的二叉樹稱為哈夫曼樹(huffman tree)。

哈夫曼節點會儲存節點的權重以及,ch是節點對應的編碼字元的下標,這裡需要過載「

//哈夫曼樹節點類

class hftnode

hftnode(int data) ://哈夫曼樹節點類初始化

leftchild(nullptr), rightchild(nullptr), weight(data) {}

//拷貝建構函式

hftnode(const hftnode& n)

else

leftchild = nullptr;

if (n.rightchild)

else

rightchild = nullptr;

} // 過載"

bool operator < (const hftnode& n) const

};

哈夫曼樹類實現了樹的構建,編碼表的實現,壓縮和解壓縮文字功能

//哈夫曼樹類

class huffman

;

簡單來說,就是將節點壓入最小優先順序佇列中(這裡也可以使用最小堆完成,不過我上一節編寫的最小堆沒有寫成模板類,所以就懶一點,直接用stl裡面的優先順序佇列了),然後每次取出最小的兩個節點,將這兩個節點作為左孩子節點和右孩子節點,將這兩個節點的權重再合併成乙個節點,那麼這個新的節點就是該子樹的根結點,再將這個紫薯的根結點壓入最小優先順序佇列中,重複這個過程,直至生成一棵樹。

//構建一顆哈夫曼樹

void huffman::buildtree()

//合併節點並生成樹

while (myqueue.size() > 1)

//生成哈夫曼樹的根結點

root = new hftnode();

*root = myqueue.top();

myqueue.pop();

}

沒啥好說的,就是遞迴,map的key是文字符號如『a』,『b』,value是文字需要壓縮成的二進位制字串,如『11』,『110』

//根據哈夫曼樹構建編碼表

void huffman::buildcode()

void huffman::_build(hftnode* root, string str)

if(root->leftchild) _build(root->leftchild, str + '0');

if(root->rightchild) _build(root->rightchild, str + '1');

}

//壓縮

string huffman::compress(const string& des)

return res;

}

//解壓縮

string huffman::expend(const string& des)

} if (des[i] == '1')

}} return res;

}

#pragma once

#ifndef huffmantree_h

#define huffmantree_h

#include#include#include#includeusing namespace std;

//哈夫曼樹節點類

class hftnode

hftnode(int data) ://哈夫曼樹節點類初始化

leftchild(nullptr), rightchild(nullptr), weight(data) {}

//拷貝建構函式

hftnode(const hftnode& n)

else

leftchild = nullptr;

if (n.rightchild)

else

rightchild = nullptr;

} // 過載"

bool operator < (const hftnode& n) const

};//哈夫曼樹類

class huffman

;#endif // !huffmantree_h

#include "huffmantree.h"

//依據字串生成編碼表以及權重陣列

huffman::huffman(const string &sample)

unordered_mapmymap;

for (int i = 0; i < len; ++i)

for (const auto& pair : mymap)

}//析構函式

huffman::~huffman()

//判斷節點是否為葉子結點

bool huffman::isleaf(hftnode* root)

//獲取當前的權重陣列

void huffman::getfreq(vector&des)

//構建一顆哈夫曼樹

void huffman::buildtree()

//合併節點並生成樹

while (myqueue.size() > 1)

//生成哈夫曼樹的根結點

root = new hftnode();

*root = myqueue.top();

myqueue.pop();

}//根據哈夫曼樹構建編碼表

void huffman::buildcode()

//遍歷編碼表和編碼表對應的編碼

void huffman::getcodelist()

void huffman::preorder()

void huffman::inorder()

//解壓縮

string huffman::expend(const string& des)

} if (des[i] == '1')

}} return res;

}//壓縮

string huffman::compress(const string& des)

return res;

}void huffman::_preorder(hftnode* root)

void huffman::_inorder(hftnode* root)

//刪除哈夫曼樹

void huffman::_del(hftnode* root)

void huffman::_build(hftnode* root, string str)

if(root->leftchild) _build(root->leftchild, str + '0');

if(root->rightchild) _build(root->rightchild, str + '1');

}

// main.cpp : 此檔案包含 "main" 函式。程式執行將在此處開始並結束。

哈夫曼樹與哈夫曼編碼(C 實現)

1 對給定的n個權值構成n棵二叉樹的初始集合f 其中每棵二叉樹ti中只有乙個權值為wi的根結點,它的左右子樹均為空。2 在f中選取兩棵根結點權值最小的樹作為新構造的二叉樹的左右子樹,新二叉樹的根結點的權值為其左右子樹的根結點的權值之和。3 從f中刪除這兩棵樹,並把這棵新的二叉樹同樣以公升序排列加入到...

哈夫曼樹 C 實現

include using namespace std define maxbit 10 define maxvalue 10000 define maxleaf 100 define maxnode maxleaf 2 1 定義哈夫曼樹編碼型別 typedef structcodetype 定義哈...

哈夫曼樹C 實現

哈夫曼樹的介紹 huffman tree,中文名是哈夫曼樹或霍夫曼樹,它是最優二叉樹。定義 給定n個權值作為n個葉子結點,構造一棵二叉樹,若樹的帶權路徑長度達到最小,則這棵樹被稱為哈夫曼樹。這個定義裡面涉及到了幾個陌生的概念,下面就是一顆哈夫曼樹,我們來看 答。01 路徑和路徑長度 定義 在一棵樹中...