6 #define MAX_TREE_HT 100
12 struct MH_Node *l, *r;
20 struct MH_Node **array;
23 struct MH_Node* newNode(int character, float frequency)
25 struct MH_Node* temp = (struct MH_Node*) malloc(sizeof(struct MH_Node));
26 temp->l = temp->r = NULL;
27 temp->character = character;
28 temp->frequency = frequency;
33 struct M_Heap* createM_Heap(unsigned space)
35 struct M_Heap* M_Heap = (struct M_Heap*) malloc(sizeof(struct M_Heap));
37 M_Heap->space = space;
38 M_Heap->array = (struct MH_Node**)malloc(M_Heap->space * sizeof(struct MH_Node*));
43 void swapMH_Node(struct MH_Node** a, struct MH_Node** b)
45 struct MH_Node* t = *a;
51 void M_Heapify(struct M_Heap* M_Heap, int idx)
53 // arrange in increasing frequency order
54 for (int i=idx;i<M_Heap->size;i++) {
55 for (int j=i+1;j<M_Heap->size;j++) {
56 if (M_Heap->array[i]->frequency > M_Heap->array[j]->frequency)
57 swapMH_Node(&M_Heap->array[i], &M_Heap->array[j]);
62 int isSizeOne(struct M_Heap* M_Heap)
64 return (M_Heap->size == 1);
68 struct MH_Node* extractMin(struct M_Heap* M_Heap)
70 struct MH_Node* temp = M_Heap->array[0];
71 M_Heap->array[0] = M_Heap->array[M_Heap->size - 1];
78 void insertM_Heap(struct M_Heap* M_Heap, struct MH_Node* MH_Node)
80 // printf("Insert %f\n",MH_Node->frequency);
81 int i = M_Heap->size - 1;
83 M_Heap->array[M_Heap->size++] = MH_Node;
86 M_Heap->array[M_Heap->size] = MH_Node;
92 void buildM_Heap(struct M_Heap* M_Heap)
98 void printArr(int arr[], int n)
101 for (i = 0; i < n; ++i) printf("%d", arr[i]);
106 int isLeaf(struct MH_Node* root)
108 return !(root->l) && !(root->r) ;
112 struct M_Heap* createAndBuildM_Heap(int character[], float frequency[], int size)
115 struct M_Heap* M_Heap = createM_Heap(size);
116 for (i = 0; i < size; ++i) M_Heap->array[i] = newNode(character[i], frequency[i]);
122 struct MH_Node* buildHuffmanTree(int character[], float frequency[], int size)
124 struct MH_Node *l, *r, *top;
125 struct M_Heap* M_Heap = createAndBuildM_Heap(character, frequency, size);
126 while (!isSizeOne(M_Heap)) {
127 l = extractMin(M_Heap);
128 r = extractMin(M_Heap);
129 top = newNode('$', l->frequency + r->frequency);
132 insertM_Heap(M_Heap, top);
134 return extractMin(M_Heap);
137 float printCodes(struct MH_Node* root, int arr[], int top)
143 totsiz += printCodes(root->l, arr, top + 1);
148 totsiz += printCodes(root->r, arr, top + 1);
152 printf("%d: (%f) ", root->character, root->frequency);
153 totsiz += top*root->frequency;
159 void HuffmanCodes(int character[], float frequency[], int size)
161 struct MH_Node* root = buildHuffmanTree(character, frequency, size);
162 int arr[MAX_TREE_HT], top = 0;
163 int defNbits = 1 + TMath::Log2(size);
165 for (int i=0;i<size;i++) {
166 freqNorm += frequency[i];
168 float packSize = printCodes(root, arr, top);
170 printf("Brute force: %d vs %f coded\n",defNbits,packSize/freqNorm);
174 void HuffmanCodes(TH1* histo, int maxBin=-1)
176 int nb = histo->GetNbinsX();
177 if (maxBin>1 && nb>maxBin) nb = maxBin;
180 for (int i=0;i<nb;i++) {
182 frq[i] = histo->GetBinContent(i+1);
184 HuffmanCodes(dat.GetArray(),frq.GetArray(),nb);