From 3285018df0f9d825dbe4daf7229a612eaf90821d Mon Sep 17 00:00:00 2001 From: Sid200026 Date: Thu, 24 Oct 2019 21:01:34 +0530 Subject: [PATCH] Updated Heap and added hash with separate chaining --- .gitignore | 1 + Heaps.cpp | 282 ++++++++++++++++++++++++++++++----------------------- hash.cpp | 133 +++++++++++++++++++++++++ 3 files changed, 295 insertions(+), 121 deletions(-) create mode 100644 .gitignore create mode 100644 hash.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8305e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/Heaps.cpp b/Heaps.cpp index a6f77ce..5757888 100644 --- a/Heaps.cpp +++ b/Heaps.cpp @@ -1,135 +1,175 @@ -#include -using namespace std; - -void insert(int* arr, int n, int &last){ - if(last == 100){ - cout << "Heap Full" << endl; - return; - } - else{ - arr[++last] = n; - int i = last; - while(i>1){ - if(arr[i] > arr[i/2]){ - swap(arr[i],arr[i/2]); - i = i/2; - } - else{ - return; - } - } - } +#include +using namespace std; + +// A heap can be implemented using an array +// The child of parent node ,which is at index i in array +// will always be at index 2i+1 and 2i+2 +class heap { + int arr[50]; + int count; + public: + heap(); + bool isFull(); + bool isEmpty(); + void insert(int); + int getSize(); // Get the current size of heap + void heapify(int); + void build_max_heap(); // Build a max heap + void display_heap(); // Print the elements in the heap + int delete_first_element(); // Remove the first element and return it + int get_left_child(int); // Returns the left child of a parent + int get_right_child(int); // Returns the right child of a parent + int get_parent(int); // Returns the value of parent index + int get_max(); // Returns the maximum element in the heap + void replace(int, int); // Replaces the key value at an index + void siftUp(int); // To re-convert the changed heap into max heap +}; +// Here we assume that the heap can have a maximum size of 50 +heap::heap() { + count = 0; } -void deleteElement(int* arr, int& last){ - arr[1] = arr[last]; - last --; - bool check = true; - int i = 1; - while(check == true){ - int maxno; - int tempdig; - if(arr[2*i] > arr[2*i + 1]){ - tempdig = 2*i; - } - else{ - tempdig = 2*i + 1; - } +bool heap::isFull() { + return count == 50; +} + +bool heap::isEmpty() { + return count == 0; +} - if(arr[i] < arr[tempdig]){ - swap(arr[i],arr[tempdig]); - i = tempdig; - check = true; +void heap::insert(int val) { + if(!isFull()) { + arr[count++] = val; + siftUp(count-1); + } +} + +int heap::getSize() { + return count+1; +} + +// This program demonstrates the logic to build max_heaps +// To build min_heaps simply change the following lines +// Line 54: arr[maxindex] > arr[leftchild] +// Line 62: arr[maxindex] > arr[rightchild] +void heap::heapify(int index) { + int parent = index; + int leftchild = 2*index+1; + int rightchild = 2*index+2; + int maxindex = parent; + if(leftchild < count) { + if(arr[maxindex] < arr[leftchild]) { + maxindex = leftchild; } - else{ - check = false; + } + if(rightchild < count) { + if(arr[maxindex] < arr[rightchild]) { + maxindex = rightchild; } } + if(maxindex != index) { + int temp = arr[maxindex]; + arr[maxindex] = arr[index]; + arr[index] = temp; + heapify(maxindex); + } } -void printArray(int *arr, int last){ - for(int i = 1; i <=last;i++){ - cout << arr[i] << " | "; +void heap::build_max_heap() { + for(int i = count/2; i >= 0; i--) { + heapify(i); } } -void heapify(int arr[], int n, int i) -{ - int largest = i; - int l = 2*i + 1; - int r = 2*i + 2; - - if (l < n && arr[l] > arr[largest]) - largest = l; - - if (r < n && arr[r] > arr[largest]) - largest = r; - - if (largest != i) - { - swap(arr[i], arr[largest]); - - heapify(arr, n, largest); - } -} - -void heapSort(int arr[], int n) -{ - for (int i = n / 2 - 1; i >= 0; i--) - heapify(arr, n, i); - - for (int i=n-1; i>=0; i--) - { - swap(arr[0], arr[i]); - heapify(arr, i, 0); - } -} - -void printArrayHeapSort(int *arr, int size){ - for(int i=0; i> c; - switch(c){ - case 1: - cout << "Enter an element" << endl; - cin >> elem; - insert(arr,elem,last); - break; - - case 2: - deleteElement(arr,last); - break; - - case 3: - printArray(arr,last); - break; - case 4: - int size; - cout << "Enter the size of array" << endl; - cin >> size; - int harr[size]; - for(int x = 0; x < size; x++){ - cout << "Enter Elements" << endl; - cin >> harr[x]; - } - heapSort(harr,size); - cout << "The sorted array" << endl; - printArrayHeapSort(harr,size); - break; - } + cout << "\n"; +} + +// This also returns the largest element in the heap +int heap::delete_first_element() { + if(isEmpty()) { + return -1; + } else { + int temp = arr[0]; + arr[0] = arr[count-1]; + arr[count-1] = temp; + heapify(0); + count--; + return arr[count]; + } +} + +int heap::get_left_child(int i) { + if(2*i+1 < count) { + return arr[2*i+1]; + } else { + return -1; + } +} + +int heap::get_right_child(int i) { + if(2*i+2 < count) { + return arr[2*i+2]; + } else { + return -1; } +} -} \ No newline at end of file +int heap::get_parent(int i) { + if(i < count) { + return arr[i]; + } else { + return -1; + } +} + +int heap::get_max() { + if(isEmpty()) { + return -1; + } else { + return arr[0]; + } +} + +void heap::replace(int value, int index) { + if(index < count) { + arr[index] = value; + siftUp(index); + } +} + +void heap::siftUp(int index) { + while(index != 0) { + index = (index-1)/2; // To get the parent of a child + heapify(index); + } +} + +// Driver method +int main() { + heap hp; + hp.insert(4); + hp.insert(2); + hp.insert(6); + hp.insert(7); + hp.insert(4); + hp.insert(9); + hp.insert(1); + hp.insert(5); + hp.insert(19); + hp.insert(5); + hp.insert(7); + hp.build_max_heap(); + hp.display_heap(); + cout << hp.delete_first_element() << "\n"; + hp.display_heap(); + cout << hp.get_max() << "\n"; + cout << "Parent" << hp.get_parent(2) << "\n"; + cout << "Left" << hp.get_left_child(2) << "\n"; + cout << "Right" << hp.get_right_child(2) << "\n"; + hp.replace(20, 9); + hp.display_heap(); +} diff --git a/hash.cpp b/hash.cpp new file mode 100644 index 0000000..7490afe --- /dev/null +++ b/hash.cpp @@ -0,0 +1,133 @@ +// Hash with separate chaining +#include +using namespace std; + +struct node +{ + int data; + node *link; +}; + +class linked_list +{ +private: + node *cur,*prev; +public: + node *head; + linked_list(); + void addbegin(int); + int search(int); + void deletepos(int); + void traverse(); +}; + +linked_list::linked_list() +{ + head = NULL; + cur = head; + prev = head; +} + +void linked_list::traverse() +{ + cur = head; + while(cur!=NULL) + { + cout<data<<" "; + cur = cur->link; + } + cout<data = item; + temp->link = head; + head = temp; +} + +int linked_list::search(int val) +{ + int count = 0; + cur = head; + while(cur!=NULL) + { + if(cur->data == val) + return count; + count++; + cur = cur->link; + } + return -1; +} + +void linked_list::deletepos(int pos) +{ + int position = 0; + prev = head; + cur = head; + while(pos!=position) + { + prev = cur; + position++; + cur = cur->link; + if(cur==NULL) + { + cout<<"Position out of bounds\n"; + return; + } + } + if(cur==head) + { + head = cur->link; + } + prev->link = cur->link; + delete cur; +} + +int hashfunc(int key, int size) +{ + int value = key; + return value % size; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int arr[5] = {400, 0, 600, 1, 2}; + linked_list hash[200]; +/* for(int i= 0;i<200;i++) + { + hash[i] = -1; + } */ + for(int j=0;j<5;j++) + { + int index = hashfunc(arr[j], 200); + hash[index].addbegin(arr[j]); + } + for(int i = 0;i<200;i++) + { +/* if(hash[i]==-1) + continue; */ + if(hash[i].head == NULL) + continue; + hash[i].traverse(); + } + + int z = 600; + int ind = hashfunc(z, 200); + int res = hash[ind].search(z); + if(res == -1) + cout<<"Not found"; + else + { + hash[ind].deletepos(res); + hash[ind].traverse(); + } + return 0; +} + + + +