生活随笔
收集整理的這篇文章主要介紹了
数据结构源码笔记(C语言):哈夫曼树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h>
#include <stdlib.h>
#define MAXINT 2147483647
#define MAXNUM 50
#define MAXNODE 100struct HtNode
{int ww
;int parent
,llink
,rlink
;
};struct HtTree
{struct HtNode ht
[MAXNODE
];int root
;
};typedef struct HtTree
*PHtTree
;PHtTree
huffman(int m
,int *w
)
{PHtTree pht
;int i
,j
,x1
,x2
,m1
,m2
;pht
=(PHtTree
)malloc(sizeof(struct HtTree
)); if(pht
==NULL){printf("Out of space!!\n");return pht
;}for(i
=0;i
<2*m
-1;i
++) {pht
->ht
[i
].llink
=-1;pht
->ht
[i
].rlink
=-1;pht
->ht
[i
].parent
=-1;if(i
<m
)pht
->ht
[i
].ww
=w
[i
];elsepht
->ht
[i
].ww
=-1;}for(i
=0;i
<m
-1;i
++) {m1
=MAXINT
;m2
=MAXINT
; x1
=-1;x2
=-1;for(j
=0;j
<m
+1;j
++) if(pht
->ht
[j
].ww
<m1
&&pht
->ht
[j
].parent
==-1){m2
=m1
;x2
=x1
;m1
=pht
->ht
[j
].ww
;x1
=j
;}elseif(pht
->ht
[j
].ww
<m2
&&pht
->ht
[j
].parent
==-1){m2
=pht
->ht
[j
].ww
;x2
=j
;}pht
->ht
[x1
].parent
=m
+i
; pht
->ht
[x2
].parent
=m
+i
;pht
->ht
[m
+i
].ww
=m1
+m2
;pht
->ht
[m
+i
].llink
=x1
;pht
->ht
[m
+i
].rlink
=x2
;pht
->root
=m
+i
;}return pht
;
}void printcode(PHtTree pht
,int m
)
{int i
,j
;int parentnode
;for(j
=0;j
<m
;j
++){if(pht
->ht
[j
].llink
!=-1||pht
->ht
[j
].rlink
!=-1) continue;printf("the Reverse conde of node%d is :",j
+1); i
=j
;while(pht
->ht
[i
].parent
!=-1){parentnode
=pht
->ht
[i
].parent
;if(pht
->ht
[parentnode
].llink
==i
)printf("0");elseprintf("1");i
=parentnode
;}printf("\n");}
}int main()
{int m
=6;int w
[]={1,3,5,7,9,11};PHtTree pht
;pht
=huffman(6,w
);printcode(pht
,m
); return 0;
}
數據結構源碼筆記(C語言描述)匯總:
數據結構源碼筆記(C語言):英文單詞按字典序排序的基數排序
數據結構源碼筆記(C語言):直接插入排序
數據結構源碼筆記(C語言):直接選擇排序
數據結構源碼筆記(C語言):置換-選擇算法
數據結構源碼筆記(C語言):Huffman樹字符編碼
數據結構源碼筆記(C語言):Josephus問題之順序表
數據結構源碼筆記(C語言):Josephus問題之循環鏈接表
數據結構源碼筆記(C語言):多項式合并
數據結構源碼筆記(C語言):二叉樹之葉子結點旋轉銷毀
數據結構源碼筆記(C語言):哈夫曼樹
數據結構源碼筆記(C語言):集合的位向量表示
數據結構源碼筆記(C語言):鏈接隊列
數據結構源碼筆記(C語言):鏈接棧
數據結構源碼筆記(C語言):線性表的單鏈表示
數據結構源碼筆記(C語言):線性表的順序表示
數據結構源碼筆記(C語言):棧的基本操作
數據結構源碼筆記(C語言):中綴表達式
數據結構源碼筆記(C語言):希爾插入排序
數據結構源碼筆記(C語言):索引文件建立和查找
數據結構源碼筆記(C語言):冒泡排序
數據結構源碼筆記(C語言):快速排序
數據結構源碼筆記(C語言):可變長度字符串的快速排序
數據結構源碼筆記(C語言):基數排序
數據結構源碼筆記(C語言):二路歸并排序
數據結構源碼筆記(C語言):堆排序
數據結構源碼筆記(C語言):二叉樹搜索樹Kruskal
數據結構源碼筆記(C語言):二叉搜索樹Prim
數據結構源碼筆記(C語言):最短路徑弗洛伊德算法
數據結構源碼筆記(C語言):深度、廣度優先生成樹
數據結構源碼筆記(C語言):鄰接矩陣轉化鄰接表
數據結構源碼筆記(C語言):統計字符串中出現的字符及其次數
數據結構源碼筆記(C語言):順序查找
數據結構源碼筆記(C語言):哈希表的相關運算算法
數據結構源碼筆記(C語言):分塊法查找
數據結構源碼筆記(C語言):二分查找
數據結構源碼筆記(C語言):二叉樹遍歷
數據結構源碼筆記(C語言):二叉平衡樹的相關操作算法
數據結構源碼筆記(C語言):二叉排序樹的基本操作算法
數據結構源碼筆記(C語言):B樹的相關運算算法
總結
以上是生活随笔為你收集整理的数据结构源码笔记(C语言):哈夫曼树的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。