├── DSA_LAB_EXAM_Question_Solve ├── MID_SEC_C │ └── Q3.cpp ├── MID_SEC_E_Spring_23 │ ├── Q1.cpp │ └── Q3.cpp ├── CT_2_Section_A │ ├── Q1.cpp │ └── Q2.cpp ├── CT_2_SEC_C │ ├── Q1.cpp │ └── Q2.cpp ├── MID_SEC_B │ └── Q1.cpp ├── CT_1 │ ├── Q2.cpp │ └── Q1.cpp ├── Summer23_Final_ │ ├── Question3.cpp │ └── Question1.cpp └── joyasree_assignment │ └── Question_3_.cpp ├── DSA_TOPICS ├── Tree │ ├── Problems │ │ ├── Diameter_Of_a_tree.cpp │ │ ├── Sum_of_Nodes.cpp │ │ ├── Depth_of_a_tree_.cpp │ │ ├── Count_Leaf_Nodes.cpp │ │ ├── Height_of_a_tree.cpp │ │ └── Count_All_Nodes.cpp │ ├── Binary_Search_Tree_(BST) │ │ ├── Conduct_Tree │ │ │ ├── Conduct_BST_from_preOrder.cpp │ │ │ └── Conduct_Tree_from_Inorder_and_Preorder.cpp │ │ ├── Transplant_in_BST.cpp │ │ ├── Predecessor.cpp │ │ ├── Successor_.cpp │ │ ├── Search_in_BST.cpp │ │ ├── Find_Minimum_and_Maximum_value.cpp │ │ └── Deletion_in_BST.cpp │ ├── Build_Tree.cpp │ ├── Build_tree_from_levelOrder.cpp │ ├── Noman │ │ ├── delete.cpp │ │ └── Insert.cpp │ └── Traversal │ │ ├── Post_Order_Traversal.cpp │ │ ├── Level_Order_traversal_.cpp │ │ ├── In_Order_traversal_.cpp │ │ └── Pre_Order_Traversal_.cpp ├── BinarySearch │ ├── Find_the_higgest_missing_number.cpp │ ├── Count_number_of_occurrences_.cpp │ ├── BinarySearch_Iteratively_NORMAL.cpp │ ├── BinarySearch_Recursively.cpp │ ├── Find_the_smallest_missing_number__.cpp │ ├── Find_first_and_last_occurrence_of _a_Number__normal.cpp │ ├── Count_number_of_occurrences_2.cpp │ └── First_and_Last_occurrence__optimal__GFG.cpp ├── QuickSort │ ├── Quicksort Partition.pdf │ ├── (lecture-2)Quick Sort with Time.pdf │ └── QuickSort.cpp ├── MergeSort │ ├── (lecture-3)Merge Sort Algorithm.pdf │ ├── (lecture-3)Merge Sort time complexity.pdf │ ├── Try.cpp │ └── MergeSort.cpp ├── Insertion Sort │ ├── (lecture-1) Introduction-Counting Sort-Insertion Sort.pdf │ └── InsertionSort.cpp ├── Counting Sort │ ├── (lecture-1) Introduction-Counting Sort-Insertion Sort.pdf - Shortcut.lnk │ └── CountingSort.cpp ├── Stack │ ├── Problems │ │ ├── else.cpp │ │ ├── Next_smaller_Element.cpp │ │ ├── Implement_Stack_using_Queues_.cpp │ │ ├── stack_link_list_parenthesis.cpp │ │ ├── Q5__Evaluation_of_Postfix_Expression_.cpp │ │ ├── Two_Stack_using_One_array.cpp │ │ ├── Reverse_a_stack_using_recursion__.cpp │ │ └── stack_link_list_and_palindrome.cpp │ ├── Stack_Pre_Build_function.cpp │ ├── Using_Array_.cpp │ ├── Using_Struct_and_linked_List.cpp │ ├── Stack_Implementation_Using_Class_and_Linked List.cpp │ └── Stack_Template_Given_CODE_.cpp ├── Application │ ├── renameFile.cpp │ ├── Evaluation_of_Postfix_Expression__.cpp │ └── Infix_To_postfix.cpp ├── Topological_Sort │ ├── else.cpp │ ├── Using_DFS_.cpp │ └── Using_BFS_kahnsAlgo_.cpp ├── Graph │ ├── Learned │ │ ├── DFS.cpp │ │ ├── creat_Graph_using_adjList_.cpp │ │ ├── DFS_using_adjList_.cpp │ │ └── BFS_using_adjList_.cpp │ ├── DFS │ │ └── Bsic.cpp │ ├── Problems │ │ ├── Backtracking__Rat_in_a_Maze.cpp │ │ ├── Lecture40_RatinaMazeProblem.cpp │ │ └── ShortestPath_using_BFS.cpp │ ├── Class_GivenCode │ │ └── maze_DFS.cpp │ ├── BFS │ │ └── BFS.cpp │ └── For_ass │ │ ├── Shakhawat.cpp │ │ ├── Ovi_noman.c │ │ ├── Question2.cpp │ │ └── DFS_given_EDIT.cpp ├── Disjoint_Set │ └── basic.cpp ├── LinearSearch │ └── linearSearch.cpp ├── BubbleSort │ ├── BubbleSort.cpp │ ├── Bubble_sort_optimizedFile.cpp │ └── ReverseBubbleSort.cpp ├── LinkedList │ └── Single_Linked_list │ │ ├── basic.cpp │ │ └── Insertion.cpp ├── SelectionSort │ └── SelectionSort.cpp ├── Queues │ ├── Queues_Using_PreBuild_function.cpp │ ├── queue_link_list_GivenCode.cpp │ ├── Basic │ │ ├── Using_array.cpp │ │ └── Circular_Queue_Using_Array.cpp │ ├── queue_array_GivenCode.cpp │ ├── circular_queue_array__GivenCode.cpp │ └── Problems │ │ └── Implement_Queue_using_two_stack_.cpp ├── Recursion_TOH │ └── Tower_of_hanoi.cpp ├── Heap │ ├── Heapify_Finction_topToBottom_and_bottomToTop.cpp │ ├── Decending_Order_Heap_sort.cpp │ ├── Heap_Sort.cpp │ ├── Max_Heapify_Simulation_TopToBottom_approach.cpp │ └── Heap_Class_Insertion_Deletion_.cpp └── Offline_DSA1_SEC_E_Summer23 │ ├── GitHub.cpp │ └── Warmhole.cpp ├── .gitignore └── Class(LAB) ├── Class 2 ├── InsertionSort.cpp ├── SelectionSort_maam.cpp ├── Q2.cpp ├── Q3.cpp └── LeetCode.cpp ├── Class 1 ├── ReverseBubbleSort.cpp ├── BubbleSort.cpp ├── nth_higgest_element.cpp └── OptimizedBubbleSort.cpp ├── Class 3 ├── LinearSearch │ └── LinearSearch_givenQuestion.cpp └── Binarysearch │ ├── BinarySearch.cpp │ └── binarySearch_givenProblem.cpp └── Class11.cpp /DSA_LAB_EXAM_Question_Solve/MID_SEC_C/Q3.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Problems/Diameter_Of_a_tree.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/Find_the_higgest_missing_number.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Conduct_Tree/Conduct_BST_from_preOrder.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | RemoveexeFile.c 2 | RemoveexeFile.exe 3 | *.exe 4 | *.vscode 5 | *.cph 6 | *tempCodeRunnerFile.cpp -------------------------------------------------------------------------------- /DSA_TOPICS/QuickSort/Quicksort Partition.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/DSA_1_UIU/HEAD/DSA_TOPICS/QuickSort/Quicksort Partition.pdf -------------------------------------------------------------------------------- /DSA_TOPICS/MergeSort/(lecture-3)Merge Sort Algorithm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/DSA_1_UIU/HEAD/DSA_TOPICS/MergeSort/(lecture-3)Merge Sort Algorithm.pdf -------------------------------------------------------------------------------- /DSA_TOPICS/QuickSort/(lecture-2)Quick Sort with Time.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/DSA_1_UIU/HEAD/DSA_TOPICS/QuickSort/(lecture-2)Quick Sort with Time.pdf -------------------------------------------------------------------------------- /DSA_TOPICS/MergeSort/(lecture-3)Merge Sort time complexity.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/DSA_1_UIU/HEAD/DSA_TOPICS/MergeSort/(lecture-3)Merge Sort time complexity.pdf -------------------------------------------------------------------------------- /DSA_TOPICS/Insertion Sort/(lecture-1) Introduction-Counting Sort-Insertion Sort.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/DSA_1_UIU/HEAD/DSA_TOPICS/Insertion Sort/(lecture-1) Introduction-Counting Sort-Insertion Sort.pdf -------------------------------------------------------------------------------- /DSA_TOPICS/Counting Sort/(lecture-1) Introduction-Counting Sort-Insertion Sort.pdf - Shortcut.lnk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/DSA_1_UIU/HEAD/DSA_TOPICS/Counting Sort/(lecture-1) Introduction-Counting Sort-Insertion Sort.pdf - Shortcut.lnk -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/else.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int32_t main() 10 | { 11 | 12 | CRACKED; 13 | } 14 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Conduct_Tree/Conduct_Tree_from_Inorder_and_Preorder.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | int32_t main() 11 | { 12 | 13 | CRACKED; 14 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Stack_Pre_Build_function.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | 6 | #define CRACKED return 0; 7 | #define nl endl; // NewLine 8 | using namespace std; 9 | 10 | int32_t main() 11 | { 12 | stack s; 13 | 14 | s.push(10); 15 | s.push(100); 16 | s.pop(); 17 | s.push(1000); 18 | 19 | cout << s.empty() << nl; 20 | cout << s.top() << nl; 21 | cout << s.size() << nl; 22 | 23 | CRACKED; 24 | } 25 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/Next_smaller_Element.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 13-07-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | using namespace std; 8 | 9 | int smallerElement(stack sp) 10 | { 11 | stack s = sp; 12 | 13 | 14 | 15 | } 16 | 17 | int main() 18 | { 19 | stack s; 20 | s.push(2); 21 | s.push(1); 22 | s.push(4); 23 | s.push(3); 24 | stack ans; 25 | for (int i = 0; i < s.size(); i++) 26 | { 27 | } 28 | 29 | CRACKED; 30 | } 31 | -------------------------------------------------------------------------------- /DSA_TOPICS/Application/renameFile.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | string s; 12 | getline(cin, s); // take line 13 | 14 | for (int i = 0; i < s.length(); i++) 15 | { 16 | if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) 17 | cout << s[i]; 18 | else 19 | cout << '_'; 20 | } 21 | cout << '_'; 22 | cout << '_'; 23 | 24 | cout << nl; 25 | 26 | CRACKED; 27 | } 28 | -------------------------------------------------------------------------------- /DSA_TOPICS/Topological_Sort/else.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | char str1[100] = "This journey is"; 6 | char str2[100] = "beautiful"; 7 | int j; 8 | 9 | strncpy(str1, str2, 8); 10 | // ekhane str 1 er 8 ta char er por 11 | 12 | printf("String 1:%s\n", str1); 13 | // printf("String 2:%s\n", str2); 14 | 15 | for (j = 3; str1[j] != '\0'; j++) 16 | str1[j] = str2[strlen(str1) - j]; 17 | 18 | // printf("String 1:%s\n", str1); 19 | strncat(str2, str1, 3); 20 | printf("String 1:%s\n", str1); 21 | printf("String 2:%s\n", str2); 22 | 23 | str2[j - 2] = '\0'; 24 | printf("String 2: %s\n", str2); 25 | 26 | for (j = 5; j > 2; j--) 27 | { 28 | strrev(str1); 29 | } 30 | printf("Final: %s", str1); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Learned/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define null NULL 4 | #define nl endl 5 | using namespace std; 6 | 7 | int visited[4] = {0}; 8 | int nodes = 4; 9 | int adjMatrix[4][4] = {1, 0, 0, 1, 10 | 0, 1, 1, 0, 11 | 0, 1, 1, 0, 12 | 1, 0, 0, 1}; 13 | 14 | void dfs(int start) // comming 2 15 | { 16 | visited[start] = 1; 17 | printf("%d ", start); 18 | 19 | for (int i = 0; i < nodes; i++) 20 | { 21 | if (adjMatrix[start][i] && !visited[i]) 22 | { 23 | dfs(i); 24 | } 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | int start = 2; 31 | 32 | printf("\nDFS Traversal: "); 33 | dfs(start); 34 | for (start = 0; start < nodes; start++) 35 | dfs(start); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Disjoint_Set/basic.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | int parent[100]; 11 | 12 | // find function 13 | // return it's parent 14 | int find(int a) 15 | { 16 | if (parent[a] == a) 17 | return a; 18 | else 19 | return find(parent[a]); 20 | } 21 | 22 | // Union Function 23 | 24 | void unionSet(int a, int b) 25 | { 26 | int u = parent[a]; 27 | int v = parent[b]; 28 | if (u == v) 29 | cout << "They are in the same set" << nl; 30 | else 31 | parent[b] = a; 32 | } 33 | 34 | int32_t main() 35 | { 36 | // main func 37 | } 38 | -------------------------------------------------------------------------------- /DSA_TOPICS/LinearSearch/linearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | int LinearSearch(int arr[], int len, int num) 15 | { 16 | for (int i = 0; i < len; i++) 17 | if (arr[i] == num) 18 | return i; 19 | return -1; 20 | } 21 | 22 | int main() 23 | { 24 | int arr[] = {-2, 45, 0, 11, -9}; 25 | int len = sizeof(arr) / sizeof(arr[0]); // length 26 | cout << "Given Array : " << nl; 27 | printArray(arr, len); 28 | 29 | cout << "Enter a number from array: "; 30 | int num; 31 | cin >> num; 32 | 33 | int index = LinearSearch(arr, len, num); 34 | 35 | cout << num << " number index is " << index << nl; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /DSA_TOPICS/BubbleSort/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int n) { 5 | for (int i = 0; i < n - 1; ++i) { 6 | for (int j = 0; j < n - 1 - i ; ++j) { 7 | if (arr[j] > arr[j + 1]) { 8 | // Swap arr[j] and arr[j+1] 9 | int temp = arr[j]; 10 | arr[j] = arr[j + 1]; 11 | arr[j + 1] = temp; 12 | } 13 | } 14 | } 15 | } 16 | 17 | void printArray(int arr[], int n) { 18 | for (int i = 0; i < n; ++i) { 19 | cout << arr[i] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | int arr[] = {-2,45,0,11,-9}; 26 | int n = sizeof(arr) / sizeof(arr[0]); 27 | 28 | cout << "Original array: "; 29 | printArray(arr, n); 30 | 31 | bubbleSort(arr, n); 32 | 33 | cout << "Sorted array: "; 34 | printArray(arr, n); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Class(LAB)/Class 2/InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | void InsertionSort(int arr[], int len) 15 | { 16 | for (int i = 1; i < len; i++) 17 | { 18 | int key = arr[i]; 19 | int j = i - 1; 20 | while (j >= 0 && arr[j] > key) 21 | { 22 | arr[j + 1] = arr[j]; 23 | j--; 24 | } 25 | arr[j + 1] = key; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int arr[] = {-2, 45, 0, 11, -9}; 32 | int n = sizeof(arr) / sizeof(arr[0]); // length 33 | cout << "Given Array : " << nl; 34 | printArray(arr, n); 35 | 36 | InsertionSort(arr, n); 37 | 38 | cout << "Sorted Array : " << nl; 39 | printArray(arr, n); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Class(LAB)/Class 1/ReverseBubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | void ReverseBubbleSort(int arr[], int len) 15 | { 16 | for (int i = 0; i < len - 1; i++) 17 | { 18 | for (int j = len - 1; j >= 0 + i; j--) 19 | { 20 | if (arr[j] > arr[j - 1]) 21 | swap(arr[j], arr[j - 1]); 22 | } 23 | } 24 | } 25 | 26 | 27 | int main() 28 | { 29 | int arr[] = {2, 45, 0, 11, 9}; 30 | int n = sizeof(arr) / sizeof(arr[0]); // length 31 | // cout< 2 | using namespace std; 3 | 4 | void printArray(int arr[], int len) 5 | { 6 | for (int i = 0; i < len; i++) 7 | cout << arr[i] << " "; 8 | cout << endl; 9 | } 10 | 11 | void InsertionSort(int arr[], int len) 12 | { 13 | for (int i = 1; i <= len - 1; i++) 14 | { 15 | int j = i - 1; 16 | int key = arr[i]; 17 | while (j >= 0 && arr[j] > key) 18 | { 19 | arr[j + 1] = arr[j]; 20 | j--; 21 | } 22 | arr[j + 1] = key; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int arr[] = {10, 22, 323, 2, 3, 4, 4, 5, 8, 9, 9}; 29 | int len = sizeof(arr) / sizeof(arr[0]); 30 | 31 | cout << "Given Array : "; 32 | printArray(arr, len); 33 | 34 | InsertionSort(arr, sizeof(arr) / sizeof(arr[0])); 35 | 36 | cout << "Sorted Array : "; 37 | printArray(arr, len); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Using_Array_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | int stackk[100]; // LIFO 7 | int top; 8 | 9 | void push(int value) 10 | { 11 | if (top >= 99) 12 | { 13 | cout << "STACK over flow" << nl; 14 | } 15 | else 16 | { 17 | top++; 18 | stackk[top] = value; 19 | } 20 | } 21 | 22 | int pop() 23 | { 24 | int value; 25 | if (top < 0) 26 | { 27 | cout << "Stack under flow" << nl; 28 | return -1; 29 | } 30 | else 31 | { 32 | value = stackk[top]; 33 | top--; 34 | return value; 35 | } 36 | } 37 | 38 | void printStack() 39 | { 40 | for (int i = 0; i <= top; i++) 41 | { 42 | cout << stackk[i] << " "; 43 | } 44 | } 45 | 46 | int main() 47 | { 48 | top = -1; 49 | push(20); 50 | push(1); 51 | push(15); 52 | 53 | pop(); 54 | pop(); 55 | pop(); 56 | pop(); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Class(LAB)/Class 1/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void bubbleSort(int arr[], int n) 6 | { 7 | for (int i = 0; i < n - 1; ++i) 8 | { 9 | for (int j = 0; j < n - i - 1; ++j) 10 | { 11 | if (arr[j] > arr[j + 1]) 12 | { 13 | // Swap arr[j] and arr[j+1] 14 | int temp = arr[j]; 15 | arr[j] = arr[j + 1]; 16 | arr[j + 1] = temp; 17 | } 18 | } 19 | } 20 | } 21 | 22 | void printArray(int arr[], int n) 23 | { 24 | for (int i = 0; i < n; ++i) 25 | { 26 | cout << arr[i] << " "; 27 | } 28 | cout << endl; 29 | } 30 | 31 | int main() 32 | { 33 | int arr[] = {-2, 45, 0, 11, -9}; 34 | int n = sizeof(arr) / sizeof(arr[0]); // length 35 | // cout< 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | // by using structure 10 | typedef struct Node 11 | { 12 | int value; 13 | Node *nextNode; 14 | } Node; 15 | 16 | // by using class 17 | class LinkedNode 18 | { 19 | public: 20 | int value = 0; 21 | LinkedNode *nextNode = NULL; 22 | 23 | // constructor 24 | LinkedNode(int value) 25 | { 26 | this->value = value; 27 | this->nextNode = NULL; 28 | } 29 | 30 | LinkedNode() // default 31 | { 32 | } 33 | }; 34 | 35 | int32_t main() 36 | { 37 | 38 | LinkedNode *node1 = new LinkedNode(); 39 | cout << node1->value << nl; 40 | cout << node1->nextNode << nl; 41 | 42 | LinkedNode *node2 = new LinkedNode(10); 43 | cout << node2->value << nl; 44 | cout << node2->nextNode << nl; 45 | 46 | CRACKED; 47 | } 48 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/Implement_Stack_using_Queues_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Stack 5 | { 6 | queue q1, q2; 7 | 8 | public: 9 | void push(int x) 10 | { 11 | q2.push(x); 12 | 13 | while (!q1.empty()) 14 | { 15 | q2.push(q1.front()); 16 | q1.pop(); 17 | } 18 | 19 | queue q = q1; 20 | q1 = q2; 21 | q2 = q; 22 | } 23 | 24 | void pop() 25 | { 26 | if (q1.empty()) 27 | return; 28 | q1.pop(); 29 | } 30 | 31 | int top() 32 | { 33 | if (q1.empty()) 34 | return -1; 35 | return q1.front(); 36 | } 37 | 38 | int size() { return q1.size(); } 39 | }; 40 | 41 | int main() 42 | { 43 | Stack s; 44 | s.push(1); 45 | s.push(2); 46 | s.push(3); 47 | 48 | cout << "current size: " << s.size() << endl; 49 | cout << s.top() << endl; 50 | s.pop(); 51 | cout << s.top() << endl; 52 | s.pop(); 53 | cout << s.top() << endl; 54 | 55 | cout << "current size: " << s.size() << endl; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /DSA_TOPICS/SelectionSort/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // out 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | void SelectionSort(int arr[], int len) 13 | { 14 | int index; 15 | 16 | for (int round = 0; round < len - 1; round++) 17 | { 18 | int min = round; 19 | 20 | for (int i = round + 1; i < len; i++) 21 | { 22 | if (arr[i] < arr[min]) 23 | { 24 | min = i; 25 | } 26 | } 27 | 28 | // swapping 29 | int temp = arr[round]; 30 | arr[round] = arr[min]; 31 | arr[min] = temp; 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | int arr[] = {22, 32, 6, -2, 2, 3, 9, 10}; 38 | int len = sizeof(arr) / sizeof(arr[0]); 39 | cout << "Given Array : "; 40 | printArray(arr, len); 41 | 42 | SelectionSort(arr, len); 43 | 44 | cout << "Sorted Array : "; 45 | printArray(arr, len); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/DFS/Bsic.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | vector graph[100]; 11 | int visited[100]; 12 | 13 | void dfs(int source) 14 | { 15 | visited[source] = 1; 16 | for (int i = 0; i < graph[source].size(); i++) 17 | { 18 | if (visited[graph[source][i]] == 0) 19 | { 20 | dfs(visited[graph[source][i]]); 21 | } 22 | } 23 | } 24 | 25 | int32_t main() 26 | { 27 | int nodes, edges; 28 | cout << "Enter nodes and edge number: "; 29 | cin >> nodes >> edges; 30 | 31 | cout << "Enter edges from u to v: "; 32 | for (int i = 0; i < edges; i++) 33 | { 34 | int u, v; 35 | cin >> u >> v; 36 | graph[u].push_back(v); 37 | } 38 | 39 | for (int i = 0; i <= nodes; i++) 40 | { 41 | if (visited[i] == 0) 42 | { 43 | dfs(i); 44 | } 45 | } 46 | 47 | CRACKED; 48 | } 49 | -------------------------------------------------------------------------------- /Class(LAB)/Class 3/LinearSearch/LinearSearch_givenQuestion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | int LinearSearch(int arr[], int len, int num) 15 | { 16 | for (int i = 0; i < len; i++) 17 | if (arr[i] == num) 18 | return i; 19 | return -1; 20 | } 21 | 22 | // Question 23 | // find the elemnt index if not exist then 24 | // find the index of the nearest largest number 25 | 26 | int main() 27 | { 28 | int arr[] = {-2, 45, 0, 11, -9}; 29 | int n = sizeof(arr) / sizeof(arr[0]); // length 30 | cout << "Given Array : " << nl; 31 | printArray(arr, n); 32 | 33 | cout << "Enter a number from array: "; 34 | int num; 35 | cin >> num; 36 | 37 | int index = LinearSearch(arr, n, num); 38 | 39 | int sum = num; 40 | 41 | for (int i = index + 1; i <= index + 3; i++) 42 | { 43 | if (i >= n) 44 | continue; 45 | sum += arr[i]; 46 | } 47 | 48 | cout << "Sum " << sum << nl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/Count_number_of_occurrences_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int arr[], int l, int r, int x) 5 | { 6 | if (r < l) 7 | return -1; 8 | 9 | int mid = l + (r - l) / 2; 10 | 11 | if (arr[mid] == x) 12 | return mid; 13 | 14 | if (arr[mid] > x) 15 | return binarySearch(arr, l, mid - 1, x); 16 | else 17 | return binarySearch(arr, mid + 1, r, x); 18 | } 19 | 20 | int countOccurrences(int arr[], int n, int x) 21 | { 22 | int ind = binarySearch(arr, 0, n - 1, x); 23 | 24 | if (ind == -1) 25 | return 0; 26 | 27 | int count = 1; 28 | int left = ind - 1; 29 | while (left >= 0 && arr[left] == x) 30 | count++, left--; 31 | 32 | int right = ind + 1; 33 | while (right < n && arr[right] == x) 34 | count++, right++; 35 | 36 | return count; 37 | } 38 | 39 | int main() 40 | { 41 | int arr[] = {1, 2, 2, 2, 2, 3, 4, 7, 8, 8}; 42 | int n = sizeof(arr) / sizeof(arr[0]); 43 | int value; 44 | cout << "enter a value forom array" << endl; 45 | int x = 2; 46 | cin >> x; 47 | cout << countOccurrences(arr, n, x); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Class(LAB)/Class 2/SelectionSort_maam.cpp: -------------------------------------------------------------------------------- 1 | // Selection sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | void printArray(int array[], int size) 7 | { 8 | for (int i = 0; i < size; i++) 9 | { 10 | cout << array[i] << " "; 11 | } 12 | cout << endl; 13 | } 14 | 15 | void selectionSort(int array[], int size) 16 | { 17 | for (int step = 0; step < size - 1; step++) 18 | { 19 | int min_idx = step; 20 | for (int i = step + 1; i < size; i++) 21 | { 22 | // To sort in descending order, change > to < in this line. 23 | // Select the minimum element in each loop. 24 | 25 | if (array[i] < array[min_idx]) // acending 26 | // if (array[i] > array[min_idx]) // decending order 27 | min_idx = i; 28 | } 29 | 30 | // put min at the correct position 31 | int temp = array[min_idx]; 32 | array[min_idx] = array[step]; 33 | array[step] = temp; 34 | } 35 | } 36 | 37 | // driver code 38 | int main() 39 | { 40 | int data[] = {20, 12, 10, 15, 2}; 41 | int size = sizeof(data) / sizeof(data[0]); 42 | 43 | cout << "Given Array :\n"; 44 | printArray(data, size); 45 | 46 | selectionSort(data, size); 47 | 48 | cout << "Sorted array in Acsending Order:\n"; 49 | printArray(data, size); 50 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/Queues_Using_PreBuild_function.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int32_t main() 10 | { 11 | queue que; 12 | que.push(10); 13 | que.push(100); 14 | que.push(1000); 15 | 16 | cout << "Queue Front " << que.front() << nl; 17 | cout << "Queue size " << que.size() << nl; 18 | cout << nl; 19 | 20 | if (que.empty()) 21 | { 22 | cout << "This queue is empty" << nl; 23 | } 24 | else 25 | { 26 | cout << "This queue is not empty" << nl; 27 | } 28 | 29 | que.pop(); 30 | cout << "Queue size " << que.size() << nl; 31 | cout << "Queue Front " << que.front() << nl; 32 | cout << nl; 33 | 34 | que.pop(); 35 | que.pop(); 36 | cout << "Queue size " << que.size() << nl; 37 | cout << "Queue Front " << que.front() << nl; 38 | if (que.empty()) 39 | { 40 | cout << "This queue is empty" << nl; 41 | } 42 | cout << nl; 43 | 44 | CRACKED; 45 | } -------------------------------------------------------------------------------- /DSA_TOPICS/BubbleSort/Bubble_sort_optimizedFile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int n) 5 | { 6 | for (int i = 0; i < n - 1; i++) 7 | { 8 | int swapped = 0; 9 | 10 | for (int j = 0; j < n - 1 - i; j++) 11 | { 12 | if (arr[j] > arr[j + 1]) 13 | { 14 | int temp = arr[j]; 15 | arr[j] = arr[j + 1]; 16 | arr[j + 1] = temp; 17 | swapped = 1; 18 | } 19 | } 20 | if (swapped == 0) 21 | break; 22 | } 23 | return; 24 | } 25 | 26 | void printArray(int arr[], int n) 27 | { 28 | for (int i = 0; i < n; ++i) 29 | { 30 | cout << arr[i] << " "; 31 | } 32 | cout << endl; 33 | } 34 | 35 | // void printArray(int arr[]) 36 | // { 37 | // for (auto i : arr) 38 | // { 39 | // cout << i << " "; 40 | // } 41 | // cout << endl; 42 | // } 43 | 44 | int main() 45 | { 46 | int arr[] = {1, 2, 3, 5, 4, 6}; 47 | int n = sizeof(arr) / sizeof(arr[0]); 48 | 49 | cout << "Original array: "; 50 | printArray(arr, n); 51 | 52 | bubbleSort(arr, n); 53 | 54 | cout << "Sorted array: "; 55 | printArray(arr, n); 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Recursion_TOH/Tower_of_hanoi.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define output(x) cout << x << nl // out 13 | #define printarray(arr, len) \ 14 | for (int i = 0; i < len; i++) \ 15 | { \ 16 | cout << arr[i] << " "; \ 17 | if (i + 1 == len) \ 18 | cout << endl; \ 19 | } // array print 20 | using namespace std; 21 | int cnt = 0; 22 | 23 | // src aux des 24 | void TOH(int n, int a, int b, int c) 25 | { 26 | if (n > 0) 27 | { 28 | TOH(n - 1, a, c, b); // n src des aux 29 | cout << a << " to " << c << nl; // print 30 | cnt++; 31 | TOH(n - 1, b, a, c); // n aux src des 32 | } 33 | } 34 | 35 | int32_t main() 36 | { 37 | faster; 38 | 39 | TOH(3, 1, 2, 3); 40 | // disks--from--using--to 41 | cout << cnt << nl; 42 | 43 | CRACKED; 44 | } 45 | -------------------------------------------------------------------------------- /Class(LAB)/Class 1/nth_higgest_element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | int bubbleSort(int arr[], int n, int higg) 6 | { 7 | int ans = -1; 8 | 9 | // for (int i = 0; i < n - 1; ++i) 10 | for (int i = 0; i < higg; ++i) 11 | { 12 | for (int j = 0; j < n - i - 1; ++j) 13 | { 14 | if (arr[j] > arr[j + 1]) 15 | { 16 | int temp = arr[j]; 17 | arr[j] = arr[j + 1]; 18 | arr[j + 1] = temp; 19 | ans = arr[j + 1]; 20 | } 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | 27 | void printArray(int arr[], int n) 28 | { 29 | for (int i = 0; i < n; ++i) 30 | { 31 | cout << arr[i] << " "; 32 | } 33 | cout << endl; 34 | } 35 | 36 | int main() 37 | { 38 | int arr[] = {-2, 45, 0, 11, -9}; 39 | int len = sizeof(arr) / sizeof(arr[0]); // length 40 | // cout< 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // out 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | 13 | int BinarySearch(int arr[], int len, int key) 14 | { 15 | int start = 0; 16 | int end = len - 1; 17 | 18 | while (start <= end) 19 | { 20 | int mid = start + ((end - start) / 2); 21 | // int mid = ( start + end ) / 2); 22 | 23 | if (arr[mid] == key) 24 | { 25 | return mid; 26 | } 27 | else if (arr[mid] < key) 28 | { 29 | start = mid + 1; 30 | } 31 | else if (arr[mid] > key) 32 | { 33 | end = mid - 1; 34 | } 35 | } 36 | return -1; 37 | } 38 | 39 | int main() 40 | { 41 | int arr[] = {2, 3, 9, 10, 22, 32}; 42 | int len = sizeof(arr) / sizeof(arr[0]); 43 | 44 | int key = 9; 45 | 46 | cout << "Given array : "; 47 | printArray(arr, len); 48 | 49 | int index = BinarySearch(arr, len, key); 50 | cout << "index of key " << key << " is " << index << nl; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Build_Tree.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | int32_t main() 46 | { 47 | node *root = NULL; 48 | 49 | // creating tree 50 | root = buildTree(root); 51 | 52 | // input 53 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 54 | 55 | CRACKED; 56 | } 57 | -------------------------------------------------------------------------------- /Class(LAB)/Class 1/OptimizedBubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int n) 5 | { 6 | int count = 0; 7 | int loop = 0; 8 | for (int i = 0; i < n - 1; i++) 9 | { 10 | int swapped = 0; 11 | 12 | for (int j = 0; j < n - 1 - i; j++) 13 | { 14 | if (arr[j] > arr[j + 1]) 15 | { 16 | int temp = arr[j]; 17 | arr[j] = arr[j + 1]; 18 | arr[j + 1] = temp; 19 | count++; 20 | swapped = 1; 21 | } 22 | } 23 | 24 | if (swapped == 0) 25 | { 26 | break; 27 | } 28 | loop++; 29 | } 30 | cout << "The numbers of swapping: " << count << endl; 31 | cout << "The numbers of loop running: " << loop << endl; 32 | } 33 | 34 | void printArray(int arr[], int n) 35 | { 36 | for (int i = 0; i < n; ++i) 37 | { 38 | cout << arr[i] << " "; 39 | } 40 | cout << endl; 41 | } 42 | 43 | int main() 44 | { 45 | int arr[] = {1, 2, 3, 5, 4, 6}; 46 | int n = sizeof(arr) / sizeof(arr[0]); 47 | 48 | cout << "Original array: "; 49 | printArray(arr, n); 50 | 51 | bubbleSort(arr, n); 52 | 53 | cout << "Sorted array: "; 54 | printArray(arr, n); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Build_tree_from_levelOrder.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | int32_t main() 46 | { 47 | node *root = NULL; 48 | 49 | // creating tree 50 | root = buildTree(root); 51 | 52 | // input: 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 53 | 54 | cout << nl; 55 | 56 | CRACKED; 57 | } 58 | -------------------------------------------------------------------------------- /Class(LAB)/Class 3/Binarysearch/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // out 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | 13 | int BinarySearch(int arr[], int len, int key) 14 | { 15 | int start = 0; 16 | int last = len - 1; 17 | 18 | while (start <= last) 19 | { 20 | // int mid = (start + last) / 2; 21 | int mid = start + ((last - start) / 2); 22 | if (arr[mid] == key) 23 | { 24 | return mid; 25 | } 26 | else if (arr[mid] < key) 27 | { 28 | start = mid + 1; 29 | } 30 | else if (arr[mid] > key) 31 | { 32 | last = mid - 1; 33 | } 34 | } 35 | return -1; 36 | } 37 | 38 | int main() 39 | { 40 | int arr[] = {2, 3, 9, 10, 22, 32}; 41 | int len = sizeof(arr) / sizeof(arr[0]); 42 | cout << "Given Array : " << nl; 43 | printArray(arr, len); 44 | cout << "Enter a number from array : "; 45 | 46 | int key; 47 | cin >> key; 48 | 49 | int index = BinarySearch(arr, len, key); 50 | 51 | cout << "index of key " << key << " is " << index << nl; 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Problems/Backtracking__Rat_in_a_Maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl // NewLine 4 | #define null NULL 5 | using namespace std; 6 | int maze[5][5] = {0, 1, 0, 0, 2, 7 | 0, 0, 0, 1, 0, 8 | 0, 3, 1, 0, 0, 9 | 0, 1, 0, 1, 0, 10 | 0, 0, 0, 0, 0}; 11 | // int visited[5][5] = {0}; 12 | int solution[5][5] = {0}; 13 | int n = 5; 14 | bool isSafe(int r, int c) 15 | { 16 | if (r < n && c << n && maze[r][c] == 0) 17 | { 18 | return true; 19 | } 20 | else 21 | return false; 22 | } 23 | bool ratinMaze(int row, int col) 24 | { 25 | if (maze[row][col] == 2) 26 | { 27 | solution[row][col] = 1; 28 | return true; 29 | } 30 | 31 | if (isSafe(row, col)) 32 | { 33 | solution[row][col] = 1; 34 | if (ratinMaze(row + 1, col)) 35 | { 36 | return true; 37 | } 38 | if (ratinMaze(row, col + 1)) 39 | { 40 | return true; 41 | } 42 | solution[row][col] = 0; 43 | return false; 44 | } 45 | return false; 46 | } 47 | 48 | int32_t main() 49 | { 50 | 51 | if (ratinMaze(1, 0)) 52 | { 53 | cout << "YES" << nl; 54 | } 55 | else 56 | cout << "No" << nl; 57 | CRACKED; 58 | } 59 | -------------------------------------------------------------------------------- /Class(LAB)/Class 3/Binarysearch/binarySearch_givenProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | int BinarySearch(int arr[], int len, int key) 15 | { 16 | int start = 0; 17 | int last = len - 1; 18 | int ans = -1; 19 | while (start <= last) 20 | { 21 | // int mid = (start + last) / 2; 22 | int mid = start + ((last - start) / 2); 23 | 24 | if (key == arr[mid]) 25 | { 26 | return mid; 27 | } 28 | else if (key < arr[mid]) 29 | { 30 | ans = mid; 31 | last = mid - 1; 32 | } 33 | else 34 | { 35 | start = mid + 1; 36 | } 37 | } 38 | return ans; 39 | } 40 | 41 | // Question 42 | // find the elemnt index if not exist then 43 | // find the index of the nearest largest number 44 | 45 | int main() 46 | { 47 | int arr[] = {-2, 0, 11, 17, 17, 90}; 48 | int n = sizeof(arr) / sizeof(arr[0]); // length 49 | cout << "Given Array : " << nl; 50 | printArray(arr, n); 51 | 52 | int key = 10; 53 | 54 | int ans = BinarySearch(arr, n, key); 55 | 56 | cout << "Ans " << ans << nl; 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /DSA_TOPICS/QuickSort/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int len) 5 | { 6 | for (int i = 0; i < len; i++) 7 | cout << arr[i] << " "; 8 | cout << endl; 9 | } 10 | 11 | int partition(int arr[], int s, int e) 12 | { 13 | int pivot = arr[e]; // select the right-most element as pivot 14 | int i = s - 1; 15 | int j = s; 16 | 17 | while (j < e) 18 | { 19 | if (arr[j] <= pivot) 20 | { 21 | i++; 22 | swap(arr[i], arr[j]); 23 | } 24 | j++; 25 | } 26 | 27 | swap(arr[i + 1], arr[e]); 28 | 29 | return (i + 1); 30 | } 31 | 32 | void quickSort(int arr[], int s, int e) 33 | { 34 | if (s >= e) 35 | return; // base case 36 | 37 | int p = partition(arr, s, e); 38 | 39 | // sort left elements 40 | quickSort(arr, s, p - 1); 41 | 42 | // sort right elements 43 | quickSort(arr, p + 1, e); 44 | } 45 | 46 | int main() 47 | { 48 | int arr[] = {3, 7, 0, 1, 5, 8, 3, 2, 34, 66, 87, 23, 12, 12, 12}; 49 | // int arr[] = {22, 32, 6, -2, 2, 3, 9, 10}; 50 | 51 | int len = sizeof(arr) / sizeof(arr[0]); 52 | 53 | cout << "Given Array : "; 54 | printArray(arr, len); 55 | 56 | quickSort(arr, 0, len - 1); 57 | 58 | cout << "Sorted Array : "; 59 | printArray(arr, len); 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/MID_SEC_E_Spring_23/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | int BinarySearch(int arr[], int len, int key) 15 | { 16 | key = sqrt(key); 17 | int start = 0; 18 | int end = len - 1; 19 | 20 | while (start <= end) 21 | { 22 | int mid = start + ((end - start) / 2); 23 | // int mid = ( start + end ) / 2); 24 | 25 | if (arr[mid] == key) 26 | { 27 | return mid; 28 | } 29 | else if (arr[mid] < key) 30 | { 31 | start = mid + 1; 32 | } 33 | else if (arr[mid] > key) 34 | { 35 | end = mid - 1; 36 | } 37 | } 38 | return -1; 39 | } 40 | int main() 41 | { 42 | int n = 700; 43 | int arr[n]; 44 | for (int i = 0; i < 700; i++) 45 | { 46 | arr[i] = i; 47 | } 48 | 49 | cout << "ENTER A NUMBER " << nl; 50 | int number; 51 | cin >> number; 52 | 53 | int output = BinarySearch(arr, n, number); 54 | 55 | cout << "ANS " << output << nl; 56 | cout << "ANS " << arr[output] << nl; 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Class_GivenCode/maze_DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define null NULL 4 | #define nl endl 5 | using namespace std; 6 | 7 | // int maze[4][5] = {0, 1, 1, 0, 2, 8 | // 0, 1, 1, 0, 1, 9 | // 0, 1, 1, 0, 1, 10 | // 0, 0, 0, 0, 1}; 11 | 12 | int maze[4][5] = {0, 1, 1, 0, 2, 13 | 0, 0, 0, 0, 1, 14 | 0, 1, 1, 0, 1, 15 | 0, 0, 0, 0, 1}; 16 | 17 | int visited[4][5] = {0}; 18 | 19 | void dfs(int row, int col) 20 | { 21 | // base case 22 | if (row < 0 || row > 3 || col < 0 || col > 4) 23 | return; 24 | 25 | // output 26 | cout << "(" << row << " " << col << ")" << endl; 27 | 28 | if (maze[row][col] == 2) 29 | cout << "We are in the exit" << endl; 30 | 31 | visited[row][col] = 1; 32 | if (maze[row][col + 1] != 1 && visited[row][col + 1] == 0) 33 | { 34 | dfs(row, col + 1); 35 | } 36 | if (maze[row][col - 1] != 1 && visited[row][col - 1] == 0) 37 | { 38 | dfs(row, col - 1); 39 | } 40 | if (maze[row + 1][col] != 1 && visited[row + 1][col] == 0) 41 | { 42 | dfs(row + 1, col); 43 | } 44 | if (maze[row - 1][col] != 1 && visited[row - 1][col] == 0) 45 | { 46 | dfs(row - 1, col); 47 | } 48 | visited[row][col] = 0; 49 | } 50 | 51 | int main() 52 | { 53 | dfs(3, 0); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Class(LAB)/Class11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | int arr[4][5] = { 5 | 0, 1, 1, 0, 2, 6 | 0, 1, 1, 0, 1, 7 | 0, 1, 1, 0, 1, 8 | 0, 0, 0, 0, 1}; 9 | 10 | int visited[4][5] = {0}; 11 | string str = "(3 , 0), "; 12 | void dfs(int row, int col) 13 | { 14 | 15 | if (row < 0 || row > 3 || col < 0 || col > 4) 16 | { 17 | str = "(3 , 0), "; 18 | return; 19 | } 20 | 21 | if (visited[row][col] == 0) 22 | { 23 | visited[row][col] = 1; 24 | cout << "Row " << row << " column " << col << nl; 25 | str += "( " + to_string(row) + " , " + to_string(col) + " ), "; 26 | } 27 | else 28 | { 29 | str = "(3 , 0), "; 30 | return; 31 | } 32 | if (arr[row][col] == 2) 33 | cout << str << nl; 34 | 35 | if (arr[row + 1][col] != 1 && visited[row + 1][col] == 0) 36 | { 37 | dfs(row + 1, col); 38 | } 39 | 40 | if (arr[row - 1][col] != 1 && visited[row - 1][col] == 0) 41 | { 42 | dfs(row - 1, col); 43 | } 44 | 45 | if (arr[row][col + 1] != 1 && visited[row][col + 1] == 0) 46 | { 47 | dfs(row, col + 1); 48 | } 49 | 50 | if (arr[row][col - 1] != 1 && visited[row][col - 1] == 0) 51 | { 52 | dfs(row, col - 1); 53 | } 54 | 55 | visited[row][col] == 0; 56 | // str = "(3 , 0), "; 57 | } 58 | 59 | int main() 60 | { 61 | dfs(3, 0); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Learned/creat_Graph_using_adjList_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class graph 11 | { 12 | public: 13 | unordered_map> adj; 14 | void addEdge(int u, int v, int direction) 15 | { 16 | // dirrection == 1 -> directed 17 | // dirrection == 0 -> Undirected 18 | adj[u].push_back(v); 19 | if (direction) 20 | { 21 | adj[v].push_back(u); 22 | } 23 | } 24 | 25 | void printAdjList() 26 | { 27 | for (auto i : adj) 28 | { 29 | cout << i.first << " -> "; 30 | for (auto j : i.second) 31 | { 32 | cout << j << ", "; 33 | } 34 | cout << nl; 35 | } 36 | } 37 | }; 38 | 39 | int32_t main() 40 | { 41 | int n; 42 | cout << "Enter number of nodes: "; 43 | cin >> n; 44 | 45 | int m; 46 | cout << "Enter number of edges: "; 47 | cin >> m; 48 | graph g; 49 | for (int i = 0; i < m; i++) 50 | { 51 | int u, v; 52 | cin >> u >> v; 53 | // creating undirected egde 54 | g.addEdge(u, v, 0); 55 | } 56 | 57 | g.printAdjList(); 58 | 59 | CRACKED; 60 | } 61 | -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/BinarySearch_Recursively.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // out 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | 13 | int BinarySearch(int arr[], int len, int start, int end, int key) 14 | { 15 | if (start <= end) 16 | { 17 | int mid = start + ((end - start) / 2); 18 | 19 | if (arr[mid] == key) 20 | { 21 | return mid; 22 | } 23 | else if (arr[mid] < key) 24 | { 25 | start = mid + 1; 26 | return BinarySearch(arr, len, start, end, key); 27 | } 28 | else if (arr[mid] > key) 29 | { 30 | end = mid - 1; 31 | return BinarySearch(arr, len, start, end, key); 32 | } 33 | } 34 | else if (start > end) // base case 35 | { 36 | return -1; 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | int arr[] = {2, 3, 9, 10, 22, 32}; 43 | int len = sizeof(arr) / sizeof(arr[0]); 44 | 45 | cout << "Given Array : " << nl; 46 | printArray(arr, len); 47 | cout << "Enter a number from array : "; 48 | 49 | int key; 50 | cin >> key; 51 | 52 | int index = BinarySearch(arr, len, 0, len - 1, key); 53 | 54 | cout << "index of key " << key << " is " << index << nl; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Class(LAB)/Class 2/Q2.cpp: -------------------------------------------------------------------------------- 1 | // Selection sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | void printArray(int array[], int size) 7 | { 8 | for (int i = 0; i < size; i++) 9 | { 10 | cout << array[i] << " "; 11 | } 12 | cout << endl; 13 | } 14 | 15 | void selectionSort(int array[], int size) 16 | { 17 | for (int step = 0; step < size - 1; step++) 18 | { 19 | int min_idx = step; 20 | for (int i = step + 1; i < size; i++) 21 | { 22 | 23 | // To sort in descending order, change > to < in this line. 24 | // Select the minimum element in each loop. 25 | if (array[i] < array[min_idx]) 26 | min_idx = i; 27 | } 28 | 29 | // put min at the correct position 30 | int temp = array[min_idx]; 31 | array[min_idx] = array[step]; 32 | array[step] = temp; 33 | } 34 | } 35 | 36 | // driver code 37 | int main() 38 | { 39 | 40 | int size; 41 | cin >> size; 42 | 43 | int data[size]; 44 | 45 | for (int i = 0; i < size; i++) 46 | { 47 | cin >> data[i]; 48 | } 49 | 50 | printArray(data, size); 51 | selectionSort(data, size); 52 | cout << "Sorted array in Acsending Order:\n"; 53 | printArray(data, size); 54 | 55 | if (size % 2 == 0) 56 | { 57 | int m = (data[size / 2] + data[size / 2 -1] )/2; 58 | cout << m << endl; 59 | } 60 | else 61 | cout << data[size / 2] << endl; 62 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Heap/Heapify_Finction_topToBottom_and_bottomToTop.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | // bottom to top approach for deletion 10 | // check children with its parent 11 | void maxHeapify_bottom_to_top(int arr[], int size, int index) // for insertion 12 | { 13 | int parent = index / 2; 14 | 15 | if (parent >= 1 && arr[parent] < arr[index]) 16 | { 17 | swap(arr[index], arr[parent]); 18 | maxHeapify_bottom_to_top(arr, size, parent); 19 | } 20 | else 21 | return; 22 | } 23 | 24 | // top to bottom approach for deletion 25 | // check parent with its children 26 | void maxheapify_top_to_bottom(int arr[], int size, int index) 27 | { 28 | int leftChild = index * 2; 29 | int rightChild = index * 2 + 1; 30 | 31 | int largest = index; // parent 32 | 33 | if (leftChild <= size && arr[largest] < arr[leftChild]) 34 | { 35 | largest = leftChild; 36 | } 37 | 38 | if (rightChild <= size && arr[largest] < arr[rightChild]) 39 | { 40 | largest = rightChild; 41 | } 42 | 43 | if (largest != index) 44 | { 45 | swap(arr[largest], arr[index]); 46 | maxheapify_top_to_bottom(arr, size, largest); 47 | } 48 | } 49 | 50 | int32_t main() 51 | { 52 | CRACKED; 53 | } 54 | -------------------------------------------------------------------------------- /DSA_TOPICS/Counting Sort/CountingSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // out 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | 13 | void countingSort(int a[], int len) 14 | { 15 | int higg = a[0]; 16 | for (int i = 1; i < len; i++) 17 | { 18 | higg = max(a[i], higg); 19 | } 20 | 21 | int count[higg + 1] = {0}; // new array 22 | 23 | for (int i = 0; i < len; i++) 24 | { 25 | count[a[i]]++; 26 | } 27 | 28 | for (int i = 1; i < higg + 1; i++) 29 | { 30 | count[i] += count[i - 1]; // prefix sum [it will give us the index no] 31 | } 32 | 33 | // printArray(count, higg + 1); 34 | 35 | int b[len]; // new array for sorted elements 36 | 37 | for (int i = len - 1; i >= 0; i--) 38 | { 39 | b[count[a[i]] - 1] = a[i]; 40 | count[a[i]]--; 41 | } 42 | 43 | // 0 1 2 5 7 7 8 8 8 8 8 8 8 8 9 44 | // 1 2 3 3 3 4 4 6 14 45 | 46 | for (int i = 0; i < len; i++) 47 | { 48 | a[i] = b[i]; // just copy to te real array 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | int a[] = {14, 3, 2, 3, 4, 1, 6, 4, 3}; 55 | int len = sizeof(a) / sizeof(a[0]); 56 | 57 | output("Given Array: "); 58 | printArray(a, len); 59 | 60 | countingSort(a, len); 61 | 62 | output("Sorted Array: "); 63 | printArray(a, len); 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Learned/DFS_using_adjList_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | void prepareAdjList(unordered_map> &adjList, vector> &edge) 10 | { 11 | for (auto i : edge) 12 | { 13 | adjList[i.first].push_back(i.second); 14 | adjList[i.second].push_back(i.first); 15 | } 16 | } 17 | void dfs(int node, unordered_map &visited, unordered_map> &adjList, vector &component) 18 | { 19 | component.push_back(node); 20 | visited[node] = 1; 21 | 22 | for (auto i : adjList[node]) 23 | { 24 | if (!visited[i]) 25 | dfs(i, visited, adjList, component); 26 | } 27 | } 28 | 29 | vector DFS(int vertex, int e, vector> edge) 30 | { 31 | unordered_map> adjList; // make adj list 32 | prepareAdjList(adjList, edge); 33 | 34 | vector> ans; // store dfs 35 | 36 | unordered_map visited; // coloring 37 | 38 | // for all nodes 39 | for (int i = 0; i < vertex; i++) 40 | { 41 | if (!visited[i]) 42 | { 43 | vector component; 44 | dfs(i, visited, adjList, component); 45 | ans.push_back(component); 46 | } 47 | } 48 | } 49 | 50 | int32_t main() 51 | { 52 | CRACKED; 53 | } 54 | -------------------------------------------------------------------------------- /DSA_TOPICS/BubbleSort/ReverseBubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // void ReverseBubbleSort(int arr[], int len) // Type 1 works 5 | // { 6 | // for (int i = 0; i < len - 1; i++) 7 | // { 8 | // bool flag = false; 9 | // for (int j = len - 1; j > i; j--) 10 | // { 11 | // if (arr[j] > arr[j - 1]) 12 | // { 13 | // flag = "true"; 14 | // int temp = arr[j]; 15 | // arr[j] = arr[j - 1]; 16 | // arr[j - 1] = temp; 17 | // } 18 | // } 19 | // if (!flag) 20 | // { 21 | // break; 22 | // } 23 | // } 24 | // } 25 | 26 | void ReverseBubbleSort(int arr[], int len) // Type 2 works 27 | { 28 | for (int i = 0; i < len - 1; i++) 29 | { 30 | for (int j = i + 1; j < len; j++) 31 | { 32 | if (arr[i] < arr[j]) 33 | { 34 | int temp = arr[i]; 35 | arr[i] = arr[j]; 36 | arr[j] = temp; 37 | } 38 | } 39 | } 40 | } 41 | 42 | void PrintArray(int arr[], int len) 43 | { 44 | for (int i = 0; i < len; i++) 45 | { 46 | cout << arr[i] << " "; 47 | } 48 | cout << endl; 49 | } 50 | 51 | int main() 52 | { 53 | int arr[] = {12, 31, 3, 43, 23, 0, 3}; 54 | // int arr[] = {-2, 45, 0, 11, -9}; 55 | int len = sizeof(arr) / sizeof(arr[0]); 56 | 57 | cout << "Given array: "; 58 | PrintArray(arr, len); 59 | 60 | ReverseBubbleSort(arr, len); 61 | 62 | cout << "Sorted array: "; 63 | PrintArray(arr, len); 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/CT_2_Section_A/Q1.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define output(x) cout << x << nl // out 13 | #define printarray(arr, len) \ 14 | for (int i = 0; i < len; i++) \ 15 | { \ 16 | cout << arr[i] << " "; \ 17 | if (i + 1 == len) \ 18 | cout << endl; \ 19 | } // array print 20 | using namespace std; 21 | 22 | /* 23 | Given_an_array_of_integers__Write_a_program_to_ 24 | calculate_the_product_of_a_particular_number_and_its_previous_number_using_linear_search___ 25 | */ 26 | 27 | int LinearSearch(int arr[], int len, int num) 28 | { 29 | for (int i = 0; i < len; i++) 30 | if (arr[i] == num) 31 | return i; 32 | return -1; 33 | } 34 | 35 | int main() 36 | { 37 | faster; 38 | 39 | int n; 40 | cout << "Enter array length \nthen elements" << nl; 41 | cin >> n; 42 | // 6 43 | // 4 1 2 7 6 5 44 | 45 | int arr[n]; 46 | for (int i = 0; i < n; i++) 47 | { 48 | cin >> arr[i]; 49 | } 50 | 51 | cout << "Enter the number : "; 52 | int number; 53 | cin >> number; 54 | 55 | int index = LinearSearch(arr, n, number); 56 | 57 | cout << arr[index] * arr[index - 1] << nl; 58 | 59 | CRACKED; 60 | } 61 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/stack_link_list_parenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | struct Node 7 | { 8 | int value; 9 | struct Node *next; 10 | }; 11 | 12 | struct Node *top; 13 | 14 | void push(int data) 15 | { 16 | struct Node *temp; 17 | temp = (struct Node *)malloc(sizeof(struct Node)); 18 | temp->value = data; 19 | temp->next = top; 20 | top = temp; 21 | } 22 | 23 | int pop() 24 | { 25 | struct Node *temp; 26 | int data; 27 | if (top == NULL) 28 | { 29 | return 0; 30 | } 31 | else 32 | { 33 | data = top->value; 34 | temp = top; 35 | top = top->next; 36 | free(temp); 37 | return data; 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | string s; 44 | cout << "Enter a string : "; 45 | cin >> s; 46 | 47 | for (int i = 0; i < s.size(); i++) 48 | { 49 | if (s[i] == '(' || s[i] == '{' || s[i] == '[') 50 | { 51 | push(s[i]); 52 | } 53 | else 54 | { 55 | if (s[i] == ')' && pop() != '(') 56 | { 57 | cout << "Invalid" << endl; 58 | return 0; 59 | } 60 | if (s[i] == '}' && pop() != '{') 61 | { 62 | cout << "Invalid" << endl; 63 | return 0; 64 | } 65 | if (s[i] == ']' && pop() != '[') 66 | { 67 | cout << "Invalid" << endl; 68 | return 0; 69 | } 70 | } 71 | } 72 | 73 | cout << "Valid" << endl; 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/Q5__Evaluation_of_Postfix_Expression_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | float scanNum(char ch) 7 | { 8 | int value; 9 | value = ch; 10 | return float(value - '0'); 11 | } 12 | bool isOperator(char ch) 13 | { 14 | if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^') 15 | return true; 16 | return false; 17 | } 18 | 19 | int isOperand(char ch) 20 | { 21 | if (ch >= '0' && ch <= '9') 22 | return true; 23 | return false; 24 | } 25 | 26 | float operation(int a, int b, char op) 27 | { 28 | if (op == '+') 29 | return b + a; 30 | else if (op == '-') 31 | return b - a; 32 | else if (op == '*') 33 | return b * a; 34 | else if (op == '/') 35 | return b / a; 36 | else if (op == '^') 37 | return pow(b, a); 38 | else 39 | return INT_MIN; 40 | } 41 | 42 | float postfixEval(string postfix) 43 | { 44 | int a, b; 45 | stack stk; 46 | int len = postfix.length(); 47 | 48 | for (int i = 0; i < len; i++) 49 | { 50 | if (isOperator(postfix[i])) 51 | { 52 | a = stk.top(); 53 | stk.pop(); 54 | b = stk.top(); 55 | stk.pop(); 56 | stk.push(operation(a, b, postfix[i])); 57 | } 58 | else if (isOperand(postfix[i])) 59 | { 60 | stk.push(scanNum(postfix[i])); 61 | } 62 | } 63 | return stk.top(); 64 | } 65 | int main() 66 | { 67 | string post = "21+3*"; 68 | // cin >> post; 69 | cout << postfixEval(post) << nl; 70 | return 0; 71 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Learned/BFS_using_adjList_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | void prepareAdjList(unordered_map> &adjList, vector> &edge) 10 | { 11 | for (auto i : edge) 12 | { 13 | adjList[i.first].push_back(i.second); 14 | adjList[i.second].push_back(i.first); 15 | } 16 | } 17 | void bfs(unordered_map> &adjList, unordered_map &visited, vector &ans, int node) 18 | { 19 | queue q; 20 | q.push(node); 21 | visited[node] = 1; 22 | while (!q.empty()) 23 | { 24 | int frontNode = q.front(); 25 | // store ans 26 | ans.push_back(frontNode); 27 | q.pop(); 28 | for (auto i : adjList[frontNode]) 29 | { 30 | if (!visited[i]) 31 | { 32 | q.push(i); 33 | visited[i] = 1; 34 | } 35 | } 36 | } 37 | } 38 | 39 | vector BFS(int vertex, vector> edge) 40 | { 41 | unordered_map> adjList; // make adj list 42 | vector ans; // store bfs 43 | unordered_map visited; // coloring 44 | prepareAdjList(adjList, edge); 45 | for (int i = 0; i < vertex; i++) 46 | { 47 | if (!visited[i]) 48 | { 49 | bfs(adjList, visited, ans, i); 50 | } 51 | } 52 | } 53 | 54 | int32_t main() 55 | { 56 | CRACKED; 57 | } 58 | -------------------------------------------------------------------------------- /DSA_TOPICS/Application/Evaluation_of_Postfix_Expression__.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl 7 | using namespace std; 8 | 9 | int32_t main() 10 | { 11 | cout << "Enter Your postfix expression : "; 12 | string s; 13 | cin >> s; 14 | 15 | // in: 8*(5^4+2)-6^2/(9*3) 16 | // post: 854^2+*62^93*/- 17 | 18 | // in: 9+2-5*6/3+2^3/4 19 | // post: 92+56*3/-23^4/+ 20 | 21 | // in: 1+3^2+(5*6-4)*7 22 | // post: 132^+56*4-7*+ 23 | 24 | stack operand; 25 | for (int i = 0; i < s.length(); i++) 26 | { 27 | if (s[i] >= '0' && s[i] <= '9') 28 | { 29 | operand.push(s[i] - '0'); 30 | } 31 | else 32 | { 33 | int b = operand.top(); 34 | operand.pop(); 35 | int a = operand.top(); 36 | operand.pop(); 37 | 38 | if (s[i] == '+') 39 | { 40 | operand.push(a + b); 41 | } 42 | else if (s[i] == '-') 43 | { 44 | operand.push(a - b); 45 | } 46 | else if (s[i] == '*') 47 | { 48 | operand.push(a * b); 49 | } 50 | else if (s[i] == '/') 51 | { 52 | operand.push(a / b); 53 | } 54 | else if (s[i] == '^') 55 | { 56 | operand.push(pow(a, b)); 57 | } 58 | } 59 | } 60 | cout << "Ans " << operand.top() << nl; 61 | 62 | CRACKED; 63 | } 64 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Noman/delete.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | int32_t main() 11 | { 12 | 13 | CRACKED; 14 | } 15 | 16 | /* 17 | 18 | node* Delete(node* root, node* keyNode) 19 | { 20 | if(keyNode->left == NULL) 21 | { 22 | if(keyNode->parent == NULL) 23 | root = keyNode->right; 24 | else 25 | transplant(keyNode, keyNode->right); 26 | } 27 | else if(keyNode->right == NULL) 28 | { 29 | if(keyNode->parent == NULL) 30 | root = keyNode->left; 31 | else 32 | transplant(keyNode, keyNode->left); 33 | } 34 | else 35 | { 36 | node* successorNode = successor(keyNode->right); // successor 37 | 38 | if(keyNode->parent == NULL) 39 | root = successorNode; 40 | 41 | if(successorNode == keyNode->right) 42 | { 43 | transplant(keyNode, successorNode); 44 | 45 | successorNode->left = keyNode->left; 46 | successorNode->left->parent = successorNode; 47 | } 48 | else 49 | { 50 | transplant(successorNode, successorNode->right); 51 | 52 | successorNode->left = keyNode->left; 53 | successorNode->left->parent = successorNode; 54 | 55 | successorNode->right = keyNode->right; 56 | successorNode->right->parent = successorNode; 57 | } 58 | } 59 | free(keyNode); 60 | return root; 61 | } 62 | 63 | 64 | 65 | */ -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Using_Struct_and_linked_List.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | struct SStackk 10 | { 11 | int value; 12 | struct SStackk *next = NULL; 13 | }; 14 | 15 | struct SStackk *top; 16 | 17 | int push(int data) 18 | { 19 | struct SStackk *newNode = (struct SStackk *)malloc(sizeof(struct SStackk)); 20 | newNode->value = data; 21 | newNode->next = top; 22 | top = newNode; 23 | } 24 | 25 | int pop() 26 | { 27 | if (top == NULL) 28 | { 29 | cout << "Stack is underFlow" << nl; 30 | return -1; 31 | } 32 | else 33 | { 34 | int data = top->value; 35 | struct SStackk *temp = (struct SStackk *)malloc(sizeof(struct SStackk)); 36 | temp = top; 37 | top = top->next; 38 | free(temp); 39 | return data; 40 | } 41 | } 42 | void printStackk() 43 | { 44 | struct SStackk *traveller = (struct SStackk *)malloc(sizeof(struct SStackk)); 45 | 46 | traveller = top; 47 | if (traveller == NULL) 48 | { 49 | printf("Stack is empty\n"); 50 | } 51 | while (traveller != NULL) 52 | { 53 | cout << traveller->value << " "; 54 | traveller = traveller->next; 55 | } 56 | } 57 | bool isEmpty() 58 | { 59 | if (top == NULL) 60 | return true; 61 | return false; 62 | } 63 | 64 | int main() 65 | { 66 | top = NULL; 67 | 68 | push(10); 69 | push(20); 70 | push(30); 71 | push(40); 72 | push(50); 73 | 74 | printStackk(); 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Transplant_in_BST.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node *parent; 17 | node(int data) 18 | { 19 | this->data = data; 20 | leftNode = NULL; 21 | rightNode = NULL; 22 | parent = NULL; 23 | } 24 | }; 25 | 26 | node *buildTree(node *root) 27 | { 28 | cout << "Enter the value of root: "; 29 | int data; 30 | cin >> data; 31 | root = new node(data); 32 | 33 | if (data == -1) 34 | { 35 | return NULL; 36 | } 37 | 38 | cout << "Enter left child of " << data << " " << nl; 39 | root->leftNode = buildTree(root->leftNode); 40 | 41 | cout << "Enter right child of " << data << " " << nl; 42 | root->rightNode = buildTree(root->rightNode); 43 | 44 | return root; 45 | } 46 | 47 | node *transplant(node *root, node *u, node *v) 48 | { 49 | if (u->parent->leftNode == u) 50 | { 51 | u->parent->leftNode = v; 52 | v->parent = u->parent; 53 | } 54 | else if (u->parent->rightNode == u) 55 | { 56 | u->parent->rightNode = v; 57 | v->parent = u->parent; 58 | } 59 | else 60 | root = v; 61 | return v; 62 | } 63 | 64 | int32_t main() 65 | { 66 | node *root = NULL; 67 | 68 | // creating tree 69 | root = buildTree(root); 70 | 71 | // input 72 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 73 | 74 | CRACKED; 75 | } 76 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/CT_2_SEC_C/Q1.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | int LinearSearch(int arr[], int len, int num) 24 | { 25 | for (int i = 0; i < len; i++) 26 | if (arr[i] == num) 27 | return i; 28 | return -1; 29 | } 30 | 31 | /* 32 | Given an array of integers. Write a program to calculate the sum of a 33 | particular number and three numbers following that number using 34 | linear search. 35 | Sample Input: 1, 4, 5, 7, 8, -2, 6, 0, 19, -15 36 | Number: 5 37 | Output : 5 + 7 + 8 - 2 = 18 38 | */ 39 | int32_t main() 40 | { 41 | faster; 42 | 43 | int arr[] = {1, 4, 5, 7, 8, -2, 6, 0, 19, -15}; 44 | int n = sizeof(arr) / sizeof(arr[0]); 45 | int value; 46 | cout << "enter a value forom array" << nl; 47 | cin >> value; 48 | 49 | int index = LinearSearch(arr, n, value); 50 | int sum = 0; 51 | 52 | // cout << index << nl; 53 | for (int i = index; i < index + 3; i++) 54 | { 55 | sum += arr[i]; 56 | } 57 | 58 | cout << sum - 2 << nl; 59 | 60 | CRACKED; 61 | } 62 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Predecessor.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node *parent; 17 | 18 | node(int data) 19 | { 20 | this->data = data; 21 | leftNode = NULL; 22 | rightNode = NULL; 23 | } 24 | }; 25 | 26 | node *maximumOfatree(node *root) 27 | { 28 | while (root->rightNode != null) 29 | root = root->rightNode; 30 | 31 | return root; 32 | } 33 | 34 | // from left to parent means = choto theke boro-te jaowa 35 | // from right to parent means = boro theke choto-te jaowa 36 | 37 | node *predecessor(node *ptr) 38 | { 39 | if (ptr->leftNode) 40 | { 41 | return maximumOfatree(ptr->leftNode); 42 | } 43 | else 44 | { 45 | // my logic *** 46 | /* 47 | node *parent = ptr->parent ; 48 | while (parent != null && parent->data > ptr->data) 49 | { 50 | parent = parent->parent; 51 | } 52 | return parent; 53 | */ 54 | 55 | node *traveller = ptr->parent; 56 | while (traveller != null && traveller == traveller->parent->leftNode) 57 | { 58 | traveller = traveller->parent; 59 | } 60 | 61 | return traveller; 62 | } 63 | } 64 | 65 | int32_t main() 66 | { 67 | node *root = NULL; 68 | 69 | // input 70 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 71 | 72 | CRACKED; 73 | } 74 | -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/Find_the_smallest_missing_number__.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | int BinarySearch(int arr[], int start, int end) 24 | { 25 | // cout << "Start " << start << " END " << end << nl; 26 | // cout << "[ "; 27 | // for (int i = start; i <= end; i++) 28 | // { 29 | // cout << arr[i] << " "; 30 | // } 31 | // cout << "]" << nl; 32 | // cout << nl; 33 | 34 | if (start > end) 35 | return start; // base case 36 | 37 | int mid = start + (end - start) / 2; 38 | 39 | if (arr[mid] == mid) 40 | return BinarySearch(arr, mid + 1, end); 41 | else 42 | return BinarySearch(arr, start, mid - 1); 43 | 44 | return -1; 45 | } 46 | 47 | int32_t main() 48 | { 49 | faster; 50 | 51 | int arr[] = {0, 1, 3, 4, 6, 9, 11, 15}; 52 | 53 | int n = sizeof(arr) / sizeof(arr[0]); 54 | cout << BinarySearch(arr, 0, n - 1) << nl; 55 | 56 | CRACKED; 57 | } 58 | /* 59 | 60 | Start 0 END 6 61 | Start 0 END 2 62 | Start 0 END 0 63 | Start 0 END -1 64 | 0 65 | 66 | 67 | */ -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Stack_Implementation_Using_Class_and_Linked List.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | using namespace std; 8 | 9 | class Stack 10 | { 11 | public: 12 | int *arr; 13 | int size; 14 | int top; 15 | 16 | Stack(int size) 17 | { 18 | this->size = size; 19 | arr = new int[size]; 20 | top = -1; 21 | } 22 | 23 | void push(int data) 24 | { 25 | if (top < size - 1) 26 | { 27 | top++; 28 | arr[top] = data; 29 | } 30 | else 31 | { 32 | cout << "This stack us Overflow" << nl; 33 | } 34 | } 35 | void pop() 36 | { 37 | if (top >= 0) 38 | { 39 | top--; 40 | } 41 | else 42 | { 43 | cout << "This Stack is UnderFlow" << nl; 44 | } 45 | } 46 | int peek() 47 | { 48 | if (top < 0) 49 | { 50 | cout << "This stack is empty" << nl; 51 | return -1; 52 | } 53 | return arr[top]; 54 | } 55 | bool empty() 56 | { 57 | if (top < 0) 58 | { 59 | return true; 60 | } 61 | else 62 | { 63 | return false; 64 | } 65 | } 66 | }; 67 | 68 | int main() 69 | { 70 | Stack s(5); 71 | s.push(12); 72 | s.push(24); 73 | 74 | cout << s.peek() << nl; 75 | s.pop(); 76 | cout << s.peek() << nl; 77 | cout << s.empty() << nl; 78 | s.empty() == 0 ? cout << "False" << nl : cout << "True" << nl; 79 | 80 | CRACKED; 81 | } 82 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/MID_SEC_B/Q1.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | void bubbleSort(int arr[], int n) 24 | { 25 | for (int i = 0; i < n - 1; i++) 26 | { 27 | int swapped = 0; 28 | 29 | for (int j = 0; j < n - 1 - i; j++) 30 | { 31 | if (arr[j] > arr[j + 1]) 32 | { 33 | int temp = arr[j]; 34 | arr[j] = arr[j + 1]; 35 | arr[j + 1] = temp; 36 | swapped = 1; 37 | } 38 | } 39 | if (swapped == 0) 40 | break; 41 | } 42 | } 43 | 44 | int32_t main() 45 | { 46 | faster; 47 | int n; 48 | cin >> n; 49 | int arr[n]; 50 | for (int i = 0; i < n; i++) 51 | { 52 | cin >> arr[i]; 53 | } 54 | bubbleSort(arr, n); 55 | int ans[n - 1]; 56 | for (int i = 0; i < n - 1; i++) 57 | { 58 | ans[i] = arr[i + 1] - arr[i]; 59 | } 60 | bubbleSort(ans, n - 1); 61 | cout << nl; 62 | cout << "ANS is " << ans[0] << nl; 63 | cout << nl; 64 | 65 | CRACKED; 66 | } 67 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/Two_Stack_using_One_array.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 13-07-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | using namespace std; 8 | 9 | class TwoStack 10 | { 11 | int *arr; 12 | int top1; 13 | int top2; 14 | int size; 15 | 16 | TwoStack(int size) 17 | { 18 | this->size = size; 19 | arr = new int[size]; 20 | top1 = -1; 21 | top2 = size; 22 | } 23 | 24 | void push1(int data) 25 | { 26 | if (top2 - top1 > 0) 27 | { 28 | top1++; 29 | arr[top1] = data; 30 | } 31 | else 32 | { 33 | cout << "This stack is overFlow" << nl; 34 | } 35 | } 36 | void push2(int data) 37 | { 38 | if (top2 - top1 > 0) 39 | { 40 | top2--; 41 | arr[top2] = data; 42 | } 43 | else 44 | { 45 | cout << "This stack is overFlow" << nl; 46 | } 47 | } 48 | 49 | int pop1() 50 | { 51 | if (top1 >= 0) 52 | { 53 | int ans = arr[top1]; 54 | top1--; 55 | return ans; 56 | } 57 | else 58 | { 59 | cout << "This Stack is UnderFlow" << nl; 60 | } 61 | return -1; 62 | } 63 | int pop2() 64 | { 65 | if (top2 < size) 66 | { 67 | int ans = arr[top2]; 68 | top2++; 69 | return ans; 70 | } 71 | else 72 | { 73 | cout << "This Stack is UnderFlow" << nl; 74 | } 75 | return -1; 76 | } 77 | }; 78 | int main() 79 | { 80 | 81 | CRACKED; 82 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Successor_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node *parent; 17 | node(int data) 18 | { 19 | this->data = data; 20 | leftNode = NULL; 21 | rightNode = NULL; 22 | } 23 | }; 24 | 25 | node *minimumOfTheTree(node *root) 26 | { 27 | while (root->leftNode != null) 28 | root = root->leftNode; 29 | 30 | return root; 31 | } 32 | 33 | // from left to parent means = choto theke boro-te jaowa 34 | // from right to parent means = boro theke choto-te jaowa 35 | 36 | node *successor(node *ptr) 37 | { 38 | // case 1: 39 | // have right child 40 | if (ptr->rightNode != null) 41 | { 42 | return minimumOfTheTree(ptr->rightNode); 43 | } 44 | 45 | // case 2: 46 | // have no roght child 47 | else 48 | { 49 | /**** My logic *****/ 50 | // node *traveller = ptr->parent; 51 | // while (traveller->data <= ptr->data) 52 | // { 53 | // traveller = traveller->parent; 54 | // } 55 | // return traveller; 56 | 57 | node *traveller = ptr->parent; 58 | while (traveller != null && traveller->rightNode == ptr) 59 | { 60 | ptr = traveller; 61 | traveller = ptr->parent; 62 | } 63 | 64 | return traveller; 65 | } 66 | } 67 | 68 | int32_t main() 69 | { 70 | node *root = NULL; 71 | 72 | // input 73 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 74 | 75 | CRACKED; 76 | } 77 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Traversal/Post_Order_Traversal.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | void postOrderTraversal(node *root) 46 | { 47 | if (root == null) 48 | return; 49 | postOrderTraversal(root->leftNode); 50 | postOrderTraversal(root->rightNode); 51 | cout << root->data << " "; 52 | } 53 | 54 | int32_t main() 55 | { 56 | node *root = NULL; 57 | 58 | // creating tree 59 | root = buildTree(root); 60 | 61 | // input: 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 62 | // In order: 4 2 5 1 3 6 63 | // Pre Order: 1 2 4 5 3 6 64 | // postOrder: 4 5 2 6 3 1 65 | 66 | // another input: 1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1 67 | // Pre Order: 1 3 7 11 5 17 68 | 69 | // level order traversal 70 | 71 | cout << nl << "Pre-Order Traversal : "; 72 | postOrderTraversal(root); 73 | cout << nl; 74 | 75 | CRACKED; 76 | } 77 | -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/Find_first_and_last_occurrence_of _a_Number__normal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // out 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | 13 | int BinarySearchIteratively(int arr[], int len, int key) 14 | { 15 | int start = 0; 16 | int last = len - 1; 17 | int ans = -1; 18 | while (start <= last) 19 | { 20 | int mid = (start + last) / 2; 21 | if (arr[mid] == key) 22 | { 23 | ans = mid; 24 | last = mid - 1; 25 | } 26 | else if (arr[mid] < key) 27 | { 28 | start = mid + 1; 29 | } 30 | else if (arr[mid] > key) 31 | { 32 | last = mid - 1; 33 | } 34 | } 35 | return ans; 36 | } 37 | 38 | int lastindexBinarySearchIteratively(int arr[], int len, int key) 39 | { 40 | int start = 0; 41 | int last = len - 1; 42 | int ans = -1; 43 | while (start <= last) 44 | { 45 | int mid = (start + last) / 2; 46 | if (arr[mid] == key) 47 | { 48 | ans = mid; 49 | start = mid + 1; 50 | } 51 | else if (arr[mid] < key) 52 | { 53 | start = mid + 1; 54 | } 55 | else if (arr[mid] > key) 56 | { 57 | last = mid - 1; 58 | } 59 | } 60 | return ans; 61 | } 62 | 63 | int main() 64 | { 65 | int arr[] = {2, 3, 9, 9, 9, 9, 9, 9, 10, 22, 32}; 66 | int len = sizeof(arr) / sizeof(arr[0]); 67 | int firstIndex = BinarySearchIteratively(arr, len, 9); 68 | int lastIndex = -1; 69 | if (firstIndex != -1) 70 | { 71 | lastIndex = lastindexBinarySearchIteratively(arr, len, 9); 72 | } 73 | cout << "Firstindex " << firstIndex << " LastIndex " << lastIndex << nl; 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /DSA_TOPICS/Topological_Sort/Using_DFS_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | vector graph[100]; 11 | int visited[100] = {0}; 12 | stack result; 13 | int cnt = 1; 14 | 15 | void dfs(int source) 16 | { 17 | visited[source] = 1; 18 | 19 | for (int i = 0; i < graph[source].size(); i++) 20 | { 21 | if (visited[graph[source][i]] == 0) 22 | { 23 | dfs(graph[source][i]); 24 | } 25 | } 26 | 27 | // all nodes are vsiited 28 | 29 | cout << "PUSH " << cnt << " " << source << nl; // debug 30 | // pushing when doing backtracking 31 | result.push(source); 32 | cnt++; 33 | } 34 | /* 35 | 36 | --------------- Logic of TopSort ----------------- 37 | if from 1 no edge i can go o 4, 5 ,7 edges 38 | then 1 should be come first then these edges in sorting order 39 | 40 | */ 41 | int32_t main() 42 | { 43 | int nodes, edges; 44 | cout << "Enter nodes and edge number: "; 45 | cin >> nodes >> edges; 46 | 47 | // input edges 48 | for (int i = 0; i < edges; i++) 49 | { 50 | cout << "Enter edges from u to v: "; 51 | int u, v; 52 | cin >> u >> v; 53 | graph[u].push_back(v); 54 | } 55 | 56 | for (int i = 1; i <= nodes; i++) 57 | { 58 | // calling DFS 59 | if (visited[i] == 0) 60 | dfs(i); 61 | } 62 | 63 | cout << nl << "Topological Order: "; 64 | while (!result.empty()) 65 | { 66 | cout << result.top() << " "; 67 | result.pop(); 68 | } 69 | cout << nl; 70 | CRACKED; 71 | } 72 | /* 73 | 6 7 74 | 75 | 1 2 76 | 2 4 77 | 1 3 78 | 3 4 79 | 4 6 80 | 4 5 81 | 5 6 82 | 83 | */ 84 | 85 | /* 86 | 87 | 5 4 88 | 89 | 3 2 90 | 1 2 91 | 2 4 92 | 2 5 93 | 94 | 95 | */ -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/queue_link_list_GivenCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node { 5 | int data; 6 | struct Node* next; 7 | } Node; 8 | 9 | typedef struct { 10 | Node* front; 11 | Node* rear; 12 | } Queue; 13 | 14 | void initializeQueue(Queue *q) { 15 | q->front = NULL; 16 | q->rear = NULL; 17 | } 18 | 19 | int isEmpty(Queue *q) { 20 | return q->front == NULL; 21 | } 22 | 23 | void enqueue(Queue *q, int item) { 24 | Node* newNode = (Node*)malloc(sizeof(Node)); 25 | newNode->data = item; 26 | newNode->next = NULL; 27 | 28 | if (isEmpty(q)) { 29 | q->front = newNode; 30 | q->rear = newNode; 31 | } else { 32 | q->rear->next = newNode; 33 | q->rear = newNode; 34 | } 35 | } 36 | 37 | int dequeue(Queue *q) { 38 | if (isEmpty(q)) { 39 | printf("Queue is empty. Cannot dequeue.\n"); 40 | return -1; // Return some invalid value to indicate an error 41 | } 42 | 43 | int item = q->front->data; 44 | Node* temp = q->front; 45 | q->front = q->front->next; 46 | free(temp); 47 | 48 | if (q->front == NULL) // If the queue became empty after dequeue 49 | initializeQueue(q); 50 | 51 | return item; 52 | } 53 | 54 | int front(Queue *q) { 55 | if (isEmpty(q)) { 56 | printf("Queue is empty.\n"); 57 | return -1; // Return some invalid value to indicate an error 58 | } 59 | 60 | return q->front->data; 61 | } 62 | 63 | int main() { 64 | Queue queue; 65 | initializeQueue(&queue); 66 | 67 | enqueue(&queue, 1); 68 | enqueue(&queue, 2); 69 | enqueue(&queue, 3); 70 | 71 | printf("Dequeue: %d\n", dequeue(&queue)); // Output: 1 72 | printf("Dequeue: %d\n", dequeue(&queue)); // Output: 2 73 | printf("Is Empty: %s\n", isEmpty(&queue) ? "true" : "false"); // Output: false 74 | printf("Dequeue: %d\n", dequeue(&queue)); // Output: 3 75 | printf("Is Empty: %s\n", isEmpty(&queue) ? "true" : "false"); // Output: true 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/CT_2_Section_A/Q2.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | int BinarySearch(int arr[], int len) 24 | { 25 | int start = 0; 26 | int end = len - 1; 27 | int ans; 28 | 29 | while (start < end) 30 | { 31 | int mid = (start + end) / 2; 32 | if (mid == arr[mid]) 33 | { 34 | ans = mid + 1; 35 | start = mid + 1; 36 | } 37 | else 38 | { 39 | ans = mid - 1; 40 | end = mid - 1; 41 | } 42 | // cout << "RUNNING" << nl; 43 | } 44 | 45 | return ans; 46 | } 47 | 48 | /* 49 | Given a sorted array of non-negative distinct integers, find the 50 | smallest missing non-negative element in it using binary search. 51 | */ 52 | int32_t main() 53 | { 54 | faster; 55 | 56 | // Find the smallest missing number 57 | 58 | // int n; 59 | // cout << "Enter array length \nthen elements" << nl; 60 | // cin >> n; 61 | // // 6 62 | // // 4 1 2 7 6 5 63 | 64 | // int arr[n]; 65 | // for (int i = 0; i < n; i++) 66 | // { 67 | // cin >> arr[i]; 68 | // } 69 | 70 | int arr[] = {0, 1, 3, 4, 6, 9, 11, 15}; 71 | 72 | int n = sizeof(arr) / sizeof(arr[0]); 73 | cout << BinarySearch(arr, n) << nl; 74 | 75 | CRACKED; 76 | } 77 | -------------------------------------------------------------------------------- /DSA_TOPICS/Heap/Decending_Order_Heap_sort.cpp: -------------------------------------------------------------------------------- 1 | // 10 18 17 20 15 2 | 3 | // **************** Author : Tashin.Parvez *************************\ 4 | // **************** Updated: 22-06-23 *************************\ 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl; // NewLine 9 | using namespace std; 10 | 11 | void printArray(int arr[], int len) 12 | { 13 | for (int i = 1; i < len; i++) 14 | cout << arr[i] << " "; 15 | cout << endl; 16 | } 17 | 18 | void minheapify(int arr[], int size, int index) 19 | { 20 | int leftChild = index * 2; 21 | int rightChild = index * 2 + 1; 22 | 23 | int smallest = index; // parent 24 | 25 | if (leftChild <= size && arr[smallest] > arr[leftChild]) 26 | { 27 | smallest = leftChild; 28 | } 29 | 30 | if (rightChild <= size && arr[smallest] > arr[rightChild]) 31 | { 32 | smallest = rightChild; 33 | } 34 | 35 | if (smallest != index) 36 | { 37 | swap(arr[smallest], arr[index]); 38 | minheapify(arr, size, smallest); 39 | } 40 | } 41 | 42 | void heapSort(int arr[], int size) 43 | { 44 | while (size > 1) 45 | { 46 | swap(arr[1], arr[size]); // take max element to the last leaf 47 | size--; // in each iteration we get left numbers higgest element 48 | minheapify(arr, size, 1); // by doing swap er get the smallest number in the root so take it its own place call heapify 49 | } 50 | } 51 | 52 | int32_t main() 53 | { 54 | int arr[] = {-1, 10, 18, 17, 20, 15}; 55 | 56 | int length = sizeof(arr) / sizeof(arr[0]); 57 | 58 | cout << "Given Array: "; 59 | printArray(arr, length); 60 | 61 | for (int i = length / 2; i >= 1; i--) 62 | { 63 | minheapify(arr, length - 1, i); 64 | } 65 | 66 | cout << "Heap Array: "; 67 | printArray(arr, length); 68 | 69 | heapSort(arr, length - 1); // send actual heap size 70 | 71 | cout << "Sorted array: "; 72 | printArray(arr, length); 73 | 74 | CRACKED; 75 | } 76 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/CT_2_SEC_C/Q2.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define output(x) cout << x << nl // out 13 | #define printarray(arr, len) \ 14 | for (int i = 0; i < len; i++) \ 15 | { \ 16 | cout << arr[i] << " "; \ 17 | if (i + 1 == len) \ 18 | cout << endl; \ 19 | } // array print 20 | using namespace std; 21 | 22 | int countOccurrences(int arr[], int len, int target) 23 | { 24 | int start = 0; 25 | int end = len - 1; 26 | int cnt = 0; 27 | int mid; 28 | while (start <= end) 29 | { 30 | mid = start + (end - start) / 2; 31 | // cout << "RUNNING" << nl; 32 | 33 | if (arr[mid] == target) 34 | { 35 | cnt++; 36 | int i = mid - 1; 37 | while (i >= 0 && arr[i] == target) 38 | { 39 | cnt++; 40 | i--; 41 | } 42 | 43 | i = mid + 1; 44 | while (i <= end && arr[i] == target) 45 | { 46 | cnt++; 47 | i++; 48 | } 49 | break; 50 | } 51 | if (arr[mid] > target) 52 | { 53 | end = mid - 1; 54 | } 55 | else 56 | { 57 | start = mid + 1; 58 | } 59 | } 60 | 61 | return cnt; 62 | } 63 | 64 | int main() 65 | { 66 | faster; 67 | 68 | int arr[] = {-5, -4, -4, -3, -3, -3, 0, 5, 5, 5, 5, 7, 8, 8, 9, 10}; 69 | int n = sizeof(arr) / sizeof(arr[0]); 70 | int value; 71 | cout << "enter a value forom array" << endl; 72 | cin >> value; 73 | 74 | cout << countOccurrences(arr, n, value); 75 | 76 | CRACKED; 77 | } 78 | -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/Count_number_of_occurrences_2.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define output(x) cout << x << nl // out 13 | #define printarray(arr, len) \ 14 | for (int i = 0; i < len; i++) \ 15 | { \ 16 | cout << arr[i] << " "; \ 17 | if (i + 1 == len) \ 18 | cout << endl; \ 19 | } // array print 20 | using namespace std; 21 | 22 | int countOccurrences(const int arr[], int start, int end, int target) 23 | { 24 | int count = 0; 25 | 26 | while (start <= end) 27 | { 28 | int mid = start + (end - start) / 2; 29 | cout << "RUNNING" << nl; 30 | 31 | if (arr[mid] == target) 32 | { 33 | count++; 34 | 35 | int i = mid - 1; // left 36 | while (i >= start && arr[i] == target) 37 | { 38 | count++; 39 | i--; 40 | } 41 | 42 | i = mid + 1; // right 43 | while (i <= end && arr[i] == target) 44 | { 45 | count++; 46 | i++; 47 | } 48 | 49 | break; 50 | } 51 | 52 | if (arr[mid] > target) 53 | end = mid - 1; 54 | else 55 | start = mid + 1; 56 | } 57 | 58 | return count; 59 | } 60 | 61 | int main() 62 | { 63 | int arr[] = {-5, -4, -4, -3, -3, -3, 0, 5, 5, 5, 5, 7, 8, 8, 9, 10}; 64 | int n = sizeof(arr) / sizeof(arr[0]); 65 | int value; 66 | cout << "enter a value forom array" << endl; 67 | cin >> value; 68 | 69 | int occurrences = countOccurrences(arr, 0, n - 1, value); 70 | cout << "Number of occurrences: " << occurrences << nl; 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/Basic/Using_array.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define nl endl; // NewLine 6 | using namespace std; 7 | 8 | int n = 100, front = -1, rear = -1; 9 | #define size 100 10 | int queue[size]; 11 | 12 | void enqueue() 13 | { 14 | int val; 15 | if (rear == n - 1) 16 | cout << "Queue Overflow" << endl; 17 | else 18 | { 19 | if (front == -1) 20 | front = 0; 21 | cout << "Insert the element in queue : " << endl; 22 | cin >> val; 23 | rear++; 24 | queue[rear] = val; 25 | } 26 | } 27 | void dequeue() 28 | { 29 | if (front == -1 || front > rear) 30 | { 31 | cout << "Queue Underflow"; 32 | return; 33 | } 34 | else 35 | { 36 | cout << "Element deleted from queue is : " << queue[front] << endl; 37 | front++; 38 | } 39 | } 40 | void Display() 41 | { 42 | if (front == -1) 43 | cout << "Queue is empty" << endl; 44 | else 45 | { 46 | cout << "Queue elements are : "; 47 | for (int i = front; i <= rear; i++) 48 | cout << queue[i] << " "; 49 | cout << endl; 50 | } 51 | } 52 | int main() 53 | { 54 | int ch; 55 | cout << "1) Insert element to queue" << endl; 56 | cout << "2) Delete element from queue" << endl; 57 | cout << "3) Display all the elements of queue" << endl; 58 | cout << "4) Exit" << endl; 59 | 60 | while (ch != 4) 61 | { 62 | cout << "Enter your choice : " << endl; 63 | cin >> ch; 64 | 65 | switch (ch) 66 | { 67 | case 1: 68 | enqueue(); 69 | break; 70 | case 2: 71 | dequeue(); 72 | break; 73 | case 3: 74 | Display(); 75 | break; 76 | case 4: 77 | cout << "Exit" << endl; 78 | break; 79 | default: 80 | cout << "Invalid choice" << endl; 81 | } 82 | } 83 | return 0; 84 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/queue_array_GivenCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | #define MAX_SIZE 100 7 | 8 | typedef struct Queuee 9 | { 10 | int arr[MAX_SIZE]; 11 | int front; 12 | int rear; 13 | } Queue; 14 | 15 | void initializeQueue(Queue *q) 16 | { 17 | q->front = -1; 18 | q->rear = -1; 19 | } 20 | 21 | int isEmpty(Queue *q) 22 | { 23 | return q->front == -1; 24 | } 25 | 26 | int isFull(Queue *q) 27 | { 28 | return (q->rear + 1) % MAX_SIZE == q->front; 29 | } 30 | 31 | void enqueue(Queue *q, int item) 32 | { 33 | if (isFull(q)) 34 | { 35 | printf("Queue is full. Cannot enqueue.\n"); 36 | return; 37 | } 38 | 39 | if (isEmpty(q)) 40 | q->front = 0; 41 | q->rear = (q->rear + 1) % MAX_SIZE; 42 | q->arr[q->rear] = item; 43 | } 44 | 45 | int dequeue(Queue *q) 46 | { 47 | if (isEmpty(q)) 48 | { 49 | printf("Queue is empty. Cannot dequeue.\n"); 50 | return -1; // Return some invalid value to indicate an error 51 | } 52 | 53 | int item = q->arr[q->front]; 54 | if (q->front == q->rear) 55 | initializeQueue(q); 56 | else 57 | q->front = (q->front + 1) % MAX_SIZE; 58 | 59 | return item; 60 | } 61 | 62 | int front(Queue *q) 63 | { 64 | if (isEmpty(q)) 65 | { 66 | printf("Queue is empty.\n"); 67 | return -1; // Return some invalid value to indicate an error 68 | } 69 | 70 | return q->arr[q->front]; 71 | } 72 | 73 | int main() 74 | { 75 | Queue queue; 76 | initializeQueue(&queue); 77 | 78 | enqueue(&queue, 1); 79 | enqueue(&queue, 2); 80 | enqueue(&queue, 3); 81 | 82 | printf("Dequeue: %d\n", dequeue(&queue)); // Output: 1 83 | printf("Dequeue: %d\n", dequeue(&queue)); // Output: 2 84 | printf("Is Empty: %s\n", isEmpty(&queue) ? "true" : "false"); // Output: false 85 | printf("Dequeue: %d\n", dequeue(&queue)); // Output: 3 86 | printf("Is Empty: %s\n", isEmpty(&queue) ? "true" : "false"); // Output: true 87 | 88 | return 0; 89 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Problems/Sum_of_Nodes.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | typedef struct Node 11 | { 12 | int data; 13 | struct Node *leftNode; 14 | struct Node *rightNode; 15 | } node; 16 | 17 | node *insert(node *&root, int key) 18 | { 19 | node *newNode = (struct Node *)malloc(sizeof(struct Node)); 20 | newNode->data = key; 21 | newNode->leftNode = null; 22 | newNode->rightNode = null; 23 | 24 | if (root == null) 25 | return newNode; 26 | 27 | node *traveller = root; 28 | while (true) 29 | { 30 | if (traveller->data < key) 31 | { 32 | if (traveller->rightNode == null) 33 | { 34 | traveller->rightNode = newNode; 35 | break; 36 | } 37 | traveller = traveller->rightNode; 38 | } 39 | else 40 | { 41 | if (traveller->leftNode == null) 42 | { 43 | traveller->leftNode = newNode; 44 | break; 45 | } 46 | traveller = traveller->leftNode; 47 | } 48 | } 49 | return root; 50 | } 51 | 52 | 53 | int countAllNodesValue(node *root) 54 | { 55 | if (root == null) 56 | return 0; 57 | 58 | int left = countAllNodesValue(root->leftNode); 59 | int right = countAllNodesValue(root->rightNode); 60 | 61 | return left + right + root->data; 62 | } 63 | 64 | int32_t main() 65 | { 66 | node *root = NULL; 67 | 68 | // input 69 | // 4 5 1 2 3 6 -1 70 | 71 | int data; 72 | while (1) 73 | { 74 | cout << "enter data: "; 75 | cin >> data; 76 | if (data == -1) 77 | break; 78 | root = insert(root, data); 79 | } 80 | 81 | cout << nl; 82 | cout << "Total nodes Sum = "; 83 | cout << countAllNodesValue(root); 84 | cout << nl; 85 | 86 | CRACKED; 87 | } 88 | -------------------------------------------------------------------------------- /DSA_TOPICS/Heap/Heap_Sort.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | void printArray(int arr[], int len) 10 | { 11 | for (int i = 1; i < len; i++) 12 | cout << arr[i] << " "; 13 | cout << endl; 14 | } 15 | 16 | int heapify_topToBottom(int arr[], int size, int index) 17 | { 18 | int largest = index; 19 | 20 | int leftChild = index * 2; 21 | int rightChild = (index * 2) + 1; 22 | 23 | if (leftChild <= size && arr[largest] < arr[leftChild]) 24 | largest = leftChild; 25 | if (rightChild <= size && arr[largest] < arr[rightChild]) 26 | largest = rightChild; 27 | 28 | if (index != largest) 29 | { 30 | swap(arr[index], arr[largest]); 31 | heapify_topToBottom(arr, size, largest); 32 | } 33 | } 34 | 35 | void heapSort(int arr[], int size) 36 | { 37 | while (size > 1) 38 | { 39 | swap(arr[1], arr[size]); // take max element to the last leaf 40 | size--; // in each iteration we get left numbers higgest element 41 | heapify_topToBottom(arr, size, 1); // by doing swap er get the smallest number in the root so take it its own place call heapify 42 | } 43 | } 44 | 45 | int32_t main() 46 | { 47 | // int arr[] = {-1, 12, 11, 13, 5, 6, 7}; 48 | // int arr[] = {-1, 10, 5, 3, 2, 4}; 49 | int arr[] = {-1, 2, 10, 4, 7, 1, 8, 9, 3, 6, 5}; 50 | 51 | int length = sizeof(arr) / sizeof(arr[0]); 52 | 53 | cout << "Given Array: "; 54 | printArray(arr, length); 55 | 56 | // creating heap [max] 57 | // buildMaxHeap 58 | for (int i = length / 2; i >= 1; i--) 59 | { 60 | heapify_topToBottom(arr, length - 1, i); 61 | } 62 | 63 | cout << "Heap Array: "; 64 | printArray(arr, length); 65 | 66 | heapSort(arr, length - 1); // send actual heap size 67 | 68 | cout << "Sorted array: "; 69 | printArray(arr, length); 70 | 71 | CRACKED; 72 | } 73 | -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/BFS/BFS.cpp: -------------------------------------------------------------------------------- 1 | // Breadth first search 2 | #include 3 | #include 4 | #define null NULL 5 | #define nl endl 6 | using namespace std; 7 | // 0 1 2 3 4 8 | int graph[5][5] = {0, 1, 1, 1, 0, 9 | 1, 0, 0, 0, 1, 10 | 1, 0, 0, 1, 0, 11 | 1, 0, 1, 0, 1, 12 | 0, 1, 0, 1, 0}; 13 | // 0, 0, 0, 0, 0}; // not work 14 | 15 | void bfs(int source) 16 | { 17 | int color[5] = {0}; // not visited 18 | int d[5]; // distance 19 | int p[5] = {-1}; // parent 20 | 21 | /* 22 | color: 23 | 0 = white // not visited 24 | 1 = Gray // visited [in queue] 25 | 2 = Black // printed 26 | */ 27 | 28 | color[source] = 1; // visited [in queue] 29 | d[source] = 0; 30 | 31 | queue Q; 32 | Q.push(source); 33 | 34 | while (!Q.empty()) 35 | { 36 | 37 | int s = Q.front(); 38 | Q.pop(); 39 | 40 | cout << s << " "; // ans 41 | 42 | for (int i = 0; i < 5; i++) // traverse that particular row 43 | { 44 | if (graph[s][i] == 1 && color[i] == 0) 45 | // graph = 1 means connected 46 | // color = 0 means not in queue 47 | { 48 | Q.push(i); 49 | color[i] = 1; // visited [in queue] 50 | d[i] = d[s] + 1; // perticular node distance [kinda level] 51 | p[i] = s; // giving parent [ int s = Q.front() ] 52 | } 53 | } 54 | color[s] = 2; // parent color black 55 | } 56 | 57 | cout << endl; 58 | 59 | for (int i = 0; i < 5; i++) // printing all nodes distance from root 60 | { 61 | cout << "distance = " << i << " is = " << d[i] << endl; 62 | } 63 | 64 | cout << endl; 65 | 66 | for (int i = 0; i < 5; i++) // printing all nodes parent 67 | { 68 | cout << "parent = " << i << " is = " << p[i] << endl; 69 | } 70 | 71 | cout << endl; 72 | } 73 | int main() 74 | { 75 | cout << nl; 76 | bfs(4); // sending one node as a root 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/CT_1/Q2.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | void bubbleSort(int arr[], int n) 24 | { 25 | for (int i = 0; i < n - 1; i++) 26 | { 27 | int swapped = 0; 28 | 29 | for (int j = 0; j < n - 1 - i; j++) 30 | { 31 | if (arr[j] > arr[j + 1]) 32 | { 33 | int temp = arr[j]; 34 | arr[j] = arr[j + 1]; 35 | arr[j + 1] = temp; 36 | swapped = 1; 37 | } 38 | } 39 | if (swapped == 0) 40 | break; 41 | } 42 | return; 43 | } 44 | 45 | /* 46 | Find the nth largest number from an unsorted array 47 | */ 48 | 49 | int32_t main() 50 | { 51 | faster; 52 | 53 | int n; 54 | cout << "Enter array length \nthen elements" << nl; 55 | cin >> n; 56 | // 6 57 | // 4 1 2 7 6 5 58 | 59 | int arr[n]; 60 | for (int i = 0; i < n; i++) 61 | { 62 | cin >> arr[i]; 63 | } 64 | bubbleSort(arr, n); 65 | 66 | for (int i = 0; i < n; i++) 67 | { 68 | cout << arr[i] << " "; 69 | } 70 | cout << nl; 71 | 72 | int postion = 1; 73 | cout << "ENTER THE POSITION : " << nl; 74 | // cin >> postion; 75 | 76 | while (postion != -1) 77 | { 78 | cin >> postion; 79 | cout << arr[(n - 1) - (postion - 1)] << nl; 80 | cout << "ENTER THE POSITION : " << nl; 81 | } 82 | 83 | CRACKED; 84 | } 85 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Problems/Depth_of_a_tree_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | typedef struct Node 11 | { 12 | int data; 13 | struct Node *leftNode; 14 | struct Node *rightNode; 15 | struct Node *parent; 16 | } node; 17 | 18 | node *insert(node *&root, int key) 19 | { 20 | node *newNode = (struct Node *)malloc(sizeof(struct Node)); 21 | newNode->data = key; 22 | newNode->leftNode = null; 23 | newNode->rightNode = null; 24 | 25 | if (root == null) 26 | { 27 | return newNode; 28 | } 29 | node *traveller = root; 30 | 31 | // cout << "ROOT " << root->data << nl; 32 | while (true) 33 | { 34 | if (traveller->data < key) 35 | { 36 | if (traveller->rightNode == null) 37 | { 38 | traveller->rightNode = newNode; 39 | break; 40 | } 41 | traveller = traveller->rightNode; 42 | } 43 | else 44 | { 45 | if (traveller->leftNode == null) 46 | { 47 | traveller->leftNode = newNode; 48 | break; 49 | } 50 | traveller = traveller->leftNode; 51 | } 52 | } 53 | return root; 54 | } 55 | 56 | int depth(node *root, node *index) 57 | { 58 | if (root == index) 59 | return 0; 60 | else 61 | return 1 + depth(root, index->parent); 62 | } 63 | 64 | int32_t main() 65 | { 66 | node *root = NULL; 67 | 68 | // input 69 | // 4 5 1 2 3 6 -1 70 | 71 | /* 72 | 4 73 | / \ 74 | 1 5 75 | \ \ 76 | 2 6 77 | \ 78 | 3 79 | */ 80 | 81 | int data; 82 | while (1) 83 | { 84 | cout << "enter data: "; 85 | cin >> data; 86 | if (data == -1) 87 | break; 88 | root = insert(root, data); 89 | } 90 | 91 | cout << nl; 92 | cout << "Total nodes "; 93 | // cout << heightOfTree(root); 94 | cout << nl; 95 | 96 | CRACKED; 97 | } 98 | -------------------------------------------------------------------------------- /DSA_TOPICS/BinarySearch/First_and_Last_occurrence__optimal__GFG.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | int lastOcc(int arr[], int start, int end, int x) 11 | { 12 | int lastans = -1; 13 | 14 | while (start <= end) 15 | { 16 | int mid = start + (end - start) / 2.0; 17 | 18 | if (arr[mid] == x) 19 | { 20 | lastans = mid; 21 | start = mid + 1; 22 | } 23 | else if (arr[mid] > x) 24 | { 25 | end = mid - 1; 26 | } 27 | else 28 | { 29 | start = mid + 1; 30 | } 31 | } 32 | 33 | return lastans; 34 | } 35 | 36 | vector find_First_and_last_Occ(int arr[], int n, int x) 37 | { 38 | int start = 0, end = n - 1; 39 | int firstans = -1, lastans = -1; 40 | 41 | while (start <= end) 42 | { 43 | int mid = start + (end - start) / 2.0; 44 | 45 | if (arr[mid] == x) 46 | { 47 | firstans = mid; 48 | lastans = mid; 49 | end = mid - 1; 50 | } 51 | else if (arr[mid] > x) 52 | { 53 | end = mid - 1; 54 | } 55 | else 56 | { 57 | start = mid + 1; 58 | } 59 | } 60 | 61 | if (firstans != -1) 62 | lastans = lastOcc(arr, lastans, n - 1, x); 63 | 64 | vector anss; 65 | 66 | anss.push_back(firstans); 67 | anss.push_back(lastans); 68 | 69 | return anss; 70 | } 71 | }; 72 | 73 | //{ Driver Code Starts. 74 | 75 | int main() 76 | { 77 | int t; 78 | cin >> t; 79 | while (t--) 80 | { 81 | int n, x; 82 | cin >> n >> x; 83 | int arr[n], i; 84 | for (i = 0; i < n; i++) 85 | cin >> arr[i]; 86 | vector ans; 87 | Solution ob; 88 | ans = ob.find_First_and_last_Occ(arr, n, x); 89 | cout << ans[0] << " " << ans[1] << endl; 90 | } 91 | return 0; 92 | } 93 | 94 | // } Driver Code Ends -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Problems/Count_Leaf_Nodes.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | typedef struct Node 11 | { 12 | int data; 13 | struct Node *leftNode; 14 | struct Node *rightNode; 15 | } node; 16 | 17 | node *insert(node *&root, int key) 18 | { 19 | node *newNode = (struct Node *)malloc(sizeof(struct Node)); 20 | newNode->data = key; 21 | newNode->leftNode = null; 22 | newNode->rightNode = null; 23 | 24 | if (root == null) 25 | { 26 | return newNode; 27 | } 28 | node *traveller = root; 29 | 30 | // cout << "ROOT " << root->data << nl; 31 | while (true) 32 | { 33 | if (traveller->data < key) 34 | { 35 | if (traveller->rightNode == null) 36 | { 37 | traveller->rightNode = newNode; 38 | break; 39 | } 40 | traveller = traveller->rightNode; 41 | } 42 | else 43 | { 44 | if (traveller->leftNode == null) 45 | { 46 | traveller->leftNode = newNode; 47 | break; 48 | } 49 | traveller = traveller->leftNode; 50 | } 51 | } 52 | 53 | return root; 54 | } 55 | 56 | int cnt = 0; 57 | void preOrderTraversal(node *root) 58 | { 59 | if (root == null) 60 | return; 61 | if (root->leftNode == null && root->rightNode == null) 62 | cnt++; 63 | preOrderTraversal(root->leftNode); 64 | preOrderTraversal(root->rightNode); 65 | } 66 | 67 | int32_t main() 68 | { 69 | node *root = NULL; 70 | 71 | // input 72 | // 4 5 1 2 3 6 -1 73 | 74 | int data; 75 | while (1) 76 | { 77 | cout << "enter data: "; 78 | cin >> data; 79 | if (data == -1) 80 | { 81 | break; 82 | } 83 | root = insert(root, data); 84 | } 85 | 86 | cout << nl; 87 | preOrderTraversal(root); 88 | 89 | cout << "Leaf nodes " << cnt; 90 | cout << nl; 91 | 92 | CRACKED; 93 | } 94 | -------------------------------------------------------------------------------- /DSA_TOPICS/MergeSort/Try.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; // NewLine 3 | #define output(x) cout << x << nl // output 4 | using namespace std; 5 | 6 | void printArray(int arr[], int len) 7 | { 8 | for (int i = 0; i < len; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | } 12 | 13 | // Merge two subarrays L and M into arr 14 | void merge(int *arr, int p, int q, int r) 15 | { 16 | 17 | // 2 array size 18 | int leftArraySize = q - p + 1; 19 | int rightArraySize = r - q; 20 | 21 | int L[leftArraySize], R[rightArraySize]; // creat array 22 | 23 | // copy element 24 | for (int i = 0; i < leftArraySize; i++) 25 | L[i] = arr[p + i]; 26 | for (int j = 0; j < rightArraySize; j++) 27 | R[j] = arr[q + 1 + j]; 28 | 29 | // Maintain current index of sub-arrays and main array 30 | int i, j; 31 | i = 0; 32 | j = 0; 33 | int index = p; 34 | 35 | // place elements in the correct position 36 | while (i < leftArraySize && j < rightArraySize) 37 | { 38 | if (L[i] <= R[j]) 39 | { 40 | arr[index] = L[i]; 41 | i++; 42 | } 43 | else 44 | { 45 | arr[index] = R[j]; 46 | j++; 47 | } 48 | index++; 49 | } 50 | 51 | // When we run out, remaining elements and put in Arr 52 | while (i < leftArraySize) 53 | { 54 | arr[index] = L[i]; 55 | i++; 56 | index++; 57 | } 58 | 59 | while (j < rightArraySize) 60 | { 61 | arr[index] = R[j]; 62 | j++; 63 | index++; 64 | } 65 | } 66 | 67 | void mergeSort(int *arr, int s, int e) 68 | { 69 | if (s >= e) 70 | return; 71 | 72 | int mid = s + (e - s) / 2; 73 | mergeSort(arr, s, mid); 74 | mergeSort(arr, mid + 1, e); 75 | merge(arr, s, mid, e); 76 | } 77 | 78 | int main() 79 | { 80 | int arr[] = {3, 7, 0, 1, 5, 8, 3, 2, 34, 66, 87, 23, 12, 12, 12}; 81 | // int arr[] = {22, 32, 6, -2, 2, 3, 9, 10}; 82 | 83 | int len = sizeof(arr) / sizeof(arr[0]); 84 | 85 | cout << "Given Array : "; 86 | printArray(arr, len); 87 | 88 | mergeSort(arr, 0, len - 1); 89 | 90 | cout << "Sorted Array : "; 91 | printArray(arr, len); 92 | 93 | return 0; 94 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/For_ass/Shakhawat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // int maze[5][5] = { 7 | // {0, 1, 2, 1, 0}, 8 | // {0, 0, 0, 1, 0}, 9 | // {0, 3, 1, 0, 0}, 10 | // {0, 1, 0, 1, 0}, 11 | // {0, 0, 0, 0, 0}}; 12 | 13 | int maze[5][5] = {0, 1, 0, 0, 2, 14 | 0, 0, 0, 1, 0, 15 | 0, 3, 1, 0, 0, 16 | 0, 1, 0, 1, 0, 17 | 0, 0, 0, 0, 0}; 18 | 19 | int visited[5][5] = {0}; 20 | 21 | bool dfs(int row, int col, int sKey, stack> &pathStack) 22 | { 23 | if (row < 0 || row > 4 || col < 0 || col > 4 || visited[row][col] == 1) 24 | { 25 | return false; 26 | } 27 | 28 | pathStack.push({row, col}); 29 | visited[row][col] = 1; 30 | 31 | // last case [ find the out gate ] 32 | if (maze[row][col] == 2 && sKey == 2) 33 | { 34 | return true; 35 | } 36 | 37 | // get 1st treasure 38 | if (maze[row][col] == 3) 39 | { 40 | sKey = 2; 41 | } 42 | 43 | if (maze[row][col + 1] != 1 && dfs(row, col + 1, sKey, pathStack)) 44 | { 45 | return true; 46 | } 47 | if (maze[row + 1][col] != 1 && dfs(row + 1, col, sKey, pathStack)) 48 | { 49 | return true; 50 | } 51 | if (maze[row][col - 1] != 1 && dfs(row, col - 1, sKey, pathStack)) 52 | { 53 | return true; 54 | } 55 | if (maze[row - 1][col] != 1 && dfs(row - 1, col, sKey, pathStack)) 56 | { 57 | return true; 58 | } 59 | 60 | visited[row][col] = 0; 61 | pathStack.pop(); 62 | return false; 63 | } 64 | 65 | int main() 66 | { 67 | stack> pathStack; 68 | int row, col; 69 | cout << "Enter your starting point : "; 70 | cin >> row >> col; 71 | 72 | if (dfs(row, col, 0, pathStack)) 73 | { 74 | cout << "Path with treasure and exit:"; 75 | while (!pathStack.empty()) 76 | { 77 | cout << " (" << pathStack.top().first << "," << pathStack.top().second << ")<--"; 78 | pathStack.pop(); 79 | } 80 | cout << "Starting point(i,j)"; 81 | cout << endl; 82 | } 83 | else 84 | { 85 | cout << "No path with treasure found." << endl; 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Problems/Lecture40_RatinaMazeProblem.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | bool isSafe(int row, int col, int n, vector> &visited, vector> &m) 11 | { 12 | if (row >= 0 && col < n && col >= 0 && col < n && visited[row][col] == 0 && m[row][col == 0]) 13 | { 14 | return true; 15 | } 16 | else 17 | return false; 18 | } 19 | 20 | void solve(vector> &m, int n, vector &ans, int row, int col, vector> &visited, string path) 21 | { 22 | if (row == n - 1 && col == n - 1) 23 | { 24 | ans.push_back(path); 25 | return; 26 | } 27 | visited[row][col] = 1; 28 | 29 | if (isSafe(row - 1, col, n, visited, m)) 30 | { 31 | path.push_back('U'); 32 | solve(m, n, ans, row - 1, col, visited, path); 33 | path.pop_back(); 34 | } 35 | 36 | if (isSafe(row + 1, col, n, visited, m)) 37 | { 38 | path.push_back('D'); 39 | solve(m, n, ans, row + 1, col, visited, path); 40 | path.pop_back(); 41 | } 42 | 43 | if (isSafe(row, col + 1, n, visited, m)) 44 | { 45 | path.push_back('R'); 46 | solve(m, n, ans, row, col + 1, visited, path); 47 | path.pop_back(); 48 | } 49 | if (isSafe(row, col - 1, n, visited, m)) 50 | { 51 | path.push_back('L'); 52 | solve(m, n, ans, row, col - 1, visited, path); 53 | path.pop_back(); 54 | } 55 | 56 | visited[row][col] = 0; 57 | } 58 | 59 | vector findpath(vector> &m, int row, int col, int n) 60 | { 61 | vector ans; 62 | if (m[row][col] == 0) 63 | { 64 | return ans; 65 | } 66 | vector> visited; 67 | for (int i = 0; i < n; i++) 68 | { 69 | for (int j = 0; j < n; j++) 70 | { 71 | visited[i][j] = 0; 72 | } 73 | } 74 | string path = ""; 75 | solve(m, n, ans, row, col, visited, path); 76 | } 77 | 78 | int32_t main() 79 | { 80 | 81 | CRACKED; 82 | } 83 | -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/Basic/Circular_Queue_Using_Array.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define nl endl; // NewLine 6 | using namespace std; 7 | 8 | #define size 100 9 | int queue[size]; 10 | int n = 100, front = -1, rear = -1; 11 | 12 | void enqueue() 13 | { 14 | int val; 15 | if ((rear + 1) % size == front) 16 | cout << "Queue Overflow" << endl; 17 | else 18 | { 19 | if (front == -1) 20 | front = 0; 21 | cout << "Insert the element in queue : " << endl; 22 | cin >> val; 23 | rear++; 24 | rear %= size; 25 | queue[rear] = val; 26 | } 27 | } 28 | void dequeue() 29 | { 30 | if (front == -1 && rear == -1) 31 | { 32 | cout << "Queue Underflow"; 33 | return; 34 | } 35 | else 36 | { 37 | cout << "Element deleted from queue is : " << queue[front] << endl; 38 | front++; 39 | front %= size; 40 | } 41 | } 42 | void Display() 43 | { 44 | if (front == -1) 45 | cout << "Queue is empty" << endl; 46 | else 47 | { 48 | cout << "Queue elements are : "; 49 | for (int i = front; i != rear;) 50 | { 51 | cout << queue[i] << " "; 52 | i++; 53 | i %= size; 54 | } 55 | cout << queue[rear]; 56 | cout << endl; 57 | } 58 | } 59 | int main() 60 | { 61 | int ch; 62 | cout << "1) Insert element to queue" << endl; 63 | cout << "2) Delete element from queue" << endl; 64 | cout << "3) Display all the elements of queue" << endl; 65 | cout << "4) Exit" << endl; 66 | 67 | while (ch != 4) 68 | { 69 | cout << "Enter your choice : " << endl; 70 | cin >> ch; 71 | 72 | switch (ch) 73 | { 74 | case 1: 75 | enqueue(); 76 | break; 77 | case 2: 78 | dequeue(); 79 | break; 80 | case 3: 81 | Display(); 82 | break; 83 | case 4: 84 | cout << "Exit" << endl; 85 | break; 86 | default: 87 | cout << "Invalid choice" << endl; 88 | } 89 | } 90 | return 0; 91 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Search_in_BST.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | //*********** recursively ********* 46 | node *search(node *root, int value) 47 | { 48 | if (root == null || root->data == value) 49 | return root; 50 | else 51 | { 52 | if (value < root->data) 53 | return search(root->leftNode, value); 54 | else 55 | return search(root->rightNode, value); 56 | } 57 | } 58 | 59 | /* //********** Iteratively ************** 60 | 61 | node *search(node *root, int value) 62 | { 63 | while (root != null && root->data != value) 64 | { 65 | if (root->data > value) 66 | root = root->leftNode; 67 | else 68 | root = root->rightNode; 69 | } 70 | return root; 71 | } 72 | 73 | */ 74 | 75 | int32_t main() 76 | { 77 | node *root = NULL; 78 | 79 | // creating tree 80 | root = buildTree(root); 81 | 82 | // input 83 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 84 | 85 | node *ptr = search(root, 5); 86 | if (ptr != null) 87 | { 88 | cout << "5 found on the tree" << nl; 89 | } 90 | else 91 | cout << "5 didnt find on the tree" << nl; 92 | 93 | CRACKED; 94 | } 95 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Traversal/Level_Order_traversal_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | void levelOrderTraversal(node *root) 46 | { 47 | queue q; 48 | q.push(root); 49 | q.push(null); 50 | 51 | while (!q.empty()) 52 | { 53 | node *temp = q.front(); 54 | q.pop(); 55 | 56 | if (temp == null) 57 | { 58 | cout << nl; 59 | if (!q.empty()) 60 | { 61 | q.push(null); 62 | } 63 | } 64 | else 65 | { 66 | cout << temp->data << " "; 67 | // only left and right node cz it's a binary tree 68 | 69 | if (temp->leftNode) 70 | { 71 | q.push(temp->leftNode); 72 | } 73 | if (temp->rightNode) 74 | { 75 | q.push(temp->rightNode); 76 | } 77 | } 78 | } 79 | } 80 | 81 | int32_t main() 82 | { 83 | node *root = NULL; 84 | 85 | // creating tree 86 | root = buildTree(root); 87 | 88 | // input 89 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 90 | 91 | // level order traversal 92 | cout << nl << "Tree Visualization level Order Traversal " << nl; 93 | levelOrderTraversal(root); 94 | 95 | CRACKED; 96 | } 97 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/Reverse_a_stack_using_recursion__.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | #define MAXSIZE 10 6 | #define TRUE 1 7 | #define FALSE 0 8 | 9 | struct Stack 10 | { 11 | int top; 12 | int array[MAXSIZE]; 13 | } st; 14 | 15 | void initialize() 16 | { 17 | st.top = -1; 18 | } 19 | 20 | int isFull() 21 | { 22 | if (st.top >= MAXSIZE - 1) 23 | return TRUE; 24 | else 25 | return FALSE; 26 | } 27 | // checking whether the stack is empty or not 28 | int isEmpty() 29 | { 30 | if (st.top == -1) 31 | return TRUE; 32 | else 33 | return FALSE; 34 | } 35 | 36 | // function to push the element into the stack. 37 | void push(int num) 38 | { 39 | if (isFull()) 40 | printf("Stack is Full...\n"); 41 | else 42 | { 43 | st.array[st.top + 1] = num; 44 | st.top++; 45 | } 46 | } 47 | 48 | // function to pop the element from the stack 49 | int pop() 50 | { 51 | if (isEmpty()) 52 | printf("Stack is Empty...\n"); 53 | else 54 | { 55 | st.top = st.top - 1; 56 | return st.array[st.top + 1]; 57 | } 58 | } 59 | 60 | void printStack() 61 | { 62 | if (!isEmpty()) 63 | { 64 | int temp = pop(); 65 | printStack(); 66 | printf(" %d ", temp); 67 | push(temp); 68 | } 69 | } 70 | 71 | void insertAtBottom(int item) 72 | { 73 | if (isEmpty()) 74 | { 75 | push(item); 76 | } 77 | else 78 | { 79 | int top = pop(); 80 | insertAtBottom(item); 81 | push(top); 82 | } 83 | } 84 | 85 | void reverse() 86 | { 87 | if (!isEmpty()) 88 | { 89 | int top = pop(); 90 | reverse(); 91 | insertAtBottom(top); 92 | } 93 | } 94 | 95 | int getSize() 96 | { 97 | return st.top + 1; 98 | } 99 | 100 | int main() 101 | { 102 | // St st; 103 | initialize(st); 104 | push(1); 105 | push(2); 106 | push(3); 107 | push(4); 108 | push(5); 109 | printf("Original Stack\n"); 110 | printStack(); 111 | 112 | reverse(); 113 | printf("\nReversed Stack\n"); 114 | printStack(); // calling printStack() method 115 | return 0; 116 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Find_Minimum_and_Maximum_value.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | //******** Iteratively ********* 46 | 47 | int minimumOfTheTree(node *root) 48 | { 49 | while (root->leftNode != null) 50 | root = root->leftNode; 51 | 52 | return root->data; 53 | } 54 | 55 | /* //******* Recursively ********* 56 | 57 | int minimumOfTheTree(node *root) 58 | { 59 | if (root->leftNode == null) 60 | return root->data; 61 | else 62 | minimumOfTheTree(root->leftNode); 63 | } 64 | 65 | */ 66 | 67 | 68 | 69 | //******** Iteratively ********* 70 | 71 | int maximumOftheTree(node *root) 72 | { 73 | while (root->rightNode != null) 74 | root = root->rightNode; 75 | 76 | return root->data; 77 | } 78 | 79 | /* //******* Recursively ********* 80 | 81 | int maximumOftheTree(node *root) 82 | { 83 | if (root->leftNode == null) 84 | return root->data; 85 | else 86 | maximumOftheTree(root->leftNode); 87 | } 88 | 89 | */ 90 | 91 | int32_t main() 92 | { 93 | node *root = NULL; 94 | 95 | // creating tree 96 | root = buildTree(root); 97 | 98 | // input 99 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 100 | 101 | CRACKED; 102 | } 103 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Traversal/In_Order_traversal_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | // inorder = assending order 46 | void InOrderTraversal(node *root) 47 | { 48 | if (root == null) 49 | return; 50 | InOrderTraversal(root->leftNode); 51 | cout << root->data << " "; 52 | InOrderTraversal(root->rightNode); 53 | } 54 | 55 | //************* basic Logic of In-Order ************* 56 | //************* it also works 57 | /* 58 | void InOrderTraversal(node *root) 59 | { 60 | if (root->leftNode) // if leftNode != null --> (LPR call) 61 | { 62 | InOrderTraversal(root->leftNode); 63 | } 64 | 65 | cout << root->data << " "; // it is printing all values 66 | 67 | if (root->rightNode) 68 | { 69 | InOrderTraversal(root->rightNode); // same as leftNode 70 | } 71 | } 72 | 73 | */ 74 | 75 | int32_t main() 76 | { 77 | node *root = NULL; 78 | 79 | // creating tree 80 | root = buildTree(root); 81 | 82 | // input: 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 83 | // In order: 4 2 5 1 3 6 84 | // Pre Order: 1 2 4 5 3 6 85 | 86 | // another input: 1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1 87 | // Pre Order: 1 3 7 11 5 17 88 | 89 | // level order traversal 90 | 91 | cout << nl << "Inorder Traversal : "; 92 | InOrderTraversal(root); 93 | cout << nl; 94 | 95 | CRACKED; 96 | } 97 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Problems/Height_of_a_tree.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | typedef struct Node 11 | { 12 | int data; 13 | struct Node *leftNode; 14 | struct Node *rightNode; 15 | } node; 16 | 17 | node *insert(node *&root, int key) 18 | { 19 | node *newNode = (struct Node *)malloc(sizeof(struct Node)); 20 | newNode->data = key; 21 | newNode->leftNode = null; 22 | newNode->rightNode = null; 23 | 24 | if (root == null) 25 | { 26 | return newNode; 27 | } 28 | node *traveller = root; 29 | 30 | // cout << "ROOT " << root->data << nl; 31 | while (true) 32 | { 33 | if (traveller->data < key) 34 | { 35 | if (traveller->rightNode == null) 36 | { 37 | traveller->rightNode = newNode; 38 | break; 39 | } 40 | traveller = traveller->rightNode; 41 | } 42 | else 43 | { 44 | if (traveller->leftNode == null) 45 | { 46 | traveller->leftNode = newNode; 47 | break; 48 | } 49 | traveller = traveller->leftNode; 50 | } 51 | } 52 | return root; 53 | } 54 | 55 | int heightOfTree(node *root) 56 | { 57 | if (root == null) 58 | return 0; 59 | 60 | int leftChild = heightOfTree(root->leftNode); 61 | int rightChild = heightOfTree(root->rightNode); 62 | 63 | return max(leftChild, rightChild) + 1; 64 | } 65 | 66 | int32_t main() 67 | { 68 | node *root = NULL; 69 | 70 | // input 71 | // 4 5 1 2 3 6 -1 72 | 73 | /* 74 | 4 75 | / \ 76 | 1 5 77 | \ \ 78 | 2 6 79 | \ 80 | 3 81 | */ 82 | 83 | int data; 84 | while (1) 85 | { 86 | cout << "enter data: "; 87 | cin >> data; 88 | if (data == -1) 89 | break; 90 | root = insert(root, data); 91 | } 92 | 93 | cout << nl; 94 | cout << "Total nodes "; 95 | cout << heightOfTree(root); 96 | cout << nl; 97 | 98 | CRACKED; 99 | } 100 | -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/circular_queue_array__GivenCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_SIZE 100 3 | 4 | typedef struct 5 | { 6 | int arr[MAX_SIZE]; 7 | int front; 8 | int rear; 9 | int size; 10 | } CircularQueue; 11 | 12 | void initializeQueue(CircularQueue *cq) 13 | { 14 | cq->front = -1; 15 | cq->rear = -1; 16 | cq->size = 0; 17 | } 18 | 19 | int isEmpty(CircularQueue *cq) 20 | { 21 | return cq->size == 0; 22 | } 23 | 24 | int isFull(CircularQueue *cq) 25 | { 26 | return cq->size == MAX_SIZE; 27 | // rear++ % size== front ==>> full 28 | } 29 | 30 | void enqueue(CircularQueue *cq, int item) 31 | { 32 | if (isFull(cq)) 33 | { 34 | printf("Queue is full. Cannot enqueue.\n"); 35 | return; 36 | } 37 | 38 | if (isEmpty(cq)) 39 | { 40 | cq->front = 0; 41 | cq->rear = 0; 42 | } 43 | else 44 | { 45 | cq->rear = (cq->rear + 1) % MAX_SIZE; 46 | } 47 | cq->arr[cq->rear] = item; 48 | cq->size++; 49 | } 50 | 51 | int dequeue(CircularQueue *cq) 52 | { 53 | if (isEmpty(cq)) 54 | { 55 | printf("Queue is empty. Cannot dequeue.\n"); 56 | return -1; // Return some invalid value to indicate an error 57 | } 58 | 59 | int item = cq->arr[cq->front]; 60 | if (cq->front == cq->rear) 61 | { 62 | initializeQueue(cq); 63 | // making front and rear -1 64 | } 65 | else 66 | { 67 | cq->front = (cq->front + 1) % MAX_SIZE; 68 | } 69 | cq->size--; 70 | 71 | return item; 72 | } 73 | 74 | int front(CircularQueue *cq) 75 | { 76 | if (isEmpty(cq)) 77 | { 78 | printf("Queue is empty.\n"); 79 | return -1; // Return some invalid value to indicate an error 80 | } 81 | 82 | return cq->arr[cq->front]; 83 | } 84 | 85 | int main() 86 | { 87 | CircularQueue cq; 88 | initializeQueue(&cq); 89 | 90 | enqueue(&cq, 1); 91 | enqueue(&cq, 2); 92 | enqueue(&cq, 3); 93 | 94 | printf("Dequeue: %d\n", dequeue(&cq)); // Output: 1 95 | printf("Dequeue: %d\n", dequeue(&cq)); // Output: 2 96 | printf("Is Empty: %s\n", isEmpty(&cq) ? "true" : "false"); // Output: false 97 | printf("Dequeue: %d\n", dequeue(&cq)); // Output: 3 98 | printf("%s\n", isEmpty(&cq) ? "true" : "false"); // Output: true 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /DSA_TOPICS/MergeSort/MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int len) 5 | { 6 | for (int i = 0; i < len; i++) 7 | cout << arr[i] << " "; 8 | cout << endl; 9 | } 10 | 11 | void merge(int *arr, int s, int e) 12 | { 13 | 14 | int mid = s + (e - s) / 2; 15 | 16 | int len1 = mid - s + 1; 17 | int len2 = e - mid; 18 | 19 | int *first = new int[len1]; // dinamic memory declear 20 | int *second = new int[len2]; 21 | 22 | // copy values 23 | int mainArrayIndex = s; 24 | for (int i = 0; i < len1; i++) 25 | { 26 | first[i] = arr[mainArrayIndex++]; 27 | } 28 | 29 | mainArrayIndex = mid + 1; // without this line code works perfectly 30 | 31 | for (int i = 0; i < len2; i++) 32 | { 33 | second[i] = arr[mainArrayIndex++]; 34 | } 35 | 36 | // merge 2 sorted arrays 37 | int index1 = 0; 38 | int index2 = 0; 39 | mainArrayIndex = s; 40 | 41 | while (index1 < len1 && index2 < len2) 42 | { 43 | if (first[index1] < second[index2]) 44 | { 45 | arr[mainArrayIndex++] = first[index1++]; 46 | } 47 | else 48 | { 49 | arr[mainArrayIndex++] = second[index2++]; 50 | } 51 | } 52 | 53 | while (index1 < len1) 54 | { 55 | arr[mainArrayIndex++] = first[index1++]; 56 | } 57 | 58 | while (index2 < len2) 59 | { 60 | arr[mainArrayIndex++] = second[index2++]; 61 | } 62 | 63 | delete[] first; 64 | delete[] second; 65 | } 66 | 67 | /* This is the implementation of the merge sort algorithm. */ 68 | void mergeSort(int *arr, int s, int e) 69 | { 70 | 71 | 72 | // base case 73 | if (s >= e) 74 | return; 75 | 76 | int mid = s + (e - s) / 2; 77 | 78 | // left part sort 79 | mergeSort(arr, s, mid); 80 | 81 | // right part sort 82 | mergeSort(arr, mid + 1, e); 83 | 84 | // merge 85 | merge(arr, s, e); 86 | } 87 | 88 | int main() 89 | { 90 | // int arr[] = {3, 7, 0, 1, 5, 8, 3, 2, 34, 66, 87, 23, 12, 12, 12}; // odd 91 | int arr[] = {22, 32, 6, -2, 2, 3, 9, 10}; // even 92 | 93 | int len = sizeof(arr) / sizeof(arr[0]); 94 | 95 | cout << "Given Array : "; 96 | printArray(arr, len); 97 | 98 | mergeSort(arr, 0, len - 1); 99 | 100 | cout << "Sorted Array : "; 101 | printArray(arr, len); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Traversal/Pre_Order_Traversal_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | cout << "Enter the value of root: "; 27 | int data; 28 | cin >> data; 29 | root = new node(data); 30 | 31 | if (data == -1) 32 | { 33 | return NULL; 34 | } 35 | 36 | cout << "Enter left child of " << data << " " << nl; 37 | root->leftNode = buildTree(root->leftNode); 38 | 39 | cout << "Enter right child of " << data << " " << nl; 40 | root->rightNode = buildTree(root->rightNode); 41 | 42 | return root; 43 | } 44 | 45 | void preOrderTraversal(node *root) 46 | { 47 | // first print 48 | // then left node visit 49 | // then right node visit 50 | 51 | if (root == null) 52 | return; 53 | cout << root->data << " "; 54 | preOrderTraversal(root->leftNode); 55 | preOrderTraversal(root->rightNode); 56 | } 57 | 58 | //************* basic Logic of In-Order ************* 59 | /* 60 | // it also works 61 | 62 | void InOrderTraversal(node *root) 63 | { 64 | if (root->leftNode) // if leftNode != null --> (LPR call) 65 | { 66 | InOrderTraversal(root->leftNode); 67 | } 68 | 69 | cout << root->data << " "; // it is printing all values 70 | 71 | if (root->rightNode) 72 | { 73 | InOrderTraversal(root->rightNode); // same as leftNode 74 | } 75 | } 76 | 77 | */ 78 | int32_t main() 79 | { 80 | node *root = NULL; 81 | 82 | // creating tree 83 | root = buildTree(root); 84 | 85 | // input: 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 86 | // In order: 4 2 5 1 3 6 87 | // Pre Order: 1 2 4 5 3 6 88 | 89 | // another input: 1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1 90 | // Pre Order: 1 3 7 11 5 17 91 | 92 | // level order traversal 93 | 94 | cout << nl << "Pre-Order Traversal : "; 95 | preOrderTraversal(root); 96 | cout << nl; 97 | 98 | CRACKED; 99 | } 100 | -------------------------------------------------------------------------------- /Class(LAB)/Class 2/Q3.cpp: -------------------------------------------------------------------------------- 1 | // Selection sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | void printArray(int array[], int size) 7 | { 8 | for (int i = 0; i < size; i++) 9 | { 10 | cout << array[i] << " "; 11 | } 12 | cout << endl; 13 | } 14 | 15 | void sortArr(int array[], int size) 16 | { 17 | 18 | for (int step = 0; step < size - 1; step++) 19 | { 20 | int min_idx = step; 21 | for (int i = step + 1; i < size; i++) 22 | { 23 | if (array[i] < array[min_idx]) 24 | min_idx = i; 25 | } 26 | 27 | int temp = array[min_idx]; 28 | array[min_idx] = array[step]; 29 | array[step] = temp; 30 | } 31 | } 32 | 33 | void selectionSort(int arr[], int len) 34 | { 35 | int even = 0; 36 | int odd = 0; 37 | 38 | for (int i = 0; i < len; i++) 39 | { 40 | if (arr[i] % 2 == 0) 41 | even++; 42 | else 43 | odd++; 44 | } 45 | int evenArr[even]; 46 | int oddArr[odd]; 47 | 48 | even = 0; 49 | odd = 0; 50 | 51 | for (int i = 0; i < len; i++) 52 | { 53 | if (arr[i] % 2 == 0) 54 | { 55 | // cout << "Input in even "; 56 | evenArr[even] = arr[i]; 57 | // cout << evenArr[even] << endl; 58 | even++; 59 | } 60 | else 61 | { 62 | oddArr[odd] = arr[i]; 63 | odd++; 64 | } 65 | } 66 | 67 | // cout << "EVEN ARR Unsort: "; 68 | printArray(evenArr, even); 69 | sortArr(evenArr, even); 70 | // cout << "EVEN ARR: "; 71 | // printArray(evenArr, even); 72 | 73 | sortArr(oddArr, odd); 74 | // cout << "ODD ARR: "; 75 | // printArray(oddArr, odd); 76 | 77 | int index = 0; 78 | 79 | // cout << "EVEN ARR: "; 80 | // printArray(evenArr, even); 81 | 82 | for (int i = 0; i < even; i++) // even 83 | { 84 | arr[i] = evenArr[i]; 85 | index = i; 86 | } 87 | 88 | index++; 89 | for (int i = odd - 1; i >= 0; i--) // odd 90 | { 91 | arr[index] = oddArr[i]; 92 | index++; 93 | } 94 | } 95 | 96 | int main() 97 | { 98 | int size; 99 | cin >> size; 100 | int data[size]; 101 | 102 | for (int i = 0; i < size; i++) 103 | { 104 | cin >> data[i]; 105 | } 106 | 107 | selectionSort(data, size); 108 | cout << "Sorted array in Acsending Order:\n"; 109 | printArray(data, size); 110 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Stack_Template_Given_CODE_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | struct node 10 | { 11 | int val; 12 | struct node *next = NULL; 13 | }; 14 | 15 | struct node *head; 16 | 17 | void push(int data) 18 | { 19 | int val = data; 20 | struct node *ptr; 21 | 22 | if (head == NULL) 23 | { 24 | ptr->val = val; 25 | ptr->next = head; 26 | head = ptr; 27 | } 28 | else 29 | { 30 | ptr->val = val; 31 | ptr->next = head; 32 | head = ptr; 33 | } 34 | cout << " DATA pushed " << nl; 35 | } 36 | 37 | void pop() 38 | { 39 | int item; 40 | struct node *ptr; 41 | if (head == NULL) 42 | { 43 | printf("Underflow"); 44 | } 45 | else 46 | { 47 | item = head->val; 48 | ptr = head; 49 | head = head->next; 50 | free(ptr); 51 | printf("Item popped"); 52 | } 53 | } 54 | 55 | void display() 56 | { 57 | int i; 58 | struct node *ptr; 59 | ptr = head; 60 | if (ptr == NULL) 61 | { 62 | printf("Stack is empty\n"); 63 | } 64 | else 65 | { 66 | printf("Printing Stack elements \n"); 67 | while (ptr != NULL) 68 | { 69 | printf("%d\n", ptr->val); 70 | ptr = ptr->next; 71 | } 72 | } 73 | } 74 | 75 | int main() 76 | { 77 | int choice; 78 | while (choice != 4) 79 | { 80 | cout << "\n1.Push \n2.Pop \n3.Show \n4.Exit" << nl; 81 | cout << "Enter your choice \n" << nl; 82 | 83 | cin >> choice; 84 | switch (choice) 85 | { 86 | case 1: 87 | { 88 | int data; 89 | cout << "Enter the value you want to push : "; 90 | 91 | cin >> data; 92 | push(data); 93 | break; 94 | } 95 | case 2: 96 | { 97 | pop(); 98 | break; 99 | } 100 | case 3: 101 | { 102 | display(); 103 | break; 104 | } 105 | case 4: 106 | { 107 | printf("Exiting...."); 108 | break; 109 | } 110 | default: 111 | { 112 | printf("Please Enter valid choice "); 113 | } 114 | }; 115 | } 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/For_ass/Ovi_noman.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ROWS 5 4 | #define COLS 5 5 | 6 | int maze[ROWS][COLS] = { 7 | {0, 1, 0, 0, 0}, 8 | {0, 0, 0, 1, 2}, 9 | {0, 3, 1, 0, 0}, 10 | {0, 1, 0, 1, 0}, 11 | {0, 0, 0, 0, 0}}; 12 | 13 | int visited[ROWS][COLS] = {0}; 14 | 15 | int shortest_path[ROWS * COLS]; // 25 16 | int shortest_length = ROWS * COLS; // 25 17 | 18 | void print_path(int path[], int length) 19 | { 20 | for (int i = 0; i < length; i++) 21 | { 22 | printf("(%d, %d)", path[i] / COLS, path[i] % COLS); 23 | if (i < length - 1) 24 | { 25 | printf(" -> "); 26 | } 27 | } 28 | printf("\n"); 29 | } 30 | 31 | void dfs(int row, int col, int path[], int length) 32 | { 33 | if (row < 0 || row >= ROWS || col < 0 || col >= COLS || visited[row][col] || maze[row][col] == 1) 34 | { 35 | return; 36 | } 37 | 38 | path[length] = row * COLS + col; 39 | visited[row][col] = 1; 40 | 41 | if (maze[row][col] == 2) // Exit found 42 | { 43 | int treasure_found = 0; 44 | for (int i = 0; i <= length; i++) 45 | { 46 | if (maze[path[i] / COLS][path[i] % COLS] == 3) 47 | { 48 | treasure_found = 1; 49 | break; 50 | } 51 | } 52 | if (treasure_found) 53 | { 54 | printf("Path with treasure: "); 55 | print_path(path, length + 1); 56 | 57 | if (length + 1 < shortest_length) 58 | { 59 | shortest_length = length + 1; 60 | for (int i = 0; i <= length; i++) 61 | { 62 | shortest_path[i] = path[i]; 63 | } 64 | } 65 | } 66 | } 67 | 68 | dfs(row + 1, col, path, length + 1); 69 | dfs(row - 1, col, path, length + 1); 70 | dfs(row, col + 1, path, length + 1); 71 | dfs(row, col - 1, path, length + 1); 72 | 73 | visited[row][col] = 0; // Backtrack 74 | } 75 | 76 | void find_paths_with_treasures(int start_row, int start_col) 77 | { 78 | int path[ROWS * COLS]; // 25 79 | 80 | dfs(start_row, start_col, path, 0); 81 | 82 | if (shortest_length != ROWS * COLS) 83 | { 84 | printf("\nShortest path with treasure: "); 85 | print_path(shortest_path, shortest_length); 86 | } 87 | else 88 | { 89 | printf("\nNo path with treasure found.\n"); 90 | } 91 | } 92 | 93 | int main() 94 | { 95 | // int start_row = 1; 96 | // int start_col = 0; 97 | find_paths_with_treasures(1, 0); 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Problems/Count_All_Nodes.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | typedef struct Node 11 | { 12 | int data; 13 | struct Node *leftNode; 14 | struct Node *rightNode; 15 | } node; 16 | 17 | node *insert(node *&root, int key) 18 | { 19 | node *newNode = (struct Node *)malloc(sizeof(struct Node)); 20 | newNode->data = key; 21 | newNode->leftNode = null; 22 | newNode->rightNode = null; 23 | 24 | if (root == null) 25 | { 26 | return newNode; 27 | } 28 | node *traveller = root; 29 | 30 | // cout << "ROOT " << root->data << nl; 31 | while (true) 32 | { 33 | if (traveller->data < key) 34 | { 35 | if (traveller->rightNode == null) 36 | { 37 | traveller->rightNode = newNode; 38 | break; 39 | } 40 | traveller = traveller->rightNode; 41 | } 42 | else 43 | { 44 | if (traveller->leftNode == null) 45 | { 46 | traveller->leftNode = newNode; 47 | break; 48 | } 49 | traveller = traveller->leftNode; 50 | } 51 | } 52 | return root; 53 | } 54 | 55 | int cnt = 0; 56 | void preOrderTraversal(node *root) // using a global variable 57 | { 58 | if (root == null) 59 | return; 60 | else 61 | { 62 | cnt++; 63 | } 64 | 65 | preOrderTraversal(root->leftNode); 66 | preOrderTraversal(root->rightNode); 67 | } 68 | 69 | //************ another logic 70 | 71 | int countAllNodes(node *root) 72 | { 73 | if (root == null) 74 | return 0; 75 | 76 | int left = countAllNodes(root->leftNode); 77 | int right = countAllNodes(root->rightNode); 78 | 79 | return left + right + 1; 80 | } 81 | 82 | int32_t main() 83 | { 84 | node *root = NULL; 85 | 86 | // input 87 | // 4 5 1 2 3 6 -1 88 | 89 | int data; 90 | while (1) 91 | { 92 | cout << "enter data: "; 93 | cin >> data; 94 | if (data == -1) 95 | break; 96 | root = insert(root, data); 97 | } 98 | 99 | cout << nl; 100 | preOrderTraversal(root); 101 | cout << "Total nodes "; 102 | cout << cnt << " "; // opt 1 103 | // cout << countAllNodes(root); // opt 2 104 | cout << nl; 105 | 106 | CRACKED; 107 | } 108 | -------------------------------------------------------------------------------- /Class(LAB)/Class 2/LeetCode.cpp: -------------------------------------------------------------------------------- 1 | // Selection sort in C++ 2 | // https://leetcode.com/problems/sort-even-and-odd-indices-independently/ 3 | 4 | #include 5 | using namespace std; 6 | 7 | void printArray(int array[], int size) 8 | { 9 | for (int i = 0; i < size; i++) 10 | { 11 | cout << array[i] << " "; 12 | } 13 | cout << endl; 14 | } 15 | 16 | void sortArr(int array[], int size) 17 | { 18 | 19 | for (int step = 0; step < size - 1; step++) 20 | { 21 | int min_idx = step; 22 | for (int i = step + 1; i < size; i++) 23 | { 24 | if (array[i] < array[min_idx]) 25 | min_idx = i; 26 | } 27 | 28 | int temp = array[min_idx]; 29 | array[min_idx] = array[step]; 30 | array[step] = temp; 31 | } 32 | } 33 | 34 | void selectionSort(int arr[], int len) 35 | { 36 | int even = 0; 37 | int odd = 0; 38 | 39 | for (int i = 0; i < len; i++) 40 | { 41 | if (arr[i] % 2 == 0) 42 | even++; 43 | else 44 | odd++; 45 | } 46 | int evenArr[even]; 47 | int oddArr[odd]; 48 | 49 | even = 0; 50 | odd = 0; 51 | 52 | for (int i = 0; i < len; i++) 53 | { 54 | if (arr[i] % 2 == 0) 55 | { 56 | // cout << "Input in even "; 57 | evenArr[even] = arr[i]; 58 | // cout << evenArr[even] << endl; 59 | even++; 60 | } 61 | else 62 | { 63 | oddArr[odd] = arr[i]; 64 | odd++; 65 | } 66 | } 67 | 68 | // cout << "EVEN ARR Unsort: "; 69 | printArray(evenArr, even); 70 | sortArr(evenArr, even); 71 | // cout << "EVEN ARR: "; 72 | // printArray(evenArr, even); 73 | 74 | sortArr(oddArr, odd); 75 | // cout << "ODD ARR: "; 76 | // printArray(oddArr, odd); 77 | 78 | int indexEven = 0; 79 | 80 | // cout << "EVEN ARR: "; 81 | // printArray(evenArr, even); 82 | 83 | int indexOdd = odd - 1; 84 | 85 | for (int i = 0; i < len; i++) // even 86 | { 87 | if (i % 2 == 0) 88 | { 89 | arr[i] = evenArr[indexEven]; 90 | indexEven++; 91 | } 92 | else 93 | { 94 | arr[i] = oddArr[indexOdd]; 95 | indexOdd--; 96 | } 97 | } 98 | } 99 | 100 | int main() 101 | { 102 | int size; 103 | cin >> size; 104 | int data[size]; 105 | 106 | for (int i = 0; i < size; i++) 107 | { 108 | cin >> data[i]; 109 | } 110 | 111 | selectionSort(data, size); 112 | cout << "Sorted array in Acsending Order:\n"; 113 | printArray(data, size); 114 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/For_ass/Question2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl // NewLine 4 | #define null NULL 5 | using namespace std; 6 | 7 | // exmp 1 8 | // int maze[5][5] = {0, 1, 2, 0, 0, 9 | // 0, 0, 0, 1, 0, 10 | // 0, 3, 1, 0, 0, 11 | // 0, 1, 0, 1, 0, 12 | // 0, 0, 0, 0, 0}; 13 | 14 | // exmp 2 15 | int maze[5][5] = {0, 1, 0, 0, 2, 16 | 0, 0, 0, 1, 0, 17 | 0, 3, 1, 0, 0, 18 | 0, 1, 0, 1, 0, 19 | 0, 0, 0, 0, 0}; 20 | 21 | int visited[5][5] = {0}; 22 | 23 | string s[5][5]; 24 | 25 | void dfs(int row, int col, int search) // 1 0 0 26 | { 27 | // base case 28 | if (row < 0 || row > 4 || col < 0 || col > 4) 29 | return; 30 | 31 | // output 32 | // cout << "(" << row << " " << col << ")" << endl; 33 | 34 | s[row][col] += "( " + to_string(row) + " " + to_string(col) + " ), "; 35 | 36 | if (maze[row][col] == 3) 37 | { 38 | search = 3; 39 | 40 | // s[row][col] = "y" + s[row][col]; // type 2 41 | 42 | // cout << "TASDJSHDAHS " << s[row][col] << nl; 43 | // cout << "TASDJSHDAHS" << nl; 44 | // cout << "TASDJSHDAHS " << s[row][col] << nl; 45 | } 46 | 47 | if (maze[row][col] == 2) 48 | { 49 | // cout << "TASDJSHDAHS " << s[row][col] << nl; 50 | // string str = s[row][col]; 51 | // if (str[0] == 'y') 52 | // { 53 | // cout << str << nl; 54 | // cout << "TASDJSHDAHS" << nl; 55 | // } 56 | 57 | if (search == 3) 58 | { 59 | cout << "Path with treasure: "; 60 | cout << s[row][col] << nl; 61 | } 62 | } 63 | 64 | visited[row][col] = 1; 65 | 66 | // right 67 | if (maze[row][col + 1] != 1 && visited[row][col + 1] == 0) 68 | { 69 | s[row][col + 1] = s[row][col]; 70 | dfs(row, col + 1, search); 71 | } 72 | // left 73 | if (maze[row][col - 1] != 1 && visited[row][col - 1] == 0) 74 | { 75 | s[row][col - 1] = s[row][col]; 76 | dfs(row, col - 1, search); 77 | } 78 | // up 79 | if (maze[row + 1][col] != 1 && visited[row + 1][col] == 0) 80 | { 81 | s[row + 1][col] = s[row][col]; 82 | dfs(row + 1, col, search); 83 | } 84 | 85 | // down 86 | if (maze[row - 1][col] != 1 && visited[row - 1][col] == 0) 87 | { 88 | s[row - 1][col] = s[row][col]; 89 | dfs(row - 1, col, search); 90 | } 91 | 92 | visited[row][col] = 0; 93 | } 94 | 95 | int main() 96 | { 97 | dfs(1, 0, 0); 98 | CRACKED; 99 | } 100 | -------------------------------------------------------------------------------- /DSA_TOPICS/Stack/Problems/stack_link_list_and_palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | struct Node 7 | { 8 | int value; 9 | struct Node *next; 10 | }; 11 | 12 | struct Node *top; 13 | 14 | void push(int data) 15 | { 16 | struct Node *temp; 17 | temp = (struct Node *)malloc(sizeof(struct Node)); 18 | temp->value = data; 19 | temp->next = top; 20 | top = temp; 21 | } 22 | 23 | int pop() 24 | { 25 | struct Node *temp; 26 | int data; 27 | if (top == NULL) 28 | { 29 | printf("\n Stack Underflow"); 30 | exit(1); 31 | } 32 | else 33 | { 34 | data = top->value; 35 | temp = top; 36 | top = top->next; 37 | free(temp); 38 | return data; 39 | } 40 | } 41 | 42 | int main() 43 | { 44 | string s; 45 | cout << "Enter a string : "; 46 | cin >> s; 47 | 48 | // getline(cin, s); 49 | 50 | bool flag = true; 51 | int len = s.size(); 52 | 53 | // string sp; // line 54 | // for (int i = 0; i < len; i++) 55 | // { 56 | // if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) 57 | // { 58 | // if ((s[i] >= 'A' && s[i] <= 'Z')) 59 | // { 60 | // s[i] = tolower(s[i]); 61 | // // cout << s[i] << nl; 62 | // } 63 | // sp += s[i]; 64 | // } 65 | // } 66 | // s = sp; 67 | 68 | len = s.size(); 69 | 70 | for (int i = 0; i < len; i++) 71 | { 72 | if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) 73 | { 74 | if ((s[i] >= 'A' && s[i] <= 'Z')) 75 | { 76 | s[i] = tolower(s[i]); 77 | } 78 | if (i == len / 2) 79 | { 80 | if (len % 2 == 0) 81 | { 82 | char k = pop(); 83 | if (s[i] != k) 84 | { 85 | // cout << "s and k " << s[i] << " " << k << endl; 86 | cout << "Not a Palindrome" << endl; 87 | return 0; 88 | } 89 | } 90 | flag = false; 91 | } 92 | else if (flag) 93 | { 94 | push(s[i]); 95 | } 96 | else 97 | { 98 | if (s[i] != pop()) 99 | { 100 | cout << "Not a Palindrome" << endl; 101 | return 0; 102 | } 103 | } 104 | } 105 | } 106 | 107 | cout << "Palindrome" << endl; 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/For_ass/DFS_given_EDIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define null NULL 4 | #define nl endl 5 | using namespace std; 6 | // exmp 1 7 | // int maze[5][5] = {0, 1, 2, 0, 0, 8 | // 0, 0, 0, 1, 0, 9 | // 0, 3, 1, 0, 0, 10 | // 0, 1, 0, 1, 0, 11 | // 0, 0, 0, 0, 0}; 12 | 13 | // exmp 2 14 | int maze[5][5] = {0, 1, 0, 0, 2, 15 | 0, 0, 0, 1, 0, 16 | 0, 3, 1, 0, 0, 17 | 0, 1, 0, 1, 0, 18 | 0, 0, 0, 0, 0}; 19 | 20 | int visited[5][5] = {0}; 21 | string s[5][5]; 22 | 23 | void dfs(int row, int col) // (3,0) call 24 | { 25 | // base case 26 | if (row < 0 || row > 4 || col < 0 || col > 4) 27 | return; 28 | 29 | if (maze[row][col] == 3) 30 | { 31 | 32 | // cout << nl; 33 | // for (int i = 0; i < 5; i++) 34 | // { 35 | // for (int j = 0; j < 5; j++) 36 | // { 37 | // cout << visited[i][j] << " "; 38 | // } 39 | // cout << nl; 40 | // } 41 | 42 | s[row][col] = "y" + s[row][col]; 43 | } 44 | 45 | // output 46 | 47 | cout << "(" << row << " " << col << ")" << endl; 48 | 49 | // s[row][col] += "(" + to_string(row) + " , " + to_string(col) + "), "; 50 | 51 | if (maze[row][col] == 2) 52 | { 53 | // string str = s[row][col]; 54 | // if (str[0] == 'y') 55 | // { 56 | // // str.erase(0, 1); 57 | // cout << "String " << str << nl; 58 | // } 59 | 60 | // cout << s[row][col] << endl; // print string 61 | cout << "We are in the exit" << endl; 62 | } 63 | 64 | // clouring 65 | visited[row][col] = 1; 66 | 67 | if (maze[row][col + 1] != 1 && visited[row][col + 1] == 0) 68 | { 69 | s[row][col + 1] = s[row][col]; 70 | dfs(row, col + 1); 71 | } 72 | 73 | if (maze[row][col - 1] != 1 && visited[row][col - 1] == 0) 74 | { 75 | s[row][col - 1] = s[row][col]; 76 | dfs(row, col - 1); 77 | } 78 | 79 | if (maze[row + 1][col] != 1 && visited[row + 1][col] == 0) 80 | { 81 | s[row + 1][col] = s[row][col]; 82 | dfs(row + 1, col); 83 | } 84 | 85 | if (maze[row - 1][col] != 1 && visited[row - 1][col] == 0) 86 | { 87 | s[row - 1][col] = s[row][col]; 88 | dfs(row - 1, col); 89 | } 90 | 91 | visited[row][col] = 0; 92 | // s[row][col] = ""; 93 | } 94 | 95 | int32_t main() 96 | { 97 | // for (int i = 0; i < 25; i++) 98 | // cout << arr[i] << nl; 99 | 100 | // exmp 1 101 | // dfs(0, 0); 102 | 103 | // exmp 2 104 | dfs(1, 0); 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /DSA_TOPICS/Topological_Sort/Using_BFS_kahnsAlgo_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class Graph 11 | { 12 | int V; // vertices 13 | unordered_map> adjList; // adj.List 14 | 15 | public: 16 | Graph(int vertices) // constructor 17 | { 18 | this->V = vertices; 19 | } 20 | 21 | void addEdge(int u, int v) 22 | { 23 | adjList[u].push_back(v); 24 | } 25 | 26 | /* 27 | Rules is : 28 | push only if its indegree is zero 29 | after every push decrease its child indegree and check its degree becomes 0 or not 30 | */ 31 | 32 | void topSort() 33 | { 34 | queue q; 35 | vector indegree(V); // we are taking nodes values from 0 to n. 36 | vector result; 37 | int visitedCount = 0; // is to check there is any circle or not 38 | 39 | // Calculate indegree 40 | for (auto p : adjList) 41 | { 42 | // int u = p.first; // comming from u 43 | for (auto v : p.second) 44 | { 45 | indegree[v]++; 46 | } 47 | } 48 | 49 | // insert vertices with 0 indegree in queue 50 | for (int i = 0; i < indegree.size(); i++) 51 | { 52 | if (indegree[i] == 0) 53 | q.push(i); 54 | } 55 | 56 | // BFS 57 | while (!q.empty()) 58 | { 59 | int x = q.front(); 60 | q.pop(); 61 | visitedCount++; 62 | result.push_back(x); 63 | 64 | // Reduce indegree of x's child 65 | for (auto dest : adjList[x]) 66 | { 67 | indegree[dest]--; 68 | if (indegree[dest] == 0) // checking any of child have 0 indegree or not 69 | q.push(dest); 70 | } 71 | } 72 | 73 | if (visitedCount != V) 74 | { 75 | cout << "There exists a cycle in the graph"; 76 | return; 77 | } 78 | else 79 | { 80 | for (int x : result) 81 | { 82 | cout << x << "\t"; 83 | } 84 | cout << endl; 85 | } 86 | } 87 | }; 88 | 89 | int main() 90 | { 91 | Graph g(6); 92 | g.addEdge(5, 2); 93 | g.addEdge(5, 0); 94 | g.addEdge(4, 0); 95 | g.addEdge(4, 1); 96 | g.addEdge(2, 3); 97 | g.addEdge(3, 1); 98 | 99 | g.topSort(); 100 | return 0; 101 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Offline_DSA1_SEC_E_Summer23/GitHub.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string getPoint(int x, int y) 5 | { 6 | return to_string(x) + to_string(y); 7 | } 8 | 9 | int getCost(int x1, int y1, int x2, int y2) 10 | { 11 | return abs(x1 - x2) + abs(y1 - y2); 12 | } 13 | 14 | int main() 15 | { 16 | 17 | int t, n, sx, sy, dx, dy, px, py, qx, qy, c; 18 | cin >> t; 19 | while (t--) 20 | { 21 | cin >> n; 22 | map>> adj; 23 | cin >> sx >> sy >> dx >> dy; 24 | string source = getPoint(sx, sy); 25 | string dest = getPoint(dx, dy); 26 | adj[source].push_back({dest, getCost(sx, sy, dx, dy)}); 27 | adj[dest].push_back({source, getCost(dx, dy, sx, sy)}); 28 | set>> points; 29 | for (int i = 0; i < n; i++) 30 | { 31 | cin >> px >> py >> qx >> qy >> c; 32 | string start = getPoint(px, py); 33 | string end = getPoint(qx, qy); 34 | for (auto currPoint : points) 35 | { 36 | int p1x = currPoint.second[0], p1y = currPoint.second[1]; 37 | adj[start].push_back({currPoint.first, getCost(px, py, p1x, p1y)}); 38 | adj[currPoint.first].push_back({start, getCost(px, py, p1x, p1y)}); 39 | adj[end].push_back({currPoint.first, getCost(qx, qy, p1x, p1y)}); 40 | adj[currPoint.first].push_back({end, getCost(qx, qy, p1x, p1y)}); 41 | } 42 | points.insert({start, {px, py}}); 43 | points.insert({end, {qx, qy}}); 44 | adj[start].push_back({end, c}); 45 | adj[end].push_back({start, c}); 46 | adj[source].push_back({start, getCost(sx, sy, px, py)}); 47 | adj[start].push_back({dest, getCost(px, py, dx, dy)}); 48 | adj[source].push_back({end, getCost(sx, sy, qx, qy)}); 49 | adj[end].push_back({dest, getCost(qx, qy, dx, dy)}); 50 | } 51 | priority_queue, vector>, greater>> pq; 52 | map isVisited; 53 | string curr = source; 54 | int minCost = 0; 55 | while (curr != dest) 56 | { 57 | isVisited[curr] = true; 58 | for (int i = 0; i < (int)adj[curr].size(); i++) 59 | { 60 | int cost = adj[curr][i].second; 61 | string endNode = adj[curr][i].first; 62 | if (isVisited[endNode]) 63 | continue; 64 | pq.push({minCost + cost, endNode}); 65 | } 66 | auto topNode = pq.top(); 67 | pq.pop(); 68 | curr = topNode.second; 69 | minCost = topNode.first; 70 | } 71 | cout << minCost << endl; 72 | } 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Graph/Problems/ShortestPath_using_BFS.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class Graph 11 | { 12 | int V; // vertices 13 | unordered_map> adjList; // adj.List 14 | 15 | public: 16 | Graph(int vertices) // constructor 17 | { 18 | this->V = vertices; 19 | } 20 | 21 | void addEdge(int u, int v) 22 | { 23 | adjList[u].push_back(v); 24 | adjList[v].push_back(u); 25 | } 26 | 27 | void findShortestPath(int source, int dest) 28 | { 29 | int parent[V]; 30 | parent[source] = -1; 31 | bool visited[V] = {0}; 32 | queue q; 33 | q.push(source); 34 | visited[source] = 1; 35 | 36 | while (!q.empty()) 37 | { 38 | int x = q.front(); 39 | q.pop(); 40 | for (auto i : adjList[x]) 41 | { 42 | if (!visited[i]) 43 | { 44 | visited[i] = 1; 45 | parent[i] = x; 46 | q.push(i); 47 | } 48 | } 49 | } 50 | // finding the shortest path 51 | int currentNode = dest; 52 | // we will go from dest to source 53 | int cnt = 0; 54 | stack s; 55 | s.push(currentNode); 56 | while (currentNode != source) 57 | { 58 | currentNode = parent[currentNode]; 59 | s.push(currentNode); 60 | cnt++; 61 | } 62 | while (!s.empty()) 63 | { 64 | cout << s.top() << " "; 65 | s.pop(); 66 | } 67 | cout << nl; 68 | } 69 | }; 70 | 71 | int main() 72 | { 73 | cout << "Enter number of vertices: "; 74 | int vertices, edges; 75 | cin >> vertices; 76 | cout << "Enter number of edges : "; 77 | cin >> edges; 78 | 79 | Graph g(vertices); 80 | for (int i = 0; i < edges; i++) 81 | { 82 | cout << "Enter edges from (u to v): "; 83 | int u, v; 84 | cin >> u >> v; 85 | g.addEdge(u, v); 86 | } 87 | 88 | cout << "enter source and destination " << nl; 89 | int s, d; 90 | cin >> s >> d; 91 | g.findShortestPath(s, d); 92 | 93 | return 0; 94 | } 95 | // question 2 96 | /* 97 | 6 8 98 | 99 | 0 1 100 | 0 5 101 | 1 2 102 | 2 3 103 | 2 6 104 | 3 4 105 | 4 6 106 | 5 6 107 | 108 | 109 | 110 | */ 111 | 112 | // question 1 113 | /* 114 | 115 | 5 4 116 | 117 | 0 2 118 | 2 4 119 | 0 1 120 | 1 3 121 | 122 | */ -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/CT_1/Q1.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | void bubbleSort(int arr[], int n) 24 | { 25 | for (int i = 0; i < n - 1; i++) 26 | { 27 | int swapped = 0; 28 | 29 | for (int j = 0; j < n - 1 - i; j++) 30 | { 31 | if (arr[j] > arr[j + 1]) 32 | { 33 | int temp = arr[j]; 34 | arr[j] = arr[j + 1]; 35 | arr[j + 1] = temp; 36 | swapped = 1; 37 | } 38 | } 39 | if (swapped == 0) 40 | break; 41 | } 42 | return; 43 | } 44 | 45 | /* 46 | 47 | Question: 48 | Find_the_number_of_elements_in_the_longest_subset_having_successive_numbers_from_an_unsorted_array_of_numbers 49 | 50 | */ 51 | 52 | int32_t main() 53 | { 54 | faster; 55 | 56 | int n; 57 | cout << "Enter array length \nthen elements" << nl; 58 | cin >> n; 59 | // 6 60 | // 4 1 2 7 6 5 61 | int arr[n]; 62 | for (int i = 0; i < n; i++) 63 | { 64 | cin >> arr[i]; 65 | } 66 | bubbleSort(arr, n); 67 | 68 | // for (int i = 0; i < n; i++) 69 | // { 70 | // cout << arr[i] << " "; 71 | // } 72 | // cout << nl; 73 | 74 | int maxlen = 0; 75 | int firstindex = 0; 76 | 77 | for (int i = 1; i < n; i++) 78 | { 79 | int len = 1; 80 | if (arr[i] == arr[i - 1] + 1) 81 | { 82 | len++; 83 | for (int j = i + 1; j < n; j++) 84 | { 85 | if (arr[j] == arr[j - 1] + 1) 86 | { 87 | len++; 88 | } 89 | else 90 | break; 91 | } 92 | } 93 | if (maxlen < len) 94 | { 95 | maxlen = len; 96 | firstindex = i - 1; 97 | } 98 | } 99 | 100 | cout << "max len " << maxlen << nl; 101 | for (int i = firstindex; i < firstindex + maxlen; i++) 102 | { 103 | cout << arr[i] << " "; 104 | } 105 | cout << endl; 106 | 107 | CRACKED; 108 | } 109 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/Summer23_Final_/Question3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl // NewLine 4 | #define null NULL 5 | using namespace std; 6 | 7 | int maze[5][5] = {0, 1, 0, 0, 2, 8 | 0, 0, 0, 1, 0, 9 | 0, 3, 1, 0, 0, 10 | 0, 1, 0, 1, 0, 11 | 0, 0, 0, 0, 0}; 12 | 13 | int visited[5][5] = {0}; 14 | 15 | string s[5][5]; 16 | 17 | void dfs(int row, int col, int search, int length) // 1 0 0 18 | { 19 | length++; 20 | // base case 21 | if (row < 0 || row > 4 || col < 0 || col > 4) 22 | return; 23 | 24 | // output 25 | // cout << "(" << row << " " << col << ")" << endl; 26 | 27 | s[row][col] += "( " + to_string(row) + " " + to_string(col) + " ), "; 28 | 29 | if (maze[row][col] == 3) 30 | { 31 | if (length <= 3) 32 | { 33 | s[row][col] = 'y' + s[row][col]; 34 | } 35 | search = 3; 36 | 37 | // s[row][col] = "y" + s[row][col]; // type 2 38 | 39 | // cout << "TASDJSHDAHS " << s[row][col] << nl; 40 | // cout << "TASDJSHDAHS" << nl; 41 | // cout << "TASDJSHDAHS " << s[row][col] << nl; 42 | } 43 | 44 | if (maze[row][col] == 2) 45 | { 46 | // cout << "TASDJSHDAHS " << s[row][col] << nl; 47 | // string str = s[row][col]; 48 | // if (str[0] == 'y') 49 | // { 50 | // cout << str << nl; 51 | // cout << "TASDJSHDAHS" << nl; 52 | // } 53 | 54 | if (search == 3) 55 | { 56 | // cout << "Path with treasure: "; 57 | // cout << s[row][col] << nl; 58 | 59 | string ss = s[row][col]; 60 | if (ss[0] == 'y') 61 | { 62 | cout << " Eureka and Can reach End " << nl; 63 | } 64 | else 65 | { 66 | cout << " Treasure hunt boring and Can reach End " << nl; 67 | } 68 | } 69 | } 70 | 71 | visited[row][col] = 1; 72 | 73 | // right 74 | if (maze[row][col + 1] != 1 && visited[row][col + 1] == 0) 75 | { 76 | s[row][col + 1] = s[row][col]; 77 | dfs(row, col + 1, search, length); 78 | } 79 | // left 80 | if (maze[row][col - 1] != 1 && visited[row][col - 1] == 0) 81 | { 82 | s[row][col - 1] = s[row][col]; 83 | dfs(row, col - 1, search, length); 84 | } 85 | // up 86 | if (maze[row + 1][col] != 1 && visited[row + 1][col] == 0) 87 | { 88 | s[row + 1][col] = s[row][col]; 89 | dfs(row + 1, col, search, length); 90 | } 91 | 92 | // down 93 | if (maze[row - 1][col] != 1 && visited[row - 1][col] == 0) 94 | { 95 | s[row - 1][col] = s[row][col]; 96 | dfs(row - 1, col, search, length); 97 | } 98 | 99 | visited[row][col] = 0; 100 | } 101 | 102 | 103 | int main() 104 | { 105 | 106 | // row col search length 107 | dfs(1, 0, 0, 0); 108 | CRACKED; 109 | } 110 | -------------------------------------------------------------------------------- /DSA_TOPICS/Queues/Problems/Implement_Queue_using_two_stack_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | /* 7 | 8 | implement enqueue and dequeue using only two stacks 9 | 10 | STEP 1 − We have two stacks, one for enqueueing and one for dequeueing. 11 | 12 | STEP 2 − We enqueue by pushing onto the enqueue stack. 13 | 14 | STEP 3 − We dequeue by popping from the dequeue stack. 15 | 16 | STEP 4 − If the dequeue stack is empty, we pop everything from the enqueue stack and push it onto the dequeue stack. 17 | 18 | STEP 5 − This reverses the order so that the oldest item is now at the top of the dequeue stack. 19 | 20 | */ 21 | 22 | class Stack // normal stack 23 | { 24 | private: 25 | static const int MAX_SIZE = 1000; 26 | int arr[MAX_SIZE]; 27 | int top; 28 | 29 | public: 30 | Stack() 31 | { 32 | top = -1; 33 | } 34 | 35 | void push(int x) 36 | { 37 | if (top == MAX_SIZE - 1) 38 | { 39 | cout << "Stack Overflow\n"; 40 | return; 41 | } 42 | arr[++top] = x; 43 | } 44 | 45 | int pop() 46 | { 47 | if (top == -1) 48 | { 49 | cout << "Stack Underflow\n"; 50 | return -1; // Return some invalid value to indicate an error 51 | } 52 | return arr[top--]; 53 | } 54 | 55 | bool isEmpty() 56 | { 57 | return top == -1; 58 | } 59 | }; 60 | 61 | class QueueUsingStacks 62 | { 63 | private: 64 | Stack stack_enqueue; 65 | Stack stack_dequeue; 66 | 67 | public: 68 | void enqueue(int x) 69 | { 70 | stack_enqueue.push(x); 71 | } 72 | 73 | int dequeue() 74 | { 75 | if (stack_dequeue.isEmpty()) 76 | { 77 | // Move all elements from the enqueue stack to the dequeue stack 78 | while (!stack_enqueue.isEmpty()) 79 | { 80 | stack_dequeue.push(stack_enqueue.pop()); 81 | } 82 | } 83 | 84 | if (stack_dequeue.isEmpty()) 85 | { 86 | cout << "Queue is empty, cannot dequeue.\n"; 87 | return -1; // Return some invalid value to indicate an error 88 | } 89 | 90 | return stack_dequeue.pop(); 91 | } 92 | 93 | bool isEmpty() 94 | { 95 | return stack_enqueue.isEmpty() && stack_dequeue.isEmpty(); 96 | } 97 | }; 98 | 99 | int main() 100 | { 101 | QueueUsingStacks queue; 102 | 103 | queue.enqueue(1); 104 | queue.enqueue(2); 105 | queue.enqueue(3); 106 | 107 | cout << "Dequeue: " << queue.dequeue() << endl; // Output: 1 108 | cout << "Dequeue: " << queue.dequeue() << endl; // Output: 2 109 | cout << "Is Empty: " << (queue.isEmpty() ? "true" : "false") << endl; // Output: false 110 | cout << "Dequeue: " << queue.dequeue() << endl; // Output: 3 111 | cout << "Is Empty: " << (queue.isEmpty() ? "true" : "false") << endl; // Output: true 112 | 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/Summer23_Final_/Question1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl; // NewLine 4 | using namespace std; 5 | 6 | int stackk[100]; // LIFO 7 | int top; 8 | 9 | void push(int value) 10 | { 11 | if (top >= 99) 12 | { 13 | cout << "STACK over flow" << nl; 14 | } 15 | else 16 | { 17 | top++; 18 | stackk[top] = value; 19 | } 20 | } 21 | 22 | int pop() 23 | { 24 | int value; 25 | if (top < 0) 26 | { 27 | cout << "Stack under flow" << nl; 28 | return -1; 29 | } 30 | else 31 | { 32 | value = stackk[top]; 33 | top--; 34 | return value; 35 | } 36 | } 37 | 38 | void printStack() 39 | { 40 | for (int i = 0; i <= top; i++) 41 | { 42 | cout << stackk[i] << " "; 43 | } 44 | } 45 | 46 | int popEnd() 47 | { 48 | int value; 49 | if (top < 0) 50 | { 51 | cout << "Stack under flow" << nl; 52 | return -1; 53 | } 54 | else 55 | { 56 | value = stackk[top]; 57 | top--; 58 | return value; 59 | } 60 | } 61 | 62 | int poipMagic() 63 | { 64 | // if ((top+1) % 2 == 0) 65 | // { 66 | // cout << " ËVEN " << nl; 67 | // } 68 | // else 69 | // { 70 | // cout << " Odd " << nl; 71 | // } 72 | 73 | if ((top + 1) % 2 == 0) 74 | { 75 | // cout << " ËVEN " << nl; 76 | 77 | int n = (top + 1) / 2; 78 | vector vec; 79 | int i; 80 | for (i = top; i > n; i--) 81 | { 82 | vec.push_back(stackk[i]); 83 | pop(); 84 | } 85 | int result = stackk[i]; 86 | 87 | int len = vec.size() / vec[0]; 88 | int arr[len]; 89 | len--; 90 | for (auto ii : vec) 91 | { 92 | arr[len] = ii; 93 | len--; 94 | } 95 | for (auto ii : arr) 96 | { 97 | push(ii); 98 | } 99 | 100 | return result; 101 | } 102 | else 103 | { 104 | // cout << " Odd " << nl; 105 | int n = (top) / 2; // 1 106 | vector vec; 107 | int i; 108 | for (i = top; i > n; i--) 109 | { 110 | vec.push_back(stackk[i]); 111 | pop(); 112 | } 113 | int result = stackk[i]; 114 | 115 | int len = vec.size() / vec[0]; 116 | int arr[len]; 117 | len--; 118 | for (auto ii : vec) 119 | { 120 | arr[len] = ii; 121 | len--; 122 | } 123 | for (auto ii : arr) 124 | { 125 | push(ii); 126 | } 127 | 128 | return result; 129 | } 130 | } 131 | 132 | int main() 133 | { 134 | top = -1; 135 | push(20); 136 | push(1); 137 | push(15); 138 | 139 | // cout<< popEnd(); 140 | printStack(); 141 | cout << nl; 142 | cout << popEnd(); 143 | cout << nl; 144 | printStack(); 145 | cout << nl; 146 | cout << popEnd(); 147 | printStack(); 148 | cout << nl; 149 | 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/MID_SEC_E_Spring_23/Q3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define nl endl; 3 | using namespace std; 4 | 5 | void printArray(int arr[], int len) 6 | { 7 | for (int i = 0; i < len; ++i) 8 | { 9 | cout << arr[i] << " "; 10 | } 11 | cout << nl; 12 | } 13 | 14 | void bubbleSort(int arr[], int n) 15 | { 16 | for (int i = 0; i < n - 1; i++) 17 | { 18 | int swapped = 0; 19 | 20 | for (int j = 0; j < n - 1 - i; j++) 21 | { 22 | if (arr[j] > arr[j + 1]) 23 | { 24 | int temp = arr[j]; 25 | arr[j] = arr[j + 1]; 26 | arr[j + 1] = temp; 27 | swapped = 1; 28 | } 29 | } 30 | if (swapped == 0) 31 | break; 32 | } 33 | return; 34 | } 35 | 36 | int SelectionSort(int arr[], int len) 37 | { 38 | int index = len / 2; 39 | 40 | for (int round = 0; round < len - 1; round++) 41 | { 42 | int min = round; 43 | 44 | for (int i = round + 1; i < len; i++) 45 | { 46 | if (arr[i] < arr[min]) 47 | { 48 | min = i; 49 | } 50 | } 51 | 52 | int temp = arr[round]; 53 | arr[round] = arr[min]; 54 | arr[min] = temp; 55 | } 56 | 57 | if (len % 2 == 0) 58 | { 59 | } 60 | else 61 | { 62 | } 63 | 64 | return -1; 65 | } 66 | 67 | void SelectionSort2(int arr[], int len) 68 | { 69 | int index; 70 | int sum = 0; 71 | for (int round = 0; round < len - 1; round++) 72 | { 73 | int min = round; 74 | 75 | for (int i = round + 1; i < len; i++) 76 | { 77 | if (arr[i] < arr[min]) 78 | { 79 | min = i; 80 | } 81 | } 82 | 83 | // swapping 84 | int temp = arr[round]; 85 | arr[round] = arr[min]; 86 | arr[min] = temp; 87 | 88 | if (round == (len / 2) - 1) 89 | { 90 | sum += arr[round]; 91 | if (len % 2 == 0) 92 | break; 93 | } 94 | if (round = (len / 2)) 95 | { 96 | sum += arr[round]; 97 | break; 98 | } 99 | } 100 | 101 | if (len % 2 == 0) 102 | { 103 | cout << "ANS " << sum / 2 << nl; 104 | } 105 | else 106 | cout << "ANS " << sum << nl 107 | } 108 | 109 | // 11 110 | // 1 3 2 0 10 7 4 8 9 6 5 111 | 112 | int main() 113 | { 114 | cout << "Enter array len then elements " << nl; 115 | int len; 116 | cin >> len; 117 | int arr[len]; 118 | for (int i = 0; i < len; i++) 119 | { 120 | cin >> arr[i]; 121 | } 122 | 123 | bubbleSort(arr, len); 124 | 125 | // for (int i = 0; i < len; i++) 126 | // { 127 | // cout << arr[i] << " "; 128 | // } 129 | 130 | // int arr[] = {1, 3, 2, 0, 10, 7, 4, 8, 9, 6, 5}; 131 | // int len = sizeof(arr) / sizeof(arr[0]); 132 | // int ans = SelectionSort(arr, len); 133 | 134 | // int ans = SelectionSort(arr, len); 135 | 136 | SelectionSort2(arr, len); 137 | 138 | return 0; 139 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Noman/Insert.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node(int data) 17 | { 18 | this->data = data; 19 | leftNode = NULL; 20 | rightNode = NULL; 21 | } 22 | }; 23 | 24 | node *buildTree(node *root) 25 | { 26 | int value; 27 | node *root; 28 | 29 | if (root->data < value) 30 | { 31 | if (root->rightNode == null) 32 | // root->rightNode=value 33 | buildTree(root); // right 34 | } 35 | else 36 | { 37 | if (root->leftNode == null) 38 | // root->leftNode=value 39 | buildTree(root); // left 40 | } 41 | } 42 | 43 | 44 | // node* insert(node* root, int key) 45 | // { 46 | // node* newNode = (node*) malloc(sizeof(node)); 47 | // newNode->value = key; 48 | 49 | // if(root == NULL) 50 | // return newNode; 51 | 52 | // node* traveller = root; 53 | // while(traveller != NULL) 54 | // { 55 | // if(traveller->left == NULL && key < traveller->value) 56 | // { 57 | // traveller->left = newNode; 58 | // newNode->parent = traveller; 59 | // break; 60 | // } 61 | // else if(traveller->right == NULL && key >= traveller->value) 62 | // { 63 | // traveller->right = newNode; 64 | // newNode->parent = traveller; 65 | // break; 66 | // } 67 | 68 | // if(key < traveller->value) 69 | // traveller = traveller->left; 70 | // else 71 | // traveller = traveller->right; 72 | 73 | // } 74 | // return root; 75 | // } 76 | 77 | 78 | //*********** recursively ********* 79 | node *search(node *root, int value) 80 | { 81 | if (root == null || root->data == value) 82 | return root; 83 | else 84 | { 85 | if (value < root->data) 86 | return search(root->leftNode, value); 87 | else 88 | return search(root->rightNode, value); 89 | } 90 | } 91 | 92 | /* //********** Iteratively ************** 93 | 94 | node *search(node *root, int value) 95 | { 96 | while (root != null && root->data != value) 97 | { 98 | if (root->data > value) 99 | root = root->leftNode; 100 | else 101 | root = root->rightNode; 102 | } 103 | return root; 104 | } 105 | 106 | */ 107 | 108 | int32_t main() 109 | { 110 | node *root = NULL; 111 | 112 | // creating tree 113 | root = buildTree(root); 114 | 115 | // input 116 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 117 | 118 | node *ptr = search(root, 5); 119 | if (ptr != null) 120 | { 121 | cout << "5 found on the tree" << nl; 122 | } 123 | else 124 | cout << "5 didnt find on the tree" << nl; 125 | 126 | CRACKED; 127 | } 128 | -------------------------------------------------------------------------------- /DSA_TOPICS/Tree/Binary_Search_Tree_(BST)/Deletion_in_BST.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class node 11 | { 12 | public: 13 | int data; 14 | node *leftNode; 15 | node *rightNode; 16 | node *parent; 17 | node(int data) 18 | { 19 | this->data = data; 20 | leftNode = NULL; 21 | rightNode = NULL; 22 | parent = NULL; 23 | } 24 | }; 25 | 26 | node *buildTree(node *root) 27 | { 28 | cout << "Enter the value of root: "; 29 | int data; 30 | cin >> data; 31 | root = new node(data); 32 | 33 | if (data == -1) 34 | { 35 | return NULL; 36 | } 37 | 38 | cout << "Enter left child of " << data << " " << nl; 39 | root->leftNode = buildTree(root->leftNode); 40 | 41 | cout << "Enter right child of " << data << " " << nl; 42 | root->rightNode = buildTree(root->rightNode); 43 | 44 | return root; 45 | } 46 | 47 | node *transplant(node *root, node *u, node *v) 48 | { 49 | if (u->parent->leftNode == u) 50 | { 51 | u->parent->leftNode = v; 52 | v->parent = u->parent; 53 | } 54 | else if (u->parent->rightNode == u) 55 | { 56 | u->parent->rightNode = v; 57 | v->parent = u->parent; 58 | } 59 | else 60 | root = v; 61 | return v; 62 | } 63 | 64 | node *minimumOfTheTree(node *root) 65 | { 66 | while (root->leftNode != null) 67 | root = root->leftNode; 68 | 69 | return root; 70 | } 71 | 72 | node *successor(node *ptr) 73 | { 74 | // case 1: 75 | // have right child 76 | if (ptr->rightNode != null) 77 | { 78 | return minimumOfTheTree(ptr->rightNode); 79 | } 80 | // case 2: 81 | // have no roght child 82 | else 83 | { 84 | node *traveller = ptr->parent; 85 | while (traveller != null && traveller->rightNode == ptr) 86 | { 87 | ptr = traveller; 88 | traveller = ptr->parent; 89 | } 90 | 91 | return traveller; 92 | } 93 | } 94 | 95 | void deletion(node *root, node *z) 96 | { 97 | // case 1 : One node 98 | // case 2: no child 99 | if (z->leftNode == null) 100 | transplant(root, z, z->rightNode); 101 | else if (z->rightNode == null) 102 | transplant(root, z, z->leftNode); 103 | else 104 | { 105 | node *succ = successor(z); 106 | transplant(root, succ, succ->rightNode); // take successor right at the place of successor 107 | // these works done by the transplant 108 | // succ->rightNode->parent = succ->parent; 109 | 110 | transplant(root, z, succ); // take successor at the place of z 111 | // succ->leftNode = z->leftNode; 112 | // succ->rightNode = z->rightNode; 113 | } 114 | } 115 | 116 | int32_t main() 117 | { 118 | node *root = NULL; 119 | 120 | // creating tree 121 | root = buildTree(root); 122 | 123 | // input 124 | // 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 125 | 126 | CRACKED; 127 | } 128 | -------------------------------------------------------------------------------- /DSA_LAB_EXAM_Question_Solve/joyasree_assignment/Question_3_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node *prev; 11 | }; 12 | 13 | Node *LL_insert_to_beginning(Node *head, int newData) 14 | { // needs assignment 15 | Node *newNode = new Node(); 16 | newNode->data = newData; 17 | newNode->next = head; 18 | newNode->prev = head->prev; 19 | head->prev = newNode; 20 | return newNode; 21 | } 22 | 23 | void LL_insert_to_beginning(Node **head, int newData) 24 | { // One without assignment needed 25 | Node *newNode = new Node(); 26 | newNode->data = newData; 27 | Node *temp = *head; 28 | 29 | newNode->prev = temp->prev; 30 | newNode->next = temp; 31 | temp->prev = newNode; 32 | *head = newNode; 33 | } 34 | 35 | void LL_insert_after(Node *head, int newData, int target) 36 | { 37 | Node *temp = head; 38 | while (temp->next != head && temp->data != target) 39 | { 40 | temp = temp->next; 41 | } 42 | Node *newNode = new Node(); 43 | newNode->data = newData; 44 | 45 | newNode->next = temp->next; 46 | temp->next->prev = newNode; 47 | temp->next = newNode; 48 | newNode->prev = temp; 49 | } 50 | 51 | void LL_insert_at_end(Node *head, int newData) 52 | { 53 | Node *temp = head; 54 | while (temp->next != head) 55 | { 56 | temp = temp->next; 57 | } 58 | Node *newNode = new Node(); 59 | newNode->data = newData; 60 | newNode->next = temp->next; 61 | temp->next = newNode; 62 | newNode->prev = temp; 63 | } 64 | 65 | void LL_delete_node(Node **head, int hasValue) 66 | { 67 | Node *temp = *head; 68 | if (temp->data == hasValue) 69 | { 70 | temp->next->prev = temp->prev; 71 | temp->prev->next = temp->next; 72 | *head = temp->next; 73 | free(temp); 74 | return; 75 | } 76 | temp = temp->next; 77 | Node *prevNode; 78 | while (temp != *head && temp->data != hasValue) 79 | { 80 | prevNode = temp; 81 | temp = temp->next; 82 | } 83 | if (temp == *head) 84 | { 85 | cout << "The item doesn't exist \n"; 86 | return; 87 | } 88 | prevNode->next = temp->next; 89 | temp->next->prev = prevNode; 90 | free(temp); 91 | } 92 | 93 | bool searchngitems(Node *head, int data) 94 | { 95 | Node *traveller = head; 96 | if (traveller->data == data) 97 | return true; 98 | traveller = traveller->next; 99 | while (traveller->next != head) 100 | { 101 | if (traveller->data == data) 102 | return true; 103 | traveller = traveller->next; 104 | } 105 | return false; 106 | } 107 | 108 | int main() 109 | { 110 | Node *head; 111 | 112 | head = new Node(); 113 | head->data = 1; 114 | 115 | head = LL_insert_to_beginning(head, 3); 116 | LL_insert_to_beginning(&head, 4); 117 | 118 | LL_insert_after(head, 2, 3); 119 | 120 | LL_insert_at_end(head, 0); 121 | 122 | LL_delete_node(&head, 10); 123 | Node *Cursor; 124 | Cursor = head; 125 | while (Cursor) 126 | { 127 | cout << Cursor->data << endl; 128 | Cursor = Cursor->next; 129 | } 130 | return 0; 131 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Offline_DSA1_SEC_E_Summer23/Warmhole.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int M = 201; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin >> t; 10 | 11 | while (t--) 12 | { 13 | int n; // how many wormnhole has 14 | cin >> n; 15 | 16 | int sx, sy, dx, dy; // input and output 17 | cin >> sx >> sy >> dx >> dy; 18 | 19 | // warmholes inputs 20 | /* 21 | 22 | 1 < t < 10 23 | 1 < N < 6 24 | -1 < x < 101 25 | -1 < y < 101 26 | 0 < cost < 101 27 | 28 | */ 29 | 30 | /* 31 | vector 32 | // value 33 | vector, int>> 34 | pair, int> 35 | pair 36 | int 37 | 38 | */ 39 | vector, int>> adj[M][M]; // {co-ordinate,cost} 40 | 41 | for (int i = 0; i < n; i++) 42 | { 43 | int ax, ay, bx, by, t; 44 | cin >> ax >> ay >> bx >> by >> t; 45 | 46 | adj[ax][ay].push_back({{bx, by}, t}); 47 | adj[bx][by].push_back({{ax, ay}, t}); 48 | } 49 | 50 | bool processed[M][M]; // colouring 51 | int dis[M][M]; // distance / cost 52 | 53 | for (int i = 0; i < M; i++) 54 | { 55 | for (int j = 0; j < M; j++) 56 | { 57 | 58 | if (i - 1 >= 0) 59 | adj[i][j].push_back({{i - 1, j}, 1}); 60 | if (j - 1 >= 0) 61 | adj[i][j].push_back({{i, j - 1}, 1}); 62 | if (i + 1 < M) 63 | adj[i][j].push_back({{i + 1, j}, 1}); 64 | if (j + 1 < M) 65 | adj[i][j].push_back({{i, j + 1}, 1}); 66 | 67 | processed[i][j] = false; // nothing else but making all index 0 = {0} 68 | dis[i][j] = 1e9; // not visited 69 | } 70 | } 71 | 72 | dis[sx][sy] = 0; // starting position 73 | 74 | priority_queue>> pq; 75 | 76 | pq.push({0, {sx, sy}}); // entering starting position 77 | 78 | while (!pq.empty()) // BFS 79 | { 80 | auto u = pq.top().second; // pair 81 | int ux = u.first; // x coordinate 82 | int uy = u.second; // y coordinate 83 | // cout< dis[ux][uy] + t) // new pos < -- > old pos + cost 98 | { 99 | dis[vx][vy] = dis[ux][uy] + t; 100 | pq.push({-dis[vx][vy], {vx, vy}}); 101 | } 102 | } 103 | } 104 | cout << dis[dx][dy] << "\n"; 105 | } 106 | return 0; 107 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Heap/Max_Heapify_Simulation_TopToBottom_approach.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | using namespace std; 8 | 9 | void printArray(int arr[], int len) 10 | { 11 | cout << "Printing Array : "; 12 | for (int i = 0; i < len; i++) 13 | cout << arr[i] << " "; 14 | cout << endl; 15 | } 16 | 17 | int Total_Call_count_of_maxheapify = 0; 18 | 19 | // heapify function for the max heap 20 | // top to bottom approach 21 | void maxheapify(int arr[], int size, int index) // maxheapify (arr, length - 1, i) 22 | { 23 | int leftChild = index * 2; 24 | int rightChild = index * 2 + 1; 25 | 26 | // debug code 27 | { 28 | cout << "Index " << index << nl; 29 | if (leftChild <= size) 30 | cout << "Left child = " << arr[leftChild] << " "; 31 | if (rightChild <= size) 32 | cout << "Right child = " << arr[rightChild]; 33 | cout << nl; 34 | } 35 | 36 | int largest = index; // parent 37 | 38 | if (leftChild <= size && arr[largest] < arr[leftChild]) 39 | { 40 | largest = leftChild; 41 | } 42 | 43 | if (rightChild <= size && arr[largest] < arr[rightChild]) 44 | { 45 | largest = rightChild; 46 | } 47 | 48 | if (largest != index) 49 | { 50 | cout << "swap " << arr[largest] << " < - - > " << arr[index] << nl; 51 | swap(arr[largest], arr[index]); 52 | printArray(arr, size + 1); 53 | cout << nl; 54 | 55 | cout << "------ Recursion ------" << nl; 56 | Total_Call_count_of_maxheapify++; 57 | maxheapify(arr, size, largest); 58 | } 59 | cout << nl; 60 | } 61 | 62 | void buildMaxHeap(int arr[], int length) 63 | { 64 | for (int i = length / 2; i >= 1; i--) 65 | { 66 | cout << "Calling maxHeapify index = " << i << " value = " << arr[i] << nl; 67 | cout << "``````````````````````````````````````````" << nl; 68 | Total_Call_count_of_maxheapify++; 69 | maxheapify(arr, length - 1, i); 70 | } 71 | } 72 | 73 | int32_t main() 74 | { 75 | 76 | // check this all arrays visualization onr by one to understand heapify clearly 77 | 78 | // int arr[] = {-1, 123, 33, 32134, 343, 31, 3, 34, 5, 44, 42, 4}; 79 | // int arr[] = {-1, 123, 33, 32134, 343, 31, 3, 34, 5, 44, 42, 4455454}; 80 | int arr[] = {-1, 9, 3, 2, 4, 1, 16, 14, 10, 8, 7}; // Good Example 81 | // int arr[] = {-1, 50,30,15,19,20,10,5,2}; 82 | // int arr[] = {-1, 12, 11, 13, 5, 6, 7}; 83 | // int arr[] = {-1, 2, 10, 4, 7, 1, 8, 9, 3, 6, 5}; 84 | 85 | int length = sizeof(arr) / sizeof(arr[0]); 86 | 87 | cout << nl; 88 | cout << "Given Array : "; 89 | printArray(arr, length); 90 | 91 | cout << "Length " << length << nl; 92 | cout << nl; 93 | cout << "---------- Calling build Max Heap Function --------" << nl << nl; 94 | buildMaxHeap(arr, length); 95 | cout << nl; 96 | 97 | cout << "Final Print " << nl; 98 | printArray(arr, length); 99 | cout << nl; 100 | 101 | cout << "Total_Call_count_of_maxheapify " << Total_Call_count_of_maxheapify << nl; 102 | cout << nl; 103 | 104 | CRACKED; 105 | } 106 | -------------------------------------------------------------------------------- /DSA_TOPICS/Application/Infix_To_postfix.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl 7 | using namespace std; 8 | 9 | string s2 = ""; 10 | 11 | void handleExpressions(stack &expressions, char c) 12 | { 13 | if (expressions.empty() || c == '(' || c == '^') 14 | { 15 | expressions.push(c); 16 | } 17 | else 18 | { 19 | if (c == ')') 20 | { 21 | while (expressions.top() != '(') 22 | { 23 | // if (expressions.empty()) 24 | // break; 25 | s2 += expressions.top(); 26 | // cout << s2 << nl; 27 | // cout << expressions.top(); 28 | expressions.pop(); 29 | } 30 | expressions.pop(); 31 | } 32 | else if (c == '+' || c == '-') 33 | { 34 | while (expressions.empty() != 1) // pop all elements from the stack 35 | { 36 | if (expressions.top() == '(') 37 | break; 38 | s2 += expressions.top(); 39 | // cout << s2 << nl; 40 | // cout << expressions.top(); 41 | expressions.pop(); 42 | } 43 | expressions.push(c); 44 | } 45 | else 46 | { 47 | if (expressions.top() == '+' || expressions.top() == '-') 48 | { 49 | expressions.push(c); 50 | } 51 | else 52 | { 53 | while (expressions.top() == '*' || expressions.top() == '/' || expressions.top() == '^') 54 | { 55 | if (expressions.empty() || expressions.top() == '(') 56 | break; 57 | s2 += expressions.top(); 58 | // cout << s2 << nl; 59 | // cout << expressions.top(); 60 | expressions.pop(); 61 | } 62 | // cout << "Working for " << c << nl; 63 | expressions.push(c); 64 | } 65 | } 66 | } 67 | } 68 | 69 | int32_t main() 70 | { 71 | cout << "Enter Your infix expression : "; 72 | string s; 73 | cin >> s; 74 | 75 | // in: 8*(5^4+2)-6^2/(9*3) 76 | // post: 854^2+*62^93*/- 77 | 78 | // in: 9+2-5*6/3+2^3/4 79 | // post: 92+56*3/-23^4/+ 80 | // ans 3 81 | 82 | // in: 1+3^2+(5*6-4)*7 83 | // post: 132^+56*4-7*+ 84 | 85 | stack expressions; 86 | for (int i = 0; i < s.length(); i++) 87 | { 88 | if (s[i] >= '0' && s[i] <= '9') 89 | { 90 | // cout << "INPUT " << s[i] << nl; 91 | s2 += s[i]; 92 | // cout << s2 << nl; 93 | // cout << s[i]; 94 | } 95 | else 96 | { 97 | // cout << "INPUT " << s[i] << nl; 98 | handleExpressions(expressions, s[i]); 99 | } 100 | } 101 | while (expressions.empty() != 1) 102 | { 103 | s2 += expressions.top(); 104 | // cout << s2 << nl; 105 | // cout << expressions.top(); 106 | expressions.pop(); 107 | } 108 | 109 | cout << nl << "Your postfix expression : " << s2 << nl << nl; 110 | 111 | CRACKED; 112 | } 113 | -------------------------------------------------------------------------------- /DSA_TOPICS/LinkedList/Single_Linked_list/Insertion.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define faster \ 6 | ios_base::sync_with_stdio(false); \ 7 | cin.tie(0); \ 8 | cout.tie(0); 9 | #define CRACKED return 0; 10 | #define nl endl; // NewLine 11 | 12 | #define int long long 13 | #define output(x) cout << x << nl // out 14 | #define printarray(arr, len) \ 15 | for (int i = 0; i < len; i++) \ 16 | { \ 17 | cout << arr[i] << " "; \ 18 | if (i + 1 == len) \ 19 | cout << endl; \ 20 | } // array print 21 | using namespace std; 22 | 23 | // by using structure 24 | /* 25 | typedef struct Node 26 | { 27 | int value; 28 | Node *nextNode; 29 | 30 | } Node; 31 | */ 32 | 33 | // by using class 34 | class Node 35 | { 36 | public: 37 | int value = 0; 38 | Node *nextNode = NULL; 39 | 40 | // constructor 41 | Node(int value) 42 | { 43 | this->value = value; 44 | this->nextNode = NULL; 45 | } 46 | }; 47 | 48 | void InsertAtFirst(Node *&head, int data) 49 | { 50 | Node *newNode = new Node(data); 51 | newNode->nextNode = head; 52 | head = newNode; 53 | } 54 | 55 | void InsertAtLast(Node *&tail, int data) 56 | { 57 | Node *newNode = new Node(data); 58 | tail->nextNode = newNode; 59 | tail = newNode; 60 | // tail = tail->nextNode; // another way 61 | } 62 | 63 | void InsertAtposition(Node *&head, Node *&tail, int position, int data) 64 | { 65 | // Insert in the fist postion 66 | if (position == 1) 67 | { 68 | InsertAtFirst(head, data); 69 | return; 70 | } 71 | 72 | Node *traveler = head; 73 | Node *nodeToInsert = new Node(data); 74 | int cnt = 1; 75 | 76 | while (cnt < position - 1) 77 | { 78 | traveler = traveler->nextNode; 79 | cnt++; 80 | } 81 | 82 | // inserting at last position 83 | if (traveler->nextNode == NULL) 84 | { 85 | InsertAtLast(tail, data); 86 | return; 87 | } 88 | 89 | nodeToInsert->nextNode = traveler->nextNode; 90 | traveler->nextNode = nodeToInsert; 91 | } 92 | 93 | void printLinkedList(Node *&head) 94 | { 95 | Node *tempHead = head; 96 | while (tempHead != NULL) 97 | { 98 | cout << tempHead->value << " "; 99 | tempHead = tempHead->nextNode; 100 | } 101 | cout << nl; 102 | } 103 | 104 | int32_t main() 105 | { 106 | faster; 107 | 108 | // creat head 109 | Node *head = NULL; 110 | 111 | // new node creat 112 | Node *newNode = new Node(100); 113 | head = newNode; 114 | cout << "In the beginning : "; 115 | printLinkedList(head); 116 | 117 | // insert in the last 118 | Node *tail = newNode; 119 | InsertAtLast(tail, 190); 120 | 121 | cout << "After Insertion in Last : "; 122 | printLinkedList(head); 123 | 124 | // insert in the begining 125 | InsertAtFirst(head, 1); 126 | InsertAtFirst(head, 12); 127 | InsertAtFirst(head, 13); 128 | 129 | // print linked list 130 | cout << "After Insertion in fast : "; 131 | printLinkedList(head); 132 | 133 | InsertAtposition(head, tail, 6, 2); 134 | 135 | cout << "After Insert_in_a_position : "; 136 | printLinkedList(head); 137 | 138 | cout << "Head value = " << head->value; 139 | cout << " && Tail value = " << tail->value << nl; 140 | 141 | CRACKED; 142 | } -------------------------------------------------------------------------------- /DSA_TOPICS/Heap/Heap_Class_Insertion_Deletion_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | class Heap 10 | { 11 | public: 12 | int arr[100]; 13 | int size = 0; 14 | Heap() 15 | { 16 | size = 0; 17 | arr[0] = -1; // index 1 base program 18 | } 19 | 20 | // void insertion(int value) // my logic 21 | // { 22 | // size++; 23 | // int index = size; 24 | // arr[index] = value; 25 | // int parent = index / 2; 26 | // while (arr[parent] < arr[index] && index > 1) 27 | // { 28 | // swap(arr[parent], arr[index]); 29 | // index = parent; 30 | // parent = index / 2; 31 | // } 32 | // } 33 | 34 | void insertion(int value) 35 | { 36 | size++; // arry size ill be size+1 cz its a 1 base program 37 | int index = size; 38 | arr[index] = value; 39 | 40 | maxHeapify_bottom_to_top(index); 41 | // 10 5 3 2 4 42 | } 43 | 44 | void maxHeapify_bottom_to_top(int index) // for insertion 45 | { 46 | int parent = index / 2; 47 | 48 | if (parent >= 1 && arr[parent] < arr[index]) 49 | { 50 | swap(arr[index], arr[parent]); 51 | maxHeapify_bottom_to_top(parent); 52 | } 53 | else 54 | return; 55 | } 56 | 57 | int deletion() // max heap deletion 58 | { 59 | if (size == 0) 60 | { 61 | cout << "Heap Empty Noting to delete" << nl; 62 | return -1; 63 | } 64 | 65 | int index = 1; 66 | arr[index] = arr[size]; // take root to the last node [swap(arr[index],arr[size]); // also correct] 67 | size--; // remove last node 68 | 69 | // take root to its correct position 70 | maxheapify(index); 71 | } 72 | 73 | // heapify function for the max heap 74 | // top to bottom approach for deletion 75 | void maxheapify(int index) 76 | { 77 | int leftChild = index * 2; 78 | int rightChild = index * 2 + 1; 79 | 80 | int largest = index; // parent 81 | 82 | if (leftChild <= size && arr[largest] < arr[leftChild]) 83 | { 84 | largest = leftChild; 85 | } 86 | 87 | if (rightChild <= size && arr[largest] < arr[rightChild]) 88 | { 89 | largest = rightChild; 90 | } 91 | 92 | if (largest != index) 93 | { 94 | swap(arr[largest], arr[index]); 95 | maxheapify(largest); 96 | } 97 | } 98 | 99 | void print() 100 | { 101 | for (int i = 1; i <= size; i++) 102 | { 103 | cout << arr[i] << " "; 104 | } 105 | cout << nl; 106 | } 107 | }; 108 | 109 | int32_t main() 110 | { 111 | // Heap h; 112 | // h.insertion(12); 113 | // h.insertion(9); 114 | // h.insertion(124); 115 | // h.insertion(34); 116 | // h.insertion(39); 117 | // h.print(); 118 | 119 | Heap h; 120 | h.insertion(10); 121 | h.insertion(2); 122 | h.insertion(3); 123 | h.insertion(4); 124 | h.insertion(5); 125 | h.print(); 126 | 127 | h.insertion(16); // new insert 128 | h.print(); 129 | 130 | // Final Heap will be: 131 | // 16 132 | // / \ 133 | // 5 10 134 | // / \ / 135 | // 2 4 3 136 | 137 | CRACKED; 138 | } 139 | --------------------------------------------------------------------------------