diff --git a/hoffman_greedy_algo.c b/hoffman_greedy_algo.c new file mode 100644 index 0000000..f0ef6dd --- /dev/null +++ b/hoffman_greedy_algo.c @@ -0,0 +1,299 @@ +// C program for Huffman Coding +#include +#include + +// This constant can be avoided by explicitly +// calculating height of Huffman Tree +#define MAX_TREE_HT 100 + +// A Huffman tree node +struct MinHeapNode { + + // One of the input characters + char data; + + // Frequency of the character + unsigned freq; + + // Left and right child of this node + struct MinHeapNode *left, *right; +}; + +// A Min Heap: Collection of +// min-heap (or Huffman tree) nodes +struct MinHeap { + + // Current size of min heap + unsigned size; + + // capacity of min heap + unsigned capacity; + + // Array of minheap node pointers + struct MinHeapNode** array; +}; + +// A utility function allocate a new +// min heap node with given character +// and frequency of the character +struct MinHeapNode* newNode(char data, unsigned freq) +{ + struct MinHeapNode* temp + = (struct MinHeapNode*)malloc +(sizeof(struct MinHeapNode)); + + temp->left = temp->right = NULL; + temp->data = data; + temp->freq = freq; + + return temp; +} + +// A utility function to create +// a min heap of given capacity +struct MinHeap* createMinHeap(unsigned capacity) + +{ + + struct MinHeap* minHeap + = (struct MinHeap*)malloc(sizeof(struct MinHeap)); + + // current size is 0 + minHeap->size = 0; + + minHeap->capacity = capacity; + + minHeap->array + = (struct MinHeapNode**)malloc(minHeap-> +capacity * sizeof(struct MinHeapNode*)); + return minHeap; +} + +// A utility function to +// swap two min heap nodes +void swapMinHeapNode(struct MinHeapNode** a, + struct MinHeapNode** b) + +{ + + struct MinHeapNode* t = *a; + *a = *b; + *b = t; +} + +// The standard minHeapify function. +void minHeapify(struct MinHeap* minHeap, int idx) + +{ + + int smallest = idx; + int left = 2 * idx + 1; + int right = 2 * idx + 2; + + if (left < minHeap->size && minHeap->array[left]-> +freq < minHeap->array[smallest]->freq) + smallest = left; + + if (right < minHeap->size && minHeap->array[right]-> +freq < minHeap->array[smallest]->freq) + smallest = right; + + if (smallest != idx) { + swapMinHeapNode(&minHeap->array[smallest], + &minHeap->array[idx]); + minHeapify(minHeap, smallest); + } +} + +// A utility function to check +// if size of heap is 1 or not +int isSizeOne(struct MinHeap* minHeap) +{ + + return (minHeap->size == 1); +} + +// A standard function to extract +// minimum value node from heap +struct MinHeapNode* extractMin(struct MinHeap* minHeap) + +{ + + struct MinHeapNode* temp = minHeap->array[0]; + minHeap->array[0] + = minHeap->array[minHeap->size - 1]; + + --minHeap->size; + minHeapify(minHeap, 0); + + return temp; +} + +// A utility function to insert +// a new node to Min Heap +void insertMinHeap(struct MinHeap* minHeap, + struct MinHeapNode* minHeapNode) + +{ + + ++minHeap->size; + int i = minHeap->size - 1; + + while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) { + + minHeap->array[i] = minHeap->array[(i - 1) / 2]; + i = (i - 1) / 2; + } + + minHeap->array[i] = minHeapNode; +} + +// A standard function to build min heap +void buildMinHeap(struct MinHeap* minHeap) + +{ + + int n = minHeap->size - 1; + int i; + + for (i = (n - 1) / 2; i >= 0; --i) + minHeapify(minHeap, i); +} + +// A utility function to print an array of size n +void printArr(int arr[], int n) +{ + int i; + for (i = 0; i < n; ++i) + printf("%d", arr[i]); + + printf("\n"); +} + +// Utility function to check if this node is leaf +int isLeaf(struct MinHeapNode* root) + +{ + + return !(root->left) && !(root->right); +} + +// Creates a min heap of capacity +// equal to size and inserts all character of +// data[] in min heap. Initially size of +// min heap is equal to capacity +struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size) + +{ + + struct MinHeap* minHeap = createMinHeap(size); + + for (int i = 0; i < size; ++i) + minHeap->array[i] = newNode(data[i], freq[i]); + + minHeap->size = size; + buildMinHeap(minHeap); + + return minHeap; +} + +// The main function that builds Huffman tree +struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) + +{ + struct MinHeapNode *left, *right, *top; + + // Step 1: Create a min heap of capacity + // equal to size. Initially, there are + // modes equal to size. + struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size); + + // Iterate while size of heap doesn't become 1 + while (!isSizeOne(minHeap)) { + + // Step 2: Extract the two minimum + // freq items from min heap + left = extractMin(minHeap); + right = extractMin(minHeap); + + // Step 3: Create a new internal + // node with frequency equal to the + // sum of the two nodes frequencies. + // Make the two extracted node as + // left and right children of this new node. + // Add this node to the min heap + // '$' is a special value for internal nodes, not used + top = newNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + insertMinHeap(minHeap, top); + } + + // Step 4: The remaining node is the + // root node and the tree is complete. + return extractMin(minHeap); +} + +// Prints huffman codes from the root of Huffman Tree. +// It uses arr[] to store codes +void printCodes(struct MinHeapNode* root, int arr[], int top) + +{ + + // Assign 0 to left edge and recur + if (root->left) { + + arr[top] = 0; + printCodes(root->left, arr, top + 1); + } + + // Assign 1 to right edge and recur + if (root->right) { + + arr[top] = 1; + printCodes(root->right, arr, top + 1); + } + + // If this is a leaf node, then + // it contains one of the input + // characters, print the character + // and its code from arr[] + if (isLeaf(root)) { + + printf("%c: ", root->data); + printArr(arr, top); + } +} + +// 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) + +{ + // Construct Huffman Tree + struct MinHeapNode* root + = buildHuffmanTree(data, freq, size); + + // Print Huffman codes using + // the Huffman tree built above + int arr[MAX_TREE_HT], top = 0; + + printCodes(root, arr, top); +} + +// Driver program to test above functions +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; +} diff --git a/operation_in_heap.cpp b/operation_in_heap.cpp new file mode 100644 index 0000000..7a9fd92 --- /dev/null +++ b/operation_in_heap.cpp @@ -0,0 +1,159 @@ +// A C++ program to demonstrate common Binary Heap Operations +#include +#include +using namespace std; + +// Prototype of a utility function to swap two integers +void swap(int *x, int *y); + +// A class for Min Heap +class MinHeap +{ + int *harr; // pointer to array of elements in heap + int capacity; // maximum possible size of min heap + int heap_size; // Current number of elements in min heap +public: + // Constructor + MinHeap(int capacity); + + // to heapify a subtree with the root at given index + void MinHeapify(int ); + + int parent(int i) { return (i-1)/2; } + + // to get index of left child of node at index i + int left(int i) { return (2*i + 1); } + + // to get index of right child of node at index i + int right(int i) { return (2*i + 2); } + + // to extract the root which is the minimum element + int extractMin(); + + // Decreases key value of key at index i to new_val + void decreaseKey(int i, int new_val); + + // Returns the minimum key (key at root) from min heap + int getMin() { return harr[0]; } + + // Deletes a key stored at index i + void deleteKey(int i); + + // Inserts a new key 'k' + void insertKey(int k); +}; + +// Constructor: Builds a heap from a given array a[] of given size +MinHeap::MinHeap(int cap) +{ + heap_size = 0; + capacity = cap; + harr = new int[cap]; +} + +// Inserts a new key 'k' +void MinHeap::insertKey(int k) +{ + if (heap_size == capacity) + { + cout << "\nOverflow: Could not insertKey\n"; + return; + } + + // First insert the new key at the end + heap_size++; + int i = heap_size - 1; + harr[i] = k; + + // Fix the min heap property if it is violated + while (i != 0 && harr[parent(i)] > harr[i]) + { + swap(&harr[i], &harr[parent(i)]); + i = parent(i); + } +} + +// Decreases value of key at index 'i' to new_val. It is assumed that +// new_val is smaller than harr[i]. +void MinHeap::decreaseKey(int i, int new_val) +{ + harr[i] = new_val; + while (i != 0 && harr[parent(i)] > harr[i]) + { + swap(&harr[i], &harr[parent(i)]); + i = parent(i); + } +} + +// Method to remove minimum element (or root) from min heap +int MinHeap::extractMin() +{ + if (heap_size <= 0) + return INT_MAX; + if (heap_size == 1) + { + heap_size--; + return harr[0]; + } + + // Store the minimum value, and remove it from heap + int root = harr[0]; + harr[0] = harr[heap_size-1]; + heap_size--; + MinHeapify(0); + + return root; +} + + +// This function deletes key at index i. It first reduced value to minus +// infinite, then calls extractMin() +void MinHeap::deleteKey(int i) +{ + decreaseKey(i, INT_MIN); + extractMin(); +} + +// A recursive method to heapify a subtree with the root at given index +// This method assumes that the subtrees are already heapified +void MinHeap::MinHeapify(int i) +{ + int l = left(i); + int r = right(i); + int smallest = i; + if (l < heap_size && harr[l] < harr[i]) + smallest = l; + if (r < heap_size && harr[r] < harr[smallest]) + smallest = r; + if (smallest != i) + { + swap(&harr[i], &harr[smallest]); + MinHeapify(smallest); + } +} + +// A utility function to swap two elements +void swap(int *x, int *y) +{ + int temp = *x; + *x = *y; + *y = temp; +} + +// Driver program to test above functions +int main() +{ + MinHeap h(11); + h.insertKey(3); + h.insertKey(2); + h.deleteKey(1); + h.insertKey(15); + h.insertKey(5); + h.insertKey(4); + h.insertKey(45); + cout << h.extractMin() << " "; + cout << h.getMin() << " "; + h.decreaseKey(2, 1); + cout << h.getMin(); + return 0; +} diff --git a/operation_in_tree.c b/operation_in_tree.c new file mode 100644 index 0000000..20f5809 --- /dev/null +++ b/operation_in_tree.c @@ -0,0 +1,69 @@ +// C program to demonstrate insert operation in binary search tree +#include +#include + +struct node +{ + int key; + struct node *left, *right; +}; + +// A utility function to create a new BST node +struct node *newNode(int item) +{ + struct node *temp = (struct node *)malloc(sizeof(struct node)); + temp->key = item; + temp->left = temp->right = NULL; + return temp; +} + +// A utility function to do inorder traversal of BST +void inorder(struct node *root) +{ + if (root != NULL) + { + inorder(root->left); + printf("%d \n", root->key); + inorder(root->right); + } +} + +/* A utility function to insert a new node with given key in BST */ +struct node* insert(struct node* node, int key) +{ + /* If the tree is empty, return a new node */ + if (node == NULL) return newNode(key); + + /* Otherwise, recur down the tree */ + if (key < node->key) + node->left = insert(node->left, key); + else if (key > node->key) + node->right = insert(node->right, key); + + /* return the (unchanged) node pointer */ + return node; +} + +// Driver Program to test above functions +int main() +{ + /* Let us create following BST + 50 + / \ + 30 70 + / \ / \ + 20 40 60 80 */ + struct node *root = NULL; + root = insert(root, 50); + insert(root, 30); + insert(root, 20); + insert(root, 40); + insert(root, 70); + insert(root, 60); + insert(root, 80); + + // print inoder traversal of the BST + inorder(root); + + return 0; +}