├── Graph ├── Bypertite.cpp ├── Basic(using adjency matrix).cpp ├── connectedcomponents.cpp ├── dwadl.cpp ├── connectedcomponents1.cpp ├── dijkstra.cpp ├── dijkstra1.cpp └── Dijikstras.cpp ├── Quick Revision └── Linkedlist.cpp ├── Pratice ├── Stack.exe ├── Array │ ├── nextLargest.exe │ └── nextLargest.cpp ├── Stack │ ├── Stack-bracket.exe │ ├── Stack-with-linkedList.exe │ ├── Stack-bracket.cpp │ └── Stack-with-linkedList.cpp ├── LinkedList │ ├── loop-finder.exe │ ├── is-palindrome.exe │ ├── deleting_middle.exe │ ├── reversingLinks.exe │ ├── deleting-duplicate.exe │ ├── nth-node-from-end.exe │ ├── count.cpp │ ├── sum-in-linkedList.cpp │ ├── search.cpp │ ├── linkedList.cpp │ ├── is-sorted.cpp │ ├── max_element.cpp │ ├── min_element.cpp │ ├── is-palindrome.cpp │ ├── middle_element.cpp │ ├── loop-finder.cpp │ ├── reversingLinks.cpp │ ├── nth-node-from-end.cpp │ ├── deleting_middle.cpp │ ├── linkedList-deletation.cpp │ ├── deleting-duplicate.cpp │ ├── insertation-in-sorted-ll.cpp │ ├── linkedList-insertion-first.cpp │ └── linkedList-insertation-at-specific-node.cpp ├── Queue │ ├── Queue- using LL.exe │ └── Queue- using LL.cpp └── Stack.cpp ├── Vector ├── Topic Vectors.pdf └── vector_tutorial.cpp ├── README.md ├── Recursion ├── Sum_of_Number.cpp ├── treeRecursion.cpp ├── Power_Recursion.cpp ├── Factorail_using_recursion.cpp ├── NestedRecursion.cpp ├── IndirectRecursion.cpp ├── Global Variabels in Recursion.cpp ├── Taylor_series.cpp └── Static Variabels in Recursion.cpp ├── CP-CA3 ├── printing-divisor2.cpp ├── gcd=eculidian.cpp ├── prinitng divisors.cpp ├── count_divisors.cpp ├── printing-divisor3.cpp └── extended_eculidian_alog.cpp ├── Stack ├── Stack-using STL.cpp ├── Stack-using Array.cpp ├── Stack-using VECTOR.cpp └── stack_ll.cpp ├── Sorting ├── Bubble Sort.cpp ├── Selection Sort.cpp ├── Insertion Sort.cpp ├── Heap Sort.cpp └── Merge Sort.cpp ├── Heap ├── max element in min heap.cpp ├── min element in max heap.cpp ├── Prioirity Queue: Max Heap.cpp ├── Basic_STL.cpp ├── Priority Queue : Min Heap.cpp ├── minHeap to max heap.cpp ├── Kth element of the heap.cpp └── K Least element in min heap.cpp ├── fractional_knapscack.cpp ├── Trees ├── basic.cpp ├── tree size.cpp ├── Max in tree.cpp ├── Is given tree a BST.cpp ├── tree Identical.cpp ├── deleting a tree.cpp ├── Iterative Inorder Traversal.cpp ├── Level Order.cpp ├── isBST.cpp ├── Print BST keys in given range.cpp ├── Iterative Insertion in BST.cpp ├── depthheight.cpp ├── Tree with traversals.cpp ├── LCA of Binary Tree.cpp ├── kth Smallest element.cpp ├── Children Sum Property.cpp ├── BST.cpp ├── levelOrder.cpp ├── LCA set 2.cpp ├── BST │ └── Basic and Bsearch.cpp ├── Check if leaves are same level.cpp ├── Leaves at same level.cpp ├── Width of tree.cpp ├── Perfect Binary Tree.cpp ├── Binary trees are same.cpp ├── LCA set 1.cpp ├── Pre-post-in order traversal.cpp ├── Create a in level order tree.cpp ├── Deletion by rightmost.cpp ├── Level order Traversal.cpp ├── Children Sum Recursive.cpp ├── tree-traversals.cpp ├── Trees are siblings.cpp ├── spiral.cpp └── AVL tree insertion.cpp ├── Mathematical Algo ├── LCM.cpp ├── Factorization.cpp ├── Euler's_Totient_function.cpp ├── Sieve_of_Eratosthenes.cpp ├── GCD_by_Euclidean_algorithm.cpp ├── Pigeonhole_Algorithm.cpp └── Matrix_Exponentiation.cpp ├── LinkedList ├── Dispaly_Creation.cpp ├── sum_of_linkedlist.cpp ├── count_in_ll.cpp ├── max_in_LL.cpp ├── min_in_LL.cpp ├── search_in_ll.cpp ├── inserting at last.cpp ├── insertation_in_front.cpp ├── linkedlist_rev.cpp ├── to check if linkedlist is sorted or not.cpp ├── reversing_links.cpp ├── insert_after_position.cpp ├── deleting_duplicate.cpp ├── insertion_in_ll.cpp ├── deletion_in_ll.cpp ├── insertion in sorted LL.cpp └── reversing links.cpp ├── Searching ├── linear_search.cpp └── binary_search.cpp └── n queen problem.cpp /Graph/Bypertite.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Quick Revision/Linkedlist.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Pratice/Stack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/Stack.exe -------------------------------------------------------------------------------- /Vector/Topic Vectors.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Vector/Topic Vectors.pdf -------------------------------------------------------------------------------- /Pratice/Array/nextLargest.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/Array/nextLargest.exe -------------------------------------------------------------------------------- /Pratice/Stack/Stack-bracket.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/Stack/Stack-bracket.exe -------------------------------------------------------------------------------- /Pratice/LinkedList/loop-finder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/LinkedList/loop-finder.exe -------------------------------------------------------------------------------- /Pratice/Queue/Queue- using LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/Queue/Queue- using LL.exe -------------------------------------------------------------------------------- /Pratice/LinkedList/is-palindrome.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/LinkedList/is-palindrome.exe -------------------------------------------------------------------------------- /Pratice/LinkedList/deleting_middle.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/LinkedList/deleting_middle.exe -------------------------------------------------------------------------------- /Pratice/LinkedList/reversingLinks.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/LinkedList/reversingLinks.exe -------------------------------------------------------------------------------- /Pratice/Stack/Stack-with-linkedList.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/Stack/Stack-with-linkedList.exe -------------------------------------------------------------------------------- /Pratice/LinkedList/deleting-duplicate.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/LinkedList/deleting-duplicate.exe -------------------------------------------------------------------------------- /Pratice/LinkedList/nth-node-from-end.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smilegupta/Mastering_DSA/HEAD/Pratice/LinkedList/nth-node-from-end.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mastering_DSA 2 | This repo will consists of the basic programs and the concepts a programmer need to know in order to master DSA 3 | -------------------------------------------------------------------------------- /Recursion/Sum_of_Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | if(n==0) 6 | return 0; 7 | else 8 | return fun(n-1)+n; 9 | } 10 | int main() 11 | {int a=fun(5); 12 | cout< 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | if(n>0) 6 | { 7 | cout< 2 | using namespace std; 3 | int fun(int m,int n) 4 | { 5 | if(n==0) 6 | return 1; 7 | else 8 | return fun(m,n-1)*m; 9 | } 10 | int main() 11 | {int a=fun(2,9); 12 | cout< 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | if(n==0 || n==1) 6 | return 1; 7 | else 8 | return fun(n-1)*n; 9 | } 10 | int main() 11 | {int a=fun(5); 12 | cout< 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | if(n>100) 6 | return n-10; 7 | else 8 | return fun(fun(n+11)); 9 | } 10 | int main() 11 | { 12 | int a=fun(95); 13 | cout< 2 | using namespace std; 3 | void funB(int n); 4 | void funA(int n) 5 | { 6 | if(n>0) 7 | { 8 | cout<1) 15 | { 16 | cout< 2 | int x=0; 3 | int fun(int n) 4 | { 5 | if(n>0) 6 | { 7 | x++; 8 | return fun(n-1)+x; 9 | } 10 | return 0; 11 | } 12 | 13 | int main() 14 | { int r; 15 | r=fun(5); 16 | printf("%d\n",r); 17 | r=fun(5); 18 | printf("%d\n",r); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Recursion/Taylor_series.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double e(int x, int n) 5 | { static double p=1,f=1; 6 | double r; 7 | if(n==0) 8 | return 1; 9 | else 10 | { 11 | r=e(x,n-1); 12 | p=p*x; 13 | f=f*n; 14 | return r+p/f; 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | printf("%lf \n",e(3,10)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Recursion/Static Variabels in Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int fun(int n) 3 | { 4 | static int x=0; 5 | if(n>0) 6 | { 7 | x++; 8 | return fun(n-1)+x; 9 | } 10 | return 0; 11 | } 12 | 13 | int main() 14 | { int r; 15 | r=fun(5); 16 | printf("%d\n",r); 17 | r=fun(5); 18 | printf("%d\n",r); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Pratice/Array/nextLargest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int arr[] = {11,13,21,3}; 5 | int size = sizeof(arr)/sizeof(arr[0]); 6 | for(int i=0; i 2 | 3 | // function to print the divisors 4 | void printDivisors(int n) 5 | { 6 | for (int i=1;i<=n;i++) 7 | if (n%i==0) 8 | printf("%d ",i); 9 | } 10 | 11 | /* Driver program to test above function */ 12 | int main() 13 | { 14 | printf("The divisors of 100 are: \n"); 15 | printDivisors(100); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Stack/Stack-using STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void showStack(stack s) 4 | { 5 | while(!s.empty()) 6 | { 7 | cout< s; 14 | s.push(10); 15 | s.push(12); 16 | s.push(16); 17 | s.push(18); 18 | s.push(20); 19 | showStack(s); 20 | s.pop(); 21 | cout< 2 | #include< conio.h > 3 | using namespace std; 4 | int main() 5 | { 6 | int a[50],n,i,j,temp; 7 | cout<<"Enter the size of array: "; 8 | cin>>n; 9 | cout<<"Enter the array elements: "; 10 | 11 | for(i=0;i< n;++i) 12 | cin>>a[i]; 13 | 14 | for(i=0;i< n-1;++i) 15 | for(j=0;j<(n-i);++j) 16 | if(a[j]>a[j+1]) 17 | { 18 | temp=a[j]; 19 | a[j]=a[j+1]; 20 | a[j+1]=temp; 21 | } 22 | cout<<"Array after bubble sort: "; 23 | for(i=0;i< n;++i) 24 | cout<< a[i]<< " "; 25 | getch(); 26 | } 27 | -------------------------------------------------------------------------------- /Graph/Basic(using adjency matrix).cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | void addedge(int G[4][4], int u, int v) 6 | { 7 | G[u][v]=G[v][u]=1; 8 | } 9 | void dfs(int G[4][4], int s, bool visited[]){ 10 | visited[s]=true; 11 | cout< 2 | using namespace std; 3 | 4 | 5 | int findMaximumElement(int heap[], int n) 6 | { 7 | int maximumElement = heap[0]; 8 | 9 | for (int i = 1; i < n; ++i) 10 | maximumElement = max(maximumElement, heap[i]); 11 | 12 | return maximumElement; 13 | } 14 | 15 | int main() 16 | { 17 | // Number of nodes 18 | int n = 10; 19 | 20 | int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 }; 21 | 22 | cout << findMaximumElement(heap, n); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Heap/min element in max heap.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of above approach 2 | #include 3 | using namespace std; 4 | 5 | int findMinimumElement(int heap[], int n) 6 | { 7 | int minimumElement = heap[0]; 8 | 9 | for (int i = 1; i < n; ++i) 10 | minimumElement = min(minimumElement, heap[i]); 11 | 12 | return minimumElement; 13 | } 14 | 15 | int main() 16 | { 17 | // Number of nodes 18 | int n = 10; 19 | 20 | int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 }; 21 | 22 | cout << findMinimumElement(heap, n); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Graph/connectedcomponents.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void addedge(int G[6][6], int u, int v){ 5 | G[u][v] = G[v][u] = 1; 6 | } 7 | 8 | void dfs(int G[6][6], int s, bool visited[6]){ 9 | visited[s] = true; 10 | for(int i = 0; i < 6; i++) 11 | if(G[s][i] && !visited[i]) 12 | dfs(G, i, visited); 13 | } 14 | 15 | int main(){ 16 | int G[6][6] = {}, cnt = 0; 17 | bool visited[6] = {}; 18 | addedge(G, 0, 1); 19 | addedge(G, 0, 2); 20 | addedge(G, 3, 4); 21 | for(int i = 0; i < 6; i++) 22 | if(!visited[i]){ 23 | dfs(G, i, visited); 24 | cnt++; 25 | } 26 | cout< 2 | using namespace std; 3 | struct item{ 4 | int value, weight; 5 | }; 6 | bool cmp(item i, item j) 7 | { 8 | return (double)i.value/i.weight>(double)j.value/j.weight; 9 | } 10 | int main() 11 | { 12 | item a[]={{100,30},{70,20}, {40,40}}; 13 | int n=3; 14 | sort(a,a+3,cmp); 15 | int capacity=60, cw=0; 16 | double val=0; 17 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void addedge(list > *al, int u, int v, int w){ 6 | al[u].push_back(make_pair(v, w)); 7 | } 8 | 9 | void printGraph(list > *al){ 10 | for(int i = 0; i < 4; i++) 11 | for(list > :: iterator it = al[i].begin(); it != al[i].end(); it++) 12 | cout<first<<" "<second< > *al = new list > [4]; 17 | addedge(al, 0, 1, 4); 18 | addedge(al, 0, 3, 5); 19 | addedge(al, 1, 2, 3); 20 | addedge(al, 1, 3, 2); 21 | addedge(al, 2, 3, 6); 22 | printGraph(al); 23 | } -------------------------------------------------------------------------------- /Trees/basic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | 9 | struct node* newNode(int data) 10 | { 11 | struct node *temp = new node(); 12 | temp->data=data; 13 | temp->left=NULL; 14 | temp->right=NULL; 15 | 16 | return(temp); 17 | } 18 | void print_node(node* temp) 19 | { 20 | cout<data; 21 | } 22 | 23 | int main() 24 | { 25 | struct node *root=newNode(1); 26 | root->left=newNode(2); 27 | root->right=newNode(3); 28 | root->left->right=newNode(5); 29 | root->left->left=newNode(4); 30 | 31 | print_node(root->left->left); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Trees/tree size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | int treesize(node *root){ 17 | if(root == NULL) 18 | return 0; 19 | return 1 + treesize(root->left) + treesize(root->right); 20 | } 21 | 22 | int main() { 23 | // your code goes here 24 | node *root = add(1); 25 | root->left = add(2); 26 | root->right = add(3); 27 | root->left->left = add(4); 28 | root->left->right = add(5); 29 | cout< 2 | using namespace std; 3 | int Stack[100], size, top, x, i; 4 | 5 | void push(int x){ 6 | if(top>=size) 7 | cout<<"Stack is full"; 8 | else{ 9 | top++; 10 | Stack[top] = x; 11 | } 12 | } 13 | 14 | void pop(){ 15 | if(top <= -1){ 16 | cout<<"Stack under Flow"; 17 | } else{ 18 | top--; 19 | } 20 | } 21 | 22 | void display(){ 23 | if(top>=0){ 24 | for(i=top; i>0; i--) 25 | cout< 5 | using namespace std; 6 | int gcd(int number1,int number2) 7 | { 8 | if(number2==0) 9 | return number1; 10 | return gcd(number2,number1%number2); 11 | } 12 | int LCM(int number1,int number2) 13 | { 14 | return (number1*number2)/gcd(number1,number2); 15 | } 16 | 17 | //Driver Code 18 | int main() 19 | { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(); cout.tie(NULL); 22 | int number1,number2; 23 | cout<<"Enter the both number's:"<>number1>>number2; 25 | cout< 2 | using namespace std; 3 | 4 | int gcd(int a, int b) 5 | { 6 | if (a == 0) 7 | return b; 8 | return gcd(b % a, a); 9 | } 10 | 11 | 12 | int main() 13 | { 14 | int a = 10, b = 15; 15 | cout << "GCD(" << a << ", " 16 | << b << ") = " << gcd(a, b) 17 | << endl; 18 | a = 35, b = 10; 19 | cout << "GCD(" << a << ", " 20 | << b << ") = " << gcd(a, b) 21 | << endl; 22 | a = 31, b = 2; 23 | cout << "GCD(" << a << ", " 24 | << b << ") = " << gcd(a, b) 25 | << endl; 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Mathematical Algo/Factorization.cpp: -------------------------------------------------------------------------------- 1 | //Given an number n, write an efficient function to print all prime factor of n 2 | 3 | //C++ program for FACTORIZATION 4 | #include 5 | using namespace std; 6 | void primeFactor(int number) 7 | { 8 | while(number%2==0) 9 | { 10 | number=number/2; 11 | cout<<"2"<<" "; 12 | } 13 | for(int i=3;i<=sqrt(number);i=i+2) 14 | { 15 | while(number%i==0) 16 | { 17 | cout<2) 22 | cout<>number; 31 | primeFactor(number); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CP-CA3/prinitng divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to print the divisors 4 | void printDivisors(int n) 5 | { 6 | // Note that this loop runs till square root 7 | for (int i=1; i<=sqrt(n); i++) 8 | { 9 | if (n%i == 0) 10 | { 11 | // If divisors are equal, print only one 12 | if (n/i == i) 13 | printf("%d ", i); 14 | 15 | else // Otherwise print both 16 | printf("%d %d ", i, n/i); 17 | } 18 | } 19 | } 20 | 21 | /* Driver program to test above function */ 22 | int main() 23 | { 24 | printf("The divisors of 100 are: \n"); 25 | printDivisors(100); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Graph/connectedcomponents1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void addedge(list *al, int u, int v){ 6 | al[u].push_back(v); 7 | al[v].push_back(u); 8 | } 9 | 10 | void dfs(list *al, int s, bool visited[]){ 11 | visited[s] = true; 12 | for(list :: iterator it = al[s].begin(); it != al[s].end(); it++) 13 | if(visited[*it] == false) 14 | dfs(al, *it, visited); 15 | } 16 | 17 | int main(){ 18 | list *al = new list[6]; 19 | bool visited[6] = {}; 20 | int cnt = 0; 21 | addedge(al, 0, 1); 22 | addedge(al, 0, 2); 23 | addedge(al, 3, 4); 24 | for(int i = 0; i < 6; i++) 25 | if(visited[i] == false){ 26 | dfs(al, i, visited); 27 | cnt++; 28 | } 29 | cout< 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | 9 | }*first=NULL; 10 | 11 | void create(int A[], int n){ 12 | int i; 13 | struct Node *t,*last; 14 | first=new Node; 15 | first->data=A[0]; 16 | first->next=NULL; 17 | last=first; 18 | 19 | for(i=1;idata=A[i]; 23 | t->next=NULL; 24 | last->next=t; 25 | last=t; 26 | } 27 | 28 | } 29 | 30 | void display(struct Node *p) 31 | { 32 | while(p!=NULL) 33 | { 34 | cout<data<<" "; 35 | p=p->next; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | int A[]={3,5,7,10,15}; 42 | int n=sizeof(A)/sizeof(A[0]); 43 | create(A,n); 44 | display(first); 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /CP-CA3/count_divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // function to count the divisors 5 | int countDivisors(int n) 6 | { 7 | int cnt = 0; 8 | for (int i = 1; i <= sqrt(n); i++) { 9 | if (n % i == 0) { 10 | // If divisors are equal, 11 | // count only one 12 | if (n / i == i) 13 | cnt++; 14 | 15 | else // Otherwise count both 16 | cnt = cnt + 2; 17 | } 18 | } 19 | return cnt; 20 | } 21 | 22 | /* Driver program to test above function */ 23 | int main() 24 | { 25 | printf("Total distinct divisors of 100 are : %d", 26 | countDivisors(100)); 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /LinkedList/sum_of_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void sum(struct Node *p) 25 | { 26 | int sum=0; 27 | while(p!=NULL) 28 | { 29 | sum=p->data+sum; 30 | p=p->next; 31 | } 32 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | 23 | } 24 | void count(struct Node *P) 25 | { 26 | int count=0; 27 | while(P!=NULL) 28 | { 29 | count++; 30 | P=P->next; 31 | } 32 | 33 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | 23 | } 24 | void count(struct Node *P) 25 | { 26 | int count=0; 27 | while(P!=NULL) 28 | { 29 | count++; 30 | P=P->next; 31 | } 32 | 33 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void sum(struct Node *p) 25 | { 26 | int sum=0; 27 | while(p!=NULL) 28 | { 29 | sum=p->data+sum; 30 | p=p->next; 31 | } 32 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | void max(struct Node *p) 24 | { 25 | int Max=INT_MIN; 26 | while(p!=NULL) 27 | { 28 | if(Maxdata) 29 | { 30 | Max=p->data; 31 | } 32 | p=p->next; 33 | } 34 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | void min(struct Node *p) 24 | { 25 | int Min=INT_MAX; 26 | while(p!=NULL) 27 | { 28 | if(Min>p->data) 29 | { 30 | Min=p->data; 31 | } 32 | p=p->next; 33 | } 34 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | int search(struct Node *p, int value){ 25 | while(p != NULL){ 26 | if(p->data == value) 27 | return 1; 28 | 29 | p=p->next; 30 | } 31 | return 0; 32 | } 33 | 34 | 35 | int main(){ 36 | int A[]={1,2,3,4,5}; 37 | int n=sizeof(A)/sizeof(A[0]); 38 | create(A,n); 39 | cout< 2 | using namespace std; 3 | int Stack[100], size, top, x,i; 4 | void push(int x) 5 | { 6 | if(top>=size) 7 | cout<<"Can't insert stack is over Flow"<<"\n"; 8 | else 9 | { 10 | top++; 11 | Stack[top]=x; 12 | } 13 | } 14 | void pop() 15 | { 16 | if(top<=-1) 17 | cout<<"Stack Under Flow"<<"\n"; 18 | else top--; 19 | } 20 | void display() 21 | { 22 | if(top>=0) 23 | { 24 | for(i=top; i>0; i--) 25 | cout< 6 | using namespace std; 7 | int gcd(int i,int number) 8 | { 9 | if(number==0) 10 | return i; 11 | return gcd(number,i%number); 12 | } 13 | int fun(int number) 14 | { 15 | int result=0; 16 | for(int i=1;i>number; 32 | cout< 5 | using namespace std; 6 | struct Node { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[], int n){ 12 | int i; 13 | struct Node *t, *last; 14 | first = new Node; 15 | first -> data = A[0]; 16 | last = first; 17 | 18 | for(i=1; i data = A[i]; 21 | t->next = NULL; 22 | last->next = t; 23 | last = t; 24 | } 25 | } 26 | 27 | void display(struct Node *p){ 28 | while(p!=NULL){ 29 | cout<data<<" "; 30 | p=p->next; 31 | } 32 | } 33 | 34 | int main(){ 35 | int A[]={1,2,3,4,5}; 36 | int n = sizeof(A)/sizeof(A[0]); 37 | create(A,n); 38 | display(first); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Sorting/Selection Sort.cpp: -------------------------------------------------------------------------------- 1 | #include< iostream> 2 | #include< conio.h> 3 | using namespace std; 4 | int main() 5 | { 6 | 7 | int n,i,loc,j,temp,a[20]; 8 | 9 | cout<<"Enter total elements: "; 10 | cin>>n; 11 | 12 | cout<<"Enter the elements: "; 13 | for(i=0;i< n;i++) 14 | cin>>a[i]; 15 | 16 | for(i=0;i< n-1;i++)//represent n-1 passes 17 | { loc=i;//intialize loc 18 | /*finding location of smallest element of unsorted subarray*/ 19 | for(j=i+1;j< n;j++) 20 | { 21 | if(a[j]< a[loc])//if smallest element found 22 | loc=j;//update loc 23 | } 24 | temp=a[i]; 25 | a[i]=a[loc]; 26 | a[loc]=temp; 27 | } 28 | cout<<"After sorting is: "; 29 | for(i=0;i< n;i++) 30 | cout<< a[i]<< endl; 31 | getch(); 32 | } 33 | -------------------------------------------------------------------------------- /Trees/Max in tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | void maxintree(node *root, int &MAX){ 17 | if(root != NULL){ 18 | maxintree(root->left, MAX); 19 | if(root->data > MAX) 20 | MAX = root->data; 21 | maxintree(root->right, MAX); 22 | } 23 | } 24 | 25 | int main() { 26 | // your code goes here 27 | node *root = add(1); 28 | root->left = add(2); 29 | root->right = add(3); 30 | root->left->left = add(4); 31 | root->left->right = add(5); 32 | int MAX = INT_MIN; 33 | maxintree(root, MAX); 34 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void search(struct Node *p, int n) 25 | { 26 | int found=0; 27 | while(p!=NULL) 28 | { 29 | if(n==p->data) 30 | found++; 31 | 32 | p=p->next; 33 | } 34 | if(found==1) 35 | cout<<"Element found"; 36 | else 37 | cout<<"Element not found"; 38 | 39 | } 40 | 41 | int main(){ 42 | int A[]={1,2,3,4,5}; 43 | int n=sizeof(A)/sizeof(A[0]); 44 | create(A,n); 45 | search(first,1); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Trees/Is given tree a BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | int pre; 10 | 11 | node* add(int data){ 12 | node *newnode = new node; 13 | newnode->data = data; 14 | newnode->left = newnode->right = NULL; 15 | return newnode; 16 | } 17 | 18 | void isbst(node *root, bool &bst){ 19 | if(root == NULL) 20 | return; 21 | isbst(root->left, bst); 22 | if(pre > root->data) 23 | bst = false; 24 | pre = root->data; 25 | isbst(root->right, bst); 26 | } 27 | 28 | int main() { 29 | // your code goes here 30 | node *root = add(3); 31 | root->left = add(2); 32 | root->right = add(5); 33 | root->left->left = add(1); 34 | root->left->right = add(4); 35 | bool bst = true; 36 | isbst(root, bst); 37 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void showpq(priority_queue gq) 7 | { 8 | priority_queue g = gq; 9 | while (!g.empty()) 10 | { 11 | cout << '\t' << g.top(); 12 | g.pop(); 13 | } 14 | cout << '\n'; 15 | } 16 | 17 | int main () 18 | { 19 | priority_queue gquiz; 20 | gquiz.push(10); 21 | gquiz.push(30); 22 | gquiz.push(20); 23 | gquiz.push(5); 24 | gquiz.push(1); 25 | 26 | cout << "The priority queue gquiz is : "; 27 | showpq(gquiz); 28 | 29 | cout << "\ngquiz.size() : " << gquiz.size(); 30 | cout << "\ngquiz.top() : " << gquiz.top(); 31 | 32 | 33 | cout << "\ngquiz.pop() : "; 34 | gquiz.pop(); 35 | showpq(gquiz); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Pratice/LinkedList/is-sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n){ 10 | int i; 11 | struct Node *t, *last; 12 | first = new Node; 13 | first -> data = A[0]; 14 | last = first; 15 | 16 | for(i=1; i data = A[i]; 19 | t->next = NULL; 20 | last->next = t; 21 | last = t; 22 | } 23 | } 24 | 25 | int elementsSorted(struct Node *p){ 26 | int x = INT_MIN; 27 | while(p!=NULL){ 28 | if(p->datadata; 31 | p=p->next; 32 | } 33 | return 1; 34 | 35 | } 36 | 37 | 38 | int main(){ 39 | int A[]={1,2,3,4,5,6,7,1}; 40 | int n = sizeof(A)/sizeof(A[0]); 41 | create(A,n); 42 | cout< 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n){ 10 | int i; 11 | struct Node *t, *last; 12 | first = new Node; 13 | first -> data = A[0]; 14 | last = first; 15 | 16 | for(i=1; i data = A[i]; 19 | t->next = NULL; 20 | last->next = t; 21 | last = t; 22 | } 23 | } 24 | 25 | void maxElementFinder(struct Node *p){ 26 | int ans = INT_MIN; 27 | while(p!=NULL){ 28 | if(p->data > ans){ 29 | ans = p->data; 30 | } 31 | p=p->next; 32 | } 33 | cout< 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n){ 10 | int i; 11 | struct Node *t, *last; 12 | first = new Node; 13 | first -> data = A[0]; 14 | last = first; 15 | 16 | for(i=1; i data = A[i]; 19 | t->next = NULL; 20 | last->next = t; 21 | last = t; 22 | } 23 | } 24 | 25 | void minElementFinder(struct Node *p){ 26 | int ans = INT_MAX; 27 | while(p!=NULL){ 28 | if(p->data < ans){ 29 | ans = p->data; 30 | } 31 | p=p->next; 32 | } 33 | cout< 2 | #include< conio.h > 3 | using namespace std; 4 | int main(){ 5 | 6 | int i,j,s,temp,a[20]; 7 | 8 | cout<<"Enter total elements: "; 9 | cin>>s; 10 | 11 | cout<<"Enter the elements: "; 12 | for(i=0;i< s;i++) 13 | cin>>a[i]; 14 | 15 | for(i=1;i< s;i++){ 16 | temp=a[i];//extracting element to be placed at proper location 17 | j=i-1;//intialize location to place the target 18 | //search the location where target is to be inserted// 19 | while((temp< a[j])&&(j>=0)) 20 | { 21 | a[j+1]=a[j];//shift element(s) right to make room for insertion 22 | j=j-1;//move left in sorted subarray 23 | } 24 | 25 | a[j+1]=temp;//location found, insert target into array 26 | } 27 | 28 | cout<<"After sorting: "; 29 | for(i=0;i< s;i++) 30 | cout<< a[i]<< endl; 31 | getch(); 32 | } 33 | -------------------------------------------------------------------------------- /Heap/Basic_STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | vector v={10,20,30,40,50}; 5 | make_heap(v.begin(),v.end()); //To make heap - by default it will make a max heap 6 | v.push_back(60); //Adding element to heap 7 | push_heap(v.begin(), v.end()); //Saving the added element to heap 8 | cout< 2 | using namespace std; 3 | void printTop(vector v) 4 | { 5 | int size=v.size(); 6 | cout< push_stack(vector v, int n){ 9 | v.push_back(n); 10 | return v; 11 | } 12 | vector pop_stack(vector v) 13 | { 14 | v.pop_back(); 15 | return v; 16 | } 17 | void printing_stack(vector v) 18 | { 19 | for(int i=v.size()-1;i>=0; i--) 20 | { 21 | cout< v; 27 | v=push_stack(v,10); 28 | v=push_stack(v,20); 29 | v=push_stack(v,30); 30 | v=push_stack(v,40); 31 | //printTop(v); 32 | v=pop_stack(v); 33 | //cout< 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | struct node* newNode(int data) 9 | { 10 | struct node *temp=new node(); 11 | temp->data=data; 12 | temp->left=NULL; 13 | temp->right=NULL; 14 | } 15 | int isIdentical(struct node* a, struct node* b) 16 | { 17 | if(a==NULL&& b==NULL) 18 | return 1; 19 | if(a!=NULL&&b!=NULL) 20 | { 21 | return (a->data==b->data && isIdentical(a->left, b->left) && isIdentical(a->right, b->right)); 22 | } 23 | else 24 | return 0; 25 | } 26 | int main(){ 27 | struct node *a=newNode(1); 28 | a->left=newNode(2); 29 | a->right=newNode(3); 30 | struct node *b=newNode(1); 31 | b->left=newNode(2); 32 | b->right=newNode(3); 33 | // struct node *c=NULL; 34 | isIdentical(a,b)==1?cout<<"Tree are identical":cout<<"Tree are not identical"; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /LinkedList/inserting at last.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL, *last=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void insert(int val) 25 | { 26 | struct Node *t =new Node; 27 | t->data=val; 28 | t->next=NULL; 29 | last->next=t; 30 | last=t; 31 | 32 | } 33 | void Display(struct Node *p) 34 | { 35 | while(p!=NULL) 36 | { 37 | cout<data<<" "; 38 | p=p->next; 39 | } 40 | } 41 | int main(){ 42 | int A[]={10,20,30,40,50}; 43 | int n=sizeof(A)/sizeof(A[0]); 44 | create(A,n); 45 | insert(0); 46 | Display(first); 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Heap/Priority Queue : Min Heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void showpq(priority_queue , greater> gq) 7 | { 8 | priority_queue , greater> g = gq; 9 | while (!g.empty()) 10 | { 11 | cout << '\t' << g.top(); 12 | g.pop(); 13 | } 14 | cout << '\n'; 15 | } 16 | 17 | int main () 18 | { 19 | priority_queue , greater> gquiz; 20 | gquiz.push(10); 21 | gquiz.push(30); 22 | gquiz.push(20); 23 | gquiz.push(5); 24 | gquiz.push(1); 25 | 26 | cout << "The priority queue gquiz is : "; 27 | showpq(gquiz); 28 | 29 | cout << "\ngquiz.size() : " << gquiz.size(); 30 | cout << "\ngquiz.top() : " << gquiz.top(); 31 | 32 | 33 | cout << "\ngquiz.pop() : "; 34 | gquiz.pop(); 35 | showpq(gquiz); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /LinkedList/insertation_in_front.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void insert(struct Node *p, int val) 25 | { 26 | struct Node *t =new Node; 27 | t->data=val; 28 | t->next=first; 29 | first=t; 30 | 31 | } 32 | void Display(struct Node *p) 33 | { 34 | while(p!=NULL) 35 | { 36 | cout<data<<" "; 37 | p=p->next; 38 | } 39 | } 40 | int main(){ 41 | int A[]={10,20,30,40,50}; 42 | int n=sizeof(A)/sizeof(A[0]); 43 | create(A,n); 44 | insert(first,0); 45 | Display(first); 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Trees/deleting a tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | struct node* newNode(int data) 9 | { 10 | struct node *temp=new node(); 11 | temp->data=data; 12 | temp->left=NULL; 13 | temp->right=NULL; 14 | return temp; 15 | } 16 | void inorder(struct node* node){ 17 | if(node==NULL) 18 | return; 19 | inorder(node->left); 20 | inorder(node->right); 21 | cout<data<<" "; 22 | } 23 | void deleteTree(struct node* node){ 24 | if(node==NULL) return; 25 | deleteTree(node->left); 26 | deleteTree(node->right); 27 | cout<<"Element deleting: "<data<<" "<left=newNode(2); 34 | root->right=newNode(3); 35 | inorder(root); 36 | cout< 2 | using namespace std; 3 | int areParanthesisBalanced(string expr){ 4 | stack s; 5 | char x; 6 | for(int i = 0; i 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | void inorder(node *root){ 17 | stack s; 18 | node *curr = root; 19 | while(curr != NULL or !s.empty()){ 20 | while(curr != NULL){ 21 | s.push(curr); 22 | curr = curr->left; 23 | } 24 | curr = s.top(); 25 | s.pop(); 26 | cout<data<<" "; 27 | curr = curr->right; 28 | } 29 | } 30 | 31 | int main() { 32 | // your code goes here 33 | node *root = add(1); 34 | root->left = add(2); 35 | root->right = add(3); 36 | root->left->left = add(4); 37 | root->left->right = add(5); 38 | inorder(root); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Trees/Level Order.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | void levelorder(node *root){ 17 | std::queue Q; 18 | Q.push(root); 19 | while(!Q.empty()){ 20 | node *temp = Q.front(); 21 | cout<data<<" "; 22 | Q.pop(); 23 | if(temp->left != NULL) 24 | Q.push(temp->left); 25 | if(temp->right != NULL) 26 | Q.push(temp->right); 27 | } 28 | } 29 | 30 | 31 | int main() { 32 | // your code goes here 33 | node *root = NULL; 34 | root = add(1); 35 | root->left = add(2); 36 | root->right = add(3); 37 | root->left->left = add(4); 38 | root->left->right = add(5); 39 | levelorder(root); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /LinkedList/linkedlist_rev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | int i; 10 | struct Node *t, *last; 11 | first=new Node; 12 | first->data=A[0]; 13 | first->next=NULL; 14 | last=first; 15 | for(i=1;idata=A[i]; 19 | t->next=NULL; 20 | last->next=t; 21 | last=t; 22 | } 23 | } 24 | 25 | void display(struct Node *p) 26 | { 27 | if(p!=NULL) 28 | { 29 | cout<data<<" "; 30 | display(p->next); 31 | } 32 | } 33 | void display_rev(struct Node *p){ 34 | if(p!=NULL) 35 | { 36 | display_rev (p->next); 37 | cout<data<<" "; 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | int A[]={3,5,7,10,15}; 44 | int n=sizeof(A)/sizeof(A[0]); 45 | create(A, n); 46 | display(first); 47 | cout< 8 | using namespace std; 9 | void Sieve(int number) 10 | { 11 | bool prime[number+1]; 12 | memset(prime, true, sizeof(prime)); 13 | for(int i=2; i*i<=number;i++) 14 | { 15 | //if prime[i] is not changed , then it is a prime 16 | if(prime[i]==true) 17 | { 18 | for(int j=i*i; j<=number;j+=i) 19 | { 20 | prime[j]=false; 21 | } 22 | } 23 | } 24 | for(int p=2;p<=number;p++) 25 | { 26 | if(prime[p]) 27 | cout<>number; 40 | Sieve(number); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /CP-CA3/printing-divisor3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printDivisors(int n) 5 | { 6 | // Vector to store half of the divisors 7 | vector v; 8 | for (int i=1; i<=sqrt(n); i++) 9 | { 10 | if (n%i==0) 11 | { 12 | if (n/i == i) // check if divisors are equal 13 | printf("%d ", i); 14 | else 15 | { 16 | printf("%d ", i); 17 | 18 | // push the second divisor in the vector 19 | v.push_back(n/i); 20 | } 21 | } 22 | } 23 | 24 | // The vector will be printed in reverse 25 | for (int i=v.size()-1; i>=0; i--) 26 | printf("%d ", v[i]); 27 | } 28 | 29 | /* Driver program to test above function */ 30 | int main() 31 | { 32 | printf("The divisors of 100 are: n"); 33 | printDivisors(100); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /CP-CA3/extended_eculidian_alog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function for extended Euclidean Algorithm 5 | int gcdExtended(int a, int b, int *x, int *y) 6 | { 7 | // Base Case 8 | if (a == 0) 9 | { 10 | *x = 0; 11 | *y = 1; 12 | return b; 13 | } 14 | 15 | int x1, y1; // To store results of recursive call 16 | int gcd = gcdExtended(b%a, a, &x1, &y1); 17 | 18 | // Update x and y using results of 19 | // recursive call 20 | *x = y1 - (b/a) * x1; 21 | *y = x1; 22 | 23 | 24 | 25 | return gcd; 26 | //cout<<*x<<" "<<*y< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | 15 | for(int i=1;idata=A[i]; 19 | t->next=NULL; 20 | last->next=t; 21 | last=t; 22 | } 23 | } 24 | int is_sorted(struct Node *p) 25 | { int x=INT_MIN; 26 | int count=0; 27 | while(p!=NULL) 28 | { if(p->data < x) 29 | return 0; 30 | x=p->data; 31 | p=p->next; 32 | } 33 | return 1; 34 | 35 | } 36 | int main() 37 | { 38 | int A[]={1,2,3,4,5}; 39 | int n=sizeof(A)/sizeof(A[0]); 40 | create(A, n); 41 | int flag=is_sorted(first); 42 | if(flag==1) 43 | cout<<"Linkedlist is sorted"; 44 | else cout<<"LinkedList is not sorted "; 45 | } 46 | -------------------------------------------------------------------------------- /Pratice/LinkedList/is-palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | 25 | int isPalindrome(struct Node *p){ 26 | stack s; 27 | struct Node *temp; 28 | temp=p; 29 | while(temp!=NULL){ 30 | s.push(temp->data); 31 | temp=temp->next; 32 | } 33 | while(p!=NULL){ 34 | if(s.top() != p->data){ 35 | return 0; 36 | } 37 | else{ 38 | s.pop(); 39 | p=p->next; 40 | } 41 | } 42 | return 1; 43 | 44 | } 45 | 46 | int main(){ 47 | int A[]={1,2,3,4,5,4,3,2,1}; 48 | int n=sizeof(A)/sizeof(A[0]); 49 | create(A,n); 50 | cout< 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n){ 10 | int i; 11 | struct Node *t, *last; 12 | first = new Node; 13 | first -> data = A[0]; 14 | last = first; 15 | 16 | for(i=1; i data = A[i]; 19 | t->next = NULL; 20 | last->next = t; 21 | last = t; 22 | } 23 | } 24 | 25 | void middleElementFinder(struct Node *p){ 26 | struct Node *fast_pointer = p; 27 | struct Node *slow_pointer = p; 28 | 29 | if(p!=NULL){ 30 | while(fast_pointer != NULL && fast_pointer -> next != NULL){ 31 | fast_pointer = fast_pointer -> next ->next; 32 | slow_pointer = slow_pointer ->next; 33 | } 34 | cout<data; 35 | } 36 | } 37 | 38 | 39 | int main(){ 40 | int A[]={1,2,3,4,5,6,7}; 41 | int n = sizeof(A)/sizeof(A[0]); 42 | create(A,n); 43 | middleElementFinder(first); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Pratice/LinkedList/loop-finder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n){ 10 | int i; 11 | struct Node *t, *last; 12 | first = new Node; 13 | first -> data = A[0]; 14 | last = first; 15 | 16 | for(i=1; i data = A[i]; 19 | t->next = NULL; 20 | last->next = t; 21 | last = t; 22 | } 23 | } 24 | 25 | int loopFinder(struct Node *p){ 26 | struct Node *fast_p = p; 27 | struct Node *slow_p = p; 28 | 29 | while(slow_p && fast_p && fast_p->next){ 30 | slow_p = slow_p->next; 31 | fast_p = fast_p->next->next; 32 | if (slow_p == fast_p) { 33 | return 1; 34 | } 35 | } 36 | return 0; 37 | } 38 | 39 | 40 | int main(){ 41 | int A[]={1,2,3,4}; 42 | int n = sizeof(A)/sizeof(A[0]); 43 | create(A,n); 44 | //first->next->next->next->next=first; 45 | cout< 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | struct node* newNode(int data){ 9 | struct node *temp=new node(); 10 | temp->data=data; 11 | temp->left=NULL; 12 | temp->right=NULL; 13 | return temp; 14 | } 15 | bool isBSTUtl(struct node* node, int min, int max){ 16 | if(node==NULL) 17 | return true; 18 | 19 | if(node->datadata>max) 20 | return false; 21 | 22 | return (isBSTUtl(node->left, min, node->data-1) && isBSTUtl(node->right, node->data+1, max)); 23 | } 24 | bool isBST(struct node* node){ 25 | return isBSTUtl(node, INT_MIN, INT_MAX); 26 | } 27 | int main(){ 28 | struct node *root=newNode(20); 29 | root->left=newNode(10); 30 | root->left->left=newNode(5); 31 | root->left->right=newNode(15); 32 | root->right=newNode(22); 33 | root->right->left=newNode(21); 34 | root->right->right=newNode(24); 35 | 36 | isBST(root)==1?cout<<"Tree is a BST" :"Tree is not a BST"; 37 | } 38 | -------------------------------------------------------------------------------- /LinkedList/reversing_links.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | 8 | void create(int A[], int n) 9 | { 10 | struct Node *t, *last; 11 | first=new Node; 12 | first->data=A[0]; 13 | first->next=NULL; 14 | last=first; 15 | 16 | for(int i=1;idata=A[i]; 20 | t->next=NULL; 21 | last->next=t; 22 | last=t; 23 | } 24 | } 25 | void display(struct Node *p) 26 | { 27 | while(p!=NULL) 28 | { 29 | cout<data<<" "; 30 | p=p->next; 31 | } 32 | } 33 | void rev(struct Node *p) 34 | { 35 | p=first; 36 | struct Node *q, *r; 37 | q=new Node; 38 | r=new Node; 39 | q=NULL; 40 | r=NULL; 41 | while(p!=NULL){ 42 | r=q; 43 | q=p; 44 | p=p->next; 45 | q->next=r; 46 | } 47 | first=q; 48 | } 49 | 50 | int main() 51 | { 52 | int A[]={10,12,15,20,35}; 53 | int n=sizeof(A)/sizeof(A[0]); 54 | create(A, n); 55 | rev(first); 56 | display(first); 57 | } 58 | -------------------------------------------------------------------------------- /Trees/Print BST keys in given range.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | return newnode; 13 | } 14 | 15 | node* addinbst(node *root, int data){ 16 | if(root == NULL) 17 | return add(data); 18 | if(data < root->data) 19 | root->left = addinbst(root->left, data); 20 | else 21 | root->right = addinbst(root->right, data); 22 | return root; 23 | } 24 | 25 | void inorder(node *root, int left, int right){ 26 | if(root == NULL) 27 | return; 28 | inorder(root->left, left, right); 29 | if(root->data >= left and root->data <= right) 30 | cout<data<<" "; 31 | inorder(root->right, left, right); 32 | } 33 | 34 | int main() { 35 | // your code goes here 36 | node *root = NULL; 37 | root = addinbst(root, 20); 38 | root = addinbst(root, 22); 39 | root = addinbst(root, 8); 40 | root = addinbst(root, 4); 41 | root = addinbst(root, 12); 42 | inorder(root, 10, 22); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Pratice/LinkedList/reversingLinks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | 23 | } 24 | void reversingLinks(struct Node *p) 25 | { 26 | struct Node *q, *r; 27 | q=NULL; 28 | r = NULL; 29 | while(p!=NULL){ 30 | r=q; 31 | q=p; 32 | p=p->next; 33 | q->next = r; 34 | } 35 | first = q; 36 | } 37 | 38 | void print(struct Node *p){ 39 | while(p!=NULL){ 40 | cout<data<<" "; 41 | p=p->next; 42 | } 43 | 44 | } 45 | 46 | int main() 47 | { 48 | int A[]={3,5,7,10,15}; 49 | int n=sizeof(A)/sizeof(A[0]); 50 | create(A,n); 51 | print(first); 52 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void insert(struct Node *p, int val, int pos) 25 | { 26 | struct Node *t =new Node; 27 | int count=1; 28 | while(p!=NULL) 29 | { 30 | if(pos==count) 31 | { 32 | t->data=val; 33 | t->next=p->next; 34 | p->next=t; 35 | } 36 | p=p->next; 37 | count++; 38 | } 39 | 40 | } 41 | void Display(struct Node *p) 42 | { 43 | while(p!=NULL) 44 | { 45 | cout<data<<" "; 46 | p=p->next; 47 | } 48 | } 49 | int main(){ 50 | int A[]={10,20,30,40,60}; 51 | int n=sizeof(A)/sizeof(A[0]); 52 | create(A,n); 53 | insert(first,50,1); 54 | Display(first); 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Searching/linear_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int n; 5 | cout<<"Enter the number of elements in array"<>n; 7 | 8 | int arr[n]; 9 | 10 | cout<<"Enter array elements"<>arr[i]; 13 | } 14 | 15 | int x,ix=-1; 16 | cout<<"Enter the element you want to search"<>x; 18 | 19 | for(int i=0;iarr[j]){ 35 | int temp = arr[i]; 36 | arr[i] = arr[j]; 37 | arr[j] = temp; 38 | } 39 | } 40 | } 41 | cout<<"Array elements are "< 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n){ 10 | int i; 11 | struct Node *t, *last; 12 | first = new Node; 13 | first -> data = A[0]; 14 | last = first; 15 | 16 | for(i=1; i data = A[i]; 19 | t->next = NULL; 20 | last->next = t; 21 | last = t; 22 | } 23 | } 24 | 25 | int nthElementFromEnd(struct Node *p,int pos){ 26 | struct Node *temp; 27 | temp=p; 28 | int count = 0; 29 | while(temp!=NULL){ 30 | count = count + 1; 31 | temp=temp->next; 32 | } 33 | int finalPos = count - pos + 1; 34 | //cout<data; 40 | } 41 | p=p->next; 42 | } 43 | return -1; 44 | } 45 | 46 | 47 | int main(){ 48 | int A[]={1,2,3,4,5,6,7,8}; 49 | int n = sizeof(A)/sizeof(A[0]); 50 | create(A,n); 51 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | node* addinbst(node *root, int data){ 17 | node *curr = root, *prev = NULL; 18 | while(1){ 19 | if(curr == NULL){ 20 | if(prev == NULL) 21 | prev = add(data); 22 | else if(data < prev->data) 23 | prev->left = add(data); 24 | else 25 | prev->right = add(data); 26 | break; 27 | } 28 | prev = curr; 29 | curr = data < curr->data ? curr->left : curr->right; 30 | } 31 | return prev; 32 | } 33 | 34 | void inorder(node *root){ 35 | if(root == NULL) 36 | return; 37 | inorder(root->left); 38 | cout<data<<" "; 39 | inorder(root->right); 40 | } 41 | 42 | int main(){ 43 | node *root = NULL; 44 | root = addinbst(root, 50); 45 | addinbst(root, 30); 46 | addinbst(root, 20); 47 | addinbst(root, 40); 48 | addinbst(root, 70); 49 | addinbst(root, 60); 50 | addinbst(root, 80); 51 | inorder(root); 52 | } -------------------------------------------------------------------------------- /Heap/minHeap to max heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void MaxHeapify(int arr[], int i, int n) 5 | { 6 | int l = 2*i + 1; 7 | int r = 2*i + 2; 8 | int largest = i; 9 | if (l < n && arr[l] > arr[i]) 10 | largest = l; 11 | if (r < n && arr[r] > arr[largest]) 12 | largest = r; 13 | if (largest != i) 14 | { 15 | swap(arr[i], arr[largest]); 16 | MaxHeapify(arr, largest, n); 17 | } 18 | } 19 | 20 | void convertMaxHeap(int arr[], int n) 21 | { 22 | for (int i = (n-2)/2; i >= 0; --i) 23 | MaxHeapify(arr, i, n); 24 | } 25 | 26 | void printArray(int* arr, int size) 27 | { 28 | for (int i = 0; i < size; ++i) 29 | printf("%d ", arr[i]); 30 | } 31 | 32 | 33 | int main() 34 | { 35 | 36 | int arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9}; 37 | int n = sizeof(arr)/sizeof(arr[0]); 38 | 39 | printf("Min Heap array : "); 40 | printArray(arr, n); 41 | 42 | convertMaxHeap(arr, n); 43 | 44 | printf("\nMax Heap array : "); 45 | printArray(arr, n); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Pratice/LinkedList/deleting_middle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void print(struct Node *p){ 25 | while(p!=NULL){ 26 | cout<data<<" "; 27 | p=p->next; 28 | } 29 | } 30 | 31 | 32 | void deletingMiddle(struct Node *p){ 33 | struct Node *slow_pointer, *prev; 34 | slow_pointer = p; 35 | 36 | while(p!=NULL && p->next != NULL){ 37 | p=p->next->next; 38 | prev=slow_pointer; 39 | slow_pointer = slow_pointer -> next; 40 | } 41 | prev->next=slow_pointer->next; 42 | delete(slow_pointer); 43 | } 44 | 45 | int main(){ 46 | int A[]={1,2,3,4,5,4,3,2,1}; 47 | int n=sizeof(A)/sizeof(A[0]); 48 | create(A,n); 49 | print(first); 50 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | 8 | void create(int A[], int n) 9 | { 10 | struct Node *t, *last; 11 | first=new Node; 12 | first->data=A[0]; 13 | first->next=NULL; 14 | last=first; 15 | 16 | for(int i=1;idata=A[i]; 20 | t->next=NULL; 21 | last->next=t; 22 | last=t; 23 | } 24 | } 25 | void display(struct Node *p) 26 | { 27 | while(p!=NULL) 28 | { 29 | cout<data<<" "; 30 | p=p->next; 31 | } 32 | } 33 | void delete_distinct(struct Node *p) 34 | { 35 | p=first; 36 | struct Node *q=new Node; 37 | q=first->next; 38 | while(q!=NULL) 39 | { 40 | if(p->data!=q->data) 41 | { 42 | p=q; 43 | q=q->next; 44 | } 45 | else 46 | { 47 | p->next=q->next; 48 | delete q; 49 | q=p->next; 50 | } 51 | } 52 | } 53 | 54 | int main() 55 | { 56 | int A[]={3,5,5,8,8,8}; 57 | int n=sizeof(A)/sizeof(A[0]); 58 | create(A, n); 59 | delete_distinct(first); 60 | display(first); 61 | } 62 | -------------------------------------------------------------------------------- /Trees/depthheight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | struct node* newNode(int data) 9 | { 10 | struct node *temp=new node(); 11 | temp->data=data; 12 | temp->left=NULL; 13 | temp->right=NULL; 14 | } 15 | int height(struct node* root) 16 | { 17 | if(root==NULL) 18 | return 0; 19 | 20 | else 21 | { 22 | return max(height(root->left), height(root->right))+1; 23 | } 24 | } 25 | int main() 26 | { 27 | struct node *root=newNode(25); 28 | root->left=newNode(15); 29 | root->right=newNode(50); 30 | root->left->left=newNode(10); 31 | root->left->left->left=newNode(4); 32 | root->left->left->right=newNode(12); 33 | root->left->right=newNode(22); 34 | root->left->right->left=newNode(18); 35 | root->left->right->right=newNode(24); 36 | root->right->left=newNode(35); 37 | root->right->left->left=newNode(31); 38 | root->right->left->right=newNode(44); 39 | root->right->right=newNode(70); 40 | root->right->right->left=newNode(66); 41 | root->right->right->right=newNode(90); 42 | cout<<"Height of the tree is: "< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | for(int i=1; idata=A[i]; 18 | t->next=NULL; 19 | last->next=t; 20 | last=t; 21 | } 22 | } 23 | 24 | void insert(struct Node *p, int val, int pos) 25 | { 26 | struct Node *t =new Node; 27 | int count=1; 28 | if(pos==0) 29 | { 30 | t->data=val; 31 | t->next=first; 32 | first=t; 33 | } 34 | else{ 35 | while(p!=NULL) 36 | { 37 | if(pos==count) 38 | { 39 | t->data=val; 40 | t->next=p->next; 41 | p->next=t; 42 | } 43 | p=p->next; 44 | count++; 45 | } 46 | } 47 | 48 | 49 | } 50 | void Display(struct Node *p) 51 | { 52 | while(p!=NULL) 53 | { 54 | cout<data<<" "; 55 | p=p->next; 56 | } 57 | } 58 | int main(){ 59 | int A[]={10,20,30,40,50}; 60 | int n=sizeof(A)/sizeof(A[0]); 61 | create(A,n); 62 | insert(first,0,5); 63 | Display(first); 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /Trees/Tree with traversals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | void inorder(node *root){ 17 | if(root == NULL) 18 | return; 19 | inorder(root->left); 20 | cout<data<<" "; 21 | inorder(root->right); 22 | } 23 | 24 | void preorder(node *root){ 25 | if(root == NULL) 26 | return; 27 | cout<data<<" "; 28 | preorder(root->left); 29 | preorder(root->right); 30 | } 31 | 32 | void postorder(node *root){ 33 | if(root == NULL) 34 | return; 35 | postorder(root->left); 36 | postorder(root->right); 37 | cout<data<<" "; 38 | } 39 | 40 | int main() { 41 | // your code goes here 42 | node *root = NULL; 43 | root = add(1); 44 | root->left = add(2); 45 | root->right = add(3); 46 | root->left->left = add(4); 47 | root->left->right = add(5); 48 | inorder(root); 49 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | 15 | for(int i=1;idata=A[i]; 19 | t->next=NULL; 20 | last->next=t; 21 | last=t; 22 | } 23 | } 24 | void display(struct Node *p) 25 | { 26 | while(p!=NULL) 27 | { 28 | cout<data<<" "; 29 | p=p->next; 30 | } 31 | } 32 | 33 | void Delete(struct Node *p,int index) 34 | {struct Node *q=NULL; 35 | int i; 36 | if(index==1) 37 | {q=first; 38 | first=first->next; 39 | free(q); 40 | } 41 | else { 42 | for(i=0;inext; 46 | } 47 | q->next=p->next; 48 | free(p); 49 | } 50 | } 51 | 52 | 53 | 54 | int main() 55 | { 56 | int A[]={3,7,9,15,20}; 57 | int n=sizeof(A)/sizeof(A[0]); 58 | create(A, n); 59 | Delete(first,5); 60 | display(first); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Pratice/LinkedList/linkedList-deletation.cpp: -------------------------------------------------------------------------------- 1 | // Deleting at first and last 2 | #include 3 | using namespace std; 4 | 5 | struct Node{ 6 | int data; 7 | struct Node *next; 8 | }*first = NULL, *last = NULL; 9 | 10 | void create(int A[], int n){ 11 | first = new Node; 12 | first->data=A[0]; 13 | struct Node *t; 14 | first->next=NULL; 15 | last = first; 16 | for(int i=1; idata=A[i]; 19 | t->next = NULL; 20 | last->next =t; 21 | last = t; 22 | } 23 | } 24 | 25 | void display(struct Node *p){ 26 | while(p!=NULL){ 27 | cout<data<<" "; 28 | p=p->next; 29 | } 30 | } 31 | 32 | void deleteFront(){ 33 | struct Node *q; 34 | q = first; 35 | first = first -> next; 36 | free(q); 37 | } 38 | 39 | 40 | void deleteEnd(){ 41 | struct Node *p = first; 42 | while(p->next->next != NULL){ 43 | p=p->next; 44 | } 45 | p->next = NULL; 46 | last = p; 47 | } 48 | 49 | int main(){ 50 | int A[]={10,11,12,13,14,15}; 51 | int n = sizeof(A)/sizeof(A[0]); 52 | create(A,n); 53 | display(first); 54 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | bool findPath(node *root, vector &v, int n){ 17 | if(root == NULL) 18 | return false; 19 | v.push_back(root->data); 20 | if(n == root->data) 21 | return true; 22 | if(findPath(root->left, v, n) or findPath(root->right, v, n)) 23 | return true; 24 | v.pop_back(); 25 | return false; 26 | } 27 | 28 | int main() { 29 | // your code goes here 30 | node *root1 = NULL; 31 | root1 = add(1); 32 | root1->left = add(2); 33 | root1->right = add(3); 34 | root1->left->left = add(4); 35 | root1->left->right = add(5); 36 | root1->right->left = add(6); 37 | root1->right->right = add(7); 38 | vector v1, v2; 39 | findPath(root1, v1, 2); 40 | findPath(root1, v2, 4); 41 | int i = 0, ans; 42 | while(1){ 43 | if(!v1.size() or !v2.size()) 44 | break; 45 | if(v1[i] == v2[i]) 46 | ans = v1[i]; 47 | else 48 | break; 49 | i++; 50 | } 51 | cout< 2 | using namespace std; 3 | 4 | void addedge(int G[5][5], int u, int v, int w){ 5 | G[u][v] = w; 6 | } 7 | 8 | int findMinDist(int dist[], bool visited[]){ 9 | int Min = INT_MAX, index = 0; 10 | for(int i = 0; i < 5; i++) 11 | if(visited[i] == false && dist[i] <= Min){ 12 | Min = dist[i]; 13 | index = i; 14 | } 15 | return index; 16 | } 17 | 18 | void dijkstra(int G[5][5], int s){ 19 | bool visited[5] = {}; 20 | int dist[5]; 21 | for(int i = 0; i < 5; i++) 22 | dist[i] = INT_MAX; 23 | dist[s] = 0; 24 | for(int i = 0; i < 4; i++){ 25 | int u = findMinDist(dist, visited); 26 | visited[u] = true; 27 | for(int v = 0; v < 5; v++) 28 | if(visited[v] == false && G[u][v] && dist[u] != INT_MAX && dist[v] > dist[u] + G[u][v]) 29 | dist[v] = dist[u] + G[u][v]; 30 | } 31 | for(int i = 0; i < 5; i++) 32 | cout<<"0 ==> "< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | int pre; 10 | 11 | node* add(int data){ 12 | node *newnode = new node; 13 | newnode->data = data; 14 | newnode->left = newnode->right = NULL; 15 | return newnode; 16 | } 17 | 18 | node* addinbst(node *root, int data){ 19 | if(root == NULL) 20 | return add(data); 21 | if(data < root->data) 22 | root->left = addinbst(root->left, data); 23 | else 24 | root->right = addinbst(root->right, data); 25 | return root; 26 | } 27 | 28 | void inorder(node *root, int &ans, int &cnt, int k){ 29 | if(root == NULL) 30 | return; 31 | inorder(root->left, ans, cnt, k); 32 | if(++cnt == k) 33 | ans = root->data; 34 | inorder(root->right, ans, cnt, k); 35 | } 36 | 37 | int main() { 38 | // your code goes here 39 | node *root = addinbst(root, 20); 40 | root = addinbst(root, 22); 41 | root = addinbst(root, 8); 42 | root = addinbst(root, 12); 43 | root = addinbst(root, 4); 44 | root = addinbst(root, 10); 45 | root = addinbst(root, 14); 46 | int ans = 0, cnt = 0; 47 | inorder(root, ans, cnt, 5); 48 | cout< 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | struct Node *next; 7 | }*first=NULL; 8 | 9 | void create(int A[], int n) 10 | { 11 | struct Node *t, *last; 12 | first=new Node; 13 | first->data=A[0]; 14 | first->next=NULL; 15 | last=first; 16 | for(int i=1; idata=A[i]; 20 | t->next=NULL; 21 | last->next=t; 22 | last=t; 23 | } 24 | } 25 | 26 | void print(struct Node *p){ 27 | while(p!=NULL){ 28 | cout<data<<" "; 29 | p=p->next; 30 | } 31 | } 32 | 33 | void deleteDuplicate(struct Node *p){ 34 | struct Node *temp, *dup; 35 | while(p!= NULL && p->next != NULL){ 36 | temp = p; 37 | while(temp->next!=NULL){ 38 | if(p->data == temp->next->data){ 39 | dup = temp->next; 40 | temp->next = temp -> next ->next; 41 | delete(dup); 42 | } else{ 43 | temp=temp->next; 44 | } 45 | } 46 | p = p -> next; 47 | } 48 | } 49 | 50 | 51 | 52 | int main(){ 53 | int A[]={1,2,3,4,5,4,3,2,1}; 54 | int n=sizeof(A)/sizeof(A[0]); 55 | create(A,n); 56 | print(first); 57 | cout< 5 | using namespace std; 6 | //First Method 7 | //GCD of two number does not change if smaller number is subtracted from a bigger number 8 | int Euclidean(int number1,int number2) 9 | { 10 | if(number1==0) 11 | return number2; 12 | if(number2==0) 13 | return number1; 14 | //base case 15 | if(number1==number2) 16 | return number1; 17 | //a is greater 18 | if(number1>number2) 19 | { 20 | return Euclidean(number1-number2,number2); 21 | } 22 | else 23 | return Euclidean(number1,number2-number1); 24 | } 25 | //Second Method 26 | int Euclidean1(int number1,int number2) 27 | { 28 | if(number2==0) 29 | return number1; 30 | return Euclidean1(number2,number1%number2); 31 | } 32 | 33 | //Driver Code 34 | int main() 35 | { 36 | ios_base::sync_with_stdio(false); 37 | cin.tie(NULL); cout.tie(NULL); 38 | int number1,number2; 39 | cout<<"Enter both number's:"<>number1>>number2; 41 | //First Method 42 | cout< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | 15 | for(int i=1;idata=A[i]; 19 | t->next=NULL; 20 | last->next=t; 21 | last=t; 22 | } 23 | } 24 | void display(struct Node *p) 25 | { 26 | while(p!=NULL) 27 | { 28 | cout<data<<" "; 29 | p=p->next; 30 | } 31 | } 32 | 33 | void insert(struct Node *p, int val ) 34 | { 35 | struct Node *t, *q=NULL; 36 | t=new Node; 37 | t->data=val; 38 | t->next=NULL; 39 | if(first==NULL) 40 | first=t; 41 | else 42 | { while(p && p->datanext; 45 | } 46 | if(p==first) 47 | { t->next=first; 48 | first=t; 49 | } 50 | else 51 | {t->next=q->next; 52 | q->next=t; 53 | } 54 | } 55 | } 56 | int main() 57 | { 58 | int A[]={3,7,9,15,20}; 59 | int n=sizeof(A)/sizeof(A[0]); 60 | create(A, n); 61 | insert(first,18); 62 | insert(first,10); 63 | insert(first,2); 64 | display(first); 65 | } 66 | 67 | -------------------------------------------------------------------------------- /Pratice/LinkedList/insertation-in-sorted-ll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first=NULL; 7 | void create(int A[], int n) 8 | { 9 | struct Node *t, *last; 10 | first=new Node; 11 | first->data=A[0]; 12 | first->next=NULL; 13 | last=first; 14 | 15 | for(int i=1;idata=A[i]; 19 | t->next=NULL; 20 | last->next=t; 21 | last=t; 22 | } 23 | } 24 | void display(struct Node *p) 25 | { 26 | while(p!=NULL) 27 | { 28 | cout<data<<" "; 29 | p=p->next; 30 | } 31 | } 32 | 33 | void insert(struct Node *p, int val ) 34 | { 35 | struct Node *t, *q=NULL; 36 | t=new Node; 37 | t->data=val; 38 | t->next=NULL; 39 | if(first==NULL) 40 | first=t; 41 | else 42 | { while(p && p->datanext; 45 | } 46 | if(p==first) 47 | { t->next=first; 48 | first=t; 49 | } 50 | else 51 | {t->next=q->next; 52 | q->next=t; 53 | } 54 | } 55 | } 56 | int main() 57 | { 58 | int A[]={3,7,9,15,20}; 59 | int n=sizeof(A)/sizeof(A[0]); 60 | create(A, n); 61 | insert(first,18); 62 | insert(first,10); 63 | insert(first,2); 64 | display(first); 65 | } 66 | -------------------------------------------------------------------------------- /Pratice/Stack/Stack-with-linkedList.cpp: -------------------------------------------------------------------------------- 1 | // Stack using a LinkedList 2 | // Stack Operation: Last in and first out 3 | 4 | #include 5 | using namespace std; 6 | struct Node { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL, *last=NULL; 10 | 11 | 12 | void push(int val){ 13 | 14 | if(first == NULL){ 15 | first = new Node; 16 | first -> data = val; 17 | first -> next = NULL; 18 | last = first; 19 | } 20 | else{ 21 | struct Node *t; 22 | t= new Node; 23 | t -> data = val; 24 | t->next = NULL; 25 | last -> next = t; 26 | last = t; 27 | } 28 | 29 | } 30 | 31 | void printStack(struct Node *p){ 32 | if(p!=NULL){ 33 | 34 | printStack(p->next); 35 | cout<data<<" "; 36 | 37 | } 38 | } 39 | 40 | 41 | void pop(){ 42 | struct Node *p = first; 43 | while(p->next->next != NULL){ 44 | p=p->next; 45 | } 46 | p->next = NULL; 47 | last = p; 48 | } 49 | 50 | 51 | void printingTop(){ 52 | cout<<"Element on top of stack is "<data< 2 | using namespace std; 3 | 4 | int binary_search(int arr[],int x,int l,int r); 5 | int main() { 6 | int n; 7 | cout<<"Enter the number of elements in array"<>n; 9 | 10 | int arr[n]; 11 | 12 | cout<<"Enter array elements"<>arr[i]; 15 | } 16 | 17 | for(int i=0;iarr[j]){ 20 | int temp = arr[i]; 21 | arr[i] = arr[j]; 22 | arr[j] = temp; 23 | } 24 | } 25 | } 26 | 27 | cout<<"Array elements are "<>x; 34 | 35 | cout<<"Index of element "<=l){ 41 | int mid = (l+r-1)/2; 42 | if(arr[mid]==x){ 43 | return mid; 44 | } 45 | else{ 46 | if(x>mid){ 47 | return binary_search(arr,x,mid+1,r); 48 | } 49 | return binary_search(arr,x,l,mid-1); 50 | } 51 | } 52 | return -1; 53 | } -------------------------------------------------------------------------------- /Trees/Children Sum Property.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | bool sumproperty(node *root){ 17 | queue Q; 18 | Q.push(root); 19 | while(!Q.empty()){ 20 | node *temp = Q.front(); 21 | Q.pop(); 22 | int sum1 = 0, sum2 = 0; 23 | if(temp->left != NULL){ 24 | sum1 = temp->left->data; 25 | Q.push(temp->left); 26 | } 27 | if(temp->right != NULL){ 28 | sum2 = temp->right->data; 29 | Q.push(temp->right); 30 | } 31 | if((temp->data != sum1 + sum2) and (temp->left != NULL or temp->right != NULL)) 32 | return false; 33 | } 34 | return true; 35 | } 36 | 37 | int main() { 38 | // your code goes here 39 | node *root = add(10); 40 | root->left = add(8); 41 | root->right = add(2); 42 | root->left->left = add(3); 43 | root->left->right = add(5); 44 | root->right->right = add(2); 45 | if(sumproperty(root)) 46 | cout<<"TRUE"; 47 | else 48 | cout<<"FALSE"; 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Trees/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | struct node *parent; 8 | }; 9 | 10 | struct node* newNode(int data) 11 | { 12 | struct node *temp = new node(); 13 | temp->data = data; 14 | temp->left = NULL; 15 | temp->right = NULL; 16 | temp->parent = NULL; 17 | 18 | return(temp); 19 | } 20 | 21 | 22 | struct node* insert(struct node* node, int data) 23 | { 24 | if (node == NULL) 25 | return(newNode(data)); 26 | else 27 | { 28 | struct node *temp; 29 | if (data <= node->data) 30 | { 31 | temp = insert(node->left, data); 32 | node->left = temp; 33 | temp->parent= node; 34 | } 35 | else 36 | { 37 | temp = insert(node->right, data); 38 | node->right = temp; 39 | temp->parent = node; 40 | } 41 | 42 | 43 | return node; 44 | } 45 | } 46 | 47 | void inorder(struct node* node){ 48 | if(node==NULL) 49 | return; 50 | inorder(node->left); 51 | cout<data<<" "; 52 | inorder(node->right); 53 | } 54 | int main(){ 55 | struct node *root=NULL; 56 | root=insert(root,20); 57 | root=insert(root,10); 58 | root=insert(root,29); 59 | inorder(root); 60 | } 61 | -------------------------------------------------------------------------------- /Trees/levelOrder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | struct node *parent; 8 | }; 9 | struct node* newNode(int data){ 10 | struct node *temp=new node(); 11 | temp->data=data; 12 | temp->left=NULL; 13 | temp->right=NULL; 14 | temp->parent=NULL; 15 | } 16 | 17 | struct node* insert(struct node* root, int data){ 18 | if(root==NULL) 19 | return (newNode(data)); 20 | else{ 21 | struct node *temp; 22 | if(data<=root->data){ 23 | temp=insert(root->left,data); 24 | root->left=temp; 25 | temp->parent=root; 26 | } 27 | else{ 28 | temp=insert(root->right,data); 29 | root->right=temp; 30 | temp->parent=root; 31 | } 32 | } 33 | return root; 34 | } 35 | 36 | void levelorder(struct node* root){ 37 | if(root==NULL) return; 38 | queueq; 39 | q.push(root); 40 | while(q.empty()==false) 41 | { 42 | struct node *node=q.front(); 43 | cout<data<<" "; 44 | q.pop(); 45 | if(node->left!=NULL) 46 | q.push(node->left); 47 | if(node->right!=NULL) 48 | q.push(node->right); 49 | } 50 | } 51 | int main(){ 52 | struct node *root=NULL; 53 | root=insert(root,20); 54 | root=insert(root,10); 55 | root=insert(root,29); 56 | levelorder(root); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Pratice/LinkedList/linkedList-insertion-first.cpp: -------------------------------------------------------------------------------- 1 | // Insertation done right at first and end position 2 | 3 | 4 | #include 5 | using namespace std; 6 | struct Node { 7 | int data; 8 | struct Node *next; 9 | }*first = NULL, *last=NULL; 10 | 11 | void create(int A[], int n){ 12 | first = new Node; 13 | first -> data = A[0]; 14 | first -> next = NULL; 15 | struct Node *t; 16 | last = first; 17 | for(int i=1; idata = A[i]; 20 | t->next = NULL; 21 | last -> next = t; 22 | last = t; 23 | } 24 | 25 | } 26 | void display(struct Node *p){ 27 | while(p!=NULL){ 28 | cout<data<<" "; 29 | p=p->next; 30 | } 31 | } 32 | void insertFirst(int value){ 33 | struct Node *t; 34 | t = new Node; 35 | t->data=value; 36 | t->next=first; 37 | first = t; 38 | } 39 | 40 | 41 | void insertEnd(int value){ 42 | struct Node *t; 43 | t = new Node; 44 | t->data = value; 45 | t->next = NULL; 46 | last->next = t; 47 | last = t; 48 | } 49 | 50 | int main(){ 51 | int A[]={10,11,12,13,14,15}; 52 | int n = sizeof(A)/sizeof(A[0]); 53 | create(A,n); 54 | display(first); 55 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | node* addinbst(node *root, int data){ 17 | if(root == NULL) 18 | return add(data); 19 | if(data < root->data) 20 | root->left = addinbst(root->left, data); 21 | else 22 | root->right = addinbst(root->right, data); 23 | return root; 24 | } 25 | 26 | int findinbst(node *root, int first, int second){ 27 | if(root == NULL) 28 | return -1; 29 | if(root->data > first and root->data > second) 30 | return findinbst(root->left, first, second); 31 | else if(root->data < first and root->data < second) 32 | return findinbst(root->right, first, second); 33 | return root->data; 34 | } 35 | 36 | 37 | int main() { 38 | // your code goes here 39 | node *root = addinbst(root, 20); 40 | root = addinbst(root, 22); 41 | root = addinbst(root, 8); 42 | root = addinbst(root, 12); 43 | root = addinbst(root, 14); 44 | root = addinbst(root, 10); 45 | root = addinbst(root, 4); 46 | std::vector v1, v2; 47 | int first = 10, second = 22; 48 | cout< 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | node *left, *right; 6 | }; 7 | 8 | node* addnode(int data){ 9 | node *newnode = new node; 10 | newnode->data=data; 11 | newnode->left=newnode->right=NULL; 12 | return newnode; 13 | } 14 | 15 | node* addinBST(node *root, int data){ 16 | if(root==NULL) 17 | root=addnode(data); 18 | else if(datadata) 19 | root->left=addinBST(root->left,data); 20 | else 21 | root->right=addinBST(root->right,data); 22 | return root; 23 | } 24 | 25 | void inorder(node *root){ 26 | if(root==NULL) 27 | return; 28 | inorder(root->left); 29 | cout<data<<" "; 30 | inorder(root->right); 31 | } 32 | bool bsearch(node *root, int data){ 33 | if(root!=NULL){ 34 | if(data==root->data) 35 | return true; 36 | else if(datadata) 37 | return bsearch(root->left,data); 38 | else 39 | return bsearch(root->right,data); 40 | } 41 | return false; 42 | 43 | } 44 | int main() 45 | { 46 | node *root=NULL; 47 | root=addinBST(root,10); 48 | root=addinBST(root,5); 49 | root=addinBST(root,15); 50 | root=addinBST(root,0); 51 | root=addinBST(root,20); 52 | inorder(root); 53 | cout<data<left->left->data<right->right->data< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | bool sameLvl(node *root){ 17 | queue Q; 18 | int lvl = 0, oldlvl = 0, prevlvl = 0; 19 | Q.push(root); 20 | Q.push(NULL); 21 | while(Q.size() > 1){ 22 | node *temp = Q.front(); 23 | Q.pop(); 24 | if(temp == NULL){ 25 | lvl++; 26 | Q.push(NULL); 27 | continue; 28 | } 29 | if(temp->left != NULL) 30 | Q.push(temp->left); 31 | if(temp->right != NULL) 32 | Q.push(temp->right); 33 | if(temp->left == NULL and temp->right == NULL){ 34 | if(oldlvl != 0 and oldlvl != lvl) 35 | return false; 36 | oldlvl = lvl; 37 | } 38 | } 39 | return true; 40 | } 41 | 42 | int main() { 43 | // your code goes here 44 | node *root = NULL; 45 | root = add(12); 46 | root->left = add(5); 47 | root->left->left = add(3); 48 | root->left->right = add(9); 49 | root->left->right->left = add(2); 50 | root->left->left->left = add(1); 51 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | return newnode; 13 | } 14 | 15 | bool leaflvl(node *root){ 16 | std::queue Q; 17 | Q.push(root); 18 | Q.push(NULL); 19 | int lvl = -1, cnt = 0; 20 | while(Q.size() > 1){ 21 | node *temp = Q.front(); 22 | Q.pop(); 23 | if(temp == NULL){ 24 | Q.push(NULL); 25 | cnt++; 26 | } 27 | else{ 28 | if(temp->left == NULL and temp->right == NULL) 29 | if(lvl == -1) 30 | lvl = cnt; 31 | else 32 | if(lvl != cnt) 33 | return false; 34 | if(temp->left != NULL) 35 | Q.push(temp->left); 36 | if(temp->right != NULL) 37 | Q.push(temp->right); 38 | } 39 | } 40 | return true; 41 | } 42 | 43 | int main() { 44 | // your code goes here 45 | node *root = add(1); 46 | root->left = add(2); 47 | root->right = add(3); 48 | root->left->left = add(4); 49 | root->left->right = add(5); 50 | root->right->left = add(6); 51 | root->right->right = add(7); 52 | cout< 2 | #include< conio.h> 3 | using namespace std; 4 | int a[50],i,n; 5 | void display(); 6 | void makeheap(int); 7 | void heapsort(); 8 | int main() 9 | { 10 | cout<<"Enter the number of elements to enter : "; 11 | cin>>n; 12 | cout<<"enter element:"; 13 | for(i=0;i< n;i++) 14 | { 15 | //cout<< i+1<<": "; 16 | cin>>a[i]; 17 | } 18 | makeheap(n); 19 | for(i=0;i< n;i++) 20 | { 21 | cout<< a[i]<<" "; 22 | } 23 | heapsort(); 24 | cout<<"****HEAPSORT****"<< endl; 25 | display(); 26 | getch(); 27 | } 28 | void makeheap(int n) 29 | { 30 | int val,k,par; 31 | for(i=1;i< n;i++) 32 | { 33 | val=a[i]; 34 | k=i; 35 | par=(k-1)/2; 36 | while(k>0&&a[par]< val) 37 | { 38 | a[k]=a[par]; 39 | k=par; 40 | par=(k-1)/2; 41 | 42 | 43 | a[k]=val; 44 | } 45 | 46 | 47 | } 48 | // for(i=0;i< n;i++) 49 | // { 50 | // cout<< a[i]<<" "; 51 | //} 52 | cout<< endl; 53 | } 54 | 55 | void heapsort() 56 | { 57 | int temp; 58 | for(i=n-1;i>0;i--) 59 | { 60 | temp=a[i]; 61 | a[i]=a[0]; 62 | a[0]=temp; 63 | makeheap(i); 64 | } 65 | } 66 | void display() 67 | { 68 | for(i=0;i< n;i++) 69 | { 70 | cout<< a[i]<<" "; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Pratice/Queue/Queue- using LL.cpp: -------------------------------------------------------------------------------- 1 | // Queue using a LinkedList 2 | // Things covered in this - push operation, pop operation, printing the list, printing first and last element 3 | 4 | #include 5 | using namespace std; 6 | struct Node{ 7 | int data; 8 | struct Node *next; 9 | }*first = NULL, *last = NULL; 10 | 11 | void push(int val){ 12 | 13 | if(first == NULL){ 14 | first = new Node; 15 | first -> data = val; 16 | first -> next = NULL; 17 | last = first; 18 | } 19 | else{ 20 | struct Node *t; 21 | t= new Node; 22 | t -> data = val; 23 | t->next = NULL; 24 | last -> next = t; 25 | last = t; 26 | } 27 | 28 | } 29 | 30 | void printQueue(struct Node *p){ 31 | while(p!= NULL){ 32 | cout<data<<" "; 33 | p=p->next; 34 | } 35 | } 36 | 37 | void printingFirstElem(){ 38 | cout<<"first element in queue "<data<data<next; 50 | free(first); 51 | first= t; 52 | } 53 | 54 | int main(){ 55 | push(10); 56 | push(20); 57 | push(30); 58 | push(40); 59 | printQueue(first); 60 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | int width(node *root){ 17 | int MAX = 0, cnt = 0; 18 | queue Q; 19 | Q.push(root); 20 | Q.push(NULL); 21 | while(Q.size() > 1){ 22 | node *temp = Q.front(); 23 | Q.pop(); 24 | if(temp == NULL){ 25 | Q.push(NULL); 26 | if(cnt > MAX) 27 | MAX = cnt; 28 | cnt = 0; 29 | continue; 30 | } 31 | cnt++; 32 | if(temp->left != NULL) 33 | Q.push(temp->left); 34 | if(temp->right != NULL) 35 | Q.push(temp->right); 36 | } 37 | if(cnt > MAX) 38 | MAX = cnt; 39 | return MAX; 40 | } 41 | 42 | 43 | int main() { 44 | // your code goes here 45 | node *root = add(1); 46 | root->left = add(2); 47 | root->right = add(3); 48 | root->left->left = add(4); 49 | root->left->left->left = add(9); 50 | root->left->left->right = add(10); 51 | root->left->right = add(5); 52 | root->right->right = add(8); 53 | root->right->right->left = add(6); 54 | root->right->right->right = add(7); 55 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void addedge(list > *al, int u, int v, int w){ 6 | al[u].push_back(make_pair(v, w)); 7 | } 8 | 9 | int min_distance(int dist[], bool visited[]){ 10 | int Min = INT_MAX, index = 0; 11 | for(int i = 0; i < 5; i++) 12 | if(dist[i] <= Min && visited[i] == false){ 13 | Min = dist[i]; 14 | index = i; 15 | } 16 | return index; 17 | } 18 | 19 | void dijkstra(list > *al, int s){ 20 | int dist[5]; 21 | bool visited[5] = {}; 22 | for(int i = 0; i < 5; i++) 23 | dist[i] = INT_MAX; 24 | dist[s] = 0; 25 | for(int i = 0; i < 4; i++){ 26 | int u = min_distance(dist, visited); 27 | visited[u] = true; 28 | for(list > :: iterator it = al[u].begin(); it != al[u].end(); it++) 29 | if(visited[it->first] == false && dist[u] != INT_MAX && dist[it->first] > dist[u] + it->second) 30 | dist[it->first] = dist[u] + it->second; 31 | } 32 | for(int i = 0; i < 5; i++) 33 | cout<<"0 --> "< "< > *al = new list >[5]; 38 | addedge(al, 0, 1, 2); 39 | addedge(al, 0, 2, 4); 40 | addedge(al, 1, 2, 1); 41 | addedge(al, 1, 3, 4); 42 | addedge(al, 1, 4, 5); 43 | addedge(al, 2, 1, 5); 44 | addedge(al, 2, 3, 2); 45 | addedge(al, 2, 4, 3); 46 | addedge(al, 3, 4, 1); 47 | dijkstra(al, 0); 48 | } -------------------------------------------------------------------------------- /Mathematical Algo/Pigeonhole_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | //PigeonHole is a sorting algorithm that is suitable for sorting lists of element 2 | //where the number of possible key values are approximately the same 3 | 4 | //C++ program for PIGEONHOLE ALGORITHM 5 | #include 6 | using namespace std; 7 | void pigeonholeSort(int *arr,int total_number) 8 | { 9 | int minimum=arr[0], maximum=arr[0]; 10 | for(int i=1;imaximum) 15 | maximum=arr[i]; 16 | } 17 | int range = maximum-minimum+1; 18 | vector holes[range]; 19 | for(int i=0;i::iterator it; 27 | for(it=holes[i].begin();it!=holes[i].end();++it) 28 | { 29 | arr[index++]=*it; 30 | } 31 | } 32 | } 33 | 34 | // Driver Code 35 | int main() 36 | { 37 | ios_base::sync_with_stdio(false); 38 | cin.tie(NULL); cout.tie(NULL); 39 | int total_number; 40 | cout<<"Enter the total number which are present in the array:"<>total_number; 42 | int arr[total_number]; 43 | cout<<"Enter the element in the array:"<>arr[i]; 45 | pigeonholeSort(arr,total_number); 46 | cout<<"Sorted Array is:"< 2 | using namespace std; 3 | bool isSafe(int board[][10], int i, int j,int n ){ 4 | //Checking for column 5 | for(int row=0; row=0 && y>=0){ 14 | if(board[x][y]==1) 15 | return false; 16 | 17 | x--; 18 | y--; 19 | } 20 | //Right Diagnonal 21 | x=i; 22 | y=j; 23 | while(x>=0 && y>=0){ 24 | if(board[x][y]==1) 25 | return false; 26 | 27 | x--; 28 | y++; 29 | } 30 | 31 | return true; 32 | } 33 | bool solveNQueen(int board[][10], int i, int n) 34 | { 35 | //Base Case 36 | if(i==n) 37 | { 38 | for(int i=0; i>n; 76 | solveNQueen(board,0,n); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Heap/Kth element of the heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Heap { 5 | vector v; 6 | int n; 7 | 8 | Heap(int i = 0) 9 | : n(i) 10 | { 11 | v = vector(n); 12 | } 13 | }; 14 | 15 | 16 | void swap(int& a, int& b) 17 | { 18 | int temp = a; 19 | a = b; 20 | b = temp; 21 | } 22 | 23 | inline int parent(int i) 24 | { 25 | return (i - 1) / 2; 26 | } 27 | 28 | inline int left(int i) 29 | { 30 | return 2 * i + 1; 31 | } 32 | 33 | inline int right(int i) 34 | { 35 | return 2 * i + 2; 36 | } 37 | 38 | void heapify(Heap& h, int i) 39 | { 40 | int l = left(i), r = right(i), m = i; 41 | if (l < h.n && h.v[i] < h.v[l]) 42 | m = l; 43 | if (r < h.n && h.v[m] < h.v[r]) 44 | m = r; 45 | if (m != i) { 46 | swap(h.v[m], h.v[i]); 47 | heapify(h, m); 48 | } 49 | } 50 | 51 | 52 | int extractMax(Heap& h) 53 | { 54 | if (!h.n) 55 | return -1; 56 | int m = h.v[0]; 57 | h.v[0] = h.v[h.n-- - 1]; 58 | heapify(h, 0); 59 | return m; 60 | } 61 | 62 | int kThGreatest(Heap &h, int k) 63 | { 64 | for (int i = 1; i < k; ++i) 65 | extractMax(h); 66 | return extractMax(h); 67 | } 68 | 69 | 70 | int main() 71 | { 72 | Heap h(7); 73 | h.v = vector{ 20, 15, 18, 8, 10, 5, 17 }; 74 | int k = 4; 75 | 76 | cout << kThGreatest(h, k); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Trees/Perfect Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | return newnode; 13 | } 14 | 15 | bool perfectBinary(node *root){ 16 | queue Q; 17 | Q.push(root); 18 | Q.push(NULL); 19 | int cnt = 0, lvl = -1; 20 | while(Q.size() > 1){ 21 | node *temp = Q.front(); 22 | Q.pop(); 23 | if(temp == NULL){ 24 | cnt++; 25 | Q.push(NULL); 26 | } 27 | else{ 28 | if(temp->left == NULL and temp->right == NULL) 29 | if(lvl == -1) 30 | cnt = lvl; 31 | else if(cnt != lvl) 32 | return false; 33 | if((temp->left != NULL and temp->right == NULL) or (temp->left == NULL and temp->right != NULL)) 34 | return false; 35 | if(temp->left != NULL) 36 | Q.push(temp->left); 37 | if(temp->right != NULL) 38 | Q.push(temp->right); 39 | } 40 | } 41 | return true; 42 | } 43 | 44 | 45 | int main() { 46 | // your code goes here 47 | node *root = add(1); 48 | root->left = add(2); 49 | root->right = add(3); 50 | root->left->right = add(4); 51 | root->right->left = add(5); 52 | root->right->right = add(6); 53 | root->left->left = add(7); 54 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | bool aresame(node *root, node *root1){ 17 | if(root1 == NULL and root == NULL) 18 | return true; 19 | if(root == NULL or root1 == NULL) 20 | return false; 21 | return root->data == root1->data and aresame(root->left, root1->left) 22 | and aresame(root->right, root1->right); 23 | } 24 | 25 | void anothersame(node *root, node *root1, bool &same){ 26 | if(root == NULL and root1 == NULL) 27 | return; 28 | if(root == NULL or root1 == NULL){ 29 | same = false; 30 | return; 31 | } 32 | anothersame(root->left, root1->left, same); 33 | if(root1->data != root->data) 34 | same = false; 35 | anothersame(root->right, root1->right, same); 36 | } 37 | 38 | int main() { 39 | // your code goes here 40 | node *root = add(1); 41 | root->left = add(2); 42 | root->right = add(3); 43 | root->left->left = add(4); 44 | root->left->right = add(5); 45 | node *root1 = add(1); 46 | root1->left = add(2); 47 | root1->right = add(30); 48 | root1->left->left = add(4); 49 | root1->left->right = add(5); 50 | cout< 2 | using namespace std; 3 | 4 | struct Heap { 5 | vector v; 6 | int n; // Size of the heap 7 | 8 | Heap(int i = 0) 9 | : n(i) 10 | { 11 | v = vector(n); 12 | } 13 | }; 14 | 15 | void swap(int& a, int& b) 16 | { 17 | int temp = a; 18 | a = b; 19 | b = temp; 20 | } 21 | 22 | 23 | inline int parent(int i) 24 | { 25 | return (i - 1) / 2; 26 | } 27 | 28 | 29 | inline int left(int i) 30 | { 31 | return 2 * i + 1; 32 | } 33 | 34 | inline int right(int i) 35 | { 36 | return 2 * i + 2; 37 | } 38 | 39 | void heapify(Heap& h, int i) 40 | { 41 | int l = left(i), r = right(i), m = i; 42 | if (l < h.n && h.v[i] > h.v[l]) 43 | m = l; 44 | if (r < h.n && h.v[m] > h.v[r]) 45 | m = r; 46 | if (m != i) { 47 | swap(h.v[m], h.v[i]); 48 | heapify(h, m); 49 | } 50 | } 51 | 52 | int extractMin(Heap& h) 53 | { 54 | if (!h.n) 55 | return -1; 56 | int m = h.v[0]; 57 | h.v[0] = h.v[h.n-- - 1]; 58 | heapify(h, 0); 59 | return m; 60 | } 61 | 62 | int findKthSmalles(Heap &h, int k) 63 | { 64 | for (int i = 1; i < k; ++i) 65 | extractMin(h); 66 | return extractMin(h); 67 | } 68 | 69 | int main() 70 | { 71 | Heap h(7); 72 | h.v = vector{ 10, 50, 40, 75, 60, 65, 45 }; 73 | int k = 2; 74 | cout << findKthSmalles(h, k); 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Trees/LCA set 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | node* addinbst(node *root, int data){ 17 | if(root == NULL) 18 | return add(data); 19 | if(data < root->data) 20 | root->left = addinbst(root->left, data); 21 | else 22 | root->right = addinbst(root->right, data); 23 | return root; 24 | } 25 | 26 | void findinbst(node *root, int num, vector &v){ 27 | if(root == NULL) 28 | return; 29 | v.push_back(root->data); 30 | if(num < root->data) 31 | findinbst(root->left, num, v); 32 | else if(num > root->data) 33 | findinbst(root->right, num, v); 34 | else 35 | return; 36 | } 37 | 38 | int main() { 39 | // your code goes here 40 | node *root = addinbst(root, 20); 41 | root = addinbst(root, 22); 42 | root = addinbst(root, 8); 43 | root = addinbst(root, 12); 44 | root = addinbst(root, 14); 45 | root = addinbst(root, 10); 46 | root = addinbst(root, 4); 47 | std::vector v1, v2; 48 | int first = 10, second = 22, ans = 0; 49 | findinbst(root, first, v1); 50 | findinbst(root, second, v2); 51 | for(int i = 0; i < v1.size(); i++) 52 | for(int j = 0; j < v2.size(); j++) 53 | if(v1[i] == v2[j]) 54 | ans = v1[i]; 55 | cout< 2 | using namespace std; 3 | void merge(int *,int, int , int ); 4 | void mergesort(int *a, int low, int high) 5 | { 6 | int mid; 7 | if (low < high) 8 | { 9 | mid=(low+high)/2; 10 | mergesort(a,low,mid); 11 | mergesort(a,mid+1,high); 12 | merge(a,low,high,mid); 13 | } 14 | return; 15 | } 16 | void merge(int *a, int low, int high, int mid) 17 | { 18 | int i, j, k, c[50]; 19 | i = low; 20 | k = low; 21 | j = mid + 1; 22 | while (i <= mid && j <= high) 23 | { 24 | if (a[i] < a[j]) 25 | { 26 | c[k] = a[i]; 27 | k++; 28 | i++; 29 | } 30 | else 31 | { 32 | c[k] = a[j]; 33 | k++; 34 | j++; 35 | } 36 | } 37 | while (i <= mid) 38 | { 39 | c[k] = a[i]; 40 | k++; 41 | i++; 42 | } 43 | while (j <= high) 44 | { 45 | c[k] = a[j]; 46 | k++; 47 | j++; 48 | } 49 | for (i = low; i < k; i++) 50 | { 51 | a[i] = c[i]; 52 | } 53 | } 54 | int main() 55 | { 56 | int *a, i, n; 57 | cout<<"enter number of element you want to sort\n"; 58 | cin>>n; 59 | a=new int[n]; 60 | cout<<"enter the elements\n"; 61 | for (i = 0; i < n; i++) 62 | { 63 | cin>>a[i]; 64 | } 65 | mergesort(a, 0, n-1); 66 | cout<<"sorted array\n"; 67 | for (i = 0; i < 5; i++) 68 | { 69 | cout<< a[i]; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Trees/Pre-post-in order traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node* left, *right; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | 16 | void printPostorder(struct Node* node) 17 | { 18 | if (node == NULL) 19 | return; 20 | 21 | 22 | printPostorder(node->left); 23 | 24 | 25 | printPostorder(node->right); 26 | 27 | 28 | cout << node->data << " "; 29 | } 30 | 31 | void printInorder(struct Node* node) 32 | { 33 | if (node == NULL) 34 | return; 35 | printInorder(node->left); 36 | cout << node->data << " "; 37 | printInorder(node->right); 38 | } 39 | void printPreorder(struct Node* node) 40 | { 41 | if (node == NULL) 42 | return; 43 | 44 | 45 | cout << node->data << " "; 46 | 47 | printPreorder(node->left); 48 | 49 | printPreorder(node->right); 50 | } 51 | 52 | int main() 53 | { 54 | struct Node *root = new Node(1); 55 | root->left = new Node(2); 56 | root->right = new Node(3); 57 | root->left->left = new Node(4); 58 | root->left->right = new Node(5); 59 | 60 | cout << "\nPreorder traversal of binary tree is \n"; 61 | printPreorder(root); 62 | 63 | cout << "\nInorder traversal of binary tree is \n"; 64 | printInorder(root); 65 | 66 | cout << "\nPostorder traversal of binary tree is \n"; 67 | printPostorder(root); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /LinkedList/reversing links.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | Node(int data) 8 | { 9 | this->data = data; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | struct LinkedList { 15 | Node* head; 16 | LinkedList() 17 | { 18 | head = NULL; 19 | } 20 | 21 | 22 | void reverse() 23 | { 24 | 25 | Node* current = head; 26 | Node *prev = NULL, *next = NULL; 27 | 28 | while (current != NULL) { 29 | 30 | next = current->next; 31 | 32 | current->next = prev; 33 | 34 | prev = current; 35 | current = next; 36 | } 37 | head = prev; 38 | } 39 | 40 | void print() 41 | { 42 | struct Node* temp = head; 43 | while (temp != NULL) { 44 | cout << temp->data << " "; 45 | temp = temp->next; 46 | } 47 | } 48 | 49 | void push(int data) 50 | { 51 | Node* temp = new Node(data); 52 | temp->next = head; 53 | head = temp; 54 | } 55 | }; 56 | 57 | 58 | int main() 59 | { 60 | 61 | LinkedList ll; 62 | ll.push(20); 63 | ll.push(4); 64 | ll.push(15); 65 | ll.push(85); 66 | 67 | cout << "Given linked list\n"; 68 | ll.print(); 69 | 70 | ll.reverse(); 71 | 72 | cout << "\nReversed Linked list \n"; 73 | ll.print(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Mathematical Algo/Matrix_Exponentiation.cpp: -------------------------------------------------------------------------------- 1 | //C++ program to find value of f(n) where f(n) is defined as 2 | // f(n)=f(n-1)+ f(n-2)+ f(n-3), n>=3 3 | //Base cases: 4 | //f(0)=0, f(1)=1, f(2)=1; 5 | 6 | //C++ program for MATRIX EXPONENTIATION 7 | #include 8 | using namespace std; 9 | 10 | //A utility function to multiply two matrices 11 | //arr1[] and arr2[] . multiplication result is stored back in arr1[] 12 | void multiply(int arr1[3][3],int arr2[3][3]) 13 | { 14 | int mul[3][3]; 15 | for(int i=0;i<3;i++) 16 | { 17 | for(int j=0;j<3;j++) 18 | { 19 | mul[i][j]=0; 20 | for(int k=0;k<3;k++) 21 | { 22 | mul[i][j]+=arr1[i][k]*arr2[k][j]; 23 | } 24 | } 25 | } 26 | //storing the multiplication result in arr1[] 27 | for(int i=0;i<3;i++) 28 | for(int j=0;j<3;j++) 29 | arr1[i][j]=mul[i][j]; 30 | } 31 | 32 | // function to compute f raise to power number-2 33 | int power(int f[3][3],int number) 34 | { 35 | int m[3][3]={{1,1,1},{1,0,0},{0,1,0}}; 36 | // multiply with the initial value 37 | if(number==1) 38 | return f[0][0]+f[0][1]; 39 | power(f,number/2); 40 | multiply(f,f); 41 | if(number%2!=0) 42 | multiply(f,m); 43 | 44 | return f[0][0]+f[0][1]; 45 | } 46 | int findNth(int number) 47 | { 48 | int f[3][3]={{1,1,1},{1,0,0},{0,1,0}}; 49 | //base case 50 | if(number==0) 51 | return 0; 52 | if(number==1||number==2) 53 | return 1; 54 | 55 | return power(f,number-2); 56 | } 57 | 58 | //Driver Code 59 | int main() 60 | { 61 | ios_base::sync_with_stdio(false); 62 | cin.tie(NULL); cout.tie(NULL); 63 | int number; 64 | cout<<"Enter the number:"<>number; 66 | cout<<"Value is: "< 2 | using namespace std; 3 | 4 | struct Node { 5 | int key; 6 | Node* left; 7 | Node* right; 8 | }; 9 | 10 | struct Node* newNode(int value) 11 | { 12 | Node* n = new Node; 13 | n->key = value; 14 | n->left = NULL; 15 | n->right = NULL; 16 | return n; 17 | } 18 | 19 | struct Node* insertValue(struct Node* root, int value, 20 | queue& q) 21 | { 22 | Node* node = newNode(value); 23 | if (root == NULL) 24 | root = node; 25 | 26 | 27 | else if (q.front()->left == NULL) 28 | q.front()->left = node; 29 | 30 | else { 31 | q.front()->right = node; 32 | q.pop(); 33 | } 34 | 35 | q.push(node); 36 | return root; 37 | } 38 | 39 | 40 | Node* createTree(int arr[], int n) 41 | { 42 | Node* root = NULL; 43 | queue q; 44 | for (int i = 0; i < n; i++) 45 | root = insertValue(root, arr[i], q); 46 | return root; 47 | } 48 | 49 | 50 | void levelOrder(struct Node* root) 51 | { 52 | if (root == NULL) 53 | return; 54 | queue n; 55 | n.push(root); 56 | while (!n.empty()) { 57 | cout << n.front()->key << " "; 58 | if (n.front()->left != NULL) 59 | n.push(n.front()->left); 60 | if (n.front()->right != NULL) 61 | n.push(n.front()->right); 62 | n.pop(); 63 | } 64 | } 65 | 66 | 67 | int main() 68 | { 69 | int arr[] = { 10, 20, 30, 40, 50, 60 }; 70 | int n = sizeof(arr) / sizeof(arr[0]); 71 | Node* root = createTree(arr, n); 72 | levelOrder(root); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Trees/Deletion by rightmost.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | void inorder(node *root){ 17 | if(root == NULL) 18 | return; 19 | inorder(root->left); 20 | cout<data<<" "; 21 | inorder(root->right); 22 | } 23 | 24 | node* removenode(node *root, int data){ 25 | std::queue Q; 26 | int cnt = 0; 27 | Q.push(root); 28 | node *rightmost = root, *target = root, *prev = root; 29 | while(!Q.empty()){ 30 | node *temp = Q.front(); 31 | Q.pop(); 32 | if(temp->data == data) 33 | target = temp; 34 | if(temp->left != NULL){ 35 | Q.push(temp->left); 36 | prev = temp; 37 | rightmost = temp->left; 38 | cnt = 1; 39 | } 40 | if(temp->right != NULL){ 41 | Q.push(temp->right); 42 | prev = temp; 43 | rightmost = temp->right; 44 | cnt = 2; 45 | } 46 | } 47 | swap(target->data, rightmost->data); 48 | if(cnt == 1) 49 | prev->left = NULL; 50 | else 51 | prev->right = NULL; 52 | delete rightmost; 53 | return root; 54 | } 55 | 56 | int main() { 57 | // your code goes here 58 | node *root = add(1); 59 | root->left = add(2); 60 | root->right = add(3); 61 | root->left->left = add(4); 62 | root->left->right = add(5); 63 | inorder(root); 64 | root = removenode(root, 1); 65 | cout< 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node *next; 7 | }; 8 | Node *head=NULL; 9 | void insert_node(); 10 | void pop_node(); 11 | void print_nodes(); 12 | 13 | void insert_node(){ 14 | 15 | cout<<"Enter value to insert"<>temp->data; 18 | temp->next = NULL; 19 | 20 | if(head==NULL){ 21 | head = temp; 22 | } 23 | else{ 24 | temp->next = head; 25 | head = temp; 26 | } 27 | 28 | } 29 | 30 | void print_nodes(){ 31 | 32 | if(head == NULL){ 33 | cout<<"NO Value in the stack"<data<next; 42 | } 43 | } 44 | } 45 | 46 | void pop_node(){ 47 | if(head == NULL){ 48 | cout<<"NO Node to pop"<data<next; 54 | free(del); 55 | } 56 | } 57 | 58 | 59 | int top=-1; 60 | 61 | int main(){ 62 | 63 | int n=0; 64 | 65 | while(n!=4){ 66 | cout<<"Enter 1 to insert value in stack"<>n; 72 | 73 | switch (n) 74 | { 75 | case 1: 76 | insert_node(); 77 | break; 78 | 79 | case 2: 80 | pop_node(); 81 | break; 82 | case 3: 83 | print_nodes(); 84 | break; 85 | 86 | default: 87 | break; 88 | } 89 | 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /Vector/vector_tutorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector insert_last(vector s, int n) 4 | { 5 | s.push_back(n); 6 | return s; 7 | } 8 | 9 | vector delete_last(vector s) 10 | { 11 | s.pop_back(); 12 | return s; 13 | } 14 | 15 | vector insert_beg(vector s, int n) 16 | { 17 | s.insert(s.begin(),n); 18 | return s; 19 | } 20 | 21 | vector delete_beg(vector s) 22 | { 23 | s.erase(s.begin()); 24 | return s; 25 | } 26 | 27 | void print_mid(vector s) 28 | { 29 | int n; 30 | n=s.size(); 31 | if(n%2!=0) 32 | cout< delete_mid(vector s) 38 | { 39 | int n=s.size(); 40 | int a; 41 | a=n/2; 42 | if(n%2!=0) 43 | s.erase(s.begin()+a); 44 | else 45 | s.erase(s.begin()+a-1); //for deleting 1stt mid, to delete second mid use s.begin+a 46 | return s; 47 | } 48 | vector insert_mid(vector s, int m) 49 | { 50 | int n=s.size(); 51 | int a; 52 | a=n/2; 53 | if(n%2!=0) 54 | s.insert(s.begin()+a,m); 55 | else 56 | s.insert(s.begin()+a-1,m); 57 | return s; 58 | } 59 | void print_vector(vector s) 60 | { 61 | int n; 62 | n=s.size(); 63 | for(int i=0; i s; 69 | s=insert_last(s,10); 70 | s=insert_last(s,15); 71 | s=insert_last(s,20); 72 | s=insert_beg(s,5); 73 | print_vector(s); 74 | cout<()); 79 | 80 | 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Pratice/LinkedList/linkedList-insertation-at-specific-node.cpp: -------------------------------------------------------------------------------- 1 | //Inseration at specific potion 2 | 3 | #include 4 | using namespace std; 5 | struct Node{ 6 | int data; 7 | struct Node *next; 8 | }*first = NULL; 9 | 10 | void create(int A[], int n){ 11 | first = new Node; 12 | first->data=A[0]; 13 | struct Node *last, *t; 14 | first->next=NULL; 15 | last = first; 16 | for(int i=1; idata=A[i]; 19 | t->next = NULL; 20 | last->next =t; 21 | last = t; 22 | } 23 | } 24 | 25 | void display(struct Node *p){ 26 | while(p!=NULL){ 27 | cout<data<<" "; 28 | p=p->next; 29 | } 30 | } 31 | 32 | 33 | void insertAfter(struct Node *p, int value, int pos){ 34 | struct Node *t; 35 | int count = 1; 36 | while(p!=NULL){ 37 | if(count == pos){ 38 | t = new Node; 39 | t->data = value; 40 | t->next = p->next; 41 | p->next = t; 42 | } 43 | p=p->next; 44 | count++; 45 | } 46 | } 47 | 48 | 49 | void insertatPos(struct Node *p, int value, int pos){ 50 | struct Node *t; 51 | int count = 1; 52 | while(p!=NULL){ 53 | if(pos == 1){ 54 | t = new Node; 55 | t->data=value; 56 | t->next=first; 57 | first = t; 58 | break; 59 | } 60 | else if(count == pos-1){ 61 | t = new Node; 62 | t->data = value; 63 | t->next = p->next; 64 | p->next = t; 65 | } 66 | p=p->next; 67 | count++; 68 | } 69 | } 70 | 71 | int main(){ 72 | int Array[]={1,2,3,4,5}; 73 | int n = sizeof(Array)/sizeof(Array[0]); 74 | create(Array,n); 75 | display(first); 76 | insertatPos(first,50,1); 77 | insertatPos(first,150,2); 78 | cout< 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node* left, *right; 9 | }; 10 | 11 | void printGivenLevel(node* root, int level); 12 | int height(node* node); 13 | node* newNode(int data); 14 | 15 | void printLevelOrder(node* root) 16 | { 17 | int h = height(root); 18 | int i; 19 | for (i = 1; i <= h; i++) 20 | printGivenLevel(root, i); 21 | } 22 | 23 | 24 | void printGivenLevel(node* root, int level) 25 | { 26 | if (root == NULL) 27 | return; 28 | if (level == 1) 29 | cout << root->data << " "; 30 | else if (level > 1) 31 | { 32 | printGivenLevel(root->left, level-1); 33 | printGivenLevel(root->right, level-1); 34 | } 35 | } 36 | 37 | int height(node* node) 38 | { 39 | if (node == NULL) 40 | return 0; 41 | else 42 | { 43 | 44 | int lheight = height(node->left); 45 | int rheight = height(node->right); 46 | 47 | if (lheight > rheight) 48 | return(lheight + 1); 49 | else return(rheight + 1); 50 | } 51 | } 52 | 53 | node* newNode(int data) 54 | { 55 | node* Node = new node(); 56 | Node->data = data; 57 | Node->left = NULL; 58 | Node->right = NULL; 59 | 60 | return(Node); 61 | } 62 | 63 | /* Driver code*/ 64 | int main() 65 | { 66 | node *root = newNode(1); 67 | root->left = newNode(2); 68 | root->right = newNode(3); 69 | root->left->left = newNode(4); 70 | root->left->right = newNode(5); 71 | 72 | cout << "Level Order traversal of binary tree is \n"; 73 | printLevelOrder(root); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Trees/Children Sum Recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | bool sumproperty(node *root){ 17 | queue Q; 18 | Q.push(root); 19 | while(!Q.empty()){ 20 | node *temp = Q.front(); 21 | Q.pop(); 22 | int sum1 = 0, sum2 = 0; 23 | if(temp->left != NULL){ 24 | sum1 = temp->left->data; 25 | Q.push(temp->left); 26 | } 27 | if(temp->right != NULL){ 28 | sum2 = temp->right->data; 29 | Q.push(temp->right); 30 | } 31 | if((temp->data != sum1 + sum2) and (temp->left != NULL or temp->right != NULL)) 32 | return false; 33 | } 34 | return true; 35 | } 36 | 37 | void inorder(node *root, bool &ans){ 38 | if(root == NULL) 39 | return; 40 | inorder(root->left, ans); 41 | int sum1 = 0, sum2 = 0; 42 | if(root->left != NULL) 43 | sum1 = root->left->data; 44 | if(root->right != NULL) 45 | sum2 = root->right->data; 46 | if((root->data != sum2 + sum1) and (root->left != NULL or root->right != NULL)) 47 | ans = false; 48 | inorder(root->right, ans); 49 | } 50 | 51 | int main() { 52 | // your code goes here 53 | node *root = add(10); 54 | root->left = add(8); 55 | root->right = add(2); 56 | root->left->left = add(3); 57 | root->left->right = add(5); 58 | root->right->right = add(2); 59 | if(sumproperty(root)) 60 | cout<<"TRUE"; 61 | else 62 | cout<<"FALSE"; 63 | bool ans = true; 64 | inorder(root, ans); 65 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | struct node* newNode(int data){ 10 | struct node *temp=new node(); 11 | temp->data=data; 12 | temp->left=NULL; 13 | temp->right=NULL; 14 | 15 | return (temp); 16 | } 17 | 18 | void inorder(struct node* node){ 19 | if (node==NULL) 20 | return; 21 | inorder(node->left); 22 | cout<data<<" "; 23 | inorder(node->right); 24 | } 25 | void preorder(struct node* node){ 26 | if(node==NULL) 27 | return; 28 | cout<data<<" "; 29 | preorder(node->left); 30 | preorder(node->right); 31 | } 32 | void postorder(struct node* node){ 33 | if(node==NULL) 34 | return; 35 | postorder(node->left); 36 | postorder(node->right); 37 | cout<data<<" "; 38 | } 39 | 40 | int sizeoftree(struct node* node){ 41 | if(node==NULL) 42 | return 0; 43 | else 44 | return (sizeoftree(node->left)+1+sizeoftree(node->right)); 45 | } 46 | 47 | int main() 48 | { 49 | struct node *root=newNode(25); 50 | root->left=newNode(15); 51 | root->right=newNode(50); 52 | root->left->left=newNode(10); 53 | root->left->left->left=newNode(4); 54 | root->left->left->right=newNode(12); 55 | root->left->right=newNode(22); 56 | root->left->right->left=newNode(18); 57 | root->left->right->right=newNode(24); 58 | root->right->left=newNode(35); 59 | root->right->left->left=newNode(31); 60 | root->right->left->right=newNode(44); 61 | root->right->right=newNode(70); 62 | root->right->right->left=newNode(66); 63 | root->right->right->right=newNode(90); 64 | cout<<"PreOrder:"<<" "; 65 | preorder(root); 66 | cout< 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | return newnode; 14 | } 15 | 16 | bool siblings(node *root, int first, int second){ 17 | std::queue Q; 18 | int cnt = 0, lvl1 = -1, lvl2 = -1, parent1 = -1, parent2 = -1; 19 | Q.push(root); 20 | Q.push(NULL); 21 | while(Q.size() > 1){ 22 | node *temp = Q.front(); 23 | Q.pop(); 24 | if(temp == NULL){ 25 | cnt++; 26 | Q.push(NULL); 27 | continue; 28 | } 29 | if(temp->left != NULL){ 30 | if(temp->left->data == first){ 31 | lvl1 = cnt; 32 | parent1 = temp->data; 33 | } 34 | else if(temp->left->data == second){ 35 | lvl2 = cnt; 36 | parent2 = temp->data; 37 | } 38 | Q.push(temp->left); 39 | } 40 | if(temp->right != NULL){ 41 | if(temp->right->data == first){ 42 | lvl1 = cnt; 43 | parent1 = temp->data; 44 | } 45 | else if(temp->right->data == second){ 46 | lvl2 = cnt; 47 | parent2 = temp->data; 48 | } 49 | Q.push(temp->right); 50 | } 51 | } 52 | if(lvl1 == lvl2 and parent1 != parent2) 53 | return true; 54 | return false; 55 | } 56 | 57 | int main() { 58 | // your code goes here 59 | node *root = add(6); 60 | root->left = add(3); 61 | root->right = add(5); 62 | root->left->left = add(7); 63 | root->left->right = add(8); 64 | root->right->left = add(1); 65 | root->right->right = add(3); 66 | cout< 5 | using namespace std; 6 | const int MAX=1e5+5; 7 | int32_t main(){ 8 | int t;cin>>t; 9 | while(t--){ 10 | map,int>mp; 11 | vectoradj[MAX]; 12 | int v,e;cin>>v>>e; 13 | int n=v; 14 | for(auto i=0;i>a>>b>>w; 16 | if(mp.count({a,b})) 17 | { 18 | auto itr=mp.find({a,b}); 19 | if(itr->second>w){ 20 | mp[{a,b}]=w; 21 | mp[{b,a}]=w; 22 | } 23 | continue; 24 | } 25 | mp[{a,b}]=w; 26 | mp[{b,a}]=w; 27 | adj[a].pb(b); 28 | adj[b].pb(a); 29 | } 30 | int src; 31 | cin>>src; 32 | int dist[n+1]; 33 | fill(dist+1,dist+n+1,INT_MAX); 34 | int mark[n+1]={0}; 35 | dist[src]=0; 36 | set>s; 37 | s.insert({0,src}); 38 | while(!s.empty()){ 39 | pair front=*s.begin(); 40 | s.erase(s.begin()); 41 | mark[front.second]=1; 42 | int node=front.second; 43 | for(auto p:adj[node]){ 44 | if(mark[p]==0) 45 | { 46 | if(dist[p]>dist[node]+mp[{node,p}]){ 47 | s.erase({dist[p],node}); 48 | dist[p]=dist[node]+mp[{node,p}]; 49 | s.insert({ dist[p] , p}); 50 | } 51 | } 52 | } 53 | } 54 | for(int i=1;i<=n;i++) 55 | { 56 | if(dist[i]==INT_MAX) 57 | { 58 | cout<<-1<<" "; 59 | continue; 60 | } 61 | if(src==i)continue; 62 | cout< 2 | using namespace std; 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | struct node *parent; 8 | }; 9 | 10 | struct node* newNode(int data) 11 | { 12 | struct node *temp = new node(); 13 | temp->data = data; 14 | temp->left = NULL; 15 | temp->right = NULL; 16 | temp->parent = NULL; 17 | 18 | return(temp); 19 | } 20 | 21 | struct node* insert(struct node* node, int data) 22 | { 23 | if (node == NULL) 24 | return(newNode(data)); 25 | else 26 | { 27 | struct node *temp; 28 | if (data <= node->data) 29 | { 30 | temp = insert(node->left, data); 31 | node->left = temp; 32 | temp->parent= node; 33 | } 34 | else 35 | { 36 | temp = insert(node->right, data); 37 | node->right = temp; 38 | temp->parent = node; 39 | } 40 | return node; 41 | } 42 | } 43 | 44 | void spiral(struct node* node) 45 | { 46 | if(node==NULL) return; 47 | 48 | stackq1; 49 | stackq2; 50 | q1.push(node); 51 | while(!q1.empty() || !q2.empty()) 52 | { 53 | while(!q1.empty()){ 54 | struct node *temp=q1.top(); 55 | q1.pop(); 56 | cout<data<<" "; 57 | if (temp->left) 58 | q2.push(temp->left); 59 | if (temp->right) 60 | q2.push(temp->right); 61 | } 62 | while(!q2.empty()){ 63 | struct node *temp=q2.top(); 64 | q2.pop(); 65 | cout<data<<" "; 66 | if (temp->right) 67 | q1.push(temp->right); 68 | if (temp->left) 69 | q1.push(temp->left); 70 | } 71 | } 72 | } 73 | 74 | int main(){ 75 | struct node *root=NULL; 76 | root=insert(root,20); 77 | root=insert(root,10); 78 | root=insert(root,22); 79 | root=insert(root,5); 80 | root=insert(root,15); 81 | root=insert(root,21); 82 | root=insert(root,24); 83 | spiral(root); 84 | 85 | } 86 | -------------------------------------------------------------------------------- /Trees/AVL tree insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data, height; 6 | node *left, *right; 7 | }; 8 | 9 | node* add(int data){ 10 | node *newnode = new node; 11 | newnode->data = data; 12 | newnode->left = newnode->right = NULL; 13 | newnode->height = 1; 14 | return newnode; 15 | } 16 | 17 | int height(node *root){ 18 | if(root == NULL) 19 | return 0; 20 | return root->height; 21 | } 22 | 23 | int getbalance(node *root){ 24 | if(root == NULL) 25 | return 0; 26 | return height(root->left) - height(root->right); 27 | } 28 | 29 | node* leftrotate(node *root){ 30 | node *first = root->right; 31 | node *second = first->left; 32 | first->left = root; 33 | root->right = second; 34 | first->height = 1 + max(height(first->left), height(first->right)); 35 | root->height = 1 + max(height(root->left), height(root->right)); 36 | return first; 37 | } 38 | 39 | node *rightrotate(node *root){ 40 | node *first = root->left; 41 | node *second = first->right; 42 | first->right = root; 43 | root->left = second; 44 | first->height = 1 + max(height(first->left), height(first->right)); 45 | root->height = 1 + max(height(root->left), height(root->right)); 46 | return first; 47 | } 48 | 49 | node* addinavl(node *root, int data){ 50 | if(root == NULL) 51 | return add(data); 52 | if(data < root->data) 53 | root->left = addinavl(root->left, data); 54 | else 55 | root->right = addinavl(root->right, data); 56 | 57 | root->height = 1 + max(height(root->left), height(root->right)); 58 | int balance = getbalance(root); 59 | if(balance > 1 and data < root->left->data) 60 | return rightrotate(root); 61 | if(balance > 1 and data > root->left->data){ 62 | root->left = leftrotate(root->left); 63 | return rightrotate(root); 64 | } 65 | if(balance < -1 and data > root->right->data) 66 | return leftrotate(root); 67 | if(balance < -1 and data < root->right->data){ 68 | root->right = rightrotate(root->right); 69 | return leftrotate(root); 70 | } 71 | return root; 72 | } 73 | 74 | void inorder(node *root){ 75 | if(root == NULL) 76 | return; 77 | inorder(root->left); 78 | cout<data<<" "; 79 | inorder(root->right); 80 | } 81 | 82 | int main(){ 83 | node *root = NULL; 84 | root = addinavl(root, 10); 85 | root = addinavl(root, 20); 86 | root = addinavl(root, 30); 87 | root = addinavl(root, 40); 88 | root = addinavl(root, 50); 89 | root = addinavl(root, 25); 90 | root = addinavl(root, 35); 91 | root = addinavl(root, 5); 92 | root = addinavl(root, 15); 93 | inorder(root); 94 | } --------------------------------------------------------------------------------