#include #include #include #includeusing namespace std;
typedef int elemtype;
typedef struct huffmantree
huffmannode;
void huffman(huffmannode* hufmtree)
if(hufmtree->lchild!=null)huffman(hufmtree->lchild);
if(hufmtree->rchild!=null)huffman(hufmtree->rchild);
}// 構建哈夫曼樹
huffmannode* createhuffmantree(int* a,int* b,int n)
for (i = 0; i < n - 1; ++i) // n-1次合併
else if (temp[j] != null)
}if (small1 > small2)
for (j = small2; j < n; ++j) // 比較權值,挪動small1和small2使之分別成為最小和次小權值的下標
else if (temp[j]->weight < temp[small2]->weight)
}} hufmtree = (huffmannode*)malloc(sizeof(huffmannode));
hufmtree->weight = temp[small1]->weight + temp[small2]->weight;
hufmtree->lchild = temp[small1];
hufmtree->rchild = temp[small2];
temp[small1] = hufmtree;
temp[small2] = null;
} return hufmtree;
}// 遞迴進行哈夫曼編碼
void huffmancode(huffmannode* hufmtree, int depth,int*code) // depth是哈夫曼樹的深度
哈夫曼編碼 哈夫曼樹
1.定義 哈夫曼編碼主要用於資料壓縮。哈夫曼編碼是一種可變長編碼。該編碼將出現頻率高的字元,使用短編碼 將出現頻率低的字元,使用長編碼。變長編碼的主要問題是,必須實現非字首編碼,即在乙個字符集中,任何乙個字元的編碼都不是另乙個字元編碼的字首。如 0 10就是非字首編碼,而0 01不是非字首編碼。2....
哈夫曼樹 哈夫曼編碼
定義從a結點到b結點所經過的分支序列為從a結點到b結點的路徑 定義從a結點到b結點所進過的分支個數為從a結點到b結點的路徑長度 從二叉樹的根結點到二叉樹中所有結點的路徑長度紙盒為該二叉樹的路徑長度 huffman樹 帶權值路徑長度最小的擴充二叉樹應是權值大的外界點舉例根結點最近的擴充二叉樹,該樹即為...
哈夫曼編碼 哈夫曼樹
哈夫曼樹是乙個利用權值進行優化編碼的乙個比較奇怪的樹,他的實現比較簡單,用途也比較單一。哈夫曼樹的實現,實現要求 通過哈夫曼樹可以保證在編碼過程中不會出現例如 1000和100這樣的編碼規則,否則就會編碼失敗,因為1000和100在某些情況下的編碼會一模一樣。通過哈夫曼樹可以保證權值大的值進行編碼時...