toolkit-ds 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- toolkit_ds-0.1.0/LICENSE +21 -0
- toolkit_ds-0.1.0/MANIFEST.in +2 -0
- toolkit_ds-0.1.0/PKG-INFO +300 -0
- toolkit_ds-0.1.0/README.md +286 -0
- toolkit_ds-0.1.0/ds_toolkit/__init__.py +21 -0
- toolkit_ds-0.1.0/ds_toolkit/avl.py +192 -0
- toolkit_ds-0.1.0/ds_toolkit/bst.py +117 -0
- toolkit_ds-0.1.0/ds_toolkit/doubly_linked_list.py +75 -0
- toolkit_ds-0.1.0/ds_toolkit/heap.py +73 -0
- toolkit_ds-0.1.0/ds_toolkit/queue.py +91 -0
- toolkit_ds-0.1.0/ds_toolkit/singly_linked_list.py +63 -0
- toolkit_ds-0.1.0/ds_toolkit/stack.py +86 -0
- toolkit_ds-0.1.0/ds_toolkit/union_find.py +36 -0
- toolkit_ds-0.1.0/pyproject.toml +3 -0
- toolkit_ds-0.1.0/setup.cfg +4 -0
- toolkit_ds-0.1.0/setup.py +19 -0
- toolkit_ds-0.1.0/tests/test_avl.py +97 -0
- toolkit_ds-0.1.0/tests/test_bst.py +100 -0
- toolkit_ds-0.1.0/tests/test_doubly_linked_list.py +65 -0
- toolkit_ds-0.1.0/tests/test_heap.py +74 -0
- toolkit_ds-0.1.0/tests/test_queue.py +75 -0
- toolkit_ds-0.1.0/tests/test_singly_linked_list.py +63 -0
- toolkit_ds-0.1.0/tests/test_stack.py +65 -0
- toolkit_ds-0.1.0/tests/test_union_find.py +70 -0
- toolkit_ds-0.1.0/toolkit_ds.egg-info/PKG-INFO +300 -0
- toolkit_ds-0.1.0/toolkit_ds.egg-info/SOURCES.txt +26 -0
- toolkit_ds-0.1.0/toolkit_ds.egg-info/dependency_links.txt +1 -0
- toolkit_ds-0.1.0/toolkit_ds.egg-info/top_level.txt +1 -0
toolkit_ds-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2024] [Aravind]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: toolkit_ds
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python package implementing core data structures and algorithms.
|
|
5
|
+
Home-page: https://github.com/ARAVIND281/ds_toolkit
|
|
6
|
+
Author: Aravind S
|
|
7
|
+
Author-email: aravind@inbo.tech
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# DS Toolkit
|
|
16
|
+
|
|
17
|
+
DS Toolkit is a Python package providing implementations of common data structures such as Stack, Queue, Linked List, Binary Search Tree (BST), AVL Tree, and more. This toolkit helps developers easily use these data structures in their Python projects.
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- **Stack**: A simple implementation of a stack data structure using Python lists.
|
|
22
|
+
- **Queue**: A basic queue implementation with both list-based and linked list-based options.
|
|
23
|
+
- **Binary Search Tree (BST)**: An implementation of a binary search tree.
|
|
24
|
+
- **AVL Tree**: An implementation of a self-balancing binary search tree.
|
|
25
|
+
- **Union Find**: Data structure for handling disjoint sets.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
You can install the `ds_toolkit` package directly from PyPI using pip:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install ds-toolkit
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Alternatively, if you want to install it directly from the source, clone the repository and use the following:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
git clone https://github.com/ARAVIND281/ds_toolkit.git
|
|
39
|
+
cd ds_toolkit
|
|
40
|
+
pip install .
|
|
41
|
+
```
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### Stack
|
|
45
|
+
|
|
46
|
+
The stack is a simple Last In, First Out (LIFO) data structure. You can perform the following operations on the stack:
|
|
47
|
+
|
|
48
|
+
- **push(value)**: Adds a value to the top of the stack.
|
|
49
|
+
- **pop()**: Removes and returns the value at the top of the stack.
|
|
50
|
+
- **top()**: Returns the value at the top without removing it.
|
|
51
|
+
- **is_empty()**: Checks if the stack is empty.
|
|
52
|
+
- **size()**: Returns the number of elements in the stack.
|
|
53
|
+
- **to_list()**: Returns a list representation of the stack.
|
|
54
|
+
|
|
55
|
+
#### Example:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from ds_toolkit.stack import Stack
|
|
59
|
+
|
|
60
|
+
stack = Stack()
|
|
61
|
+
|
|
62
|
+
# Push items onto the stack
|
|
63
|
+
stack.push(10)
|
|
64
|
+
stack.push(20)
|
|
65
|
+
|
|
66
|
+
# Pop the top item
|
|
67
|
+
print(stack.pop()) # Output: 20
|
|
68
|
+
|
|
69
|
+
# Peek at the top item
|
|
70
|
+
print(stack.top()) # Output: 10
|
|
71
|
+
|
|
72
|
+
# Check if the stack is empty
|
|
73
|
+
print(stack.is_empty()) # Output: False
|
|
74
|
+
|
|
75
|
+
# Get the size of the stack
|
|
76
|
+
print(stack.size()) # Output: 1
|
|
77
|
+
|
|
78
|
+
# Convert stack to list
|
|
79
|
+
print(stack.to_list()) # Output: [10]
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Queue
|
|
83
|
+
|
|
84
|
+
The queue is a simple First In, First Out (FIFO) data structure. You can perform the following operations on the queue:
|
|
85
|
+
|
|
86
|
+
### Queue
|
|
87
|
+
|
|
88
|
+
A queue is a First In, First Out (FIFO) data structure. This means that elements are processed in the order in which they are added, with the first element added being the first one to be removed. The following operations are supported:
|
|
89
|
+
|
|
90
|
+
- **enqueue(value)**: Adds a value to the end of the queue.
|
|
91
|
+
- **dequeue()**: Removes and returns the value at the front of the queue.
|
|
92
|
+
- **front()**: Returns the value at the front of the queue without removing it.
|
|
93
|
+
- **is_empty()**: Checks if the queue is empty.
|
|
94
|
+
- **size()**: Returns the number of elements in the queue.
|
|
95
|
+
- **to_list()**: Returns the queue as a list.
|
|
96
|
+
|
|
97
|
+
#### Example:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from ds_toolkit.queue import Queue
|
|
101
|
+
|
|
102
|
+
# Create a new Queue instance
|
|
103
|
+
queue = Queue()
|
|
104
|
+
|
|
105
|
+
# Enqueue items into the queue
|
|
106
|
+
queue.enqueue(10)
|
|
107
|
+
queue.enqueue(20)
|
|
108
|
+
queue.enqueue(30)
|
|
109
|
+
|
|
110
|
+
# Dequeue the first item (10)
|
|
111
|
+
print(queue.dequeue()) # Output: 10
|
|
112
|
+
|
|
113
|
+
# Check if the queue is empty
|
|
114
|
+
print(queue.is_empty()) # Output: False
|
|
115
|
+
|
|
116
|
+
# Get the size of the queue
|
|
117
|
+
print(queue.size()) # Output: 2
|
|
118
|
+
|
|
119
|
+
# Peek at the front item (20)
|
|
120
|
+
print(queue.front()) # Output: 20
|
|
121
|
+
|
|
122
|
+
# Convert the queue to a list
|
|
123
|
+
print(queue.to_list()) # Output: [20, 30]
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Binary Search Tree (BST)
|
|
127
|
+
|
|
128
|
+
A **Binary Search Tree (BST)** is a type of binary tree where each node has at most two children, and for each node:
|
|
129
|
+
|
|
130
|
+
- The left child has a value less than the node's value.
|
|
131
|
+
- The right child has a value greater than the node's value.
|
|
132
|
+
|
|
133
|
+
BST supports efficient search, insertion, and deletion operations, with an average time complexity of **O(log n)**.
|
|
134
|
+
|
|
135
|
+
#### Operations:
|
|
136
|
+
|
|
137
|
+
- **insert(value)**: Inserts a value into the BST.
|
|
138
|
+
- **search(value)**: Searches for a value in the BST and returns `True` if it exists, otherwise `False`.
|
|
139
|
+
- **delete(value)**: Deletes a node with the given value.
|
|
140
|
+
- **find_min()**: Returns the minimum value in the BST.
|
|
141
|
+
- **find_max()**: Returns the maximum value in the BST.
|
|
142
|
+
- **inorder_traversal()**: Returns a list of values in ascending order (left-root-right traversal).
|
|
143
|
+
- **preorder_traversal()**: Returns a list of values in preorder (root-left-right traversal).
|
|
144
|
+
- **postorder_traversal()**: Returns a list of values in postorder (left-right-root traversal).
|
|
145
|
+
- **height()**: Returns the height (max depth) of the tree.
|
|
146
|
+
|
|
147
|
+
#### Example:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from ds_toolkit.bst import BinarySearchTree
|
|
151
|
+
|
|
152
|
+
# Create a new Binary Search Tree
|
|
153
|
+
bst = BinarySearchTree()
|
|
154
|
+
|
|
155
|
+
# Insert values into the BST
|
|
156
|
+
bst.insert(50)
|
|
157
|
+
bst.insert(30)
|
|
158
|
+
bst.insert(70)
|
|
159
|
+
bst.insert(20)
|
|
160
|
+
bst.insert(40)
|
|
161
|
+
bst.insert(60)
|
|
162
|
+
bst.insert(80)
|
|
163
|
+
|
|
164
|
+
# Search for a value
|
|
165
|
+
print(bst.search(40)) # Output: True
|
|
166
|
+
print(bst.search(25)) # Output: False
|
|
167
|
+
|
|
168
|
+
# Inorder traversal (sorted order)
|
|
169
|
+
print(bst.inorder_traversal()) # Output: [20, 30, 40, 50, 60, 70, 80]
|
|
170
|
+
|
|
171
|
+
# Find minimum and maximum values
|
|
172
|
+
print(bst.find_min()) # Output: 20
|
|
173
|
+
print(bst.find_max()) # Output: 80
|
|
174
|
+
|
|
175
|
+
# Delete a node
|
|
176
|
+
bst.delete(20) # Deletes the node with value 20
|
|
177
|
+
print(bst.inorder_traversal()) # Output: [30, 40, 50, 60, 70, 80]
|
|
178
|
+
|
|
179
|
+
# Get tree height
|
|
180
|
+
print(bst.height()) # Output: 3 (based on the tree's structure)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### AVL Tree
|
|
184
|
+
|
|
185
|
+
An **AVL Tree** is a self-balancing binary search tree (BST) where the difference in heights between the left and right subtrees (the balance factor) is at most 1 for all nodes. If the balance factor goes beyond this limit, the tree is rebalanced using rotations.
|
|
186
|
+
|
|
187
|
+
#### Operations:
|
|
188
|
+
|
|
189
|
+
- **insert(value)**: Inserts a value into the AVL tree while maintaining the balance.
|
|
190
|
+
- **delete(value)**: Deletes a node with the specified value while maintaining the balance.
|
|
191
|
+
- **search(value)**: Searches for a value in the AVL tree and returns `True` if found, otherwise `False`.
|
|
192
|
+
- **find_min()**: Returns the node with the minimum value.
|
|
193
|
+
- **find_max()**: Returns the node with the maximum value.
|
|
194
|
+
- **height()**: Returns the height (maximum depth) of the AVL tree.
|
|
195
|
+
- **inorder_traversal()**: Returns a list of values in ascending order (left-root-right traversal).
|
|
196
|
+
- **preorder_traversal()**: Returns a list of values in preorder (root-left-right traversal).
|
|
197
|
+
- **postorder_traversal()**: Returns a list of values in postorder (left-right-root traversal).
|
|
198
|
+
|
|
199
|
+
#### Example:
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
from ds_toolkit.avl import AVLTree
|
|
203
|
+
|
|
204
|
+
# Create a new AVL Tree
|
|
205
|
+
avl = AVLTree()
|
|
206
|
+
|
|
207
|
+
# Insert values into the AVL Tree
|
|
208
|
+
avl.insert(50)
|
|
209
|
+
avl.insert(30)
|
|
210
|
+
avl.insert(70)
|
|
211
|
+
avl.insert(20)
|
|
212
|
+
avl.insert(40)
|
|
213
|
+
avl.insert(60)
|
|
214
|
+
avl.insert(80)
|
|
215
|
+
|
|
216
|
+
# Search for a value
|
|
217
|
+
print(avl.search(40)) # Output: True
|
|
218
|
+
print(avl.search(25)) # Output: False
|
|
219
|
+
|
|
220
|
+
# Inorder traversal (sorted order)
|
|
221
|
+
print(avl.inorder_traversal()) # Output: [20, 30, 40, 50, 60, 70, 80]
|
|
222
|
+
|
|
223
|
+
# Find minimum and maximum values
|
|
224
|
+
print(avl.find_min()) # Output: 20
|
|
225
|
+
print(avl.find_max()) # Output: 80
|
|
226
|
+
|
|
227
|
+
# Delete a node
|
|
228
|
+
avl.delete(20) # Deletes the node with value 20
|
|
229
|
+
print(avl.inorder_traversal()) # Output: [30, 40, 50, 60, 70, 80]
|
|
230
|
+
|
|
231
|
+
# Get tree height
|
|
232
|
+
print(avl.height()) # Output: 3 (based on the tree's structure)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Union Find (Disjoint Set Union - DSU)
|
|
236
|
+
|
|
237
|
+
The **Union Find** data structure efficiently handles the dynamic connectivity problem. It supports two primary operations:
|
|
238
|
+
1. **Union**: Merges two sets into one.
|
|
239
|
+
2. **Find**: Determines which set an element belongs to.
|
|
240
|
+
|
|
241
|
+
Union Find is commonly used for problems involving connectivity, such as determining connected components in a graph or finding the minimum spanning tree.
|
|
242
|
+
|
|
243
|
+
#### Operations:
|
|
244
|
+
|
|
245
|
+
- **find(x)**: Returns the representative (or leader) of the set containing element `x`.
|
|
246
|
+
- **union(x, y)**: Merges the sets containing `x` and `y`.
|
|
247
|
+
- **connected(x, y)**: Returns `True` if `x` and `y` are in the same set, otherwise `False`.
|
|
248
|
+
- **size(x)**: Returns the size of the set containing element `x`.
|
|
249
|
+
- **make_set(x)**: Initializes a set for a new element `x`.
|
|
250
|
+
|
|
251
|
+
#### Example:
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
from ds_toolkit.union_find import UnionFind
|
|
255
|
+
|
|
256
|
+
# Create a UnionFind object
|
|
257
|
+
uf = UnionFind()
|
|
258
|
+
|
|
259
|
+
# Add elements to the UnionFind structure
|
|
260
|
+
uf.make_set(1)
|
|
261
|
+
uf.make_set(2)
|
|
262
|
+
uf.make_set(3)
|
|
263
|
+
uf.make_set(4)
|
|
264
|
+
uf.make_set(5)
|
|
265
|
+
|
|
266
|
+
# Perform union operations
|
|
267
|
+
uf.union(1, 2) # Union of 1 and 2
|
|
268
|
+
uf.union(3, 4) # Union of 3 and 4
|
|
269
|
+
uf.union(2, 3) # Union of 2 and 3 (now 1, 2, 3, and 4 are in the same set)
|
|
270
|
+
|
|
271
|
+
# Check if two elements are connected
|
|
272
|
+
print(uf.connected(1, 4)) # Output: True (since 1, 2, 3, and 4 are in the same set)
|
|
273
|
+
print(uf.connected(1, 5)) # Output: False (1 and 5 are in different sets)
|
|
274
|
+
|
|
275
|
+
# Find the representative of a set
|
|
276
|
+
print(uf.find(3)) # Output: 1 (since 1, 2, 3, and 4 are all connected, and 1 is the root)
|
|
277
|
+
|
|
278
|
+
# Check the size of a set
|
|
279
|
+
print(uf.size(1)) # Output: 4 (the set containing 1, 2, 3, and 4 has size 4)
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Author
|
|
283
|
+
|
|
284
|
+
This project is maintained by [Aravind S](https://github.com/ARAVIND281).
|
|
285
|
+
|
|
286
|
+
If you have any questions or suggestions, feel free to reach out via GitHub or through the issues section of the repository.
|
|
287
|
+
|
|
288
|
+
### About the Author
|
|
289
|
+
|
|
290
|
+
This project is developed by **Aravind S**, the founder of **Finmitr** and **Golden Crop**.
|
|
291
|
+
|
|
292
|
+
- GitHub: [@ARAVIND281](https://github.com/ARAVIND281)
|
|
293
|
+
- Email: [aravind@inbo.tech](mailto:aravind@inbo.tech)
|
|
294
|
+
- Website: [inbo.tech](https://inbo.tech)
|
|
295
|
+
|
|
296
|
+
You can connect with Aravind on [LinkedIn](https://www.linkedin.com/in/aravinds28/).
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
Thank you for exploring this project!
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# DS Toolkit
|
|
2
|
+
|
|
3
|
+
DS Toolkit is a Python package providing implementations of common data structures such as Stack, Queue, Linked List, Binary Search Tree (BST), AVL Tree, and more. This toolkit helps developers easily use these data structures in their Python projects.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Stack**: A simple implementation of a stack data structure using Python lists.
|
|
8
|
+
- **Queue**: A basic queue implementation with both list-based and linked list-based options.
|
|
9
|
+
- **Binary Search Tree (BST)**: An implementation of a binary search tree.
|
|
10
|
+
- **AVL Tree**: An implementation of a self-balancing binary search tree.
|
|
11
|
+
- **Union Find**: Data structure for handling disjoint sets.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
You can install the `ds_toolkit` package directly from PyPI using pip:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install ds-toolkit
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Alternatively, if you want to install it directly from the source, clone the repository and use the following:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
git clone https://github.com/ARAVIND281/ds_toolkit.git
|
|
25
|
+
cd ds_toolkit
|
|
26
|
+
pip install .
|
|
27
|
+
```
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Stack
|
|
31
|
+
|
|
32
|
+
The stack is a simple Last In, First Out (LIFO) data structure. You can perform the following operations on the stack:
|
|
33
|
+
|
|
34
|
+
- **push(value)**: Adds a value to the top of the stack.
|
|
35
|
+
- **pop()**: Removes and returns the value at the top of the stack.
|
|
36
|
+
- **top()**: Returns the value at the top without removing it.
|
|
37
|
+
- **is_empty()**: Checks if the stack is empty.
|
|
38
|
+
- **size()**: Returns the number of elements in the stack.
|
|
39
|
+
- **to_list()**: Returns a list representation of the stack.
|
|
40
|
+
|
|
41
|
+
#### Example:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from ds_toolkit.stack import Stack
|
|
45
|
+
|
|
46
|
+
stack = Stack()
|
|
47
|
+
|
|
48
|
+
# Push items onto the stack
|
|
49
|
+
stack.push(10)
|
|
50
|
+
stack.push(20)
|
|
51
|
+
|
|
52
|
+
# Pop the top item
|
|
53
|
+
print(stack.pop()) # Output: 20
|
|
54
|
+
|
|
55
|
+
# Peek at the top item
|
|
56
|
+
print(stack.top()) # Output: 10
|
|
57
|
+
|
|
58
|
+
# Check if the stack is empty
|
|
59
|
+
print(stack.is_empty()) # Output: False
|
|
60
|
+
|
|
61
|
+
# Get the size of the stack
|
|
62
|
+
print(stack.size()) # Output: 1
|
|
63
|
+
|
|
64
|
+
# Convert stack to list
|
|
65
|
+
print(stack.to_list()) # Output: [10]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Queue
|
|
69
|
+
|
|
70
|
+
The queue is a simple First In, First Out (FIFO) data structure. You can perform the following operations on the queue:
|
|
71
|
+
|
|
72
|
+
### Queue
|
|
73
|
+
|
|
74
|
+
A queue is a First In, First Out (FIFO) data structure. This means that elements are processed in the order in which they are added, with the first element added being the first one to be removed. The following operations are supported:
|
|
75
|
+
|
|
76
|
+
- **enqueue(value)**: Adds a value to the end of the queue.
|
|
77
|
+
- **dequeue()**: Removes and returns the value at the front of the queue.
|
|
78
|
+
- **front()**: Returns the value at the front of the queue without removing it.
|
|
79
|
+
- **is_empty()**: Checks if the queue is empty.
|
|
80
|
+
- **size()**: Returns the number of elements in the queue.
|
|
81
|
+
- **to_list()**: Returns the queue as a list.
|
|
82
|
+
|
|
83
|
+
#### Example:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from ds_toolkit.queue import Queue
|
|
87
|
+
|
|
88
|
+
# Create a new Queue instance
|
|
89
|
+
queue = Queue()
|
|
90
|
+
|
|
91
|
+
# Enqueue items into the queue
|
|
92
|
+
queue.enqueue(10)
|
|
93
|
+
queue.enqueue(20)
|
|
94
|
+
queue.enqueue(30)
|
|
95
|
+
|
|
96
|
+
# Dequeue the first item (10)
|
|
97
|
+
print(queue.dequeue()) # Output: 10
|
|
98
|
+
|
|
99
|
+
# Check if the queue is empty
|
|
100
|
+
print(queue.is_empty()) # Output: False
|
|
101
|
+
|
|
102
|
+
# Get the size of the queue
|
|
103
|
+
print(queue.size()) # Output: 2
|
|
104
|
+
|
|
105
|
+
# Peek at the front item (20)
|
|
106
|
+
print(queue.front()) # Output: 20
|
|
107
|
+
|
|
108
|
+
# Convert the queue to a list
|
|
109
|
+
print(queue.to_list()) # Output: [20, 30]
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Binary Search Tree (BST)
|
|
113
|
+
|
|
114
|
+
A **Binary Search Tree (BST)** is a type of binary tree where each node has at most two children, and for each node:
|
|
115
|
+
|
|
116
|
+
- The left child has a value less than the node's value.
|
|
117
|
+
- The right child has a value greater than the node's value.
|
|
118
|
+
|
|
119
|
+
BST supports efficient search, insertion, and deletion operations, with an average time complexity of **O(log n)**.
|
|
120
|
+
|
|
121
|
+
#### Operations:
|
|
122
|
+
|
|
123
|
+
- **insert(value)**: Inserts a value into the BST.
|
|
124
|
+
- **search(value)**: Searches for a value in the BST and returns `True` if it exists, otherwise `False`.
|
|
125
|
+
- **delete(value)**: Deletes a node with the given value.
|
|
126
|
+
- **find_min()**: Returns the minimum value in the BST.
|
|
127
|
+
- **find_max()**: Returns the maximum value in the BST.
|
|
128
|
+
- **inorder_traversal()**: Returns a list of values in ascending order (left-root-right traversal).
|
|
129
|
+
- **preorder_traversal()**: Returns a list of values in preorder (root-left-right traversal).
|
|
130
|
+
- **postorder_traversal()**: Returns a list of values in postorder (left-right-root traversal).
|
|
131
|
+
- **height()**: Returns the height (max depth) of the tree.
|
|
132
|
+
|
|
133
|
+
#### Example:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from ds_toolkit.bst import BinarySearchTree
|
|
137
|
+
|
|
138
|
+
# Create a new Binary Search Tree
|
|
139
|
+
bst = BinarySearchTree()
|
|
140
|
+
|
|
141
|
+
# Insert values into the BST
|
|
142
|
+
bst.insert(50)
|
|
143
|
+
bst.insert(30)
|
|
144
|
+
bst.insert(70)
|
|
145
|
+
bst.insert(20)
|
|
146
|
+
bst.insert(40)
|
|
147
|
+
bst.insert(60)
|
|
148
|
+
bst.insert(80)
|
|
149
|
+
|
|
150
|
+
# Search for a value
|
|
151
|
+
print(bst.search(40)) # Output: True
|
|
152
|
+
print(bst.search(25)) # Output: False
|
|
153
|
+
|
|
154
|
+
# Inorder traversal (sorted order)
|
|
155
|
+
print(bst.inorder_traversal()) # Output: [20, 30, 40, 50, 60, 70, 80]
|
|
156
|
+
|
|
157
|
+
# Find minimum and maximum values
|
|
158
|
+
print(bst.find_min()) # Output: 20
|
|
159
|
+
print(bst.find_max()) # Output: 80
|
|
160
|
+
|
|
161
|
+
# Delete a node
|
|
162
|
+
bst.delete(20) # Deletes the node with value 20
|
|
163
|
+
print(bst.inorder_traversal()) # Output: [30, 40, 50, 60, 70, 80]
|
|
164
|
+
|
|
165
|
+
# Get tree height
|
|
166
|
+
print(bst.height()) # Output: 3 (based on the tree's structure)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### AVL Tree
|
|
170
|
+
|
|
171
|
+
An **AVL Tree** is a self-balancing binary search tree (BST) where the difference in heights between the left and right subtrees (the balance factor) is at most 1 for all nodes. If the balance factor goes beyond this limit, the tree is rebalanced using rotations.
|
|
172
|
+
|
|
173
|
+
#### Operations:
|
|
174
|
+
|
|
175
|
+
- **insert(value)**: Inserts a value into the AVL tree while maintaining the balance.
|
|
176
|
+
- **delete(value)**: Deletes a node with the specified value while maintaining the balance.
|
|
177
|
+
- **search(value)**: Searches for a value in the AVL tree and returns `True` if found, otherwise `False`.
|
|
178
|
+
- **find_min()**: Returns the node with the minimum value.
|
|
179
|
+
- **find_max()**: Returns the node with the maximum value.
|
|
180
|
+
- **height()**: Returns the height (maximum depth) of the AVL tree.
|
|
181
|
+
- **inorder_traversal()**: Returns a list of values in ascending order (left-root-right traversal).
|
|
182
|
+
- **preorder_traversal()**: Returns a list of values in preorder (root-left-right traversal).
|
|
183
|
+
- **postorder_traversal()**: Returns a list of values in postorder (left-right-root traversal).
|
|
184
|
+
|
|
185
|
+
#### Example:
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from ds_toolkit.avl import AVLTree
|
|
189
|
+
|
|
190
|
+
# Create a new AVL Tree
|
|
191
|
+
avl = AVLTree()
|
|
192
|
+
|
|
193
|
+
# Insert values into the AVL Tree
|
|
194
|
+
avl.insert(50)
|
|
195
|
+
avl.insert(30)
|
|
196
|
+
avl.insert(70)
|
|
197
|
+
avl.insert(20)
|
|
198
|
+
avl.insert(40)
|
|
199
|
+
avl.insert(60)
|
|
200
|
+
avl.insert(80)
|
|
201
|
+
|
|
202
|
+
# Search for a value
|
|
203
|
+
print(avl.search(40)) # Output: True
|
|
204
|
+
print(avl.search(25)) # Output: False
|
|
205
|
+
|
|
206
|
+
# Inorder traversal (sorted order)
|
|
207
|
+
print(avl.inorder_traversal()) # Output: [20, 30, 40, 50, 60, 70, 80]
|
|
208
|
+
|
|
209
|
+
# Find minimum and maximum values
|
|
210
|
+
print(avl.find_min()) # Output: 20
|
|
211
|
+
print(avl.find_max()) # Output: 80
|
|
212
|
+
|
|
213
|
+
# Delete a node
|
|
214
|
+
avl.delete(20) # Deletes the node with value 20
|
|
215
|
+
print(avl.inorder_traversal()) # Output: [30, 40, 50, 60, 70, 80]
|
|
216
|
+
|
|
217
|
+
# Get tree height
|
|
218
|
+
print(avl.height()) # Output: 3 (based on the tree's structure)
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Union Find (Disjoint Set Union - DSU)
|
|
222
|
+
|
|
223
|
+
The **Union Find** data structure efficiently handles the dynamic connectivity problem. It supports two primary operations:
|
|
224
|
+
1. **Union**: Merges two sets into one.
|
|
225
|
+
2. **Find**: Determines which set an element belongs to.
|
|
226
|
+
|
|
227
|
+
Union Find is commonly used for problems involving connectivity, such as determining connected components in a graph or finding the minimum spanning tree.
|
|
228
|
+
|
|
229
|
+
#### Operations:
|
|
230
|
+
|
|
231
|
+
- **find(x)**: Returns the representative (or leader) of the set containing element `x`.
|
|
232
|
+
- **union(x, y)**: Merges the sets containing `x` and `y`.
|
|
233
|
+
- **connected(x, y)**: Returns `True` if `x` and `y` are in the same set, otherwise `False`.
|
|
234
|
+
- **size(x)**: Returns the size of the set containing element `x`.
|
|
235
|
+
- **make_set(x)**: Initializes a set for a new element `x`.
|
|
236
|
+
|
|
237
|
+
#### Example:
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from ds_toolkit.union_find import UnionFind
|
|
241
|
+
|
|
242
|
+
# Create a UnionFind object
|
|
243
|
+
uf = UnionFind()
|
|
244
|
+
|
|
245
|
+
# Add elements to the UnionFind structure
|
|
246
|
+
uf.make_set(1)
|
|
247
|
+
uf.make_set(2)
|
|
248
|
+
uf.make_set(3)
|
|
249
|
+
uf.make_set(4)
|
|
250
|
+
uf.make_set(5)
|
|
251
|
+
|
|
252
|
+
# Perform union operations
|
|
253
|
+
uf.union(1, 2) # Union of 1 and 2
|
|
254
|
+
uf.union(3, 4) # Union of 3 and 4
|
|
255
|
+
uf.union(2, 3) # Union of 2 and 3 (now 1, 2, 3, and 4 are in the same set)
|
|
256
|
+
|
|
257
|
+
# Check if two elements are connected
|
|
258
|
+
print(uf.connected(1, 4)) # Output: True (since 1, 2, 3, and 4 are in the same set)
|
|
259
|
+
print(uf.connected(1, 5)) # Output: False (1 and 5 are in different sets)
|
|
260
|
+
|
|
261
|
+
# Find the representative of a set
|
|
262
|
+
print(uf.find(3)) # Output: 1 (since 1, 2, 3, and 4 are all connected, and 1 is the root)
|
|
263
|
+
|
|
264
|
+
# Check the size of a set
|
|
265
|
+
print(uf.size(1)) # Output: 4 (the set containing 1, 2, 3, and 4 has size 4)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Author
|
|
269
|
+
|
|
270
|
+
This project is maintained by [Aravind S](https://github.com/ARAVIND281).
|
|
271
|
+
|
|
272
|
+
If you have any questions or suggestions, feel free to reach out via GitHub or through the issues section of the repository.
|
|
273
|
+
|
|
274
|
+
### About the Author
|
|
275
|
+
|
|
276
|
+
This project is developed by **Aravind S**, the founder of **Finmitr** and **Golden Crop**.
|
|
277
|
+
|
|
278
|
+
- GitHub: [@ARAVIND281](https://github.com/ARAVIND281)
|
|
279
|
+
- Email: [aravind@inbo.tech](mailto:aravind@inbo.tech)
|
|
280
|
+
- Website: [inbo.tech](https://inbo.tech)
|
|
281
|
+
|
|
282
|
+
You can connect with Aravind on [LinkedIn](https://www.linkedin.com/in/aravinds28/).
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
Thank you for exploring this project!
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .singly_linked_list import SinglyLinkedList
|
|
2
|
+
from .doubly_linked_list import DoublyLinkedList
|
|
3
|
+
from .stack import Stack, LinkedListStack
|
|
4
|
+
from .queue import Queue, LinkedListQueue
|
|
5
|
+
from .heap import BinaryMinHeap
|
|
6
|
+
from .bst import BinarySearchTree
|
|
7
|
+
from .avl import AVLTree
|
|
8
|
+
from .union_find import UnionFind
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"SinglyLinkedList",
|
|
12
|
+
"DoublyLinkedList",
|
|
13
|
+
"Stack",
|
|
14
|
+
"LinkedListStack",
|
|
15
|
+
"Queue",
|
|
16
|
+
"LinkedListQueue",
|
|
17
|
+
"BinaryMinHeap",
|
|
18
|
+
"BinarySearchTree",
|
|
19
|
+
"AVLTree",
|
|
20
|
+
"UnionFind",
|
|
21
|
+
]
|