├── Array └── Intersection.py ├── Binary Search Tree ├── BST_Deletion.ipynb └── Binary_Search_Tree.ipynb ├── Graph Traversal └── Graph_Traversal.ipynb ├── Hashing ├── HashTable.ipynb └── student_score.csv ├── Heap Data Structure └── Max-3-Product.py ├── Iterator vs Generator └── Python-Function.ipynb ├── Linked List ├── Doubly_Linked_List.ipynb ├── LinkedList_Operations.ipynb └── Reversal_of_Linked_List.ipynb ├── README.md └── Stack and Queue └── Stack_and_Queue.ipynb /Array/Intersection.py: -------------------------------------------------------------------------------- 1 | # Method definition 2 | def intersect_array(arr1, arr2): 3 | set_arr1 = set(arr1) 4 | set_arr2 = set(arr2) 5 | 6 | if set_arr1 < set_arr2: 7 | return [x for x in set_arr1 if x in set_arr2] 8 | else: 9 | return [x for x in set_arr2 if x in set_arr1] 10 | 11 | # Driver code 12 | arr1 = [1, 3, 5, 7, 9, 10, 12] 13 | arr2 = [2, 5, 6, 8, 12] 14 | 15 | result = [] 16 | result = intersect_array(arr1, arr2) 17 | print("Intersected values between arr1 and arr2:") 18 | 19 | for i in range(len(result)): 20 | print(result[i], end=' ') -------------------------------------------------------------------------------- /Binary Search Tree/BST_Deletion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "BST Deletion.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "code", 20 | "metadata": { 21 | "colab": { 22 | "base_uri": "https://localhost:8080/" 23 | }, 24 | "id": "pD6VRcpitu1Z", 25 | "outputId": "0960ac4e-b454-47b2-cb67-e4b30bae1082" 26 | }, 27 | "source": [ 28 | "class BinarySearchTreeNode:\n", 29 | " def __init__(self, data):\n", 30 | " self.left = None\n", 31 | " self.right = None\n", 32 | " self.val = data\n", 33 | "\n", 34 | "## Method definition to do insertion in Binary Search Tree\n", 35 | "def insert_BST(root, data):\n", 36 | " if root is None:\n", 37 | " return BinarySearchTreeNode(data)\n", 38 | " else:\n", 39 | " ## It is not allowing duplicate values\n", 40 | " if root.val == data:\n", 41 | " return root\n", 42 | " ## If data is greater than the root element\n", 43 | " ## Traverse at the right hand side\n", 44 | " elif root.val < data:\n", 45 | " root.right = insert_BST(root.right, data)\n", 46 | " else:\n", 47 | " ## If data is lesser than the root element\n", 48 | " ## Traverse at the left hand side\n", 49 | " root.left = insert_BST(root.left, data)\n", 50 | " return root\n", 51 | "\n", 52 | "## Method definition to do inorder traversal\n", 53 | "def inorder(root):\n", 54 | " if root:\n", 55 | " inorder(root.left)\n", 56 | " print(root.val, end = \" \")\n", 57 | " inorder(root.right)\n", 58 | "\n", 59 | "## Method definition to find minimum value in BST\n", 60 | "def minValueNode(node):\n", 61 | " curr = node\n", 62 | " while(curr.left is not None):\n", 63 | " curr = curr.left\n", 64 | "\n", 65 | " return curr\n", 66 | "\n", 67 | "# Given a binary search tree and a data, this function\n", 68 | "# delete the key and returns the new root\n", 69 | "def deleteNode(root, data):\n", 70 | " # Base Case\n", 71 | " if root is None:\n", 72 | " return root\n", 73 | " # If the data to be deleted\n", 74 | " # is smaller than the root's\n", 75 | " # data then it lies in left subtree\n", 76 | " if (data < root.val):\n", 77 | " root.left = deleteNode(root.left, data)\n", 78 | " \n", 79 | " # If the data to be delete\n", 80 | " # is greater than the root's\n", 81 | " # data then it lies in right subtree\n", 82 | " elif (data > root.val):\n", 83 | " root.right = deleteNode(root.right, data)\n", 84 | " \n", 85 | " # If data is same as root's data, then this is the node\n", 86 | " # to be deleted\n", 87 | " # Case 2\n", 88 | " else:\n", 89 | " # Node with only one child or no child\n", 90 | " if root.left is None:\n", 91 | " temp = root.right\n", 92 | " root = None\n", 93 | " return temp\n", 94 | " elif root.right is None:\n", 95 | " temp = root.left\n", 96 | " root = None\n", 97 | " return temp\n", 98 | " \n", 99 | " # Node with two children:\n", 100 | " # Get the inorder successor\n", 101 | " # (smallest in the right subtree)\n", 102 | " # Case 3\n", 103 | " temp = minValueNode(root.right)\n", 104 | " \n", 105 | " # Copy the inorder successor's\n", 106 | " # content to this node\n", 107 | " root.val = temp.val\n", 108 | " \n", 109 | " # Delete the inorder successor\n", 110 | " root.right = deleteNode(root.right, temp.val)\n", 111 | " \n", 112 | " return root\n", 113 | "\n", 114 | "r = BinarySearchTreeNode(50)\n", 115 | "r = insert_BST(r, 30)\n", 116 | "r = insert_BST(r, 20)\n", 117 | "r = insert_BST(r, 40)\n", 118 | "r = insert_BST(r, 70)\n", 119 | "r = insert_BST(r, 60)\n", 120 | "r = insert_BST(r, 80)\n", 121 | "inorder(r)\n", 122 | "r = deleteNode(r,30)\n", 123 | "print(\"After DELETION of 30\")\n", 124 | "inorder(r)\n", 125 | "r = deleteNode(r,70)\n", 126 | "print(\"After DELETION of 70\")\n", 127 | "inorder(r)" 128 | ], 129 | "execution_count": 20, 130 | "outputs": [ 131 | { 132 | "output_type": "stream", 133 | "text": [ 134 | "20 30 40 50 60 70 80 After DELETION of 30\n", 135 | "20 40 50 60 70 80 After DELETION of 70\n", 136 | "20 40 50 60 80 " 137 | ], 138 | "name": "stdout" 139 | } 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": { 145 | "id": "tJrVQdCcPD1-" 146 | }, 147 | "source": [ 148 | "**Excercise Problem**\n", 149 | "\n", 150 | "Use maxValueNode function in above code and then use maximum value from left subtree in order to do deletion operation in case 3. \n", 151 | "\n" 152 | ] 153 | } 154 | ] 155 | } -------------------------------------------------------------------------------- /Binary Search Tree/Binary_Search_Tree.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Binary Search Tree.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "b-i3zJL8YGKo" 22 | }, 23 | "source": [ 24 | "**Binary Search Tree Implementation**\n", 25 | "\n", 26 | "Operations :\n", 27 | "* Insertion of an element\n", 28 | "* Searching of an element\n", 29 | "* Inorder Traversal" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "metadata": { 35 | "colab": { 36 | "base_uri": "https://localhost:8080/" 37 | }, 38 | "id": "LGUx3-fO80Pm", 39 | "outputId": "a2b1e43f-4f79-4dc7-eb0e-bb65664e41aa" 40 | }, 41 | "source": [ 42 | "class BinarySearchTreeNode:\n", 43 | " def __init__(self, data):\n", 44 | " self.left = None\n", 45 | " self.right = None\n", 46 | " self.val = data\n", 47 | "\n", 48 | "## Method definition to do insertion in Binary Search Tree\n", 49 | "def insert_BST(root, data):\n", 50 | " if root is None:\n", 51 | " return BinarySearchTreeNode(data)\n", 52 | " else:\n", 53 | " ## It is not allowing duplicate values\n", 54 | " if root.val == data:\n", 55 | " return root\n", 56 | " ## If data is greater than the root element\n", 57 | " ## Traverse at the right hand side\n", 58 | " elif root.val < data:\n", 59 | " root.right = insert_BST(root.right, data)\n", 60 | " else:\n", 61 | " ## If data is lesser than the root element\n", 62 | " ## Traverse at the left hand side\n", 63 | " root.left = insert_BST(root.left, data)\n", 64 | " return root\n", 65 | "\n", 66 | "## Method definition to do inorder traversal\n", 67 | "def inorder(root):\n", 68 | " if root:\n", 69 | " inorder(root.left)\n", 70 | " print(root.val, end = \" \")\n", 71 | " inorder(root.right)\n", 72 | "\n", 73 | "## Method definition to do search in BST\n", 74 | "def search_BST(root, data):\n", 75 | " while root != None:\n", 76 | " ## data might be in right subtree\n", 77 | " if root.val < data:\n", 78 | " return search_BST(root.right, data)\n", 79 | " ## data might be in left subtree\n", 80 | " elif root.val > data:\n", 81 | " return search_BST(root.left, data)\n", 82 | " else:\n", 83 | " return True\n", 84 | " return False\n", 85 | "\n", 86 | "r = BinarySearchTreeNode(50)\n", 87 | "r = insert_BST(r, 30)\n", 88 | "r = insert_BST(r, 20)\n", 89 | "r = insert_BST(r, 40)\n", 90 | "r = insert_BST(r, 70)\n", 91 | "r = insert_BST(r, 60)\n", 92 | "r = insert_BST(r, 80)\n", 93 | "print(search_BST(r, 75))\n", 94 | "inorder(r)" 95 | ], 96 | "execution_count": 11, 97 | "outputs": [ 98 | { 99 | "output_type": "stream", 100 | "text": [ 101 | "False\n", 102 | "20 30 40 50 60 70 80 " 103 | ], 104 | "name": "stdout" 105 | } 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": { 111 | "id": "bGb4scTaYhjn" 112 | }, 113 | "source": [ 114 | "**Time Complexity :**\n", 115 | "\n", 116 | "Worst case time complexity of searching and insertion of an element in a BST is O(h) where h is the height of the BST. Because in worst case we may have to travel from root node to the leaf node and the height of the skewed binary search tree is n. \n", 117 | "\n", 118 | "So, overall time complexity of insertion and searching in BST is O(n)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": { 124 | "id": "m0RIYqR2ZLDs" 125 | }, 126 | "source": [ 127 | "**Assignment Problem :**\n", 128 | "\n", 129 | "Write the method definition of below operations on Binary Search Tree.\n", 130 | "\n", 131 | "* find_max() - to find the maximum element in BST\n", 132 | "* find_min() - to find the minimum element in BST\n", 133 | "* count_nodes() - to count the total number of nodes in BST\n", 134 | "* Postorder() - to do the postorder traversal of BST\n", 135 | "* Preorder() - to do the preorder traversal of BST\n", 136 | "\n" 137 | ] 138 | } 139 | ] 140 | } -------------------------------------------------------------------------------- /Graph Traversal/Graph_Traversal.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Graph Traversal.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "tJQVlEp0nF49" 22 | }, 23 | "source": [ 24 | "**Depth First Search Implementation**\n", 25 | "\n", 26 | "**Used Data Structure : Stack**" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "colab": { 33 | "base_uri": "https://localhost:8080/" 34 | }, 35 | "id": "hYnBMeaCladm", 36 | "outputId": "77cc37c3-c13a-452b-da07-f3b9041c27ca" 37 | }, 38 | "source": [ 39 | "## Graph Representation\n", 40 | "graph = {\n", 41 | " 'A' : ['B','C','D'],\n", 42 | " 'B' : ['E'],\n", 43 | " 'C' : ['E', 'F'],\n", 44 | " 'D' : ['F'],\n", 45 | " 'E' : ['G'],\n", 46 | " 'F' : ['G'],\n", 47 | " 'G' : []\n", 48 | "}\n", 49 | "\n", 50 | "## visited set is to keep track of visited nodes\n", 51 | "visited = set()\n", 52 | "\n", 53 | "## Method Definition\n", 54 | "def depth_first_search(visited, graph, node):\n", 55 | " if node not in visited:\n", 56 | " print(node, end = \" \")\n", 57 | " visited.add(node)\n", 58 | " for adjacent in graph[node]:\n", 59 | " ## Recursive call - Stack Data Structure\n", 60 | " depth_first_search(visited, graph, adjacent)\n", 61 | "\n", 62 | "## Driver Code\n", 63 | "## Function Call\n", 64 | "depth_first_search(visited, graph, 'A')" 65 | ], 66 | "execution_count": 1, 67 | "outputs": [ 68 | { 69 | "output_type": "stream", 70 | "text": [ 71 | "A B E G C F D " 72 | ], 73 | "name": "stdout" 74 | } 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": { 80 | "id": "c8b85F6anZkf" 81 | }, 82 | "source": [ 83 | "**Time Complexity : O(V + E)**" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": { 89 | "id": "pDe4lPHwn3Gi" 90 | }, 91 | "source": [ 92 | "**Breadth First Search Implementation**\n", 93 | "\n", 94 | "**Used Data Structure : Queue**" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "metadata": { 100 | "colab": { 101 | "base_uri": "https://localhost:8080/" 102 | }, 103 | "id": "8beCDbn8nkpR", 104 | "outputId": "e3dc7501-6bfa-4a8c-e280-017a7bd27e34" 105 | }, 106 | "source": [ 107 | "## Graph Representation\n", 108 | "graph = {\n", 109 | " 'A' : ['B','C','D'],\n", 110 | " 'B' : ['E'],\n", 111 | " 'C' : ['E', 'F'],\n", 112 | " 'D' : ['F'],\n", 113 | " 'E' : ['G'],\n", 114 | " 'F' : ['G'],\n", 115 | " 'G' : []\n", 116 | "}\n", 117 | "\n", 118 | "## visited set is to keep track of visited nodes\n", 119 | "visited = set()\n", 120 | "## Queue is used for the implementation of BFS\n", 121 | "from collections import deque\n", 122 | "queue = deque()\n", 123 | "\n", 124 | "## Method Definition\n", 125 | "def breadth_first_search(visited, graph, node):\n", 126 | " visited.add(node)\n", 127 | " queue.appendleft(node)\n", 128 | "\n", 129 | " while queue:\n", 130 | " node = queue.pop() \n", 131 | " print (node, end = \" \") \n", 132 | "\n", 133 | " for neighbour in graph[node]:\n", 134 | " if neighbour not in visited:\n", 135 | " visited.add(neighbour)\n", 136 | " queue.appendleft(neighbour)\n", 137 | "\n", 138 | "## Driver Code\n", 139 | "## Function Call\n", 140 | "breadth_first_search(visited, graph, 'A')" 141 | ], 142 | "execution_count": 2, 143 | "outputs": [ 144 | { 145 | "output_type": "stream", 146 | "text": [ 147 | "A B C D E F G " 148 | ], 149 | "name": "stdout" 150 | } 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": { 156 | "id": "qAsdHjQxq9M3" 157 | }, 158 | "source": [ 159 | "**Time Complexity : O(V + E)**" 160 | ] 161 | } 162 | ] 163 | } -------------------------------------------------------------------------------- /Hashing/HashTable.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "HashTable.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | }, 16 | "accelerator": "GPU" 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "OBgqczuvmbqI" 23 | }, 24 | "source": [ 25 | "**Implementation of HashTable**" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "metadata": { 31 | "id": "BfDTeLFWkUqc" 32 | }, 33 | "source": [ 34 | "student_score = []\n", 35 | "with open(\"student_score.csv\",\"r\") as f:\n", 36 | " for line in f:\n", 37 | " tokens = line.split(',')\n", 38 | " roll_num = tokens[0]\n", 39 | " score = float(tokens[1])\n", 40 | " student_score.append([roll_num,score])" 41 | ], 42 | "execution_count": 4, 43 | "outputs": [] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "metadata": { 48 | "colab": { 49 | "base_uri": "https://localhost:8080/" 50 | }, 51 | "id": "yydVxEMRlE48", 52 | "outputId": "4227092b-aaaf-4217-fb55-b799e64bf32f" 53 | }, 54 | "source": [ 55 | "student_score" 56 | ], 57 | "execution_count": 5, 58 | "outputs": [ 59 | { 60 | "output_type": "execute_result", 61 | "data": { 62 | "text/plain": [ 63 | "[['AI20MTECH12021', 56.0],\n", 64 | " ['AI20MTECH12013', 89.0],\n", 65 | " ['AI20MTECH14010', 40.0],\n", 66 | " ['AI20MTECH14011', 60.0],\n", 67 | " ['AI20MTECH14012', 80.0],\n", 68 | " ['AI20MTECH14015', 90.0],\n", 69 | " ['AI20MTECH14016', 45.0],\n", 70 | " ['AI20MTECH14017', 67.0],\n", 71 | " ['AI20MTECH14019', 89.0]]" 72 | ] 73 | }, 74 | "metadata": { 75 | "tags": [] 76 | }, 77 | "execution_count": 5 78 | } 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "metadata": { 84 | "colab": { 85 | "base_uri": "https://localhost:8080/" 86 | }, 87 | "id": "MBBg_Y-DlKfP", 88 | "outputId": "846ff998-f2ce-478a-e659-733adc69452d" 89 | }, 90 | "source": [ 91 | "student_score[4]" 92 | ], 93 | "execution_count": 6, 94 | "outputs": [ 95 | { 96 | "output_type": "execute_result", 97 | "data": { 98 | "text/plain": [ 99 | "['AI20MTECH14012', 80.0]" 100 | ] 101 | }, 102 | "metadata": { 103 | "tags": [] 104 | }, 105 | "execution_count": 6 106 | } 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": { 112 | "id": "yWQNOq0al7ya" 113 | }, 114 | "source": [ 115 | "**Find the score of student having roll number 'AI20MTECH14015'**" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "metadata": { 121 | "colab": { 122 | "base_uri": "https://localhost:8080/" 123 | }, 124 | "id": "jO03kqo4l15n", 125 | "outputId": "52aaac75-a286-4149-b8cb-d13ca8462ab0" 126 | }, 127 | "source": [ 128 | "for records in student_score:\n", 129 | " if records[0] == 'AI20MTECH14015':\n", 130 | " print(records[1])" 131 | ], 132 | "execution_count": 7, 133 | "outputs": [ 134 | { 135 | "output_type": "stream", 136 | "text": [ 137 | "90.0\n" 138 | ], 139 | "name": "stdout" 140 | } 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "id": "320Sm6oSmPWV" 147 | }, 148 | "source": [ 149 | "**Time Complexity of getting the score of a particular student is O(n)**" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": { 155 | "id": "KwuDecKcmrhX" 156 | }, 157 | "source": [ 158 | "**Implementation of above task using Python Dictionary**" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "metadata": { 164 | "id": "hQBNx5LdmMtl" 165 | }, 166 | "source": [ 167 | "student_data = {}\n", 168 | "with open('student_score.csv','r') as f:\n", 169 | " for line in f:\n", 170 | " tokens = line.split(',')\n", 171 | " roll_num = tokens[0]\n", 172 | " score = float(tokens[1])\n", 173 | " student_data[roll_num] = score" 174 | ], 175 | "execution_count": 10, 176 | "outputs": [] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "metadata": { 181 | "colab": { 182 | "base_uri": "https://localhost:8080/" 183 | }, 184 | "id": "8IPNLnw-nJ7d", 185 | "outputId": "4cecd7a4-186a-485c-ff37-ed730b61b0cd" 186 | }, 187 | "source": [ 188 | "student_data" 189 | ], 190 | "execution_count": 11, 191 | "outputs": [ 192 | { 193 | "output_type": "execute_result", 194 | "data": { 195 | "text/plain": [ 196 | "{'AI20MTECH12013': 89.0,\n", 197 | " 'AI20MTECH12021': 56.0,\n", 198 | " 'AI20MTECH14010': 40.0,\n", 199 | " 'AI20MTECH14011': 60.0,\n", 200 | " 'AI20MTECH14012': 80.0,\n", 201 | " 'AI20MTECH14015': 90.0,\n", 202 | " 'AI20MTECH14016': 45.0,\n", 203 | " 'AI20MTECH14017': 67.0,\n", 204 | " 'AI20MTECH14019': 89.0}" 205 | ] 206 | }, 207 | "metadata": { 208 | "tags": [] 209 | }, 210 | "execution_count": 11 211 | } 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "metadata": { 217 | "colab": { 218 | "base_uri": "https://localhost:8080/" 219 | }, 220 | "id": "Fdi-dSXnnLQ8", 221 | "outputId": "6e86514b-b79c-4f1e-d351-5de424dac54c" 222 | }, 223 | "source": [ 224 | "student_data['AI20MTECH14015']" 225 | ], 226 | "execution_count": 13, 227 | "outputs": [ 228 | { 229 | "output_type": "execute_result", 230 | "data": { 231 | "text/plain": [ 232 | "90.0" 233 | ] 234 | }, 235 | "metadata": { 236 | "tags": [] 237 | }, 238 | "execution_count": 13 239 | } 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": { 245 | "id": "624YublUnYfb" 246 | }, 247 | "source": [ 248 | "**Time Complexity of getting the score of a particular student is O(1)**" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": { 254 | "id": "9LbU3GcNnl3m" 255 | }, 256 | "source": [ 257 | "**Internal Implementation of Python Dictionary - Hashing Data Structure**" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "metadata": { 263 | "id": "Nxzi9elqnTWu" 264 | }, 265 | "source": [ 266 | "class HashTable: \n", 267 | " def __init__(self):\n", 268 | " self.MAX = 10\n", 269 | " ## List Comprehension\n", 270 | " self.arr = [None for i in range(self.MAX)]\n", 271 | "\n", 272 | " ## Method to calculate the hash function for a given value \n", 273 | " def get_hash(self, key):\n", 274 | " hash = 0\n", 275 | " for char in key:\n", 276 | " ## ord function in python helps us to get ascii value of \n", 277 | " ## any character\n", 278 | " hash += ord(char)\n", 279 | " return hash % self.MAX\n", 280 | " \n", 281 | " ## Method definition of standard operators as functions in Python\n", 282 | " ## getitem - to fetch the value of given index inside hash table\n", 283 | " def __getitem__(self, index):\n", 284 | " h = self.get_hash(index)\n", 285 | " return self.arr[h]\n", 286 | " ## setitem - to set the value corresponding to key inside hash table\n", 287 | " def __setitem__(self, key, val):\n", 288 | " h = self.get_hash(key)\n", 289 | " self.arr[h] = val \n", 290 | " ## delitem - to delete the values inside the hash table for a given key \n", 291 | " def __delitem__(self, key):\n", 292 | " h = self.get_hash(key)\n", 293 | " self.arr[h] = None" 294 | ], 295 | "execution_count": 14, 296 | "outputs": [] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "metadata": { 301 | "id": "L2_mFtMXsxSL" 302 | }, 303 | "source": [ 304 | "h = HashTable()\n", 305 | "h['AI20MTECH14015'] = 60\n", 306 | "h['AI20MTECH15092'] = 70" 307 | ], 308 | "execution_count": 15, 309 | "outputs": [] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "metadata": { 314 | "colab": { 315 | "base_uri": "https://localhost:8080/" 316 | }, 317 | "id": "YGeLUEUvtBxn", 318 | "outputId": "82830033-a247-4c40-e45a-fcde5d5a4c3b" 319 | }, 320 | "source": [ 321 | "h.arr" 322 | ], 323 | "execution_count": 16, 324 | "outputs": [ 325 | { 326 | "output_type": "execute_result", 327 | "data": { 328 | "text/plain": [ 329 | "[None, None, 70, None, None, None, 60, None, None, None]" 330 | ] 331 | }, 332 | "metadata": { 333 | "tags": [] 334 | }, 335 | "execution_count": 16 336 | } 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "metadata": { 342 | "colab": { 343 | "base_uri": "https://localhost:8080/" 344 | }, 345 | "id": "C0p7RglJtDRL", 346 | "outputId": "ed75cf32-4602-4fb5-a9ff-909a74c5ea3d" 347 | }, 348 | "source": [ 349 | "h.get_hash('AI20MTECH14015')" 350 | ], 351 | "execution_count": 18, 352 | "outputs": [ 353 | { 354 | "output_type": "execute_result", 355 | "data": { 356 | "text/plain": [ 357 | "6" 358 | ] 359 | }, 360 | "metadata": { 361 | "tags": [] 362 | }, 363 | "execution_count": 18 364 | } 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "metadata": { 370 | "id": "Bsq3rykttTn5" 371 | }, 372 | "source": [ 373 | "del h['AI20MTECH14015']" 374 | ], 375 | "execution_count": 19, 376 | "outputs": [] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "metadata": { 381 | "colab": { 382 | "base_uri": "https://localhost:8080/" 383 | }, 384 | "id": "_IYbKTzGtjNF", 385 | "outputId": "4a727ed1-42d9-4b75-8ed1-625102c52203" 386 | }, 387 | "source": [ 388 | "h.arr" 389 | ], 390 | "execution_count": 20, 391 | "outputs": [ 392 | { 393 | "output_type": "execute_result", 394 | "data": { 395 | "text/plain": [ 396 | "[None, None, 70, None, None, None, None, None, None, None]" 397 | ] 398 | }, 399 | "metadata": { 400 | "tags": [] 401 | }, 402 | "execution_count": 20 403 | } 404 | ] 405 | } 406 | ] 407 | } -------------------------------------------------------------------------------- /Hashing/student_score.csv: -------------------------------------------------------------------------------- 1 | AI20MTECH12021,56 2 | AI20MTECH12013,89 3 | AI20MTECH14010,40 4 | AI20MTECH14011,60 5 | AI20MTECH14012,80 6 | AI20MTECH14015,90 7 | AI20MTECH14016,45 8 | AI20MTECH14017,67 9 | AI20MTECH14019,89 10 | -------------------------------------------------------------------------------- /Heap Data Structure/Max-3-Product.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | # Method Definitions -------------------------------- 4 | def max_product(arr): 5 | ''' 6 | heap = [] 7 | for i in range(len(arr)): 8 | heapq.heappush(heap, arr[i]) 9 | ''' 10 | a = heapq.nlargest(3, arr) 11 | b = heapq.nsmallest(2, arr) 12 | 13 | return max((a[0]*a[1]*a[2]),(b[0]*b[1]*a[0])) 14 | 15 | # Driver code 16 | arr = [-5, -4, 3, 1] 17 | result = max_product(arr) 18 | print("Maximum Product of three numbers is:", result) -------------------------------------------------------------------------------- /Iterator vs Generator/Python-Function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "small-conspiracy", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def sample(**kwargs):\n", 11 | " print(kwargs.items())" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "id": "inner-grill", 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "dict_items([('FirstName', 'Priya'), ('LastName', 'Bhatia')])\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "sample(FirstName = \"Priya\", LastName = \"Bhatia\" )" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 9, 35 | "id": "checked-millennium", 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "def sample(*args):\n", 40 | " print(args)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 10, 46 | "id": "basic-affiliate", 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "('Priya', 'Bhatia')\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "sample(\"Priya\",\"Bhatia\")" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "id": "vital-fifty", 64 | "metadata": {}, 65 | "source": [ 66 | "### Anonymous function in python" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 16, 72 | "id": "freelance-stomach", 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "a = 10\n", 77 | "b = 20\n", 78 | "\n", 79 | "def sum(a, b):\n", 80 | " \"\"\"\n", 81 | " Function used to do the summation of two numbers\n", 82 | " \"\"\"\n", 83 | " return a+b" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 17, 89 | "id": "proper-wrist", 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "30" 96 | ] 97 | }, 98 | "execution_count": 17, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "sum(a,b)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 13, 110 | "id": "patient-modern", 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "m = lambda x,y : x+y" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 14, 120 | "id": "soviet-sixth", 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "9" 127 | ] 128 | }, 129 | "execution_count": 14, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "m(3,6)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 15, 141 | "id": "competitive-treasure", 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "30" 148 | ] 149 | }, 150 | "execution_count": 15, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "m(10,20)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "id": "human-compilation", 162 | "metadata": {}, 163 | "source": [ 164 | "### Python List Iterables vs Iterators" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 26, 170 | "id": "animal-bicycle", 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "str = iter(\"Priya\")\n" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 27, 180 | "id": "bulgarian-infrared", 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "'P'" 187 | ] 188 | }, 189 | "execution_count": 27, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "next(str)" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "id": "different-suffering", 201 | "metadata": {}, 202 | "source": [ 203 | "### Python Generators" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 28, 209 | "id": "concerned-intention", 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "def test5(n):\n", 214 | " for i in range(n):\n", 215 | " yield i ** 3" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 29, 221 | "id": "completed-arrangement", 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "data": { 226 | "text/plain": [ 227 | "" 228 | ] 229 | }, 230 | "execution_count": 29, 231 | "metadata": {}, 232 | "output_type": "execute_result" 233 | } 234 | ], 235 | "source": [ 236 | "test5(30)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 30, 242 | "id": "american-judge", 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "name": "stdout", 247 | "output_type": "stream", 248 | "text": [ 249 | "0\n", 250 | "1\n", 251 | "8\n", 252 | "27\n", 253 | "64\n", 254 | "125\n", 255 | "216\n", 256 | "343\n", 257 | "512\n", 258 | "729\n", 259 | "1000\n", 260 | "1331\n", 261 | "1728\n", 262 | "2197\n", 263 | "2744\n", 264 | "3375\n", 265 | "4096\n", 266 | "4913\n", 267 | "5832\n", 268 | "6859\n", 269 | "8000\n", 270 | "9261\n", 271 | "10648\n", 272 | "12167\n", 273 | "13824\n", 274 | "15625\n", 275 | "17576\n", 276 | "19683\n", 277 | "21952\n", 278 | "24389\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "for i in test5(30):\n", 284 | " print(i)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 31, 290 | "id": "interior-treaty", 291 | "metadata": {}, 292 | "outputs": [], 293 | "source": [ 294 | "def fib(n):\n", 295 | " a = 1\n", 296 | " b = 1\n", 297 | " for i in range(n):\n", 298 | " yield a\n", 299 | " a , b = b , a+b" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 32, 305 | "id": "coupled-prince", 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "data": { 310 | "text/plain": [ 311 | "" 312 | ] 313 | }, 314 | "execution_count": 32, 315 | "metadata": {}, 316 | "output_type": "execute_result" 317 | } 318 | ], 319 | "source": [ 320 | "fib(6)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 33, 326 | "id": "graduate-wayne", 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "1\n", 334 | "1\n", 335 | "2\n", 336 | "3\n", 337 | "5\n", 338 | "8\n" 339 | ] 340 | } 341 | ], 342 | "source": [ 343 | "for i in fib(6):\n", 344 | " print(i)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "id": "equipped-buffalo", 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [] 354 | } 355 | ], 356 | "metadata": { 357 | "kernelspec": { 358 | "display_name": "Python 3", 359 | "language": "python", 360 | "name": "python3" 361 | }, 362 | "language_info": { 363 | "codemirror_mode": { 364 | "name": "ipython", 365 | "version": 3 366 | }, 367 | "file_extension": ".py", 368 | "mimetype": "text/x-python", 369 | "name": "python", 370 | "nbconvert_exporter": "python", 371 | "pygments_lexer": "ipython3", 372 | "version": "3.7.4" 373 | } 374 | }, 375 | "nbformat": 4, 376 | "nbformat_minor": 5 377 | } 378 | -------------------------------------------------------------------------------- /Linked List/Doubly_Linked_List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Doubly Linked List.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "LV6jOv4YvDDs" 22 | }, 23 | "source": [ 24 | "**Implementation of Doubly Linked List**\n", 25 | "\n", 26 | "Operations Performed :\n", 27 | "\n", 28 | "\n", 29 | "1. Insertion in DLL\n", 30 | "2. Deletion in DLL\n", 31 | "\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "metadata": { 37 | "colab": { 38 | "base_uri": "https://localhost:8080/" 39 | }, 40 | "id": "qwkEhWkKu4Tz", 41 | "outputId": "4ed153dc-2e7d-4e62-c409-a2dc2383c0d1" 42 | }, 43 | "source": [ 44 | "## Class of Node containing data and two pointers namely next and prev in DLL\n", 45 | "class Node:\n", 46 | " def __init__(self, data):\n", 47 | " self.data = data\n", 48 | " self.prev = None\n", 49 | " self.next = None\n", 50 | "\n", 51 | "## Class of DLL\n", 52 | "class DoublyLinkedList:\n", 53 | " ## Constructor for DLL which is empty\n", 54 | " def __init__(self):\n", 55 | " self.head = None\n", 56 | "\n", 57 | " ## Function to delete a node in DLL\n", 58 | " def deleteNode(self, deleted_node):\n", 59 | " if self.head is None or deleted_node is None:\n", 60 | " return\n", 61 | " \n", 62 | " ## If node to be deleted is the first node\n", 63 | " if self.head == deleted_node:\n", 64 | " self.head = deleted_node.next\n", 65 | "\n", 66 | " ## Change the prev pointer if the node to be deleted is not the first node\n", 67 | " if deleted_node.prev is not None:\n", 68 | " deleted_node.prev.next = deleted_node.prev\n", 69 | "\n", 70 | " ## Change the next pointer if the node to be deleted is not the last node\n", 71 | " if deleted_node.next is not None:\n", 72 | " deleted_node.next.prev = deleted_node.prev\n", 73 | " \n", 74 | " ## Function to insert a node is DLL - Insertion in the beginning of the DLL\n", 75 | " def insertNode(self, data):\n", 76 | " node = Node(data)\n", 77 | "\n", 78 | " node.next = self.head\n", 79 | " if self.head is not None:\n", 80 | " self.head.prev = node\n", 81 | " \n", 82 | " self.head = node\n", 83 | "\n", 84 | " ## Function to print the DLL\n", 85 | " def traverse_list(self):\n", 86 | " if self.head is None:\n", 87 | " print(\"List has no element\")\n", 88 | " return\n", 89 | " else:\n", 90 | " iterator = self.head\n", 91 | " while iterator is not None:\n", 92 | " print(iterator.data , end = \"-->\")\n", 93 | " iterator = iterator.next\n", 94 | "\n", 95 | "dll_list1 = DoublyLinkedList()\n", 96 | "dll_list1.insertNode(2)\n", 97 | "dll_list1.insertNode(4)\n", 98 | "dll_list1.insertNode(5)\n", 99 | "dll_list1.insertNode(10)\n", 100 | "dll_list1.insertNode(12)\n", 101 | "print(\"Doubly Linked List After insertion of nodes\")\n", 102 | "dll_list1.traverse_list()\n", 103 | "dll_list1.deleteNode(dll_list1.head)\n", 104 | "print(\"\\n Doubly Linked List after deletion\")\n", 105 | "dll_list1.traverse_list()" 106 | ], 107 | "execution_count": 16, 108 | "outputs": [ 109 | { 110 | "output_type": "stream", 111 | "text": [ 112 | "Doubly Linked List After insertion of nodes\n", 113 | "12-->10-->5-->4-->2-->\n", 114 | " Doubly Linked List after deletion\n", 115 | "10-->5-->4-->2-->" 116 | ], 117 | "name": "stdout" 118 | } 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": { 124 | "id": "lHUE_FI__jY4" 125 | }, 126 | "source": [ 127 | "**Assignment Problem:**\n", 128 | "\n", 129 | "Try to implement all the operations which we have studied in Singly Linked List in above code of Doubly Linked List and after that evaluate the time complexity of each function in your code.\n", 130 | "\n" 131 | ] 132 | } 133 | ] 134 | } -------------------------------------------------------------------------------- /Linked List/LinkedList_Operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "LinkedList Operations.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "PHSVVoTlORo-" 22 | }, 23 | "source": [ 24 | "**Data Structure : Linked List**\n", 25 | "\n", 26 | "**Implementation : Singly Linked List**\n", 27 | "\n", 28 | "**Operation Performed:**\n", 29 | "\n", 30 | "* Insertion of an element\n", 31 | "* Count of an element in a Linked List\n", 32 | "* Removal of an element in a Linked List" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "metadata": { 38 | "colab": { 39 | "base_uri": "https://localhost:8080/" 40 | }, 41 | "id": "qwou-OxJ2Ws7", 42 | "outputId": "eeedaa46-49c8-4dbc-eda3-d38831e1bde8" 43 | }, 44 | "source": [ 45 | "class Node:\n", 46 | " def __init__(self, data=None, next_node=None):\n", 47 | " self.data = data\n", 48 | " self.next_node = next_node\n", 49 | "\n", 50 | "class LinkedList:\n", 51 | " def __init__(self):\n", 52 | " self.head = None\n", 53 | "\n", 54 | " ## Method to display all elements inside the Linked List\n", 55 | " def print(self):\n", 56 | " if self.head is None:\n", 57 | " print(\"Linked List is empty\")\n", 58 | " return\n", 59 | " iterator = self.head\n", 60 | " list_str = ' '\n", 61 | " while iterator:\n", 62 | " list_str += str(iterator.data) + '-->'\n", 63 | " iterator = iterator.next_node\n", 64 | " print(list_str)\n", 65 | "\n", 66 | " ## Insertion of an element\n", 67 | "\n", 68 | " ## 1. Beginning of the Linked List\n", 69 | " ## Time Complexity : O(1)\n", 70 | " def insert_at_begining(self, data):\n", 71 | " node = Node(data, self.head)\n", 72 | " self.head = node\n", 73 | " \n", 74 | " ## 2. End of the Linked List\n", 75 | " ## Time Complexity : O(n)\n", 76 | " def insert_at_end(self, data):\n", 77 | " if self.head is None:\n", 78 | " self.head = Node(data, None)\n", 79 | " return\n", 80 | "\n", 81 | " iterator = self.head\n", 82 | " while iterator.next_node:\n", 83 | " iterator = iterator.next_node\n", 84 | "\n", 85 | " iterator.next_node = Node(data, None)\n", 86 | "\n", 87 | " ## Method to count number of elements inside Linked List\n", 88 | " def count_length(self):\n", 89 | " count = 0\n", 90 | " iterator = self.head\n", 91 | " while iterator:\n", 92 | " count += 1\n", 93 | " iterator = iterator.next_node\n", 94 | " return count\n", 95 | "\n", 96 | " ## 3. Insertion at any index\n", 97 | " def insert_at_index(self, data, idx):\n", 98 | " if idx < 0 or idx > self.count_length():\n", 99 | " raise Exception(\"Invalid Index\")\n", 100 | " \n", 101 | " if idx == 0:\n", 102 | " self.insert_at_begining(data)\n", 103 | " return\n", 104 | " \n", 105 | " count = 0\n", 106 | " iterator = self.head\n", 107 | " while iterator:\n", 108 | " if count == idx - 1:\n", 109 | " node = Node(data, iterator.next_node)\n", 110 | " iterator.next_node = node\n", 111 | " break\n", 112 | " \n", 113 | " iterator = iterator.next_node\n", 114 | " count += 1\n", 115 | " \n", 116 | " ## To store all the elements in a list\n", 117 | " def insert_values(self, data_list):\n", 118 | " self.head = None\n", 119 | " for data in data_list:\n", 120 | " self.insert_at_end(data)\n", 121 | " \n", 122 | " ## Removal of an element inside Linked List\n", 123 | " def removal_at_index(self, idx):\n", 124 | " ## Invalid Index\n", 125 | " if idx<0 or idx >= self.count_length():\n", 126 | " raise Exception(\"Invalid Index\")\n", 127 | " ## Deletion at beginning\n", 128 | " if idx == 0:\n", 129 | " self.head = self.head.next_node\n", 130 | " return\n", 131 | " ## Deletion at any other index value\n", 132 | " count = 0\n", 133 | " iterator = self.head\n", 134 | " while iterator:\n", 135 | " if count == idx - 1:\n", 136 | " iterator.next_node = iterator.next_node.next_node\n", 137 | " break\n", 138 | " \n", 139 | " iterator = iterator.next_node\n", 140 | " count += 1\n", 141 | "\n", 142 | "if __name__ == '__main__':\n", 143 | " list_1 = LinkedList()\n", 144 | " list_1.insert_at_begining(45)\n", 145 | " list_1.insert_at_begining(50)\n", 146 | " list_1.insert_at_end(46)\n", 147 | " list_1.insert_at_end(34)\n", 148 | " list_1.print()\n", 149 | " list_1.removal_at_index(2)\n", 150 | " list_1.print()\n", 151 | " list_1.insert_at_index(37,2)\n", 152 | " list_1.print()\n", 153 | "\n", 154 | " list_2 = LinkedList()\n", 155 | " list_2.insert_values([\"Jatin\",\"Arvind\",\"Suneetha\",\"Ritu\",\"Taral\",\"Nafisa\",\"Sriteja\"])\n", 156 | " list_2.print()\n", 157 | " print(\"Count of number of elements inside Linked List:\",list_2.count_length())" 158 | ], 159 | "execution_count": 36, 160 | "outputs": [ 161 | { 162 | "output_type": "stream", 163 | "text": [ 164 | " 50-->45-->46-->34-->\n", 165 | " 50-->45-->34-->\n", 166 | " 50-->45-->37-->34-->\n", 167 | " Jatin-->Arvind-->Suneetha-->Ritu-->Taral-->Nafisa-->Sriteja-->\n", 168 | "Count of number of elements inside Linked List: 7\n" 169 | ], 170 | "name": "stdout" 171 | } 172 | ] 173 | } 174 | ] 175 | } -------------------------------------------------------------------------------- /Linked List/Reversal_of_Linked_List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Reversal of Linked List.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "aVr_06afDLjs" 22 | }, 23 | "source": [ 24 | "**Reversal of Singly Linked List**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "metadata": { 30 | "colab": { 31 | "base_uri": "https://localhost:8080/" 32 | }, 33 | "id": "60UEgntaBCB_", 34 | "outputId": "e70c3f30-6528-433f-b08a-4a42486af99d" 35 | }, 36 | "source": [ 37 | "class Node:\n", 38 | " def __init__(self, data=None, next_node=None):\n", 39 | " self.data = data\n", 40 | " self.next_node = next_node\n", 41 | "\n", 42 | "class LinkedList:\n", 43 | " def __init__(self):\n", 44 | " self.head = None\n", 45 | "## Method to display all elements inside the Linked List\n", 46 | " def print_LL(self):\n", 47 | " if self.head is None:\n", 48 | " print(\"Linked List is empty\")\n", 49 | " return\n", 50 | " iterator = self.head\n", 51 | " list_str = ' '\n", 52 | " while iterator:\n", 53 | " list_str += str(iterator.data) + '-->'\n", 54 | " iterator = iterator.next_node\n", 55 | " print(list_str)\n", 56 | "\n", 57 | "## Method definition to insert the elements at the end of the Linked List\n", 58 | " def insert_at_end(self, data):\n", 59 | " if self.head is None:\n", 60 | " self.head = Node(data, None)\n", 61 | " return\n", 62 | "\n", 63 | " iterator = self.head\n", 64 | " while iterator.next_node:\n", 65 | " iterator = iterator.next_node\n", 66 | "\n", 67 | " iterator.next_node = Node(data, None)\n", 68 | "## Method Definition to reverse the elements inside the linked list\n", 69 | " def reversal_LL(self):\n", 70 | " prev = None\n", 71 | " next = None\n", 72 | " curr = self.head\n", 73 | " while (curr is not None):\n", 74 | " next = curr.next_node\n", 75 | " curr.next_node = prev\n", 76 | " prev = curr\n", 77 | " curr = next\n", 78 | " self.head = prev\n", 79 | "\n", 80 | "## Driver Code\n", 81 | "if __name__ == '__main__':\n", 82 | " list_1 = LinkedList()\n", 83 | " list_1.insert_at_end(46)\n", 84 | " list_1.insert_at_end(34)\n", 85 | " list_1.insert_at_end(56)\n", 86 | " list_1.insert_at_end(67)\n", 87 | " list_1.insert_at_end(78)\n", 88 | " list_1.insert_at_end(87)\n", 89 | " list_1.print_LL()\n", 90 | " list_1.reversal_LL()\n", 91 | " print(\"Linked List after reversal operation:\")\n", 92 | " list_1.print_LL()" 93 | ], 94 | "execution_count": 7, 95 | "outputs": [ 96 | { 97 | "output_type": "stream", 98 | "text": [ 99 | " 46-->34-->56-->67-->78-->87-->\n", 100 | "Linked List after reversal operation:\n", 101 | " 87-->78-->67-->56-->34-->46-->\n" 102 | ], 103 | "name": "stdout" 104 | } 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "metadata": { 110 | "id": "bvy0XOAPBzlj" 111 | }, 112 | "source": [ 113 | "" 114 | ], 115 | "execution_count": null, 116 | "outputs": [] 117 | } 118 | ] 119 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structure 2 | Python Implementation of various Data Structure 3 | 1. Array 4 | 2. Stack and Queue 5 | 3. Hashing 6 | 4. Tree 7 | 5. Linked List 8 | 6. Graph Traversal - BFT and DFT 9 | 7. Heap Data Structure 10 | -------------------------------------------------------------------------------- /Stack and Queue/Stack_and_Queue.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Stack and Queue.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "ePWV7SbpNnaO" 22 | }, 23 | "source": [ 24 | "**Implementation of Stack using List in Python**" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "metadata": { 30 | "id": "DdjRVJnlKtQJ" 31 | }, 32 | "source": [ 33 | "stack = []\n", 34 | "stack.append('https://leetcode.com/')\n", 35 | "stack.append('https://leetcode.com/problemset/all/')\n", 36 | "stack.append('https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/')" 37 | ], 38 | "execution_count": 1, 39 | "outputs": [] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "metadata": { 44 | "colab": { 45 | "base_uri": "https://localhost:8080/", 46 | "height": 35 47 | }, 48 | "id": "dhvnSmIwLfsT", 49 | "outputId": "22835829-8af1-4f52-817c-6cfd8de26cf8" 50 | }, 51 | "source": [ 52 | "stack[-1]" 53 | ], 54 | "execution_count": 2, 55 | "outputs": [ 56 | { 57 | "output_type": "execute_result", 58 | "data": { 59 | "application/vnd.google.colaboratory.intrinsic+json": { 60 | "type": "string" 61 | }, 62 | "text/plain": [ 63 | "'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/'" 64 | ] 65 | }, 66 | "metadata": { 67 | "tags": [] 68 | }, 69 | "execution_count": 2 70 | } 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "metadata": { 76 | "colab": { 77 | "base_uri": "https://localhost:8080/" 78 | }, 79 | "id": "EdzcfPBn-XRF", 80 | "outputId": "da3540dc-9f82-462b-e29d-0e20e86bd676" 81 | }, 82 | "source": [ 83 | "stack" 84 | ], 85 | "execution_count": 3, 86 | "outputs": [ 87 | { 88 | "output_type": "execute_result", 89 | "data": { 90 | "text/plain": [ 91 | "['https://leetcode.com/',\n", 92 | " 'https://leetcode.com/problemset/all/',\n", 93 | " 'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/']" 94 | ] 95 | }, 96 | "metadata": { 97 | "tags": [] 98 | }, 99 | "execution_count": 3 100 | } 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "metadata": { 106 | "colab": { 107 | "base_uri": "https://localhost:8080/", 108 | "height": 35 109 | }, 110 | "id": "yxoafeMuLjeP", 111 | "outputId": "df29a851-0af6-4c5b-bf98-3a6120793bde" 112 | }, 113 | "source": [ 114 | "stack.pop()" 115 | ], 116 | "execution_count": 4, 117 | "outputs": [ 118 | { 119 | "output_type": "execute_result", 120 | "data": { 121 | "application/vnd.google.colaboratory.intrinsic+json": { 122 | "type": "string" 123 | }, 124 | "text/plain": [ 125 | "'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/'" 126 | ] 127 | }, 128 | "metadata": { 129 | "tags": [] 130 | }, 131 | "execution_count": 4 132 | } 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "metadata": { 138 | "colab": { 139 | "base_uri": "https://localhost:8080/" 140 | }, 141 | "id": "Km2xtuJHLmv1", 142 | "outputId": "ce3afbaf-76bf-49e2-c31c-6a8d76404209" 143 | }, 144 | "source": [ 145 | "stack" 146 | ], 147 | "execution_count": 5, 148 | "outputs": [ 149 | { 150 | "output_type": "execute_result", 151 | "data": { 152 | "text/plain": [ 153 | "['https://leetcode.com/', 'https://leetcode.com/problemset/all/']" 154 | ] 155 | }, 156 | "metadata": { 157 | "tags": [] 158 | }, 159 | "execution_count": 5 160 | } 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": { 166 | "id": "D2d6dVoRNs6y" 167 | }, 168 | "source": [ 169 | "**Implementation of Stack Using collections.deque**" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "metadata": { 175 | "id": "P8nmXXijLojN" 176 | }, 177 | "source": [ 178 | "from collections import deque\n", 179 | "stack = deque()" 180 | ], 181 | "execution_count": 6, 182 | "outputs": [] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "metadata": { 187 | "id": "peZExRtmNWRH" 188 | }, 189 | "source": [ 190 | "stack.append('https://leetcode.com/')\n", 191 | "stack.append('https://leetcode.com/problemset/all/')\n", 192 | "stack.append('https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/')" 193 | ], 194 | "execution_count": 7, 195 | "outputs": [] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "metadata": { 200 | "colab": { 201 | "base_uri": "https://localhost:8080/" 202 | }, 203 | "id": "Y_TTzkePNij-", 204 | "outputId": "50c6e95e-0e26-4a16-d109-a92d4fbe681e" 205 | }, 206 | "source": [ 207 | "stack" 208 | ], 209 | "execution_count": 8, 210 | "outputs": [ 211 | { 212 | "output_type": "execute_result", 213 | "data": { 214 | "text/plain": [ 215 | "deque(['https://leetcode.com/',\n", 216 | " 'https://leetcode.com/problemset/all/',\n", 217 | " 'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/'])" 218 | ] 219 | }, 220 | "metadata": { 221 | "tags": [] 222 | }, 223 | "execution_count": 8 224 | } 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "metadata": { 230 | "colab": { 231 | "base_uri": "https://localhost:8080/", 232 | "height": 35 233 | }, 234 | "id": "p67x46aWNjYe", 235 | "outputId": "7fbf9342-a4f8-4d34-aa46-bd798d45c840" 236 | }, 237 | "source": [ 238 | "stack.pop()" 239 | ], 240 | "execution_count": 9, 241 | "outputs": [ 242 | { 243 | "output_type": "execute_result", 244 | "data": { 245 | "application/vnd.google.colaboratory.intrinsic+json": { 246 | "type": "string" 247 | }, 248 | "text/plain": [ 249 | "'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/'" 250 | ] 251 | }, 252 | "metadata": { 253 | "tags": [] 254 | }, 255 | "execution_count": 9 256 | } 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "metadata": { 262 | "colab": { 263 | "base_uri": "https://localhost:8080/" 264 | }, 265 | "id": "2vNBPZWfN09L", 266 | "outputId": "137ea733-7cc2-45e5-b5fd-846db3ecd5bc" 267 | }, 268 | "source": [ 269 | "stack" 270 | ], 271 | "execution_count": 10, 272 | "outputs": [ 273 | { 274 | "output_type": "execute_result", 275 | "data": { 276 | "text/plain": [ 277 | "deque(['https://leetcode.com/', 'https://leetcode.com/problemset/all/'])" 278 | ] 279 | }, 280 | "metadata": { 281 | "tags": [] 282 | }, 283 | "execution_count": 10 284 | } 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "metadata": { 290 | "colab": { 291 | "base_uri": "https://localhost:8080/" 292 | }, 293 | "id": "ORwLqLIWOGva", 294 | "outputId": "1a1f550c-ca99-4525-da2a-a9f968a2a79d" 295 | }, 296 | "source": [ 297 | "stack.insert(2,'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/') \n", 298 | "stack" 299 | ], 300 | "execution_count": 11, 301 | "outputs": [ 302 | { 303 | "output_type": "execute_result", 304 | "data": { 305 | "text/plain": [ 306 | "deque(['https://leetcode.com/',\n", 307 | " 'https://leetcode.com/problemset/all/',\n", 308 | " 'https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/'])" 309 | ] 310 | }, 311 | "metadata": { 312 | "tags": [] 313 | }, 314 | "execution_count": 11 315 | } 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": { 321 | "id": "piH8NO75Pbel" 322 | }, 323 | "source": [ 324 | "**Implementation of Queue using List in Python**" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "metadata": { 330 | "id": "vl-jhEb1PgmG" 331 | }, 332 | "source": [ 333 | "queue = []" 334 | ], 335 | "execution_count": 12, 336 | "outputs": [] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "metadata": { 341 | "id": "OwyjV4hMl0IR" 342 | }, 343 | "source": [ 344 | "queue.insert(0, 290)\n", 345 | "queue.insert(0, 230)\n", 346 | "queue.insert(0, 983)" 347 | ], 348 | "execution_count": 13, 349 | "outputs": [] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "metadata": { 354 | "colab": { 355 | "base_uri": "https://localhost:8080/" 356 | }, 357 | "id": "LPAIQnh2l8j7", 358 | "outputId": "b600296b-8723-412f-8bc9-87b3e1196524" 359 | }, 360 | "source": [ 361 | "queue" 362 | ], 363 | "execution_count": 14, 364 | "outputs": [ 365 | { 366 | "output_type": "execute_result", 367 | "data": { 368 | "text/plain": [ 369 | "[983, 230, 290]" 370 | ] 371 | }, 372 | "metadata": { 373 | "tags": [] 374 | }, 375 | "execution_count": 14 376 | } 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "metadata": { 382 | "colab": { 383 | "base_uri": "https://localhost:8080/" 384 | }, 385 | "id": "saQSYqr0l_xj", 386 | "outputId": "45a471df-cc2d-4d50-d5fd-879890264e8d" 387 | }, 388 | "source": [ 389 | "queue.pop()" 390 | ], 391 | "execution_count": 15, 392 | "outputs": [ 393 | { 394 | "output_type": "execute_result", 395 | "data": { 396 | "text/plain": [ 397 | "290" 398 | ] 399 | }, 400 | "metadata": { 401 | "tags": [] 402 | }, 403 | "execution_count": 15 404 | } 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "metadata": { 410 | "colab": { 411 | "base_uri": "https://localhost:8080/" 412 | }, 413 | "id": "Vk2NLPz0mCBP", 414 | "outputId": "84140b89-46fb-4942-8d34-469c1f1b1729" 415 | }, 416 | "source": [ 417 | "queue" 418 | ], 419 | "execution_count": 16, 420 | "outputs": [ 421 | { 422 | "output_type": "execute_result", 423 | "data": { 424 | "text/plain": [ 425 | "[983, 230]" 426 | ] 427 | }, 428 | "metadata": { 429 | "tags": [] 430 | }, 431 | "execution_count": 16 432 | } 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": { 438 | "id": "AnU5Od1yPhrV" 439 | }, 440 | "source": [ 441 | "**Implementation of Queue using collections.deque**" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "metadata": { 447 | "id": "JOR6mNwhPnLW" 448 | }, 449 | "source": [ 450 | "from collections import deque\n", 451 | "queue = deque()" 452 | ], 453 | "execution_count": 17, 454 | "outputs": [] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "metadata": { 459 | "id": "F07aMCZKmVAX" 460 | }, 461 | "source": [ 462 | "queue.appendleft(290)\n", 463 | "queue.appendleft(230)\n", 464 | "queue.appendleft(983)" 465 | ], 466 | "execution_count": 18, 467 | "outputs": [] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "metadata": { 472 | "colab": { 473 | "base_uri": "https://localhost:8080/" 474 | }, 475 | "id": "FEd0jc2Bmd_m", 476 | "outputId": "b275965b-b61b-4959-bef9-b444ba08c39c" 477 | }, 478 | "source": [ 479 | "queue" 480 | ], 481 | "execution_count": 19, 482 | "outputs": [ 483 | { 484 | "output_type": "execute_result", 485 | "data": { 486 | "text/plain": [ 487 | "deque([983, 230, 290])" 488 | ] 489 | }, 490 | "metadata": { 491 | "tags": [] 492 | }, 493 | "execution_count": 19 494 | } 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "metadata": { 500 | "colab": { 501 | "base_uri": "https://localhost:8080/" 502 | }, 503 | "id": "UUniv4Jcme6F", 504 | "outputId": "5d5bbd45-24b3-4912-94d3-47b81a7d7599" 505 | }, 506 | "source": [ 507 | "queue.pop()" 508 | ], 509 | "execution_count": 20, 510 | "outputs": [ 511 | { 512 | "output_type": "execute_result", 513 | "data": { 514 | "text/plain": [ 515 | "290" 516 | ] 517 | }, 518 | "metadata": { 519 | "tags": [] 520 | }, 521 | "execution_count": 20 522 | } 523 | ] 524 | } 525 | ] 526 | } --------------------------------------------------------------------------------