1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
using namespace std;
int w1[11] = {13,1,45,7,20,4,19,13,40,33,38};
typedef struct TreeNode *HuffmanTree; typedef struct HeapStruct *MinHeap; struct HeapStruct { HuffmanTree *Data; int Size; int Capacity; }; struct TreeNode { int Weight; HuffmanTree Left; HuffmanTree Right; };
MinHeap CreateHeap(); // 初始化堆 HuffmanTree Create(); // 初始化哈夫曼树 void sort(MinHeap H, int i); // 调整子最小堆 void adjust(MinHeap H); // 调整最小堆 void BuildMinHeap(MinHeap H); // 建堆 HuffmanTree Delete(MinHeap H); // 删除最小堆元素 void Insert(MinHeap H, HuffmanTree Huff); // 插入最小堆元素 void PreOrderTraversal(HuffmanTree Huff); // 先序遍历 HuffmanTree Huffman(MinHeap H); // 哈夫曼树的构建
MinHeap CreateHeap() { MinHeap H = new struct HeapStruct; H->Data = new struct TreeNode*[MaxSize+1]; H->Capacity = MaxSize; H->Size = 0;
// 哨兵 HuffmanTree Huff = Create(); Huff->Weight = MinData; H->Data[0] = Huff; return H; }
HuffmanTree Create() { HuffmanTree Huff = new struct TreeNode; Huff->Weight = 0; Huff->Left = Huff->Right = NULL; return Huff; }
void sort(MinHeap H, int i) { int parent, child; int tmp = H->Data[i]->Weight; for(parent = i; parent*2 <= H->Size; parent = child) { child = 2*parent; if(child!=H->Size && H->Data[child+1]->Weight < H->Data[child]->Weight) child++; if(H->Data[child]->Weight >= tmp) break; else H->Data[parent] = H->Data[child]; } H->Data[parent]->Weight = tmp; }
void adjust(MinHeap H) { for(int i=H->Size/2; i>0; i--) sort(H, i); }
void BuildMinHeap(MinHeap H) { HuffmanTree Huff; for(int i=0; i<sizeof(w1)/sizeof(int); i++) { Huff = Create(); Huff->Weight = w1[i]; H->Data[++H->Size] = Huff; } adjust(H); }
HuffmanTree Delete(MinHeap H) { int parent, child; HuffmanTree T = H->Data[1]; HuffmanTree tmp = H->Data[H->Size--]; for(parent = 1; parent*2 <= H->Size; parent = child) { child = 2*parent; if(child!=H->Size && H->Data[child+1]->Weight < H->Data[child]->Weight) child++; if(H->Data[child]->Weight >= tmp->Weight) break; else H->Data[parent] = H->Data[child]; } H->Data[parent] = tmp; return T; }
void Insert(MinHeap H, HuffmanTree Huff) { int Weight = Huff->Weight; int i = ++H->Size; for(; H->Data[i/2]->Weight > Weight; i/=2) H->Data[i] = H->Data[i/2]; H->Data[i] = Huff; }
void PreOrderTraversal(HuffmanTree Huff) { if(Huff) { cout << Huff->Weight << " "; PreOrderTraversal(Huff->Left); PreOrderTraversal(Huff->Right); } // cout << endl; }
HuffmanTree Huffman(MinHeap H) { int i; HuffmanTree T; BuildMinHeap(H); for(i = 1; i<H->Size; i++) { T = new struct TreeNode; T->Left = Delete(H); T->Right = Delete(H); T->Weight = T->Left->Weight + T->Right->Weight; Insert(H, T); } T = Delete(H); return T; }
|