Skip to content

Commit 24f1dab

Browse files
authored
Dev (#12)
* Add GenericTree, BinaryTree and BinarySearchTree. * Change typing (any --> Any). * README and `pyproject.toml` changes.
1 parent a51e396 commit 24f1dab

13 files changed

+661
-50
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,27 @@
66
<!-- [![Python Versions](https://img.shields.io/pypi/pyversions/pystrukts.svg)](https://pypi.org/project/pystrukts) -->
77

88

9-
A Python library for data structures. This library provides more than 20 advanced data structures not available in the Python standard library, including:
9+
A Python library for data structures. This library provides more than 20 advanced data structures not available in the Python standard library.
1010

11+
## Installation
12+
13+
```bash
14+
pip install pystrukts
15+
```
16+
17+
## Data Structures
18+
### Lists
19+
- [x] Singly Linked Node
1120
- [x] Singly Linked List
21+
- [x] Doubly Linked Node
1222
- [x] Doubly Linked List
1323
- [x] Circular Linked List (Single, Double)
14-
- [ ] Generic Tree
15-
- [ ] Binary Search Tree
24+
- [ ] Skip List
25+
26+
### Trees
27+
- [x] Binary Tree
28+
- [x] Generic Tree
29+
- [x] Binary Search Tree
1630
- [ ] Ternary Search Tree
1731
- [ ] Suffix Tree
1832
- [ ] AVL Tree
@@ -24,9 +38,12 @@ A Python library for data structures. This library provides more than 20 advance
2438
- [ ] Interval Tree
2539
- [ ] Segment Tree
2640
- [ ] Fenwick Tree
27-
- [x] Disjoint Set
28-
- [ ] Skip List
2941
- [x] Trie
42+
43+
### Graphs
44+
- [x] Adjacency Matrix
45+
- [x] Adjacency List
46+
47+
### Others
48+
- [x] Disjoint Set (Union Find)
3049
- [x] Max Heap
31-
- [x] Graph (Adjacency Matrix)
32-
- [x] Graph (Adjacency List)

pyproject.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pystrukts"
7-
description = "Advanced data structures for Python."
87
readme = "README.md"
98
requires-python = ">=3.8"
10-
dynamic = ["version"]
9+
dynamic = ["version", "description", "authors", "urls", "keywords"]
1110
license = { file = "LICENSE" }
1211
classifiers = [
1312
"License :: OSI Approved :: Apache Software License",
@@ -22,10 +21,6 @@ classifiers = [
2221
dependencies = [
2322
]
2423

25-
authors = [
26-
{ name = "Rubén Pérez Mercado", email = "[email protected]" }
27-
]
28-
2924
[tool.setuptools_scm]
3025
write_to = "pystrukts/_version.py"
3126

pystrukts/list/doubly_linked_list.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'''
66

77
from dataclasses import dataclass
8+
from typing import Any
89
from .linked_list import LinkedList
910

1011
@dataclass
@@ -13,15 +14,15 @@ class DoublyLinkedNode:
1314
DoublyLinkedNode Class.
1415
1516
Attributes:
16-
data (any): The data stored in the node.
17+
data (Any): The data stored in the node.
1718
prev (DoublyLinkedNode): The previous node in the list.
1819
next (DoublyLinkedNode): The next node in the list.
1920
2021
Methods:
2122
`__str__()`: Return the string representation of the node.
2223
'''
2324

24-
data: any = None
25+
data: Any = None
2526
prev: 'DoublyLinkedNode' = None
2627
next: 'DoublyLinkedNode' = None
2728

@@ -46,14 +47,14 @@ class DoublyLinkedList(LinkedList[DoublyLinkedNode]):
4647
`iscircular()`: Check if the list is circular.
4748
`_link(node1: Type[T], node2: Type[T])`: Link two nodes.
4849
`_preprocess_index(index: int)`: Preprocess the index.
49-
`insert(data: any, index: int)`: Insert data at the specified index.
50-
`append(data: any)`: Append data to the end of the list.
51-
`appendleft(data: any)`: Append data to the beginning of the list.
50+
`insert(data: Any, index: int)`: Insert data at the specified index.
51+
`append(data: Any)`: Append data to the end of the list.
52+
`appendleft(data: Any)`: Append data to the beginning of the list.
5253
`remove(index: int)`: Remove data at the specified index.
5354
`pop()`: Remove data from the end of the list.
5455
`popleft()`: Remove data from the beginning of the list.
5556
`get(index: int)`: Get data at the specified index.
56-
`index(data: any)`: Get the index of the specified data.
57+
`index(data: Any)`: Get the index of the specified data.
5758
`clear()`: Clear the list.
5859
'''
5960

@@ -63,6 +64,7 @@ def __init__(self, circular: bool = False):
6364
def _link(self, node1: DoublyLinkedNode, node2: DoublyLinkedNode):
6465
'''
6566
Link two nodes.
67+
Link two nodes.
6668
6769
Args:
6870
node1 (DoublyLinkedNode): The first node.

pystrukts/list/linked_list.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from dataclasses import dataclass
88
from abc import ABC, abstractmethod
9-
from typing import Type, TypeVar, Generic
9+
from typing import Type, TypeVar, Generic, Any
1010

1111
T = TypeVar('T')
1212

@@ -28,14 +28,14 @@ class LinkedList(ABC, Generic[T]):
2828
`iscircular()`: Check if the list is circular.
2929
`_link(node1: Type[T], node2: Type[T])`: Link two nodes.
3030
`_preprocess_index(index: int)`: Preprocess the index.
31-
`insert(data: any, index: int)`: Insert data at the specified index.
32-
`append(data: any)`: Append data to the end of the list.
33-
`appendleft(data: any)`: Append data to the beginning of the list.
31+
`insert(data: Any, index: int)`: Insert data at the specified index.
32+
`append(data: Any)`: Append data to the end of the list.
33+
`appendleft(data: Any)`: Append data to the beginning of the list.
3434
`remove(index: int)`: Remove data at the specified index.
3535
`pop()`: Remove data from the end of the list.
3636
`popleft()`: Remove data from the beginning of the list.
3737
`get(index: int)`: Get data at the specified index.
38-
`index(data: any)`: Get the index of the specified data.
38+
`index(data: Any)`: Get the index of the specified data.
3939
`clear()`: Clear the list.
4040
'''
4141

@@ -72,7 +72,7 @@ def __iter__(self):
7272
out (Iterator): The iterator for the list.
7373
7474
Yields:
75-
out (any): The data in the list.
75+
out (Any): The data in the list.
7676
'''
7777

7878
current_node = self.__head
@@ -125,12 +125,12 @@ def _preprocess_index(self, index: int, insert: bool = False):
125125

126126
return index
127127

128-
def insert(self, data: any, index: int):
128+
def insert(self, data: Any, index: int):
129129
'''
130130
Insert data at the specified index.
131131
132132
Args:
133-
data (any): The data to be inserted.
133+
data (Any): The data to be inserted.
134134
index (int): The index to insert the data.
135135
136136
Raises:
@@ -158,12 +158,12 @@ def insert(self, data: any, index: int):
158158

159159
self.__length += 1
160160

161-
def append(self, data: any):
161+
def append(self, data: Any):
162162
'''
163163
Append data to the end of the list.
164164
165165
Args:
166-
data (any): The data to be appended.
166+
data (Any): The data to be appended.
167167
'''
168168

169169
new_node = self.__cls(data)
@@ -181,12 +181,12 @@ def append(self, data: any):
181181

182182
self.__length += 1
183183

184-
def appendleft(self, data: any):
184+
def appendleft(self, data: Any):
185185
'''
186186
Append data to the beginning of the list.
187187
188188
Args:
189-
data (any): The data to be appended.
189+
data (Any): The data to be appended.
190190
'''
191191

192192
new_node = self.__cls(data)
@@ -212,7 +212,7 @@ def remove(self, index: int):
212212
index (int): The index to remove the data.
213213
214214
Returns:
215-
out (any): The removed data.
215+
out (Any): The removed data.
216216
217217
Raises:
218218
IndexError: If the index is out of bounds.
@@ -243,7 +243,7 @@ def pop(self):
243243
Extract data from the end of the list.
244244
245245
Returns:
246-
out (any): The extracted data.
246+
out (Any): The extracted data.
247247
248248
Raises:
249249
IndexError: If the list is empty.
@@ -275,7 +275,7 @@ def popleft(self):
275275
Extract data from the beginning of the list.
276276
277277
Returns:
278-
out (any): The extracted data.
278+
out (Any): The extracted data.
279279
280280
Raises:
281281
IndexError: If the list is empty.
@@ -308,7 +308,7 @@ def get(self, index: int):
308308
index (int): The index to get the data.
309309
310310
Returns:
311-
out (any): The data at the specified index.
311+
out (Any): The data at the specified index.
312312
313313
Raises:
314314
IndexError: If the index is out of bounds.
@@ -322,12 +322,12 @@ def get(self, index: int):
322322

323323
return current_node.data
324324

325-
def index(self, data: any):
325+
def index(self, data: Any):
326326
'''
327327
Get the index of the first occurrence of the specified data.
328328
329329
Args:
330-
data (any): The data to search for.
330+
data (Any): The data to search for.
331331
332332
Returns:
333333
out (int): The index of the specified data.

pystrukts/list/singly_linked_list.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'''
66

77
from dataclasses import dataclass
8+
from typing import Any
89
from .linked_list import LinkedList
910

1011
@dataclass
@@ -13,20 +14,19 @@ class SinglyLinkedNode:
1314
SinglyLinkedNode Class.
1415
1516
Attributes:
16-
data (any): The data stored in the node.
17+
data (Any): The data stored in the node.
1718
next (SinglyLinkedNode): The next node in the list.
1819
1920
Methods:
2021
`__str__()`: Return the string representation of the node.
2122
'''
2223

23-
data: any = None
24+
data: Any = None
2425
next: 'SinglyLinkedNode' = None
2526

2627
def __str__(self):
2728
return f"SinglyLinkedNode({self.data}){' -> ' + str(self.next) if self.next else ''}"
2829

29-
3030
class SinglyLinkedList(LinkedList[SinglyLinkedNode]):
3131
'''
3232
SinglyLinkedList Class.
@@ -44,14 +44,14 @@ class SinglyLinkedList(LinkedList[SinglyLinkedNode]):
4444
`iscircular()`: Check if the list is circular.
4545
`_link(node1: Type[T], node2: Type[T])`: Link two nodes.
4646
`_preprocess_index(index: int)`: Preprocess the index.
47-
`insert(data: any, index: int)`: Insert data at the specified index.
48-
`append(data: any)`: Append data to the end of the list.
49-
`appendleft(data: any)`: Append data to the beginning of the list.
47+
`insert(data: Any, index: int)`: Insert data at the specified index.
48+
`append(data: Any)`: Append data to the end of the list.
49+
`appendleft(data: Any)`: Append data to the beginning of the list.
5050
`remove(index: int)`: Remove data at the specified index.
5151
`pop()`: Remove data from the end of the list.
5252
`popleft()`: Remove data from the beginning of the list.
5353
`get(index: int)`: Get data at the specified index.
54-
`index(data: any)`: Get the index of the specified data.
54+
`index(data: Any)`: Get the index of the specified data.
5555
`clear()`: Clear the list.
5656
'''
5757

@@ -61,6 +61,7 @@ def __init__(self, circular: bool = False):
6161
def _link(self, node1: SinglyLinkedNode, node2: SinglyLinkedNode):
6262
'''
6363
Link two nodes.
64+
Link two nodes.
6465
6566
Args:
6667
node1 (SinglyLinkedNode): The first node.

pystrukts/max_heap.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'''
77

88
from dataclasses import field, dataclass
9+
from typing import Any
910
import heapq
1011

1112
@dataclass
@@ -19,7 +20,7 @@ class MaxHeap:
1920
Methods:
2021
`__post_init__()`: Creates the heap from the data.
2122
`__str__()`: Return the string representation of the heap.
22-
`push(value: any)`: Insert a value in the heap.
23+
`push(value: Any)`: Insert a value in the heap.
2324
`pop()`: Extract the maximum value from the heap.
2425
`peek()`: Peek the maximum value in the heap.
2526
'''
@@ -36,12 +37,12 @@ def __post_init__(self):
3637
def __str__(self):
3738
return f"MaxHeap({[-val for val in self.data]})"
3839

39-
def push(self, value: any):
40+
def push(self, value: Any):
4041
'''
4142
Insert a value in the heap.
4243
4344
Args:
44-
value (any): The value to be inserted.
45+
value (Any): The value to be inserted.
4546
4647
Raises:
4748
TypeError: If the value is not comparable.
@@ -54,7 +55,7 @@ def pop(self):
5455
Extract the maximum value from the heap.
5556
5657
Returns:
57-
out (any): The maximum value in the heap.
58+
out (Any): The maximum value in the heap.
5859
5960
Raises:
6061
IndexError: If the heap is empty.
@@ -66,6 +67,6 @@ def peek(self):
6667
Peek the maximum value in the heap.
6768
6869
Returns:
69-
out (any): The maximum value in the heap or `None` if the heap is empty.
70+
out (Any): The maximum value in the heap or `None` if the heap is empty.
7071
'''
7172
return -self.data[0]

pystrukts/tree/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# pylint: skip-file
22

3-
from .trie import Trie, TrieNode
3+
from .trie import Trie, TrieNode
4+
from .generic_tree import GenericTree
5+
from .binary_tree import BinaryTree
6+
from .binary_search_tree import BinarySearchTree

0 commit comments

Comments
 (0)