Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions src/data_structures/binary_tree_node.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
class BinaryTreeNode:
class BinaryTreeNode[T]:
"""Binary Tree Node.
A node in a binary tree, holding data plus references to its left and right child nodes.
"""

def __init__(self, data, left_node=None, right_node=None):
self.__data = data
self.__left_node = left_node
self.__right_node = right_node

@property
def data(self):
return self.__data

@data.setter
def data(self, data):
self.__data = data

@property
def left_node(self):
return self.__left_node

@left_node.setter
def left_node(self, node):
self.__left_node = node

@property
def right_node(self):
return self.__right_node

@right_node.setter
def right_node(self, node):
self.__right_node = node
def __init__(self, data: T, left: BinaryTreeNode[T] | None = None, right: BinaryTreeNode[T] | None = None):
self.data = data
self.left = left
self.right = right
38 changes: 20 additions & 18 deletions src/data_structures/circularly_linked_list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


class CircularlyLinkedList:
class CircularlyLinkedList[T]:
"""Circularly Linked List.
In the last node of a list, the link field often contains a null reference, a special value used to
Expand All @@ -12,22 +12,22 @@ class CircularlyLinkedList:
This is more of an example. It can be optimized in a variety of ways depending on its intended usage.
"""

def __init__(self, node: SinglyLinkedListNode | None = None):
self.__head: SinglyLinkedListNode = node
def __init__(self, node: SinglyLinkedListNode[T] | None = None):
self._head: SinglyLinkedListNode[T] | None = node
if node:
node.next = self.__head
node.next = self._head

@property
def head(self) -> SinglyLinkedListNode:
return self.__head
def head(self) -> SinglyLinkedListNode[T] | None:
return self._head

@head.setter
def head(self, node: SinglyLinkedListNode):
self.__head = node
def head(self, node: SinglyLinkedListNode[T] | None):
self._head = node
if node:
node.next = self.__head
node.next = self._head

def all_values(self) -> []:
def all_values(self) -> list[T]:
"""Return a list of every value in the list, starting from head and stopping once head is seen again."""
values = []
node = self.head
Expand All @@ -39,18 +39,20 @@ def all_values(self) -> []:
break
return values

def append(self, node: SinglyLinkedListNode):
def append(self, node: SinglyLinkedListNode[T]):
"""Insert node just before head, so the list stays circular with head as the last-linked node."""
previous_node = self.head

if not previous_node:
if not self.head:
self.head = node
node.next = self.head
return

while previous_node.next != self.head:
previous_node = self.head

while previous_node and previous_node.next != self.head:
previous_node = previous_node.next

if not previous_node:
return
next_node = previous_node.next
previous_node.next = node
node.next = next_node
Expand All @@ -66,17 +68,17 @@ def remove(self, index: int):
self.head = None
return

if index == 0:
if index == 0 and self.head:
previous_node.next = self.head.next
self.head = previous_node.next
return

list_index = 1
while previous_node.next is not self.head and list_index < index:
while previous_node and previous_node.next is not self.head and list_index < index:
previous_node = previous_node.next
list_index += 1

if list_index == index:
if list_index == index and previous_node and previous_node.next:
next_node = previous_node.next
previous_node.next = next_node.next
else:
Expand Down
34 changes: 17 additions & 17 deletions src/data_structures/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class DoublyLinkedList[T]:
"""

def __init__(self):
self.__head: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
self.__tail: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
self._head: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
self._tail: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)

self.__head.next = self.__tail
self.__tail.previous = self.__head
self._head.next = self._tail
self._tail.previous = self._head

# O(N)
def all_values(self) -> list[T | None]:
"""Return a list of every value in the list, in order from head to tail."""
values = []
node = self.__head.next
node = self._head.next

while node and node is not self.__tail:
while node and node is not self._tail:
values.append(node.data)
node = node.next

Expand All @@ -34,22 +34,22 @@ def append(self, data: T):
"""Add a new node containing data to the end of the list, just before the sentinel tail."""
# Last <-> Tail --> Last <-> New <-> Tail
node = DoublyLinkedListNode(data)
last_node = self.__tail.previous
last_node = self._tail.previous

if last_node:
last_node.next = node
node.previous = last_node

node.next = self.__tail
self.__tail.previous = node
node.next = self._tail
self._tail.previous = node

# O(N)
def get_node(self, index: int) -> DoublyLinkedListNode | None:
"""Return the node at index, or None if the index is out of bounds."""
current_node = self.__head.next
current_node = self._head.next
count = 0

while current_node and current_node is not self.__tail:
while current_node and current_node is not self._tail:
if count == index:
return current_node
current_node = current_node.next
Expand All @@ -71,10 +71,10 @@ def insert(self, data: T, index: int):
current_node: DoublyLinkedListNode[T] | None = self.get_node(index)

if not current_node and index == 0:
self.__head.next = node
node.previous = self.__head
node.next = self.__tail
self.__tail.previous = node
self._head.next = node
node.previous = self._head
node.next = self._tail
self._tail.previous = node
return

if not current_node or not current_node.previous:
Expand Down Expand Up @@ -103,9 +103,9 @@ def remove(self, index: int) -> T | None:
def size(self) -> int:
"""Return the number of nodes in the list."""
count = 0
node = self.__head.next
node = self._head.next

while node and node != self.__tail:
while node and node != self._tail:
count += 1
node = node.next
return count
4 changes: 2 additions & 2 deletions src/data_structures/doubly_linked_list_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class DoublyLinkedListNode[T]:

def __init__(
self,
data: T | None = None,
data: T,
previous_node: "DoublyLinkedListNode[T] | None" = None,
next_node: "DoublyLinkedListNode[T] | None" = None,
):
self.data: T | None = data
self.data: T = data
self.previous: "DoublyLinkedListNode[T] | None" = previous_node
self.next: "DoublyLinkedListNode[T] | None" = next_node
14 changes: 7 additions & 7 deletions src/data_structures/singly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class SinglyLinkedList[T]:
"""

def __init__(self):
self.__head: SinglyLinkedListNode[T] = SinglyLinkedListNode[T](None)
self._head: SinglyLinkedListNode[T] = SinglyLinkedListNode[T](None)

def all_values(self) -> list[T]:
"""Return a list of every value in the list, in order from head to tail."""
values = []
node = self.__head.next
node = self._head.next

while node:
values.append(node.data)
Expand All @@ -38,7 +38,7 @@ def all_values(self) -> list[T]:
def append(self, data: T) -> None:
"""Add a new node containing data to the end of the list."""
node = SinglyLinkedListNode(data)
last_node = self.__head
last_node = self._head

while last_node:
if not last_node.next:
Expand All @@ -49,7 +49,7 @@ def append(self, data: T) -> None:
# O(N)
def get(self, index: int) -> T | None:
"""Return the data stored at index, or None if the index is out of bounds."""
current_node = self.__head.next
current_node = self._head.next
count = 0

while current_node:
Expand All @@ -64,7 +64,7 @@ def get(self, index: int) -> T | None:
def insert(self, data: T, index: int) -> None:
"""Insert a new node containing data before the node currently at index."""
node = SinglyLinkedListNode(data)
current_node = self.__head
current_node = self._head
count = 0
while current_node:
if count == index:
Expand All @@ -80,7 +80,7 @@ def insert(self, data: T, index: int) -> None:
def remove(self, index: int) -> None:
"""Remove the node at index by linking its predecessor directly to its successor."""
count = 0
current_node = self.__head
current_node = self._head

while current_node:
if count == index:
Expand All @@ -96,7 +96,7 @@ def remove(self, index: int) -> None:
def size(self) -> int:
"""Return the number of nodes in the list."""
count = 0
current_node = self.__head.next
current_node = self._head.next

while current_node:
count += 1
Expand Down
34 changes: 17 additions & 17 deletions tests/data_structures/test_binary_tree_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@


def test_init():
tree = BinaryTreeNode(1)
left_node = BinaryTreeNode(2)
right_node = BinaryTreeNode(3)
root = BinaryTreeNode(1)
left = BinaryTreeNode(2)
right = BinaryTreeNode(3)

assert tree.data == 1
assert not tree.left_node
assert not tree.right_node
assert root.data == 1
assert not root.left
assert not root.right

tree = BinaryTreeNode(1, left_node, right_node)
assert tree.left_node.data == 2
assert tree.right_node.data == 3
root = BinaryTreeNode(1, left, right)
assert root.left and root.left.data == 2
assert root.right and root.right.data == 3


def test_left_node():
left_node = BinaryTreeNode(2)
tree = BinaryTreeNode(1)
tree.left_node = left_node
assert tree.left_node.data == 2
left = BinaryTreeNode(2)
root = BinaryTreeNode(1)
root.left = left
assert root.left and root.left.data == 2


def test_right_node():
right_node = BinaryTreeNode(2)
tree = BinaryTreeNode(1)
tree.right_node = right_node
assert tree.right_node.data == 2
right = BinaryTreeNode(2)
root = BinaryTreeNode(1)
root.right = right
assert root.right.data == 2
4 changes: 3 additions & 1 deletion tests/data_structures/test_doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def test_get_value():
def test_get_node():
doubly_linked_list = DoublyLinkedList()
doubly_linked_list.insert(3, 0)
assert doubly_linked_list.get_node(0).data == 3
node = doubly_linked_list.get_node(0)
assert node is not None
assert node.data == 3


def test_insert_empty():
Expand Down
9 changes: 2 additions & 7 deletions tests/data_structures/test_doubly_linked_list_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@


def test_doubly_linked_list_node():
node = DoublyLinkedListNode()
assert node.data is None
assert node.next is None
assert node.previous is None

node = DoublyLinkedListNode(1)
assert node.data == 1
assert node.previous is None
assert node.next is None

node = DoublyLinkedListNode(2, DoublyLinkedListNode(1), DoublyLinkedListNode(3))
assert node.data == 2
assert node.previous.data == 1
assert node.next.data == 3
assert node.previous and node.previous.data == 1
assert node.next and node.next.data == 3
Loading