From c198438c44596ebf7ecf6a2659a707a0c851c84b Mon Sep 17 00:00:00 2001 From: sumitbhore06 <73064513+sumitbhore06@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:45:28 +0530 Subject: [PATCH 1/4] created recursion.cpp --- C++/Recursion.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 C++/Recursion.py diff --git a/C++/Recursion.py b/C++/Recursion.py new file mode 100644 index 0000000..f001ee4 --- /dev/null +++ b/C++/Recursion.py @@ -0,0 +1,25 @@ +# recurion in c++ + + #include + using namespace std; + int main() + { + int factorial(int); + int fact,value; + cout<<"Enter any number: "; + cin>>value; + fact=factorial(value); + cout<<"Factorial of a number is: "< Date: Fri, 29 Oct 2021 20:48:37 +0530 Subject: [PATCH 2/4] created merge_sort.cpp --- C++/merge_sort.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 C++/merge_sort.cpp diff --git a/C++/merge_sort.cpp b/C++/merge_sort.cpp new file mode 100644 index 0000000..d68570b --- /dev/null +++ b/C++/merge_sort.cpp @@ -0,0 +1,94 @@ +// C++ program for Merge Sort +#include +using namespace std; + +// Merges two subarrays of array[]. +// First subarray is arr[begin..mid] +// Second subarray is arr[mid+1..end] +void merge(int array[], int const left, int const mid, int const right) +{ + auto const subArrayOne = mid - left + 1; + auto const subArrayTwo = right - mid; + + // Create temp arrays + auto *leftArray = new int[subArrayOne], + *rightArray = new int[subArrayTwo]; + + // Copy data to temp arrays leftArray[] and rightArray[] + for (auto i = 0; i < subArrayOne; i++) + leftArray[i] = array[left + i]; + for (auto j = 0; j < subArrayTwo; j++) + rightArray[j] = array[mid + 1 + j]; + + auto indexOfSubArrayOne = 0, // Initial index of first sub-array + indexOfSubArrayTwo = 0; // Initial index of second sub-array + int indexOfMergedArray = left; // Initial index of merged array + + // Merge the temp arrays back into array[left..right] + while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) { + if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo]) { + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + } + else { + array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + } + indexOfMergedArray++; + } + // Copy the remaining elements of + // left[], if there are any + while (indexOfSubArrayOne < subArrayOne) { + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + indexOfMergedArray++; + } + // Copy the remaining elements of + // right[], if there are any + while (indexOfSubArrayTwo < subArrayTwo) { + array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + indexOfMergedArray++; + } +} + +// begin is for left index and end is +// right index of the sub-array +// of arr to be sorted */ +void mergeSort(int array[], int const begin, int const end) +{ + if (begin >= end) + return; // Returns recursively + + auto mid = begin + (end - begin) / 2; + mergeSort(array, begin, mid); + mergeSort(array, mid + 1, end); + merge(array, begin, mid, end); +} + +// UTILITY FUNCTIONS +// Function to print an array +void printArray(int A[], int size) +{ + for (auto i = 0; i < size; i++) + cout << A[i] << " "; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + auto arr_size = sizeof(arr) / sizeof(arr[0]); + + cout << "Given array is \n"; + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + cout << "\nSorted array is \n"; + printArray(arr, arr_size); + return 0; +} + +// This code is contributed by Mayank Tyagi +// This code was revised by Joshua Estes From 290815e5b4525e480f5f0b59c06bb99d8b96f3d8 Mon Sep 17 00:00:00 2001 From: sumitbhore06 <73064513+sumitbhore06@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:51:19 +0530 Subject: [PATCH 3/4] created binary_search.cpp --- C++/binary_search.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 C++/binary_search.cpp diff --git a/C++/binary_search.cpp b/C++/binary_search.cpp new file mode 100644 index 0000000..542ad06 --- /dev/null +++ b/C++/binary_search.cpp @@ -0,0 +1,43 @@ +// C++ program to implement recursive Binary Search + +#include +using namespace std; + +// A recursive binary search function. It returns +// location of x in given array arr[l..r] is present, +// otherwise -1 +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the middle + // itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not + // present in array + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? cout << "Element is not present in array" + : cout << "Element is present at index " << result; + return 0; +} From 11744b8b54839ba2ca0952b388a9334a60d300ca Mon Sep 17 00:00:00 2001 From: sumitbhore06 <73064513+sumitbhore06@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:53:41 +0530 Subject: [PATCH 4/4] created avl_tree.cpp --- C++/AVL_Tree.cpp | 190 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 C++/AVL_Tree.cpp diff --git a/C++/AVL_Tree.cpp b/C++/AVL_Tree.cpp new file mode 100644 index 0000000..acd879c --- /dev/null +++ b/C++/AVL_Tree.cpp @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#define pow2(n) (1 << (n)) +using namespace std; +struct avl { + int d; + struct avl *l; + struct avl *r; +}*r; +class avl_tree { + public: + int height(avl *); + int difference(avl *); + avl *rr_rotat(avl *); + avl *ll_rotat(avl *); + avl *lr_rotat(avl*); + avl *rl_rotat(avl *); + avl * balance(avl *); + avl * insert(avl*, int); + void show(avl*, int); + void inorder(avl *); + void preorder(avl *); + void postorder(avl*); + avl_tree() { + r = NULL; + } +}; +int avl_tree::height(avl *t) { + int h = 0; + if (t != NULL) { + int l_height = height(t->l); + int r_height = height(t->r); + int max_height = max(l_height, r_height); + h = max_height + 1; + } + return h; +} +int avl_tree::difference(avl *t) { + int l_height = height(t->l); + int r_height = height(t->r); + int b_factor = l_height - r_height; + return b_factor; +} +avl *avl_tree::rr_rotat(avl *parent) { + avl *t; + t = parent->r; + parent->r = t->l; + t->l = parent; + cout<<"Right-Right Rotation"; + return t; +} +avl *avl_tree::ll_rotat(avl *parent) { + avl *t; + t = parent->l; + parent->l = t->r; + t->r = parent; + cout<<"Left-Left Rotation"; + return t; +} +avl *avl_tree::lr_rotat(avl *parent) { + avl *t; + t = parent->l; + parent->l = rr_rotat(t); + cout<<"Left-Right Rotation"; + return ll_rotat(parent); +} +avl *avl_tree::rl_rotat(avl *parent) { + avl *t; + t = parent->r; + parent->r = ll_rotat(t); + cout<<"Right-Left Rotation"; + return rr_rotat(parent); +} +avl *avl_tree::balance(avl *t) { + int bal_factor = difference(t); + if (bal_factor > 1) { + if (difference(t->l) > 0) + t = ll_rotat(t); + else + t = lr_rotat(t); + } else if (bal_factor < -1) { + if (difference(t->r) > 0) + t = rl_rotat(t); + else + t = rr_rotat(t); + } + return t; +} +avl *avl_tree::insert(avl *r, int v) { + if (r == NULL) { + r = new avl; + r->d = v; + r->l = NULL; + r->r = NULL; + return r; + } else if (v< r->d) { + r->l = insert(r->l, v); + r = balance(r); + } else if (v >= r->d) { + r->r = insert(r->r, v); + r = balance(r); + } return r; +} +void avl_tree::show(avl *p, int l) { + int i; + if (p != NULL) { + show(p->r, l+ 1); + cout<<" "; + if (p == r) + cout << "Root -> "; + for (i = 0; i < l&& p != r; i++) + cout << " "; + cout << p->d; + show(p->l, l + 1); + } +} +void avl_tree::inorder(avl *t) { + if (t == NULL) + return; + inorder(t->l); + cout << t->d << " "; + inorder(t->r); +} +void avl_tree::preorder(avl *t) { + if (t == NULL) + return; + cout << t->d << " "; + preorder(t->l); + preorder(t->r); +} +void avl_tree::postorder(avl *t) { + if (t == NULL) + return; + postorder(t ->l); + postorder(t ->r); + cout << t->d << " "; +} +int main() { + int c, i; + avl_tree avl; + while (1) { + cout << "1.Insert Element into the tree" << endl; + cout << "2.show Balanced AVL Tree" << endl; + cout << "3.InOrder traversal" << endl; + cout << "4.PreOrder traversal" << endl; + cout << "5.PostOrder traversal" << endl; + cout << "6.Exit" << endl; + cout << "Enter your Choice: "; + cin >> c; + switch (c) { + case 1: + cout << "Enter value to be inserted: "; + cin >> i; + r = avl.insert(r, i); + break; + case 2: + if (r == NULL) { + cout << "Tree is Empty" << endl; + continue; + } + cout << "Balanced AVL Tree:" << endl; + avl.show(r, 1); + cout<