From 210a0d47f48989431ae26c14a4372a2c7169dc42 Mon Sep 17 00:00:00 2001 From: soulzstriker Date: Thu, 2 Aug 2018 17:02:37 +0800 Subject: [PATCH 01/11] Clean up the HTML comments --- Decimal To Binary.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Decimal To Binary.cpp b/Decimal To Binary.cpp index 3794d2a96..682cb6b05 100644 --- a/Decimal To Binary.cpp +++ b/Decimal To Binary.cpp @@ -1,20 +1,24 @@ -#include +// This function convert decimal to binary number +// +#include "stdafx.h" +#include using namespace std; + int main() { int number; - cin>>number; - int remainder,binary=0,var=1; - -do{ - - remainder=number%2; - number=number/2; - binary=binary+(remainder*var); - var=var*10; + cout << "Enter a number:"; + cin >> number; + int remainder, binary = 0, var = 1; + do { + remainder = number % 2; + number = number / 2; + binary = binary + (remainder*var); + var = var * 10; -} -while(number>0); - cout<0); + cout << "the binary is :"; + cout << binary; + cout << endl; } From 0e3b07ec479baa0041a3dc656bccb8a25a871536 Mon Sep 17 00:00:00 2001 From: CrazyMerlyn Date: Sun, 16 Sep 2018 16:47:02 +0530 Subject: [PATCH 02/11] Fix bugs in edit distance implementation --- Dynamic Programming/Edit Distance.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dynamic Programming/Edit Distance.cpp b/Dynamic Programming/Edit Distance.cpp index d99d03c5a..ff0d3fe19 100644 --- a/Dynamic Programming/Edit Distance.cpp +++ b/Dynamic Programming/Edit Distance.cpp @@ -30,7 +30,7 @@ int editDist(string str1, string str2, int m, int n) { //If last characters are same then continue //for the rest of them. - if(str1[m-1] == str2[n-2]) + if(str1[m-1] == str2[n-1]) return editDist(str1, str2, m-1, n-1); //If last not same, then 3 possibilities @@ -63,7 +63,7 @@ int editDistDP(string str1, string str2, int m, int n) { //If character same. Recur for remaining else if(str1[i-1] == str2[j-1]) - dp[i][j] == dp[i-1][j-1]; + dp[i][j] = dp[i-1][j-1]; else dp[i][j] = 1 + min(dp[i][j-1],//Insert @@ -80,8 +80,8 @@ int main() { string str1 = "sunday"; string str2 = "saturday"; - cout << editDist(str1, str1, str1.length(), str2.length()) << endl; - cout << editDistDP(str1, str1, str1.length(), str2.length()) << endl; + cout << editDist(str1, str2, str1.length(), str2.length()) << endl; + cout << editDistDP(str1, str2, str1.length(), str2.length()) << endl; return 0; } From 4ff86897b1b6f764b8841ab7a372565c37a04dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Do=C4=9Fan?= Date: Wed, 26 Sep 2018 00:19:09 +0300 Subject: [PATCH 03/11] n is now taken as an input. --- Sorting/Insertion Sort.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sorting/Insertion Sort.cpp b/Sorting/Insertion Sort.cpp index 45e860dde..f449ecb77 100644 --- a/Sorting/Insertion Sort.cpp +++ b/Sorting/Insertion Sort.cpp @@ -6,8 +6,10 @@ using namespace std; int main() { int n; + cout<<"\nEnter the length of your array : "; + cin>>n; int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; + cout<<"\nEnter the Numbers for Unsorted Array : "; //Input for(int i=0; i Date: Fri, 28 Sep 2018 23:41:01 +0530 Subject: [PATCH 04/11] Added EXIT option and some fine-tunings Added EXIT option in the menu for getting out of the loop and added lines between printing the list and choices for fine-tuning the output. --- Datastructures/Linked List.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Datastructures/Linked List.cpp b/Datastructures/Linked List.cpp index 9319249fd..991c7da09 100644 --- a/Datastructures/Linked List.cpp +++ b/Datastructures/Linked List.cpp @@ -85,6 +85,7 @@ int main() cout<<"\n2. Delete"; cout<<"\n3. Search"; cout<<"\n4. Print"; + cout<<"\n0. Exit"; cout<<"\n\nEnter you choice : "; cin>>choice; switch (choice) @@ -98,7 +99,8 @@ int main() case 3 : cout<<"\nEnter the element to be searched : "; cin>>x; search(x); break; - case 4 : show(); break; + case 4 : show(); + cout<<"\n"; break; } } while(choice!=0); From cbef5275bb5ac2b34d4ec0ed986cb1129486e85a Mon Sep 17 00:00:00 2001 From: shubhamguptaji Date: Thu, 11 Oct 2018 09:50:16 +0530 Subject: [PATCH 05/11] Trie Tree --- Datastructures/TrieTree.cpp | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Datastructures/TrieTree.cpp diff --git a/Datastructures/TrieTree.cpp b/Datastructures/TrieTree.cpp new file mode 100644 index 000000000..abf0da08b --- /dev/null +++ b/Datastructures/TrieTree.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +using namespace std; + +typedef struct trie +{ + struct trie *arr[26]; + bool isEndofWord; +} trie; + +trie *createNode() +{ + trie *nn = new trie(); + for (int i = 0; i < 26; i++) + nn->arr[i] = NULL; + nn->isEndofWord = false; + return nn; +} + +void insert(trie *root, char* str) +{ + for (int i = 0; i < strlen(str); i++) + { + int j = str[i] - 'a'; + if (root->arr[j]) + { + root = root->arr[j]; + } + else + { + root->arr[j] = createNode(); + root = root->arr[j]; + } + } + root->isEndofWord = true; +} + +bool search(trie *root, char* str, int index) +{ + if (index == strlen(str)) + { + if (!root->isEndofWord) + return false; + return true; + } + int j = str[index] - 'a'; + if (!root->arr[j]) + return false; + return search(root->arr[j], str, index + 1); +} + +bool deleteString (trie *root, char* str, int index) +{ + if (index == strlen(str)) + { + if (!root->isEndofWord) + return false; + root->isEndofWord = false; + for (int i = 0; i < 26; i++) + return false; + return true; + } + int j = str[index] - 'a'; + if (!root->arr[j]) + return false; + bool var = deleteString (root, str, index + 1); + if (var) + { + root->arr[j] = NULL; + if (root->isEndofWord) + return false; + else + { + int i; + for (i = 0; i < 26; i++) + if (root->arr[i]) + return false; + return true; + } + } +} + +int main() +{ + trie *root = createNode(); + insert(root, "hello"); + insert(root, "world"); + int a = search(root, "hello", 0); + int b = search(root, "word", 0); + printf("%d %d ", a, b); + return 0; +} \ No newline at end of file From 975cefb62b25d492f45ec217c116649a24b5515f Mon Sep 17 00:00:00 2001 From: shubhamguptaji Date: Thu, 11 Oct 2018 09:57:49 +0530 Subject: [PATCH 06/11] comments added --- Datastructures/TrieTree.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Datastructures/TrieTree.cpp b/Datastructures/TrieTree.cpp index abf0da08b..866087d87 100644 --- a/Datastructures/TrieTree.cpp +++ b/Datastructures/TrieTree.cpp @@ -2,13 +2,13 @@ #include #include using namespace std; - +// structure definition typedef struct trie { struct trie *arr[26]; bool isEndofWord; } trie; - +// create a new node for trie trie *createNode() { trie *nn = new trie(); @@ -18,6 +18,7 @@ trie *createNode() return nn; } +// insert string into the trie void insert(trie *root, char* str) { for (int i = 0; i < strlen(str); i++) @@ -36,6 +37,7 @@ void insert(trie *root, char* str) root->isEndofWord = true; } +// search a string exists inside the trie bool search(trie *root, char* str, int index) { if (index == strlen(str)) @@ -49,7 +51,9 @@ bool search(trie *root, char* str, int index) return false; return search(root->arr[j], str, index + 1); } - +/* removes the string if it is not a prefix of any other + string, if it is then just sets the endofword to false, else + removes the given string*/ bool deleteString (trie *root, char* str, int index) { if (index == strlen(str)) From 7698ff7b917071cbfff441bc4b191912ea0bbc33 Mon Sep 17 00:00:00 2001 From: Shruti Sheoran Date: Thu, 11 Oct 2018 10:45:50 +0530 Subject: [PATCH 07/11] AVL Tree(Balanced BST) --- Datastructures/AVLtree.cpp | 115 +++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Datastructures/AVLtree.cpp diff --git a/Datastructures/AVLtree.cpp b/Datastructures/AVLtree.cpp new file mode 100644 index 000000000..9c3b31d00 --- /dev/null +++ b/Datastructures/AVLtree.cpp @@ -0,0 +1,115 @@ +/********************** + author: shrutisheoran +***********************/ + +#include +#include + +using namespace std; + +typedef struct node { + int data; + int height; + struct node* left; + struct node* right; +}node; + +int max(int a, int b) { + return a > b ? a : b; +} + +node* createNode(int data) { + node *nn = new node(); + nn->data = data; + nn->height = 0; + nn->left = NULL; + nn->right = NULL; + return nn; +} + +// Returns height of tree +int height(node *root) { + if(root==NULL) + return 0; + return 1 + max(height(root->left), height(root->right)); +} + +// Returns difference between height of left and right subtree +int getBalance(node *root) { + return height(root->left) - height(root->right); +} + +/************************************ + Returns Node after Right Rotation +*************************************/ +node* rightRotate(node *root) { + node *t = root->left; + node *u = t->right; + t->right = root; + root->left = u; + return t; +} + +/************************************ + Returns Node after Left Rotation +*************************************/ +node* leftRotate(node *root) { + node *t = root->right; + node *u = t->left; + t->left = root; + root->right = u; + return t; +} + +/************************ + Balanced Insertion +************************/ +node* insert(node* root, int item) { + node *nn = createNode(item); + if(root == NULL) + return nn; + if(itemdata) + root->left = insert(root->left, item); + else + root->right = insert(root->right, item); + int b = getBalance(root); + if(b>1) { + if(getBalance(root->left)<0) + root->left = leftRotate(root->left); // Left-Right Case + return rightRotate(root); // Left-Left Case + } + else if(b<-1) { + if(getBalance(root->right)>0) + root->right = rightRotate(root->right); // Right-Left Case + return leftRotate(root); // Right-Right Case + } + return root; +} + +/************************************** + LevelOrder (Breadth First Search) +*************************************/ +void levelOrder(node* root) { + queue q; + q.push(root); + while(!q.empty()) { + root = q.front(); + cout<data<<" "; + q.pop(); + if(root->left) + q.push(root->left); + if(root->right) + q.push(root->right); + } +} + +int main() { + node *root = NULL; + // Testing AVL Tree + int i; + for(i = 1 ; i <= 7 ; i++) + root = insert(root, i); + cout<<"LevelOrder: "; + levelOrder(root); + return 0; +} From 62834c14322fd42d27e381be8951b9c0ae61afde Mon Sep 17 00:00:00 2001 From: Shruti Sheoran Date: Thu, 11 Oct 2018 11:54:29 +0530 Subject: [PATCH 08/11] Add Morris Inorder Algorithm (Binary Tree) --- Datastructures/MorrisInorder.cpp | 94 ++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Datastructures/MorrisInorder.cpp diff --git a/Datastructures/MorrisInorder.cpp b/Datastructures/MorrisInorder.cpp new file mode 100644 index 000000000..5ea630fac --- /dev/null +++ b/Datastructures/MorrisInorder.cpp @@ -0,0 +1,94 @@ +#include +#include + +/************************** + @author shrutisheoran +**************************/ + +using namespace std; + +struct Btree { + int data; + struct Btree* left; //Pointer to left subtree + struct Btree* right; //Pointer to right subtree +}; + +void insert(Btree **root, int d) { + Btree *nn = new Btree(); //Creating new node + nn->data = d; + nn->left = NULL; + nn->right = NULL; + if(*root == NULL) { + *root = nn; + return; + } + else { + queue q; + // Adding root node to queue + q.push(*root); + while(!q.empty()) { + Btree *node = q.front(); + // Removing parent node from queue + q.pop(); + if(node->left) + // Adding left child of removed node to queue + q.push(node->left); + else { + // Adding new node if no left child is present + node->left = nn; + return; + } + if(node->right) + // Adding right child of removed node to queue + q.push(node->right); + else { + // Adding new node if no right child is present + node->right = nn; + return; + } + } + } +} + +void morrisInorder(Btree *root) { + Btree *curr = root; + Btree *temp; + while(curr) { + if(curr->left==NULL) { + cout<data<<" "; + // If left of current node is NULL then curr is shifted to right + curr = curr->right; + } + else { + // Left of current node is stored in temp + temp = curr->left; + // Moving to extreme right of temp + while(temp->right&&temp->right!=curr) + temp = temp->right; + // If extreme right is null it is made to point to currrent node (will be used for backtracking) + if(temp->right == NULL) { + temp->right = curr; + // current node is made to point its left subtree + curr = curr->left; + } + // If extreme right already points to currrent node it it set to null + else if(temp->right == curr) { + cout<data<<" "; + temp->right = NULL; + // current node is made to point its right subtree + curr = curr->right; + } + } + } +} + +int main() { + // Testing morrisInorder funtion + Btree *root = NULL; + int i; + for(i = 1 ; i <= 7 ; i++) + insert(&root, i); + cout<<"Morris Inorder: "; + morrisInorder(root); + return 0; +} From e34368fcb6ed4f05277be2986c45a4cbb85d21bf Mon Sep 17 00:00:00 2001 From: Shruti Sheoran Date: Thu, 11 Oct 2018 12:53:27 +0530 Subject: [PATCH 09/11] Add deletion function --- Datastructures/AVLtree.cpp | 78 +++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/Datastructures/AVLtree.cpp b/Datastructures/AVLtree.cpp index 9c3b31d00..a3cd52919 100644 --- a/Datastructures/AVLtree.cpp +++ b/Datastructures/AVLtree.cpp @@ -1,7 +1,3 @@ -/********************** - author: shrutisheoran -***********************/ - #include #include @@ -18,6 +14,8 @@ int max(int a, int b) { return a > b ? a : b; } +// Returns a new Node + node* createNode(int data) { node *nn = new node(); nn->data = data; @@ -28,6 +26,7 @@ node* createNode(int data) { } // Returns height of tree + int height(node *root) { if(root==NULL) return 0; @@ -35,13 +34,13 @@ int height(node *root) { } // Returns difference between height of left and right subtree + int getBalance(node *root) { return height(root->left) - height(root->right); } -/************************************ - Returns Node after Right Rotation -*************************************/ +// Returns Node after Right Rotation + node* rightRotate(node *root) { node *t = root->left; node *u = t->right; @@ -50,9 +49,8 @@ node* rightRotate(node *root) { return t; } -/************************************ - Returns Node after Left Rotation -*************************************/ +// Returns Node after Left Rotation + node* leftRotate(node *root) { node *t = root->right; node *u = t->left; @@ -61,9 +59,16 @@ node* leftRotate(node *root) { return t; } -/************************ - Balanced Insertion -************************/ +// Returns node with minimum value in the tree + +node* minValue(node* root) { + if(root->left==NULL) + return root; + return minValue(root->left); +} + +// Balanced Insertion + node* insert(node* root, int item) { node *nn = createNode(item); if(root == NULL) @@ -86,9 +91,42 @@ node* insert(node* root, int item) { return root; } -/************************************** - LevelOrder (Breadth First Search) -*************************************/ +// Balanced Deletion + +node* deleteNode(node *root, int key) { + if(root == NULL) + return root; + if(key < root->data) + root->left = deleteNode(root->left, key); + else if(key > root->data) + root->right = deleteNode(root->right, key); + + else { + // Node to be deleted is leaf node or have only one Child + if(!root->right) { + node* temp = root->left; + delete(root); + root = NULL; + return temp; + } + else if(!root->left) { + node* temp = root->right; + delete(root); + root = NULL; + return temp; + } + // Node to be deleted have both left and right subtrees + node *temp = minValue(root->right); + root->data = temp->data; + root->right = deleteNode(root->right, temp->data); + } + // Balancing Tree after deletion + return root; +} + + +// LevelOrder (Breadth First Search) + void levelOrder(node* root) { queue q; q.push(root); @@ -104,12 +142,18 @@ void levelOrder(node* root) { } int main() { - node *root = NULL; // Testing AVL Tree + node *root = NULL; int i; for(i = 1 ; i <= 7 ; i++) root = insert(root, i); cout<<"LevelOrder: "; levelOrder(root); + root = deleteNode(root, 1); // Deleting key with value 1 + cout<<"\nLevelOrder: "; + levelOrder(root); + root = deleteNode(root, 4); // Deletin key with value 4 + cout<<"\nLevelOrder: "; + levelOrder(root); return 0; } From 672f9dc57f66495d9784c71fd55d51ac75505902 Mon Sep 17 00:00:00 2001 From: William Grigor Date: Fri, 21 Dec 2018 23:01:52 -0600 Subject: [PATCH 10/11] Bubble Sort now uses vectors --- Sorting/Bubble Sort.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Sorting/Bubble Sort.cpp b/Sorting/Bubble Sort.cpp index c94a45efc..01c421314 100644 --- a/Sorting/Bubble Sort.cpp +++ b/Sorting/Bubble Sort.cpp @@ -1,19 +1,22 @@ //Bubble Sort #include +#include using namespace std; int main() { int n; + cout << "Enter the amount of numbers to sort: "; cin >> n; - int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; - + vector numbers; + cout << "Enter " << n << " numbers: "; + int num; //Input for(int i=0; i>Array[i]; + cin >> num; + numbers.push_back(num); } //Bubble Sorting @@ -21,19 +24,23 @@ int main() { for(int j=0; jArray[j+1]) + if(numbers[j]>numbers[j+1]) { - int temp=Array[j]; - Array[j]=Array[j+1]; - Array[j+1]=temp; + swap(numbers[j], numbers[j+1]); } } } //Output cout<<"\nSorted Array : "; - for(int i=0; i Date: Sat, 9 Feb 2019 12:54:49 +0530 Subject: [PATCH 11/11] Update Decimal To Binary.cpp --- Decimal To Binary.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Decimal To Binary.cpp b/Decimal To Binary.cpp index 682cb6b05..b8eaba1f1 100644 --- a/Decimal To Binary.cpp +++ b/Decimal To Binary.cpp @@ -1,6 +1,5 @@ // This function convert decimal to binary number // -#include "stdafx.h" #include using namespace std; @@ -21,4 +20,5 @@ int main() cout << "the binary is :"; cout << binary; cout << endl; + return 0; }