From ebc76fae34f5881155fa49224019fefc0191df46 Mon Sep 17 00:00:00 2001 From: Naman Singh <85234010+Naman-OO7@users.noreply.github.com> Date: Wed, 19 Oct 2022 19:23:22 +0530 Subject: [PATCH] Add files via upload added a tree program on Huffman Encoding. --- C++/MEX.cpp | 62 ++++++++++++++++++++++++++++++ C++/hauff.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 C++/MEX.cpp create mode 100644 C++/hauff.cpp diff --git a/C++/MEX.cpp b/C++/MEX.cpp new file mode 100644 index 0000000..9bd90ab --- /dev/null +++ b/C++/MEX.cpp @@ -0,0 +1,62 @@ + +#include +using namespace std; + +#define MAXN 100001 + +void constructMEX(int arr[], int N) +{ + + int hash[MAXN] = { 0 }; + + + for (int i = 0; i < N; i++) { + + hash[arr[i]] = 1; + } + + + int MexOfArr; + + // Find MEX of arr[] + for (int i = 1; i < MAXN; i++) { + if (hash[i] == 0) { + MexOfArr = i; + break; + } + } + + // Stores MEX for all indices + int B[N]; + + // Traverse the given array + for (int i = 0; i < N; i++) { + + // Update MEX + if (arr[i] < MexOfArr) + B[i] = arr[i]; + + // MEX default + else + B[i] = MexOfArr; + } + + // Print the array B + for (int i = 0; i < N; i++) + cout << B[i] << ' '; +} + +// Driver Code +int main() +{ + // Given array + int arr[] = { 2, 1, 5, 3 }; + + // Given size + int N = sizeof(arr) + / sizeof(arr[0]); + + // Function call + constructMEX(arr, N); + return 0; +} \ No newline at end of file diff --git a/C++/hauff.cpp b/C++/hauff.cpp new file mode 100644 index 0000000..9ed1123 --- /dev/null +++ b/C++/hauff.cpp @@ -0,0 +1,102 @@ + + +#include +using namespace std; + +// A Huffman tree node +struct MinHeapNode { + + // One of the input characters + char data; + + // Frequency of the character + unsigned freq; + + // Left and right child + MinHeapNode *left, *right; + + MinHeapNode(char data, unsigned freq) + + { + + left = right = NULL; + this->data = data; + this->freq = freq; + } +}; + +// For comparison of +// two heap nodes (needed in min heap) +struct compare { + + bool operator()(MinHeapNode* l, MinHeapNode* r) + + { + return (l->freq > r->freq); + } +}; + +// Prints huffman codes from +// the root of Huffman Tree. +void printCodes(struct MinHeapNode* root, string str) +{ + + if (!root) + return; + + if (root->data != '$') + cout << root->data << ": " << str << "\n"; + + printCodes(root->left, str + "0"); + printCodes(root->right, str + "1"); +} + +// The main function that builds a Huffman Tree and +// print codes by traversing the built Huffman Tree +void HuffmanCodes(char data[], int freq[], int size) +{ + struct MinHeapNode *left, *right, *top; + + // Create a min heap & inserts all characters of data[] + priority_queue, compare> minHeap; + + for (int i = 0; i < size; ++i) + minHeap.push(new MinHeapNode(data[i], freq[i])); + + // Iterate while size of heap doesn't become 1 + while (minHeap.size() != 1) { + + // Extract the two minimum + // freq items from min heap + left = minHeap.top(); + minHeap.pop(); + + right = minHeap.top(); + minHeap.pop(); + + top = new MinHeapNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + minHeap.push(top); + } + + // Print Huffman codes using + // the Huffman tree built above + printCodes(minHeap.top(), ""); +} + +// Driver Code +int main() +{ + + char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; + int freq[] = { 5, 9, 12, 13, 16, 45 }; + + int size = sizeof(arr) / sizeof(arr[0]); + + HuffmanCodes(arr, freq, size); + + return 0; +} \ No newline at end of file