diff --git a/Datastructures/AVLtree.cpp b/Datastructures/AVLtree.cpp new file mode 100644 index 000000000..a3cd52919 --- /dev/null +++ b/Datastructures/AVLtree.cpp @@ -0,0 +1,159 @@ +#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; +} + +// Returns a new Node + +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; +} + +// 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) + 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; +} + +// 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); + 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() { + // 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; +} 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); 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; +} diff --git a/Datastructures/TrieTree.cpp b/Datastructures/TrieTree.cpp new file mode 100644 index 000000000..866087d87 --- /dev/null +++ b/Datastructures/TrieTree.cpp @@ -0,0 +1,97 @@ +#include +#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(); + for (int i = 0; i < 26; i++) + nn->arr[i] = NULL; + nn->isEndofWord = false; + return nn; +} + +// insert string into the trie +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; +} + +// search a string exists inside the trie +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); +} +/* 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)) + { + 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 diff --git a/Decimal To Binary.cpp b/Decimal To Binary.cpp index 3794d2a96..b8eaba1f1 100644 --- a/Decimal To Binary.cpp +++ b/Decimal To Binary.cpp @@ -1,20 +1,24 @@ -#include +// This function convert decimal to binary number +// +#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; + return 0; } 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; } diff --git a/Sorting/Bubble Sort.cpp b/Sorting/Bubble Sort.cpp index 21402f334..aa4930e82 100644 --- a/Sorting/Bubble Sort.cpp +++ b/Sorting/Bubble Sort.cpp @@ -1,37 +1,39 @@ //Bubble Sort #include +#include using namespace std; int main() { int n; - short swap=0; + short swap_check=0; + cout << "Enter the amount of numbers to sort: "; cin >> n; - int Array[n]; - cout<<"\nEnter any "< numbers; + cout << "Enter " << n << " numbers: "; + int num; - //Input + //Input for(int i=0; i>Array[i]; + cin >> num; + numbers.push_back(num); } //Bubble Sorting for(int i=0; iArray[j+1]) + if(numbers[j]>numbers[j+1]) { - swap=1; - int temp=Array[j]; - Array[j]=Array[j+1]; - Array[j+1]=temp; + swap_check=1; + swap(numbers[j], numbers[j+1]); } } - if(swap == 0) + if(swap_check == 0) { break; } @@ -39,8 +41,15 @@ int main() //Output cout<<"\nSorted Array : "; - for(int i=0; i>n; int Array[n]; cout<<"\nEnter any "<