public huffman extractMin() { huffman min = arr[0]; delete(0); return(min); }
void minHeapify(huffman[] arr, int i) { int min = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < size && arr[left].data < arr[min].data) { min = left; } if (right < size && arr[right].data < arr[min].data) { min = right; } if (min != i) { huffman temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; minHeapify(arr, min); } }
void buildTree(minheap obj) { while (getSize(obj) >= 1) { if (top == null) { huffman left = obj.extractMin(); huffman right = obj.extractMin(); top = new huffman(left.data + right.data, '-'); top.left = left; top.right = right; obj.insert(left.data + right.data, '-'); } else { huffman left = obj.extractMin(); huffman right = top; huffman obj_huff = new huffman(left.data + right.data, '-'); obj_huff.left = left; obj_huff.right = right; top = obj_huff; obj.insert(left.data + right.data, '-'); } } }
public huffman(int data, char c) { this.data = data; this.c = c; left = null; right = null; }
huffman[] getArrayFromDict(Dictionary <char, int> dict) { int index = 0; huffman[] arr = new huffman[dict.Count]; foreach (KeyValuePair <char, int> pair in dict) { huffman obj = new huffman(pair.Value, pair.Key); arr[index] = obj; index++; } return(arr); }
void printtree(huffman root, string str) { if (root == null) { return; } if (root.left == null && root.right == null) { Console.WriteLine(root.c + ":" + str); } printtree(root.left, str + "0"); printtree(root.right, str + "1"); }
public void insert(int data, char c) { size++; int i = size - 1; arr[i] = new huffman(data, c); while (i != 0 && arr[getParent(i)].data > arr[i].data) { huffman temp = arr[getParent(i)]; arr[getParent(i)] = arr[i]; arr[i] = temp; i = getParent(i); } }