diff --git a/Algorithms/Recurssion/String_length_recursion.cpp b/Algorithms/Recurssion/String_length_recursion.cpp new file mode 100644 index 00000000..645df9eb --- /dev/null +++ b/Algorithms/Recurssion/String_length_recursion.cpp @@ -0,0 +1,22 @@ +// CPP program to calculate length of +// a string using recursion +#include +using namespace std; + +/* Function to calculate length */ +int recLen(char* str) +{ + // if we reach at the end of the string + if (*str == '\0') + return 0; + else + return 1 + recLen(str + 1); +} + +/* Driver program to test above function */ +int main() +{ + char str[] = "GeeksforGeeks"; + cout << recLen(str); + return 0; +} diff --git a/Algorithms/SearchingALgo/interpolation_search.cpp b/Algorithms/SearchingALgo/interpolation_search.cpp new file mode 100644 index 00000000..767dfd3a --- /dev/null +++ b/Algorithms/SearchingALgo/interpolation_search.cpp @@ -0,0 +1,94 @@ +//Algoritm----------------------------------------------- +// The idea of formula is to return higher value of pos +// when element to be searched is closer to arr[hi]. And +// smaller value when closer to arr[lo] +//pos = lo + [ (x - arr[lo]) * (hi - lo) / (arr[hi] - arr[Lo]) ] +/* +arr[] ==> Array where elements need to be searched +x ==> Element to be searched +lo ==> Starting index in arr[] +hi ==> Ending index in arr[] + + +Let's assume that the elements of the array are linearly distributed. + +General equation of line : y = m*x + c. +y is the value in the array and x is its index. + +Now putting value of lo,hi and x in the equation +arr[hi] = m*hi+c ----(1) +arr[lo] = m*lo+c ----(2) +x = m*pos + c ----(3) + +m = (arr[hi] - arr[lo] )/ (hi - lo) + +subtracting eqxn (2) from (3) +x - arr[lo] = m * (pos - lo) +lo + (x - arr[lo])/m = pos +pos = lo + (x - arr[lo]) *(hi - lo)/(arr[hi] - arr[lo]) + + +*/ + +// C++ program to implement interpolation +// search with recursion +#include +using namespace std; + +// If x is present in arr[0..n-1], then returns +// index of it, else returns -1. +int interpolationSearch(int arr[], int lo, int hi, int x) +{ + int pos; + + // Since array is sorted, an element present + // in array must be in range defined by corner + if (lo <= hi && x >= arr[lo] && x <= arr[hi]) { + + // Probing the position with keeping + // uniform distribution in mind. + pos = lo + + (((double)(hi - lo) / (arr[hi] - arr[lo])) + * (x - arr[lo])); + + // Condition of target found + if (arr[pos] == x) + return pos; + + // If x is larger, x is in right sub array + if (arr[pos] < x) + return interpolationSearch(arr, pos + 1, hi, x); + + // If x is smaller, x is in left sub array + if (arr[pos] > x) + return interpolationSearch(arr, lo, pos - 1, x); + } + return -1; +} + +// Driver Code +int main() +{ + + // Array of items on which search will + // be conducted. + int arr[] = { 10, 12, 13, 16, 18, 19, 20, 21, + 22, 23, 24, 33, 35, 42, 47 + }; + + int n = sizeof(arr) / sizeof(arr[0]); + + // Element to be searched + int x = 18; + int index = interpolationSearch(arr, 0, n - 1, x); + + // If element was found + if (index != -1) + cout << "Element found at index " << index; + else + cout << "Element not found."; + + return 0; +} + +// This code is contributed by equbalzeeshan diff --git a/Data Structure/559_Maximum_Depth_of_N-ary_Tree.java b/Data Structure/559_Maximum_Depth_of_N-ary_Tree.java deleted file mode 100644 index a2241296..00000000 --- a/Data Structure/559_Maximum_Depth_of_N-ary_Tree.java +++ /dev/null @@ -1,40 +0,0 @@ -/* -// Definition for a Node. -class Node { - public int val; - public List children; - - public Node() {} - - public Node(int _val) { - val = _val; - } - - public Node(int _val, List _children) { - val = _val; - children = _children; - } -}; -*/ - -class Solution { - public int maxDepth(Node root) { - int maxDepth = 0; - if( root == null ){ - return maxDepth; - } - maxDepth = depthUtil( root, maxDepth + 1 ); - return maxDepth; - - } - public int depthUtil( Node root, int h ){ - if( root.children.isEmpty()){ - return h; - } - int maxDepth = 0; - for(Node n : root.children){ - maxDepth = Math.max( maxDepth, depthUtil( n, h+1 ) ); - } - return maxDepth; - } -} \ No newline at end of file diff --git a/Data Structure/589_N-ary_Tree_Preorder_Traversal.java b/Data Structure/589_N-ary_Tree_Preorder_Traversal.java deleted file mode 100644 index cf2e3bc8..00000000 --- a/Data Structure/589_N-ary_Tree_Preorder_Traversal.java +++ /dev/null @@ -1,40 +0,0 @@ -/* -// Definition for a Node. -class Node { - public int val; - public List children; - - public Node() {} - - public Node(int _val) { - val = _val; - } - - public Node(int _val, List _children) { - val = _val; - children = _children; - } -}; -*/ - -class Solution { - public List preorder(Node root) { - Stack st = new Stack<>(); - LinkedList ll = new LinkedList<>(); - - if( root == null ){ - return ll; - } - st.add(root); - - while( !st.isEmpty() ){ - Node curr = st.pop(); - ll.add(curr.val); - Collections.reverse(curr.children); - for( Node i : curr.children ){ - st.add(i); - } - } - return ll; - } -} \ No newline at end of file diff --git a/Data Structure/590_N-ary_Tree_Postorder_Traversal.java b/Data Structure/590_N-ary_Tree_Postorder_Traversal.java deleted file mode 100644 index 70959f53..00000000 --- a/Data Structure/590_N-ary_Tree_Postorder_Traversal.java +++ /dev/null @@ -1,38 +0,0 @@ -/* -// Definition for a Node. -class Node { - public int val; - public List children; - - public Node() {} - - public Node(int _val) { - val = _val; - } - - public Node(int _val, List _children) { - val = _val; - children = _children; - } -}; -*/ - -class Solution { - public List postorder(Node root) { - Stack st = new Stack<>(); - LinkedList l = new LinkedList<>(); - - if( root == null ){ - return l; - } - st.add(root); - while( !st.isEmpty() ){ - Node curr = st.pop(); - l.addFirst( curr.val ); - for( Node i : curr.children ){ - st.add( i ); - } - } - return l; - } -} \ No newline at end of file diff --git a/Data Structure/Array/MatrixMultiplication.c b/Data Structure/Array/MatrixMultiplication.c deleted file mode 100644 index 41b4a1f2..00000000 --- a/Data Structure/Array/MatrixMultiplication.c +++ /dev/null @@ -1,115 +0,0 @@ -//C program to implement Matrix multiplication - #include - #include - - void enterData(int Matrix1[][10], int Matrix2[][10], int row1, int column1, int row2, int column2); - void multiplyMatrices(int Matrix1[][10], int Matrix2[][10], int mult[][10], int row1, int column1, int row2, int column2); - void display(int mult[][10], int row1, int column2); - - int main() - { - int Matrix1[10][10], Matrix2[10][10], mult[10][10], row1, column1, row2, column2, i, j, k; - - printf("Enter rows and column for first matrix: "); - scanf("%d %d", &row1, &column1); - - printf("Enter rows and column for second matrix: "); - scanf("%d %d", &row2, &column2); - - // If column of first matrix in not equal to row of second matrix, ask user to enter the size of matrix again. - - while (column1 != row2) - { - printf("Error! column of first matrix not equal to row of second.\n"); - printf("Enter rows and column for first matrix: "); - scanf("%d%d", &row1, &column1); - printf("Enter rows and column for second matrix: "); - scanf("%d%d", &row2, &column2); - } - - // Function that takes matrices data - - enterData(Matrix1, Matrix2, row1, column1, row2, column2); - - // Function to multiply two matrices. - - multiplyMatrices(Matrix1, Matrix2, mult, row1, column1, row2, column2); - - // Function to display resultant matrix after multiplication - - display(mult, row1, column2); - - return 0; - } - - //function to fill elements in the matrix - - void enterData(int Matrix1[][10], int Matrix2[][10], int row1, int column1, int row2, int column2) - { - int i, j; - printf("\nEnter elements of matrix 1:\n"); - for(i = 0; i < row1; ++i) - { - for(j = 0; j < column1; ++j) - { - printf("Enter elements a%d%d: ", i + 1, j + 1); - scanf("%d", &Matrix1[i][j]); - } - } - - printf("\nEnter elements of matrix 2:\n"); - for(i = 0; i < row2; ++i) - { - for(j = 0; j < column2; ++j) - { - printf("Enter elements b%d%d: ", i + 1, j + 1); - scanf("%d", &Matrix2[i][j]); - } - } - } - - void multiplyMatrices(int Matrix1[][10], int Matrix2[][10], int mult[][10], int row1, int column1, int row2, int column2) - { - int i, j, k; - - // Initializing elements of matrix mult to 0. - - for(i = 0; i < row1; ++i) - { - for(j = 0; j < column2; ++j) - { - mult[i][j] = 0; - } - } - - // Multiplying matrix firstMatrix and secondMatrix and storing in array mult. - - for(i = 0; i < row1; ++i) - { - for(j = 0; j < column2; ++j) - { - for(k=0; k - -using namespace std; - -int sumOfN(int n) -{ - return (n * n + n) / 2; -} - -void missingNumb(int arr[], int n) -{ - int sum = sumOfN(n); - int arrSum = 0; - for (int i = 0; i < n - 1; i++) - { - arrSum += arr[i]; - } - cout << sum - arrSum; - cout << endl; -} - -int main() -{ - int t; - cin >> t; - while (t--) - { - int n; - cin >> n; - int arr[n]; - for (int i = 0; i < n - 1; i++) - { - cin >> arr[i]; - } - missingNumb(arr, n); - } -} \ No newline at end of file diff --git a/Data Structure/Array/README.md b/Data Structure/Array/README.md deleted file mode 100644 index fcb4962c..00000000 --- a/Data Structure/Array/README.md +++ /dev/null @@ -1,10 +0,0 @@ -## Array - - -Array is a linear data structure in computer science.Values stored in array should be of same data type. -DECLARATION OF ARRAY: int arr[n] -->Here "int" denotes the datatype of values stored in array,n denotes the size of array if array elements are not known beforehand. -TAKING INPUT: -->Run a loop till n ans store values in it. -->Pseudo code: - for(int i=0;i>arr[i]; diff --git a/Data Structure/Array/UnionOfTwoArrays.cpp b/Data Structure/Array/UnionOfTwoArrays.cpp deleted file mode 100644 index 84188db2..00000000 --- a/Data Structure/Array/UnionOfTwoArrays.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include - -using namespace std; - -int unionArray(int a[], int b[], int n, int m) -{ - int i, j; - int count = 0; - while (i < n && j < m) - { - if (a[i] < b[j]) - { - count++; - i++; - } - else if (a[i] > b[j]) - { - count++; - j++; - } - else - { - count++; - i++, j++; - } - } - - while (i < n) - { - count++; - i++; - } - - while (j < m) - { - count++; - j++; - } - - return count; -} - -int main() -{ - int t; - cin >> t; - while (t--) - { - int n, m; - cin >> n >> m; - int a[n], b[m]; - for (int i = 0; i < n; i++) - { - cin >> a[i]; - } - - for (int i = 0; i < m; i++) - { - cin >> b[i]; - } - - int counter = unionArray(a, b, n, m); - cout << counter << endl; - } -} \ No newline at end of file diff --git a/Data Structure/Array/binarysearch.cpp b/Data Structure/Array/binarysearch.cpp deleted file mode 100644 index 03dc859c..00000000 --- a/Data Structure/Array/binarysearch.cpp +++ /dev/null @@ -1,42 +0,0 @@ -// 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; -} diff --git a/Data Structure/Array/binarysearchinredundantarray.c b/Data Structure/Array/binarysearchinredundantarray.c deleted file mode 100644 index 2fa8e980..00000000 --- a/Data Structure/Array/binarysearchinredundantarray.c +++ /dev/null @@ -1,79 +0,0 @@ - - -#include -int first(int a[],int l,int r, int s) -{ - int mid = 0; - while(l<=r) - { - mid = l + (r-l)/2; - if(s==a[mid]) - { - if(s==a[mid - 1]) - { - r = mid - 1; - continue; - } - return mid; - } - else if(sa[mid]) - { - l = mid + 1; - } - } - return -1; -} -int last(int a[],int l,int r, int s) -{ - int mid = 0; - while(l<=r) - { - mid = l + (r-l)/2; - if(s==a[mid]) - { - if(s==a[mid + 1]) - { - l = mid + 1; - continue; - } - return mid; - } - else if(sa[mid]) - { - l = mid + 1; - } - } - return -1; -} -int main() -{ - int a[10000],n,i,l,r,s; - scanf("%d",&n); - for(i=0;i -void insertion_sort(int arr[], int size) -{ int i,j,temp; - for(i=1;i=0 ) ) - { - arr[j+1] = arr[j]; - j=j-1; - } - arr[j+1]=temp; - } -} - -int main() -{ - int arr[100],i,j,temp,size; - printf("Enter the size of the array : "); - scanf("%d",&size); //reading the size of the array - printf("Type the unsorted array : "); - for(i=0;i -#include -#include -int partition(int a[],int lb,int ub); -void quicksort(int a[],int lb,int ub); -void main() -{ - int arr[1000],n,i,lb,ub; - printf("Array Size \n"); - scanf("%d",&n); - printf("Enter elements in array \n"); - for(i=0;ipivot && end>lb) - { - end--; - } - if(start -using namespace std; -void swap(int *a,int *b) -{ - int temp=*a; - *a=*b; - *b=temp; -} -int partition(int a[],int l,int h) -{ - int pivot=a[l]; - int i=l,j=h; - do - { - do{i++;}while(a[i]<=pivot); - do{j--;}while(a[j]>pivot); - if(i0) - { - count++; - n/=10; - } - return count; -} -struct node -{ - int data; - node *next; -}; -void Insert(node** root,int key) -{ - node *t; - t=new node; - t->data=key; - t->next=NULL; - if(!*root) - { - *root=t; - } - else - { - node *p=*root; - while(p->next) - p=p->next; - p->next=t; - } -} -int del(node **root) -{ - if(!*root) - return -1; - else - { - node *t=*root; - *root=t->next; - int x=t->data; - delete t; - return x; - } -} -void RadixSort(int a[],int n) -{ - node* *bins=new node*[10]; - int i,j,k,Max,div=1; - for(i=0;i<10;i++) - bins[i]=NULL; - Max=*max_element(a,a+n); - int cd=count_digit(Max); - for(k=0;k=1;gap/=2) - { - for(i=gap;i=0 && a[j]>temp) - { - a[j+gap]=a[j]; - j=j-gap; - } - a[j+gap]=temp; - } - } -} -int main() -{ - int n; - cin>>n; - int a[n],i; - for(i=0;i>a[i]; - ShellSort(a,n); - for(i=0;i q = new LinkedList<>(); - Queue ele = new PriorityQueue<>(Collections.reverseOrder()); - - q.add(root); - - while( !q.isEmpty() ){ - TreeNode curr = q.poll(); - ele.add(curr.val); - - if( curr.left != null ){ - q.add(curr.left); - } - if( curr.right != null ){ - q.add(curr.right); - } - - while( ele.size() > k ){ - ele.poll(); - } - } - - return ele.poll(); - } -} \ No newline at end of file diff --git a/Data Structure/BinarySearchTree/README.md b/Data Structure/BinarySearchTree/README.md deleted file mode 100644 index c023489b..00000000 --- a/Data Structure/BinarySearchTree/README.md +++ /dev/null @@ -1,3 +0,0 @@ -## Binary Search Tree - - diff --git a/Data Structure/BinarySearchTree/binary_search_tree.cpp b/Data Structure/BinarySearchTree/binary_search_tree.cpp deleted file mode 100644 index c1e6c311..00000000 --- a/Data Structure/BinarySearchTree/binary_search_tree.cpp +++ /dev/null @@ -1,46 +0,0 @@ -// just an implementation for BST -#include -using namespace std; -struct BstNode -{ - int data; - BstNode *left; - BstNode *right; -}; -BstNode * getNewNode() -{ - BstNode *temp = new BstNode; - temp->left = NULL; - temp->right = NULL; - return temp; -} -BstNode * Insert(BstNode *root, int data) -{ - if(root == NULL) - { - BstNode *newNode = getNewNode(); - newNode->data = data; - root = newNode; - } - else if(data <= root->data) - { - root->left = Insert(root->left, data); - } - else - { - root->right = Insert(root->right, data); - } - return root; -} -int main() -{ - BstNode *root = NULL; - root = Insert(root, 15); - root = Insert(root, 8); - root = Insert(root, 10); - root = Insert(root, 26); - cout<data<<"\n"; - cout<left->right->data<<"\n"; //should be 10 (15->left(8)->right(10)) - cout<right->data<<"\n"; // should be 26 (15->right(26)) - return 0; -} \ No newline at end of file diff --git a/Data Structure/BinarySearchTree/levelorder_BST.cpp b/Data Structure/BinarySearchTree/levelorder_BST.cpp deleted file mode 100644 index 3ad0fc25..00000000 --- a/Data Structure/BinarySearchTree/levelorder_BST.cpp +++ /dev/null @@ -1,90 +0,0 @@ -//traversals in Binary Search Tree -#include -#include -using namespace std; -class node -{ - public: - int data; - node* left; - node* right; - node(int d) - { - data=d; - left=right=NULL; - } -}; -// creating bst -node* insertbst(node* root,int data) -{ - if(root == NULL) - { - root= new node(data); - return root; - } - if (root->data>data) - { - root->left= insertbst(root->left,data); - } - else - { - root->right= insertbst(root->right,data); - } - return root; -} -// enter nodes value -node* bst() -{ - node* root = NULL; - int data; - cin>>data; - if (data == -1) - { - return root; - } - while (data != -1) - { - root = insertbst(root,data); - cin>>data; - } - return root; -} -void levelorder(node* root) -{ - queue q; - q.push(root); - q.push(NULL); - - while (!q.empty()) - { - node* n=q.front(); - q.pop(); - if (n==NULL) - { - cout<data<<" "; - if (n->left) - { - q.push(n->left); - } - if(n->right) - { - q.push(n->right); - } - } - } -} -int main() -{ - node* root = bst(); - cout<<"Levelorder"< -using namespace std; - -class node -{ - public: - int data; - node* left, *right; -}; - -/* Function to find LCA of n1 and n2. -The function assumes that both n1 and n2 -are present in BST */ -struct node *lca(struct node* root, int n1, int n2) -{ - while (root != NULL) - { - // If both n1 and n2 are smaller than root, - // then LCA lies in left - if (root->data > n1 && root->data > n2) - root = root->left; - - // If both n1 and n2 are greater than root, - // then LCA lies in right - else if (root->data < n1 && root->data < n2) - root = root->right; - - else break; - } - return root; -} - - -/* Helper function that allocates -a new node with the given data.*/ -node* newNode(int data) -{ - node* Node = new node(); - Node->data = data; - Node->left = Node->right = NULL; - return(Node); -} - -/* Driver code*/ -int main() -{ - // Let us construct the BST - // shown in the above figure - node *root = newNode(20); - root->left = newNode(8); - root->right = newNode(22); - root->left->left = newNode(4); - root->left->right = newNode(12); - root->left->right->left = newNode(10); - root->left->right->right = newNode(14); - - int n1 = 10, n2 = 14; - node *t = lca(root, n1, n2); - cout << "LCA of " << n1 << " and " << n2 << " is " << t->data<data << endl; - - n1 = 10, n2 = 22; - t = lca(root, n1, n2); - cout << "LCA of " << n1 << " and " << n2 << " is " << t->data << endl; - return 0; -} diff --git a/Data Structure/BinaryTree/Balanced binary tree b/Data Structure/BinaryTree/Balanced binary tree deleted file mode 100644 index 4e0c8ed7..00000000 --- a/Data Structure/BinaryTree/Balanced binary tree +++ /dev/null @@ -1,95 +0,0 @@ -/* CPP program to check if -a tree is height-balanced or not */ -#include -using namespace std; - -/* A binary tree node has data, -pointer to left child and -a pointer to right child */ -class node { -public: - int data; - node* left; - node* right; -}; - -/* Returns the height of a binary tree */ -int height(node* node); - -/* Returns true if binary tree -with root as root is height-balanced */ -bool isBalanced(node* root) -{ - int lh; /* for height of left subtree */ - int rh; /* for height of right subtree */ - - /* If tree is empty then return true */ - if (root == NULL) - return 1; - - /* Get the height of left and right sub trees */ - lh = height(root->left); - rh = height(root->right); - - if (abs(lh - rh) <= 1 && isBalanced(root->left) && isBalanced(root->right)) - return 1; - - /* If we reach here then - tree is not height-balanced */ - return 0; -} - -/* UTILITY FUNCTIONS TO TEST isBalanced() FUNCTION */ - -/* returns maximum of two integers */ -int max(int a, int b) -{ - return (a >= b) ? a : b; -} - -/* The function Compute the "height" -of a tree. Height is the number of -nodes along the longest path from -the root node down to the farthest leaf node.*/ -int height(node* node) -{ - /* base case tree is empty */ - if (node == NULL) - return 0; - - /* If tree is not empty then - height = 1 + max of left - height and right heights */ - return 1 + max(height(node->left), - height(node->right)); -} - -/* Helper function that allocates -a new node with the given data -and NULL left and right pointers. */ -node* newNode(int data) -{ - node* Node = new node(); - Node->data = data; - Node->left = NULL; - Node->right = NULL; - - return (Node); -} - -// Driver code -int main() -{ - node* root = newNode(1); - root->left = newNode(2); - root->right = newNode(3); - root->left->left = newNode(4); - root->left->right = newNode(5); - root->left->left->left = newNode(8); - - if (isBalanced(root)) - cout << "Tree is balanced"; - else - cout << "Tree is not balanced"; - return 0; -} diff --git a/Data Structure/BinaryTree/README.md b/Data Structure/BinaryTree/README.md deleted file mode 100644 index ec373eaf..00000000 --- a/Data Structure/BinaryTree/README.md +++ /dev/null @@ -1 +0,0 @@ -## Binary Tree \ No newline at end of file diff --git a/Data Structure/BinaryTree/Same Tree b/Data Structure/BinaryTree/Same Tree deleted file mode 100644 index 5269152e..00000000 --- a/Data Structure/BinaryTree/Same Tree +++ /dev/null @@ -1,71 +0,0 @@ -// C++ program to see if two trees are identical -#include -using namespace std; - -/* A binary tree node has data, pointer to left child -and a pointer to right child */ -class node -{ - public: - int data; - node* left; - node* right; -}; - -/* Helper function that allocates a new node with the -given data and NULL left and right pointers. */ -node* newNode(int data) -{ - node* Node = new node(); - Node->data = data; - Node->left = NULL; - Node->right = NULL; - - return(Node); -} - -/* Given two trees, return true if they are -structurally identical */ -int identicalTrees(node* a, node* b) -{ - /*1. both empty */ - if (a == NULL && b == NULL) - return 1; - - /* 2. both non-empty -> compare them */ - if (a != NULL && b != NULL) - { - return - ( - a->data == b->data && - identicalTrees(a->left, b->left) && - identicalTrees(a->right, b->right) - ); - } - - /* 3. one empty, one not -> false */ - return 0; -} - -/* Driver code*/ -int main() -{ - node *root1 = newNode(1); - node *root2 = newNode(1); - root1->left = newNode(2); - root1->right = newNode(3); - root1->left->left = newNode(4); - root1->left->right = newNode(5); - - root2->left = newNode(2); - root2->right = newNode(3); - root2->left->left = newNode(4); - root2->left->right = newNode(5); - - if(identicalTrees(root1, root2)) - cout << "Both tree are identical."; - else - cout << "Trees are not identical."; - -return 0; -} diff --git a/Data Structure/BinaryTree/Symmetric Tree b/Data Structure/BinaryTree/Symmetric Tree deleted file mode 100644 index 95699b30..00000000 --- a/Data Structure/BinaryTree/Symmetric Tree +++ /dev/null @@ -1,65 +0,0 @@ -// C++ program to check if a given Binary Tree is symmetric or not -#include -using namespace std; - -// A Binary Tree Node -struct Node -{ - int key; - struct Node* left, *right; -}; - -// Utility function to create new Node -Node *newNode(int key) -{ - Node *temp = new Node; - temp->key = key; - temp->left = temp->right = NULL; - return (temp); -} - -// Returns true if trees with roots as root1 and root2 are mirror -bool isMirror(struct Node *root1, struct Node *root2) -{ - // If both trees are emptu, then they are mirror images - if (root1 == NULL && root2 == NULL) - return true; - - // For two trees to be mirror images, the following three - // conditions must be true - // 1 - Their root node's key must be same - // 2 - left subtree of left tree and right subtree - // of right tree have to be mirror images - // 3 - right subtree of left tree and left subtree - // of right tree have to be mirror images - if (root1 && root2 && root1->key == root2->key) - return isMirror(root1->left, root2->right) && - isMirror(root1->right, root2->left); - - // if neither of above conditions is true then root1 - // and root2 are not mirror images - return false; -} - -// Returns true if a tree is symmetric i.e. mirror image of itself -bool isSymmetric(struct Node* root) -{ - // Check if tree is mirror of itself - return isMirror(root, root); -} - -// Driver program -int main() -{ - // Let us construct the Tree shown in the above figure - Node *root = newNode(1); - root->left = newNode(2); - root->right = newNode(2); - root->left->left = newNode(3); - root->left->right = newNode(4); - root->right->left = newNode(4); - root->right->right = newNode(3); - - cout << isSymmetric(root); - return 0; -} diff --git a/Data Structure/Graph/Dijkstra_Algorithm.cpp b/Data Structure/Graph/Dijkstra_Algorithm.cpp deleted file mode 100644 index 91bdf6bd..00000000 --- a/Data Structure/Graph/Dijkstra_Algorithm.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/*Dijkstra's algorithm performs n iterations. On each iteration it selects an unmarked vertex v with the lowest value d[v], marks it and checks all the edges (v,to) attempting to improve the value d[to]. - -The running time of the algorithm consists of: - -n searches for a vertex with the smallest value d[v] among O(n) unmarked vertices -m relaxation attempts -For the simplest implementation of these operations on each iteration vertex search requires O(n) operations, and each relaxation can be performed in O(1). Hence, the resulting asymptotic behavior of the algorithm is: - -O(n2+m) -This complexity is optimal for dense graph, i.e. when m≈n2. However in sparse graphs, when m is much smaller than the maximal number of edges n2, the problem can be solved in O(nlogn+m) complexity. The algorithm and implementation can be found on the article Dijkstra on sparse graphs. -*/ - - -const int INF = 1000000000; -vector>> adj; - -void dijkstra(int s, vector & d, vector & p) { - int n = adj.size(); - d.assign(n, INF); - p.assign(n, -1); - vector u(n, false); - - d[s] = 0; - for (int i = 0; i < n; i++) { - int v = -1; - for (int j = 0; j < n; j++) { - if (!u[j] && (v == -1 || d[j] < d[v])) - v = j; - } - - if (d[v] == INF) - break; - - u[v] = true; - for (auto edge : adj[v]) { - int to = edge.first; - int len = edge.second; - - if (d[v] + len < d[to]) { - d[to] = d[v] + len; - p[to] = v; - } - } - } -} - - -/* -Here the graph adj is stored as adjacency list: for each vertex v adj[v] contains the list of edges going from this vertex, i.e. the list of pair where the first element in the pair is the vertex at the other end of the edge, and the second element is the edge weight. - -The function takes the starting vertex s and two vectors that will be used as return values. - -First of all, the code initializes arrays: distances d[], labels u[] and predecessors p[]. Then it performs n iterations. At each iteration the vertex v is selected which has the smallest distance d[v] among all the unmarked vertices. If the distance to selected vertex v is equal to infinity, the algorithm stops. Otherwise the vertex is marked, and all the edges going out from this vertex are checked. If relaxation along the edge is possible (i.e. distance d[to] can be improved), the distance d[to] and predecessor p[to] are updated. - -After performing all the iterations array d[] stores the lengths of the shortest paths to all vertices, and array p[] stores the predecessors of all vertices (except starting vertex s). The path to any vertex t can be restored in the following way: -*/ - - -vector restore_path(int s, int t, vector const& p) { - vector path; - - for (int v = t; v != s; v = p[v]) - path.push_back(v); - path.push_back(s); - - reverse(path.begin(), path.end()); - return path; -} diff --git a/Data Structure/Graph/Dijkstra_algorithm.py b/Data Structure/Graph/Dijkstra_algorithm.py deleted file mode 100644 index 0ced0c8a..00000000 --- a/Data Structure/Graph/Dijkstra_algorithm.py +++ /dev/null @@ -1,79 +0,0 @@ -# Python program for Dijkstra's single -# source shortest path algorithm. The program is -# for adjacency matrix representation of the graph - -# Library for INT_MAX -import sys - -class Graph(): - - def __init__(self, vertices): - self.V = vertices - self.graph = [[0 for column in range(vertices)] - for row in range(vertices)] - - def printSolution(self, dist): - print ("Vertex tDistance from Source") - for node in range(self.V): - print (node, "t", dist[node]) - - def minDistance(self, dist, sptSet): - - # Initilaize minimum distance for next node - min = sys.maxsize - - # Search not nearest vertex not in the - # shortest path tree - for v in range(self.V): - if dist[v] < min and sptSet[v] == False: - min = dist[v] - min_index = v - - return min_index - - # Funtion that implements Dijkstra's single source - # shortest path algorithm for a graph represented - # using adjacency matrix representation - def dijkstra(self, src): - - dist = [sys.maxsize] * self.V - dist[src] = 0 - sptSet = [False] * self.V - - for cout in range(self.V): - - # Pick the minimum distance vertex from - # the set of vertices not yet processed. - # u is always equal to src in first iteration - u = self.minDistance(dist, sptSet) - - # Put the minimum distance vertex in the - # shotest path tree - sptSet[u] = True - - # Update dist value of the adjacent vertices - # of the picked vertex only if the current - # distance is greater than new distance and - # the vertex in not in the shotest path tree - for v in range(self.V): - if self.graph[u][v] > 0 and \ - sptSet[v] == False and \ - dist[v] > dist[u] + self.graph[u][v]: - dist[v] = dist[u] + self.graph[u][v] - - self.printSolution(dist) - -# Driver program -g = Graph(9) -g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0], - [4, 0, 8, 0, 0, 0, 0, 11, 0], - [0, 8, 0, 7, 0, 4, 0, 0, 2], - [0, 0, 7, 0, 9, 14, 0, 0, 0], - [0, 0, 0, 9, 0, 10, 0, 0, 0], - [0, 0, 4, 14, 10, 0, 2, 0, 0], - [0, 0, 0, 0, 0, 2, 0, 1, 6], - [8, 11, 0, 0, 0, 0, 1, 0, 7], - [0, 0, 2, 0, 0, 0, 6, 7, 0] - ]; - -g.dijkstra(0); diff --git a/Data Structure/Graph/Kruskal_Algorithm.cpp b/Data Structure/Graph/Kruskal_Algorithm.cpp deleted file mode 100644 index 1ae9b1a6..00000000 --- a/Data Structure/Graph/Kruskal_Algorithm.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include -#include -using std::vector; -struct point{ - int x; int y; int parent; int rank; - point(int a, int b, int c, int d):x(a), y(b), parent(c), rank(d){} -}; -struct Edge{ - int indexa, indexb; - double length; - Edge(int a, int b, double c):indexa(a), indexb(b), length(c){} -}; -double distance(int x1, int y1, int x2, int y2){ - return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); -} -struct sort_edges{ - inline bool operator()(const Edge& struct1, const Edge& struct2){ - return (struct1.length < struct2.length); - } -}; -int find(int i, vector &points){ - int x=i; - while(i != points[i].parent) - i=points[i].parent; - points[x].parent=i; - return i; -} -void Union(int a, int b, vector &points){ - if(points[a].rank < points[b].rank) - points[a].parent = b; - else { - points[b].parent = a; - if(points[a].rank == points[b].rank){ - points[a].rank++; - } - } -} -double minimum_distance(vector x, vector y) { - double result = 0.0; - vector points; - for(int i=0; i edges; - for(int i=0; i> n; - vector x(n), y(n); - for (size_t i = 0; i < n; i++) { - std::cin >> x[i] >> y[i]; - } - std::cout << std::setprecision(10) << minimum_distance(x, y) << std::endl; -} diff --git a/Data Structure/Graph/README.md b/Data Structure/Graph/README.md deleted file mode 100644 index 498019f0..00000000 --- a/Data Structure/Graph/README.md +++ /dev/null @@ -1,3 +0,0 @@ -## Graph - - diff --git a/Data Structure/HashTable/README.md b/Data Structure/HashTable/README.md deleted file mode 100644 index 25303c42..00000000 --- a/Data Structure/HashTable/README.md +++ /dev/null @@ -1,3 +0,0 @@ -## Hash Table - - diff --git a/Data Structure/Linked List Reversal b/Data Structure/Linked List Reversal deleted file mode 100644 index e40af2ff..00000000 --- a/Data Structure/Linked List Reversal +++ /dev/null @@ -1,82 +0,0 @@ -// Iterative C++ program to reverse -// a linked list -#include -using namespace std; - -/* Link list node */ -struct Node { - int data; - struct Node* next; - Node(int data) - { - this->data = data; - next = NULL; - } -}; - -struct LinkedList { - Node* head; - LinkedList() - { - head = NULL; - } - - /* Function to reverse the linked list */ - void reverse() - { - // Initialize current, previous and - // next pointers - Node* current = head; - Node *prev = NULL, *next = NULL; - - while (current != NULL) { - // Store next - next = current->next; - - // Reverse current node's pointer - current->next = prev; - - // Move pointers one position ahead. - prev = current; - current = next; - } - head = prev; - } - - /* Function to print linked list */ - void print() - { - struct Node* temp = head; - while (temp != NULL) { - cout << temp->data << " "; - temp = temp->next; - } - } - - void push(int data) - { - Node* temp = new Node(data); - temp->next = head; - head = temp; - } -}; - -/* Driver program to test above function*/ -int main() -{ - /* Start with the empty list */ - LinkedList ll; - ll.push(20); - ll.push(4); - ll.push(15); - ll.push(85); - - cout << "Given linked list\n"; - ll.print(); - - ll.reverse(); - - cout << "\nReversed Linked list \n"; - ll.print(); - return 0; -} diff --git a/Data Structure/LinkedList/876_Middle_of_the_Linked_List.java b/Data Structure/LinkedList/876_Middle_of_the_Linked_List.java deleted file mode 100644 index 448486d0..00000000 --- a/Data Structure/LinkedList/876_Middle_of_the_Linked_List.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode() {} - * ListNode(int val) { this.val = val; } - * ListNode(int val, ListNode next) { this.val = val; this.next = next; } - * } - */ -class Solution { - public ListNode middleNode(ListNode head) { - ListNode node = head; - int len = 0; - while(node != null){ - len++; - node = node.next; - } - ListNode c = head; - int mid = (int)Math.ceil(len/2); - for(int i = 0; i diff --git a/Data Structure/LinkedList/SingleLinkedList_Insertion.cpp b/Data Structure/LinkedList/SingleLinkedList_Insertion.cpp deleted file mode 100644 index 5bd6975f..00000000 --- a/Data Structure/LinkedList/SingleLinkedList_Insertion.cpp +++ /dev/null @@ -1,36 +0,0 @@ -Node *insertAtBegining(Node *head, int newData) -{ - Node *temp = new Node(newData); - if (head == NULL) - { - head = temp; - return head; - } - else - { - temp->next = head; - head = temp; - return head; - } -} - -Node *insertAtEnd(Node *head, int newData) -{ - Node *temp = new Node(newData); - if (head == NULL) - { - head = temp; - return head; - } - else - { - Node *last = head; - while (last->next != NULL) - { - last = last->next; - } - last->next = temp; - - return head; - } -} \ No newline at end of file diff --git a/Data Structure/LinkedList/circular_LinkedList_traversal.cpp b/Data Structure/LinkedList/circular_LinkedList_traversal.cpp deleted file mode 100644 index c8c79d44..00000000 --- a/Data Structure/LinkedList/circular_LinkedList_traversal.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// C++ program to implement -// the above approach -#include -using namespace std; - -/* structure for a node */ -class Node -{ - public: - int data; - Node *next; -}; - -/* Function to insert a node at the beginning -of a Circular linked list */ -void push(Node **head_ref, int data) -{ - Node *ptr1 = new Node(); - Node *temp = *head_ref; - ptr1->data = data; - ptr1->next = *head_ref; - - /* If linked list is not NULL then - set the next of last node */ - if (*head_ref != NULL) - { - while (temp->next != *head_ref) - temp = temp->next; - temp->next = ptr1; - } - else - ptr1->next = ptr1; /*For the first node */ - - *head_ref = ptr1; -} - -/* Function to print nodes in -a given Circular linked list */ -void printList(Node *head) -{ - Node *temp = head; - if (head != NULL) - { - do - { - cout << temp->data << " "; - temp = temp->next; - } - while (temp != head); - } -} - -/* Driver program to test above functions */ -int main() -{ - /* Initialize lists as empty */ - Node *head = NULL; - - /* Created linked list will be 11->2->56->12 */ - push(&head, 12); - push(&head, 56); - push(&head, 2); - push(&head, 11); - - cout << "Contents of Circular Linked List\n "; - printList(head); - - return 0; -} - -// This is code is contributed by vickyjsr diff --git a/Data Structure/LinkedList/dllusingc.c b/Data Structure/LinkedList/dllusingc.c deleted file mode 100644 index 783f78a9..00000000 --- a/Data Structure/LinkedList/dllusingc.c +++ /dev/null @@ -1,189 +0,0 @@ -#include -#include - -struct node { - int data; - struct node *next; // Pointer to next node - struct node *prev; // Pointer to previous node -}; - -void insertatfront(struct node **head, int x) -{ - struct node *newnode = (struct node*)malloc(sizeof(struct node)); - newnode->data = x; - newnode->next = (*head); - newnode->prev = NULL; - if (*head != NULL) - { - (*head)->prev = newnode; - } - *head = newnode; -} -void insertafter(struct node **prev, int x) -{ - struct node *newnode = (struct node*)malloc(sizeof(struct node)); - newnode->data = x; - newnode->next = (*prev)->next; - (*prev)->next = newnode; - newnode->prev = *prev; - if (newnode->next != NULL) - { - newnode->next->prev = newnode; - } -} -void append(struct node **head, int x) -{ - struct node *newnode = (struct node*)malloc(sizeof(struct node)); - newnode->data = x; - newnode->next = NULL; - struct node *temp; - temp = *head; - while(temp->next!=NULL) - { - temp = temp->next; - } - temp->next = newnode; - newnode->prev = temp; -} -void insertbefore(struct node **next,int x) -{ - struct node* newnode = (struct node*)malloc(sizeof(struct node)); - newnode->data = x; - newnode->prev = (*next)->prev; - (*next)->prev = newnode; - newnode->next = *next; - if (newnode->prev != NULL) - { - newnode->prev->next = newnode; - } -} -void deleteNode(struct node** head_ref, struct node* del) -{ - if (*head_ref == NULL || del == NULL) - return; - if (*head_ref == del) - *head_ref = del->next; - if (del->next != NULL) - del->next->prev = del->prev; - if (del->prev != NULL) - del->prev->next = del->next; - free(del); - return; -} -void printforward(struct node **head) -{ - struct node *temp; - temp = *head; - printf("\nTraversal in forward direction \n"); - while (temp != NULL) - { - printf(" %d ", temp->data); - temp = temp->next; - } - printf("\n"); -} -void printbackward(struct node **head) -{ - struct node *last,*node; - node = *head; - printf("\nTraversal in reverse direction \n"); - while (node != NULL) - { - last = node; - node = node->next; - } - while (last != NULL) - { - printf(" %d ", last->data); - last = last->prev; - } - printf("\n"); -} - -int main() -{ - struct node *head,*temp; - int n,x,i,l; - printf("Enter no of elements: \n"); - scanf("%d",&n); - if(n==0) - { - printf("empty list"); - } - else - { - printf("Enter elements: \n"); - scanf("%d",&x); - head->data = x; - head->next = NULL; - head->prev = NULL; - for(i=2;i<=n;i++) - { - scanf("%d",&x); - append(&head,x);//basically insertion at end operation - } - printforward(&head); - printbackward(&head); - printf("Enter no of elements to be inserted at front\n"); - scanf("%d",&l); - int pos; - for(i=0;inext; - pos--; - } - insertbefore(&temp,x); - } - printforward(&head); - printf("enter no of elements to be inserted after a node \n"); - scanf("%d",&l); - for(i=0;inext; - pos--; - } - insertafter(&temp,x); - } - printf("enter no of elements to delete \n"); - scanf("%d",&l); - for(i=0;inext; - } - if (current == NULL) - { - printf("Can't delete"); - } - deleteNode(&head, current); - } - } - return 0; -} - diff --git a/Data Structure/LinkedList/doubly-linked-list.cpp b/Data Structure/LinkedList/doubly-linked-list.cpp deleted file mode 100644 index a1a9951a..00000000 --- a/Data Structure/LinkedList/doubly-linked-list.cpp +++ /dev/null @@ -1,95 +0,0 @@ -using namespace std; -# include -struct Node{ - int data; - Node *Prev; - Node *Next; -}; -Node *head; -Node *tail; -struct Node* GetNode(int x) -{ - Node *newNode = new Node(); - newNode->data = x; - newNode->Next = NULL; - newNode->Prev = NULL; - return newNode; -} -void InsertatHead(int x) -{ - Node *temp = GetNode(x); - if(head==NULL) - { - head = temp; - tail = temp; - return; - } - head->Prev = temp; - temp->Next = head; - head = temp; -} -void PrintfromHead() -{ - cout << "The actual linked list is :\n"; - Node *temp = head; - while (temp != NULL) - { - cout << temp->data<<", "; - temp = temp->Next; - } - cout << "\n"; -} -void PrintfromTail() -{ - cout << "The reversed linked list is :\n"; - Node *temp = tail; - while(temp!=NULL) - { - cout << temp->data << ", "; - temp = temp->Prev; - } - cout << "\n"; -} -void InsertatTail(int x) -{ - Node *temp = GetNode(x); - if(tail == NULL) - { - head = temp; - tail = temp; - return; - } - temp->Prev = tail; - tail->Next = temp; - tail = temp; -} -int main() -{ - char ch,ch1; - int n,x; - do - { - cout << "Enter your choice A. Insert a node at tail B. Insert a node at head. "; - cin >> ch; - switch (ch) - { - case 'A': - cout << "Enter Number :"; - cin >> x; - InsertatTail(x); - break; - case 'B': - cout << "Enter Number :"; - cin >> x; - InsertatHead(x); - break; - default: - cout << "Invalid Input. Try again"; - break; - } - cout << "Enter 'Y' to continue. Enter 'N' to exit."; - cin >> ch1; - } while (ch1 == 'Y'); - PrintfromHead(); - PrintfromTail(); -} \ No newline at end of file diff --git a/Data Structure/LinkedList/linked-list.cpp b/Data Structure/LinkedList/linked-list.cpp deleted file mode 100644 index 48889b04..00000000 --- a/Data Structure/LinkedList/linked-list.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include -using namespace std; -struct Node { - int data; - Node* next; -}; -Node* head; -void Print(); -void Insert(int data,int n) -{ - cout << "Inserting at the given position \n"; - Node *temp1 = new Node(); - temp1->data = data; - temp1->next = NULL; - if(n==1) - { - temp1->next = head; - head = temp1; - Print(); - return; - } - Node *temp2 = head; - for (int i = 0; i < n - 2;i++) - { - temp2 = temp2->next; - } - temp1->next = temp2->next; - temp2->next = temp1; - Print(); -} -void Reverse() -{ - cout << "Reversing \n"; - Node *current = head; - Node *prev = NULL; - Node *next = NULL; - while(current != NULL) - { - next = current->next; - current->next = prev; - prev = current; - current = next; - } - head = prev; - Print(); -} -void InsertEnd(int data) -{ - cout << "Inserting at Tail \n"; - Node *temp = new Node; - temp->data = data; - Node* temp2 = head; - while (temp2->next != NULL) - { - temp2 = temp2->next; - } - temp2->next = temp; - temp->next = NULL; - Print(); -} -void InsertFirst(int data) -{ - cout << "Inserting at head \n"; - Node *temp = new Node; - temp->data = data; - temp->next = head; - head = temp; - Print(); -} -void DeleteEnd() -{ - cout << "Deleting from tail \n"; - Node *temp = head; - while (temp->next->next != NULL) - { - temp = temp->next; - } - delete (temp->next); - temp->next = NULL; - - Print(); -} -void DeleteFirst() -{ - cout << "Deleting from head \n"; - Node *temp = head; - head = temp->next; - delete (temp); - Print(); -} -void Print() -{ - cout << "The resulting list is : " - << "\n"; - Node *temp = head; - while (temp != NULL) - { - cout << temp->data<<" "; - temp = temp->next; - } - cout << "\n"; -} -int main() -{ - Insert(3, 1); - Insert(2, 2); - Insert(1, 1); - InsertEnd(13); - InsertEnd(12); - InsertFirst(98); - DeleteEnd(); - DeleteFirst(); - InsertEnd(243); - InsertFirst(128); - Reverse(); -} \ No newline at end of file diff --git a/Data Structure/LinkedList/rotate_LL.cpp b/Data Structure/LinkedList/rotate_LL.cpp deleted file mode 100644 index 65931c50..00000000 --- a/Data Structure/LinkedList/rotate_LL.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include -using namespace std; -struct LLNode{ - int data; - LLNode *next; -}; -//This funnction will rotate the list counter clockwise by k nodes -void rotate(LLNode** head, int k) -{ - LLNode* temp = *head; - int count = 1; - if (k==0) - { - return ; - } - //Pointing temp to the kth node - while(countnext; - count++; - } - //If it is NULL then dont rotate - if (temp==NULL) - { - return; - } - //point KthNode to temp - LLNode* KthNode = temp; - //Move temp to the last node - while(temp->next != NULL) { - temp = temp->next; - } - //point next of temp to the head - temp->next = *head; - //change head to the next of Kth node - *head = KthNode->next; - //Point next of KthNode to NULL - KthNode->next = NULL; -} -void insertAtBeginning(LLNode**head,int dataToBeInserted) -{ - LLNode*curr=new LLNode; - //make a new node with this data and next pointing to NULL - curr->data=dataToBeInserted; - curr->next=NULL; - if(*head==NULL) //if list is empty then set the current formed node as head of list - *head=curr; - - else //make the next of the current node point to the present head and make the current node as the new head - { - curr->next=*head; - *head=curr; - } - - //O(1) constant time -} -void display(LLNode**head) -{ - LLNode*temp=*head; - while(temp!=NULL) //till the list ends (NULL marks ending of list) - { - if(temp->next!=NULL) - cout<data<<" ->"; - else - cout<data; - - temp=temp->next; //move to next node - } - //O(number of nodes) - cout< -using namespace std; - -struct Queue { - stack s1, s2; - - void enQueue(int x) - { - // Move all elements from s1 to s2 - while (!s1.empty()) { - s2.push(s1.top()); - s1.pop(); - } - - // Push item into s1 - s1.push(x); - - // Push everything back to s1 - while (!s2.empty()) { - s1.push(s2.top()); - s2.pop(); - } - } - - // Dequeue an item from the queue - int deQueue() - { - // if first stack is empty - if (s1.empty()) { - cout << "Q is Empty"; - exit(0); - } - - // Return top of s1 - int x = s1.top(); - s1.pop(); - return x; - } -}; - -// Driver code -int main() -{ - Queue q; - q.enQueue(1); - q.enQueue(2); - q.enQueue(3); - - cout << q.deQueue() << '\n'; - cout << q.deQueue() << '\n'; - cout << q.deQueue() << '\n'; - - return 0; -} diff --git a/Data Structure/Queue/QueueWithoutUsingStacks.cpp b/Data Structure/Queue/QueueWithoutUsingStacks.cpp deleted file mode 100644 index 0985305d..00000000 --- a/Data Structure/Queue/QueueWithoutUsingStacks.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include -using namespace std; -template -class queue -{ - int size; - int front; - int rear; - T *a; - - public: - queue(int size) - { - this->size=size; - front=rear=-1; - a=new T[this->size]; - } - void enqueue(); - T dequeue(); - void display(); -}; -template -void queue::enqueue() -{ - if(this->rear==this->size-1) - { - cout<<"Queue is full."; - return; - } - this->rear++; - cout<<"Enter the element:"; - cin>>this->a[this->rear]; -} -template -T queue::dequeue() -{ - if(this->front==this->rear) - { - cout<<"Queue is empty."<front++; - return this->a[this->front]; - } -} -template -void queue::display() -{ - cout<<"Elements of queue:"<front+1;i<=this->rear;i++) - { - cout<a[i]< Q(10); - int t=5; - while(t--) - Q.enqueue(); - Q.display(); - cout<<"Deleted element:"< - -- [Queue in Javascript](queue_in_javascript.js) -- [Queue Using Stacks in C++](QueueUsingStacks.cpp) diff --git a/Data Structure/Queue/queue_in_javascript.js b/Data Structure/Queue/queue_in_javascript.js deleted file mode 100644 index 2b03db21..00000000 --- a/Data Structure/Queue/queue_in_javascript.js +++ /dev/null @@ -1,94 +0,0 @@ -class Node { - constructor(value) { - this.value = value; - this.next = null; - } -} - -class Queue { - constructor(value) { - this.first = new Node(value); - this.last = this.first; - this.length = 1; - } - - // adds the element to the last of Queue - // constant time complexity - enqueue(value = null) { - if (!value) { - console.log("Cannot push to the Queue: Value Empty"); - return; - } - const newNode = new Node(value); - this.last.next = newNode; - this.last = newNode; - this.length++; - } - - // removes the element from the first of the Queue - // constant time complex... - dequeue() { - if (this.isEmpty()) { - console.log("Cannot pop from the Queue : Queue Empty"); - return; - } - const unWantedNode = this.first; - console.log("Node deleted from first : ", unWantedNode.value); - if (this.first === this.last) { - this.first = null; - this.last = null; - } else { - this.first = this.first.next; - } - this.length--; - } - - // traverse through the list - // linear time - traverse() { - if (this.isEmpty()) { - console.log("Cannot traverse : Queue Empty"); - return; - } - let leader = this.first; - console.log("Queue displayed : "); - while (leader) { - console.log(leader.value); - leader = leader.next; - } - } - - // returns the top element from the Queue - // constant time - peek() { - if (this.isEmpty()) { - console.log("Cannot peek from the Queue : Queue Empty"); - return; - } - console.log("Peek Node value : ", this.first.value); - return this.first; - } - - // checks if the Queue is empty - // constant time complexity - isEmpty() { - if (!this.first) { - return true; - } - return false; - } -} - -const myQueue = new Queue(10); -myQueue.enqueue(20); -myQueue.enqueue(30); -myQueue.enqueue(40); -myQueue.traverse(); -myQueue.peek(); -myQueue.dequeue(); -myQueue.dequeue(); -myQueue.dequeue(); -myQueue.dequeue(); -myQueue.dequeue(); -myQueue.peek(); -console.log(myQueue); diff --git a/Data Structure/README.md b/Data Structure/README.md deleted file mode 100644 index c6569cc2..00000000 --- a/Data Structure/README.md +++ /dev/null @@ -1,23 +0,0 @@ - -## Data Structure - - - -![Datastructures](https://i.imgur.com/Qycb64x.png) -> ## Oh you've come so far please do check out the other submissions as well ! 😁 -[Algorithms](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Algorithms) - -[Data structures](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Data%20Structure) - -[Friend functions](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Friend%20Function) - -[Games](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Games) - -[Hello World Programs](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Hello%20World%20Programs) - -[Java Programs](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Java%20programs) - -[Machine Learning Projects](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Machine%20Learning%20Projects) - -[Projects](https://github.com/Mustafiz04/Contribute-to-HacktoberFest2020/tree/main/Projects) - diff --git a/Data Structure/Segment Tree/First element at least X - CF edu b/Data Structure/Segment Tree/First element at least X - CF edu deleted file mode 100644 index f270d58a..00000000 --- a/Data Structure/Segment Tree/First element at least X - CF edu +++ /dev/null @@ -1,87 +0,0 @@ -/* -author: accesss_denied -*/ -#include -using namespace std; -#define ll long long int -#define endl "\n" -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll inf=1e10; -vectorseg; - - void build(ll id,ll s, ll e,ll arr[]) - { - if(s==e) - { - seg[id]=arr[s]; - return ; - } - ll mid=(s+e)/2; - build(2*id,s,mid,arr); - build(2*id+1,mid+1,e,arr); - seg[id]=max(seg[2*id],seg[2*id+1]); - } - ll query(ll id, ll s,ll e,ll val) - { - if(s==e) - { - if(seg[id]>=val) - return s; - return -1; - } - ll mid=(s+e)/2; - if(seg[2*id]>=val) - return query(2*id,s,mid,val); - else - return query(2*id+1,mid+1,e,val); - } - void update(ll id, ll s,ll e, ll i,ll val) - { - if(s==e) - { - seg[id]=val; - return ; - } - ll mid=(s+e)/2; - if(i<=mid) - update(2*id,s,mid,i,val); - else - update(2*id+1,mid+1,e,i,val); - seg[id]=max(seg[2*id],seg[2*id+1]); - } - -int main() -{ - fast; - #ifndef ONLINE_JUDGE - freopen("input.txt","r",stdin); - freopen("output.txt","w",stdout); - #endif - ll n,m; - cin>>n>>m; - seg.assign(4*n+2,0); - ll arr[n]; - for(ll i=0;i>arr[i]; - - build(1,0,n-1,arr); - - while(m--) - { - ll a,b,c; - cin>>a; - if(a==1) - { - cin>>b>>c; - update(1,0,n-1,b,c); - } - else - { - cin>>b; - cout< -using namespace std; -#define ll long long int -#define ff first -#define ss second -#define pb push_back -#define endl "\n" -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll mod=1e9+7; -ll inf=1e18; - - struct node{ - ll prefix; - ll suffix; - ll total; - ll ms; - }; - - void build(ll id,ll s,ll e,node* seg[],ll arr[]) - { - if(s==e) - { - seg[id]->ms=arr[s]; - seg[id]->total=arr[s]; - seg[id]->prefix=arr[s]; - seg[id]->suffix=arr[s]; - return; - } - ll mid=(s+e)/2; - build(2*id,s,mid,seg,arr); - build(2*id+1,mid+1,e,seg,arr); - - seg[id]->total=seg[2*id]->total+seg[2*id+1]->total; - seg[id]->prefix=max(seg[2*id]->prefix, seg[2*id]->total+seg[2*id+1]->prefix); - seg[id]->suffix=max(seg[2*id+1]->suffix, seg[2*id+1]->total+seg[2*id]->suffix); - seg[id]->ms=max(seg[2*id]->ms,max(seg[2*id+1]->ms, seg[2*id]->suffix+seg[2*id+1]->prefix)); - } - void update(ll id,ll s,ll e,node* seg[],ll arr[],ll i,ll val) - { - - if(s==e) - { - seg[id]->ms=val; - seg[id]->total=val; - seg[id]->prefix=val; - seg[id]->suffix=val; - return; - } - ll mid=(s+e)/2; - if(i<=mid) - update(2*id,s,mid,seg,arr,i,val); - else - update(2*id+1,mid+1,e,seg,arr,i,val); - - seg[id]->total=seg[2*id]->total+seg[2*id+1]->total; - seg[id]->prefix=max(seg[2*id]->prefix, seg[2*id]->total+seg[2*id+1]->prefix); - seg[id]->suffix=max(seg[2*id+1]->suffix, seg[2*id+1]->total+seg[2*id]->suffix); - seg[id]->ms=max(seg[2*id]->ms,max(seg[2*id+1]->ms, seg[2*id]->suffix+seg[2*id+1]->prefix)); - } - - -int main() { - fast - #ifndef ONLINE_JUDGE - freopen("input.txt","r",stdin); - freopen("output.txt","w",stdout); - #endif - - ll n,m; - cin>>n>>m; - ll arr[n]; - - node* seg[4*n+1]; - for(ll i=1;i<=4*n;i++) - { - node* temp=new node(); - seg[i]=temp; - } - for(ll i=0;i>arr[i]; - - build(1,0,n-1,seg,arr); - cout<ms)<>x>>y; - update(1,0,n-1,seg,arr,x,y); - cout<ms)< -using namespace std; -#define ll long long int -#define endl "\n" -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll inf=1e10; -vectorseg; - - void build(ll id,ll s, ll e,ll arr[]) - { - if(s==e) - { - seg[id]=arr[s]; - return ; - } - ll mid=(s+e)/2; - build(2*id,s,mid,arr); - build(2*id+1,mid+1,e,arr); - seg[id]=seg[2*id]+seg[2*id+1]; - } - ll query(ll id, ll s,ll e,ll k) - { - if(s==e) - return s; - ll mid=(s+e)/2; - if(seg[2*id]>=k) - return query(2*id,s,mid,k); - else - return query(2*id+1,mid+1,e,k-seg[2*id]); - } - void update(ll id, ll s,ll e, ll i) - { - if(s==e) - { - seg[id]^=1; - return ; - } - ll mid=(s+e)/2; - if(i<=mid) - update(2*id,s,mid,i); - else - update(2*id+1,mid+1,e,i); - seg[id]=seg[2*id]+seg[2*id+1]; - } - -int main() -{ - fast; - #ifndef ONLINE_JUDGE - freopen("input.txt","r",stdin); - freopen("output.txt","w",stdout); - #endif - ll n,m; - cin>>n>>m; - seg.assign(4*n+2,0); - ll arr[n]; - for(ll i=0;i>arr[i]; - - build(1,0,n-1,arr); - - while(m--) - { - ll a,b; - cin>>a>>b; - if(a==1) - { - update(1,0,n-1,b); - } - else - { - cout< -using namespace std; -#define ll long long int -#define ff first -#define ss second -#define pb push_back -#define endl "\n" -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll mod=1e9+7; -ll inf=1e11; - - struct node{ - ll prefix; - ll suffix; - ll total; - ll ms; - }; - - void build(ll id,ll s,ll e,node* seg[],ll arr[]) - { - if(s==e) - { - seg[id]->ms=arr[s]; - seg[id]->total=arr[s]; - seg[id]->prefix=arr[s]; - seg[id]->suffix=arr[s]; - return; - } - ll mid=(s+e)/2; - build(2*id,s,mid,seg,arr); - build(2*id+1,mid+1,e,seg,arr); - - seg[id]->total=seg[2*id]->total+seg[2*id+1]->total; - seg[id]->prefix=max(seg[2*id]->prefix, seg[2*id]->total+seg[2*id+1]->prefix); - seg[id]->suffix=max(seg[2*id+1]->suffix, seg[2*id+1]->total+seg[2*id]->suffix); - seg[id]->ms=max(seg[2*id]->ms,max(seg[2*id+1]->ms, seg[2*id]->suffix+seg[2*id+1]->prefix)); - } - - node query(ll id,ll s,ll e, ll qs,ll qe,node * seg[]) - { - - if(qee) - { - node temp; - temp.total=-inf; - temp.ms=-inf; - temp.prefix=-inf; - temp.suffix=-inf; - return temp; - } - if(qs<=s and qe>=e) - { - node temp; - temp.total=seg[id]->total; - temp.ms=seg[id]->ms; - temp.prefix=seg[id]->prefix; - temp.suffix=seg[id]->suffix; - return temp; - } - - ll mid=(s+e)/2; - node left=query(2*id,s,mid,qs,qe,seg); - node right=query(2*id+1,mid+1,e,qs,qe,seg); - - node temp; - temp.total=left.total+right.total; - temp.prefix=max(left.prefix, left.total+right.prefix); - temp.suffix=max(right.suffix, right.total+left.suffix); - temp.ms=max(left.ms,max(right.ms, left.suffix+right.prefix)); - - - return temp; - } - - -int main() { - fast - #ifndef ONLINE_JUDGE - freopen("input.txt","r",stdin); - freopen("output.txt","w",stdout); - #endif - - ll n,m; - cin>>n; - ll arr[n]; - - node* seg[4*n+1]; - for(ll i=1;i<=4*n;i++) - { - node* temp=new node(); - seg[i]=temp; - } - for(ll i=0;i>arr[i]; - - build(1,0,n-1,seg,arr); - cin>>m; - while(m--) - { - ll x,y; - cin>>x>>y; - node xxx; - xxx=query(1,0,n-1,x-1,y-1,seg); - cout< -using namespace std; -#define ll long long int -#define endl "\n" -#define ff first -#define ss second -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll inf=1e10; -vector>seg; - - void build(ll id,ll s, ll e,ll arr[]) - { - if(s==e) - { - seg[id]={arr[s],1}; - return ; - } - ll mid=(s+e)/2; - build(2*id,s,mid,arr); - build(2*id+1,mid+1,e,arr); - if(seg[2*id].ff query(ll id, ll s,ll e, ll qs,ll qe) - { - if(qs>e or qe=e) - return {seg[id].ff,seg[id].ss}; - ll mid=(s+e)/2; - pairleft=query(2*id,s,mid,qs,qe); - pairright=query(2*id+1,mid+1,e,qs,qe); - if(left.ff>n>>m; - seg.resize(4*n+2); - ll arr[n]; - for(ll i=0;i>arr[i]; - - build(1,0,n-1,arr); - - while(m--) - { - ll a,b,c; - cin>>a>>b>>c; - if(a==1) - { - update(1,0,n-1,b,c); - } - else - { - pairans=query(1,0,n-1,b,c-1); - cout< -using namespace std; -#define ll long long int -#define ff first -#define ss second -#define pb push_back -#define endl "\n" -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll mod=1e9+7; -ll inf=1e18; -vectorseg; - - void build(ll index, ll start, ll end, ll arr[]) - { - if(start==end) - { - seg[index]=arr[start]; - return; - } - //apply postorder tree traversal and build tree for every node - //call left subtree, then right then build the root node - ll mid=(start+end)/2; - - build(2*index,start,mid,arr); //left recursive call - build(2*index+1,mid+1,end,arr); //right recursive call - - seg[index]=min(seg[2*index],seg[2*index+1]); //calculating value at root node - } - - ll query(ll index,ll start,ll end, ll qstart, ll qend) - { - //no overlap - if(qstart>end or qend=end) - return seg[index]; - - //partial overlap - ll mid=(start+end)/2; - - ll left=query(2*index,start,mid,qstart,qend); - ll right=query(2*index+1,mid+1,end,qstart,qend); - - return min(left,right); - } - - void update(ll index,ll start,ll end,ll id,ll val) - { - //out of bound - if(id>end or id>n>>q; - ll arr[n]; - seg.assign(4*n+1,0); - - for(ll i=0;i>arr[i]; - - build(1,0,n-1,arr); - while(q--) - { - char a; - ll b,c; - cin>>a>>b>>c; - if(a=='q') - { - cout< -using namespace std; -#define ll long long int -#define endl "\n" -#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -ll inf=1e10; -vectorseg; - - void build(ll id,ll s, ll e,ll arr[]) - { - if(s==e) - { - seg[id]=arr[s]; - return ; - } - ll mid=(s+e)/2; - build(2*id,s,mid,arr); - build(2*id+1,mid+1,e,arr); - seg[id]=seg[2*id]+seg[2*id+1]; - } - ll query(ll id, ll s,ll e, ll qs,ll qe) - { - if(qs>e or qe=e) - return seg[id]; - ll mid=(s+e)/2; - ll left=query(2*id,s,mid,qs,qe); - ll right=query(2*id+1,mid+1,e,qs,qe); - return left+right; - } - void update(ll id, ll s,ll e, ll i,ll val) - { - if(s==e) - { - seg[id]=val; - return ; - } - ll mid=(s+e)/2; - if(i<=mid) - update(2*id,s,mid,i,val); - else - update(2*id+1,mid+1,e,i,val); - seg[id]=seg[2*id]+seg[2*id+1]; - } - -int main() -{ - fast; - #ifndef ONLINE_JUDGE - freopen("input.txt","r",stdin); - freopen("output.txt","w",stdout); - #endif - ll n,m; - cin>>n>>m; - seg.assign(4*n+2,0); - ll arr[n]; - for(ll i=0;i>arr[i]; - - build(1,0,n-1,arr); - while(m--) - { - ll a,b,c; - cin>>a>>b>>c; - if(a==1) - { - update(1,0,n-1,b,c); - } - else - { - cout< diff --git a/Data Structure/Stack/parenthesisCheck.cpp b/Data Structure/Stack/parenthesisCheck.cpp deleted file mode 100644 index 5a5c5467..00000000 --- a/Data Structure/Stack/parenthesisCheck.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include - -struct stack{ - int size; - int top; - char *arr; -}; - -int isEmpty(struct stack *ptr){ - if(ptr->top == -1){ - return 1; - } - return 0; -} - -int isFull(struct stack *ptr){ - if(ptr->top == ptr->size-1){ - return 1; - } - return 0; -} - -void push(struct stack *s,char value){ - if(isFull(s)){ - printf("\nStack Overflow! %d cannot be entered in stack\n",value); - } - else{ - s->top++; //increment the top and.. - s->arr[s->top] = value; - } -} - -char pop(struct stack *s){ - if(isEmpty(s)){ - printf("\nStack Underflow!\n"); - return -1; - } - else{ - char value = s->arr[s->top]; - s->top--; - return value; - } -} - -int match(char a, char b){ - if(a == '{' && b == '}'){ - return 1; - } - if(a == '(' && b == ')'){ - return 1; - } - if(a == '[' && b == ']'){ - return 1; - } - return 0; -} - -int parathesisCheck(char * exp){ - - //-Initialized the stack - struct stack *s; - s->size = 100; - s->top= -1; - s->arr = (char *) malloc (s->size * sizeof(char)); - char poppedElement; - - for (int i = 0; i < exp[i]!='\0'; i++) //till null character - { - if(exp[i] == '(' || exp[i] == '{' || exp[i] == '['){ - push(s,exp[i]); - } - else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){ - if(isEmpty(s)){ - return 0; //false - } - poppedElement = pop(s); - if(!match(poppedElement,exp[i])){ - return 0; - } - } - } - if(isEmpty(s)){ - return 1; - } - else{ - return 0; - } - -} - -int main() -{ - - char * exp = " (a+b) [c+d] "; - - if(parathesisCheck(exp)){ - printf("Parenthesis is matching : Balanced \n"); - } - else{ - printf("Parenthesis is not matching : Not Balanced \n"); - } - - return 0; -} diff --git a/Data Structure/Stack/postfix_expression_using_stack.cpp b/Data Structure/Stack/postfix_expression_using_stack.cpp deleted file mode 100644 index 5048aa44..00000000 --- a/Data Structure/Stack/postfix_expression_using_stack.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -using namespace std; -int val(char c,int a,int b) -{ - switch(c) - { - case '+':return (a+b); - break; - case '-' :return (a-b); - break; - case '*' :return (a*b); - break; - case '/' :return (a/b); - break; - default: return 0; - } - - -} -int main() -{ - stack st; - string s; - getline(cin,s); - for(int i=0;i -#include -#include -#include -struct stack -{ - int top; - unsigned capacity; - char* array; -}; - -struct stack* create(int capacity) -{ - struct stack* stack = (struct stack*)malloc(sizeof(struct stack)); - stack->capacity = capacity; - stack->top = -1; - stack->array = (char*)malloc(stack->capacity * sizeof(char)); - return stack; -} - - -int isfull(struct stack **stack) -{ - return (*stack)->top == (*stack)->capacity - 1; -} - -int isempty(struct stack **stack) -{ - return (*stack)->top == -1; -} - -void push(struct stack **stack, char item) -{ - if (isfull(&(*stack))) - { - return; - } - (*stack)->array[++(*stack)->top] = item; - printf("%c pushed to stack\n", item); -} - -char pop(struct stack **stack) -{ - if (isempty(&(*stack))) - { - return -144255114554; - } - return (*stack)->array[(*stack)->top--]; -} - -int main() -{ - struct stack* stack = create(100); - char n[100]; - int i,x; - printf("enter string\n"); - scanf("%s",n); - x = strlen(n); - for(i=0;i -#include -#include - -struct stack -{ - int top; - unsigned capacity; - int* array; -}; - -struct stack* create(int capacity) -{ - struct stack* stack = (struct stack*)malloc(sizeof(struct stack)); - stack->capacity = capacity; - stack->top = -1; - stack->array = (int*)malloc(stack->capacity * sizeof(int)); - return stack; -} - - -int isfull(struct stack **stack) -{ - return (*stack)->top == (*stack)->capacity - 1; -} - -int isempty(struct stack **stack) -{ - return (*stack)->top == -1; -} - -void push(struct stack **stack, int item) -{ - if (isfull(&(*stack))) - { - return; - } - (*stack)->array[++(*stack)->top] = item; - printf("%d pushed to stack\n", item); -} - -int pop(struct stack **stack) -{ - if (isempty(&(*stack))) - { - return -144255114554; - } - return (*stack)->array[(*stack)->top--]; -} - -int main() -{ - struct stack* stack = create(100); - int n; - int i,x; - printf("enter No. of elements\n"); - scanf("%d",&n); - printf("Enter elements \n"); - for(i=0;i -#include -#include - -struct StackNode { - int data; - struct StackNode* next; -}; - -struct StackNode* newnode(int data) -{ - struct StackNode* stackNode = (struct StackNode*)malloc(sizeof(struct StackNode)); - stackNode->data = data; - stackNode->next = NULL; - return stackNode; -} - -int isEmpty(struct StackNode **head) -{ - return !(*head); -} - -void push(struct StackNode **root, int data) -{ - struct StackNode* stackNode = newnode(data); - stackNode->next = *root; - *root = stackNode; - printf("%d pushed to stack\n", data); -} - -int pop(struct StackNode **head) -{ - if (isEmpty(&(*head))) - return -101154454155; - struct StackNode* temp = *head; - *head = (*head)->next; - int popped = temp->data; - free(temp); - - return popped; -} - -int main() -{ - struct StackNode* head = NULL; - int i,n,x; - printf("Enter no of elements \n"); - scanf("%d",&n); - printf("Enter elements\n"); - for(i=0;i -using namespace std; -int main() -{ - int t; - cout<<"Enter the number of testcases"; - cin>>t; - while(t--) - { - int n; - cout<<"Enter a no. "; - cin>>n; - int d = log(n)/log(10)+1; - int temp = n; - while(temp != 0) - { - - } - } -} diff --git a/Data Structure/binary.cpp b/Data Structure/binary.cpp deleted file mode 100644 index b8cda27a..00000000 --- a/Data Structure/binary.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -using namespace std; -int BinarySearch(int a[], int start, int end, int x) -{ - if (end >= start) { - int mid = start + (end - start) / 2; - - // If the element is present at the middle - // itself - if (a[mid] == x) - return mid; - - // If element is smaller than mid, then - // it can only be present in left subarray - if (a[mid] > x) - return BinarySearch(a, start, mid - 1, x); - - // Else the element can only be present - // in right subarray - return BinarySearch(a, mid + 1, end, x); - } - - // We reach here when element is not - // present in array - return -1; -} - -int main() -{ - int arr[] = { 2, 3, 4, 10, 40 },x; - cout<<"ENter a number for search"; - cin>>x; - int n = sizeof(arr) / sizeof(arr[0]); - int flag = BinarySearch(arr, 0, n - 1, x); - (flag == -1) ? cout << "Element is not present in array" - : cout << "Element is present at index " << flag; - return 0; -} diff --git a/Data Structure/flattenAMultilevelDoublyLinkedList.cpp b/Data Structure/flattenAMultilevelDoublyLinkedList.cpp deleted file mode 100644 index 5072fcae..00000000 --- a/Data Structure/flattenAMultilevelDoublyLinkedList.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* -// Definition for a Node. -class Node { -public: - int val; - Node* prev; - Node* next; - Node* child; -}; -*/ - -class Solution { - Node* flatten_rec(Node* head){ - Node *curr = head, *tail = head; - while(curr){ - Node *child = curr->child; - Node *next = curr->next; - if(child){ - Node* _tail = flatten_rec(child); - _tail->next = next; - if(next) next->prev = _tail; - - curr->next = child; - child->prev = curr; - - curr->child = nullptr; - - curr = tail; - - } - else - curr = next; - if(curr) tail = curr; - } - return tail; - } -public: - Node* flatten(Node* head) { - if(head) flatten_rec(head); - return head; - } -}; diff --git a/Data Structure/insert a node at nth pos.cpp b/Data Structure/insert a node at nth pos.cpp deleted file mode 100644 index 1027dd4f..00000000 --- a/Data Structure/insert a node at nth pos.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include -using namespace std; -struct Node{ - int data; - Node* next; -};Node* head; -void insert(int data,int n) -{ - Node* temp1 = new Node(); - temp1->data = data; - temp1->next = NULL; - if(n==1) - { - temp1->next = head; - head= temp1; - return; - } - Node* temp2 = head; - for(int i = 0;inext; - } - temp1->next = temp2->next; - temp2->next = temp1; -} -void insertend(int data) -{ - Node* temp = new Node(); - temp->data = data; - temp->next = NULL; - Node* temp2 = head; - while( temp2->next!= NULL) - { - temp2 = temp2->next; - }temp2->next = temp; -} -void Print() -{ - cout<<"The resulting list is : "; - Node* temp = head; - while(temp != NULL) - { - cout<data<<" "; - temp = temp->next; - } -} -void Delete(int n) -{ - Node* temp = head; - if(n == 1) - { - head = temp->next; - delete temp; - return; - } - for(int i = 0;inext; - } - Node* temp2 = new Node; - temp2 = temp->next; - temp->next = temp2->next; - delete temp2; -} -int main() -{ - int data,n; -/* cout<<"Enter the data and the position separated by space."; - cout<0) - { - cin>>data; - cin>>n; - insert(data,n); - Print(); - cout< -using namespace std; -void swap(int* a, int* b) -{ - int temp=*a; - *a=*b; - *b=temp; -} -int main() -{ - int a[] = {0,1,2,3,4,5,6,7}; - int x=4,start=0, end=4; - int len =sizeof(a)/sizeof(int); - do - { - for(int i=start;i=0); - cout<<"The reversed array \n"; - int len2=sizeof(a)/sizeof(int); - for(int i=0;i 0; --i) - tree[i] = tree[i<<1] + tree[i<<1 | 1]; -} - -// function to update a tree node -void upN(ll p, ll value) -{ - // set value at position p - tree[p+n] += value; - p = p+n; - - // move upward and update parents - for (ll i=p; i > 1; i >>= 1) - tree[i>>1] = tree[i] + tree[i^1]; -} - -ll query(ll l, ll r) -{ - ll res = 0; - - // loop to find the sum in the range - for (l += n, r += n; l < r; l >>= 1, r >>= 1) - { - if (l&1) - res += tree[l++]; - - if (r&1) - res += tree[--r]; - } - - return res; -} -void solsegt(){ - ll a[size];//fill array - n = sizeof(a)/sizeof(a[0]); // n is global - // build tree - build(a); // build tree - //if position-based - cout << query(5-1, 9)< -using namespace std; -struct node -{ - int info; - node* next; -}; -class stacktype -{ - node* top; - -public: - stacktype() - { - top = NULL; - } - void push(node*); - node* pop(); - bool isempty(); - void display(); - node* createnewnode(int); -}; -node* stacktype:: createnewnode(int add) -{ - node* ptr; - ptr = new node; - ptr->info = add; - ptr->next = NULL; - return ptr; -} -void stacktype:: push(node* nptr) -{ - if (top == NULL) - top = nptr; - else - { - nptr->next = top; - top = nptr; - } -} -node* stacktype:: pop() -{ - node* temp; - temp = top; - top = top->next; - return temp; -} -bool stacktype::isempty() -{ - if (top == NULL) - return 1; - else - return 0; -} -void stacktype::display() -{ - if (isempty()) - cout << "\nCannot display.The stack is empty."; - else - { - cout << "\nContents of the stack: "; - node* temp; - for (temp = top;temp != NULL;temp = temp->next) - cout << temp->info << " "; - } -} -int main() -{ - stacktype s; - int add, del, e, f, choice; - char ch = 'y'; - node* nptr; - while (ch == 'y' || ch == 'Y') - { - cout << "Menu:"; - cout << "\n1.Push."; - cout << "\n2.Pop."; - cout << "\n3.Isempty?"; - cout << "\n4.Display."; - cout << "\nEnter your choice(1-4):"; - cin >> choice; - switch (choice) - { - case 1: - cout << "Enter an integer to be added:"; - cin >> add; - nptr = s.createnewnode(add); - s.push(nptr); - s.display(); - break; - case 2: - e = s.isempty(); - if (e == 1) - cout << "Cannot pop.Underflow."; - else - { - nptr = s.pop(); - cout << nptr->info << " has been deleted."; - delete nptr; - s.display(); - } - break; - case 3: - e = s.isempty(); - if (e == 1) - cout << "Stack is empty."; - else - cout << "Stack is not empty."; - break; - case 4: - s.display(); - break; - default: - cout << "Wrong input.Enter correct choice."; - } - cout << "\nDo you want to continue?(Y/N) "; - cin >> ch; - } -} -