From 40868f440272680d1381fa1a8cbdfa4647f32afd Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 2 Aug 2026 15:32:12 -0700 Subject: [PATCH] Update node code --- src/data_structures/binary_tree_node.py | 34 +++-------------- src/data_structures/circularly_linked_list.py | 38 ++++++++++--------- src/data_structures/doubly_linked_list.py | 34 ++++++++--------- .../doubly_linked_list_node.py | 4 +- src/data_structures/singly_linked_list.py | 14 +++---- .../data_structures/test_binary_tree_node.py | 34 ++++++++--------- .../test_doubly_linked_list.py | 4 +- .../test_doubly_linked_list_node.py | 9 +---- 8 files changed, 73 insertions(+), 98 deletions(-) diff --git a/src/data_structures/binary_tree_node.py b/src/data_structures/binary_tree_node.py index dee8e50..797521f 100644 --- a/src/data_structures/binary_tree_node.py +++ b/src/data_structures/binary_tree_node.py @@ -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 diff --git a/src/data_structures/circularly_linked_list.py b/src/data_structures/circularly_linked_list.py index 284dc0e..da272e7 100644 --- a/src/data_structures/circularly_linked_list.py +++ b/src/data_structures/circularly_linked_list.py @@ -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 @@ -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 @@ -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 @@ -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: diff --git a/src/data_structures/doubly_linked_list.py b/src/data_structures/doubly_linked_list.py index f6dc573..74c70ed 100644 --- a/src/data_structures/doubly_linked_list.py +++ b/src/data_structures/doubly_linked_list.py @@ -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 @@ -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 @@ -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: @@ -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 diff --git a/src/data_structures/doubly_linked_list_node.py b/src/data_structures/doubly_linked_list_node.py index 93aef84..c37c923 100644 --- a/src/data_structures/doubly_linked_list_node.py +++ b/src/data_structures/doubly_linked_list_node.py @@ -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 diff --git a/src/data_structures/singly_linked_list.py b/src/data_structures/singly_linked_list.py index cc1592b..7843cc5 100644 --- a/src/data_structures/singly_linked_list.py +++ b/src/data_structures/singly_linked_list.py @@ -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) @@ -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: @@ -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: @@ -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: @@ -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: @@ -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 diff --git a/tests/data_structures/test_binary_tree_node.py b/tests/data_structures/test_binary_tree_node.py index 320b05d..ac4a442 100644 --- a/tests/data_structures/test_binary_tree_node.py +++ b/tests/data_structures/test_binary_tree_node.py @@ -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 diff --git a/tests/data_structures/test_doubly_linked_list.py b/tests/data_structures/test_doubly_linked_list.py index 97d11e0..73f3fa0 100644 --- a/tests/data_structures/test_doubly_linked_list.py +++ b/tests/data_structures/test_doubly_linked_list.py @@ -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(): diff --git a/tests/data_structures/test_doubly_linked_list_node.py b/tests/data_structures/test_doubly_linked_list_node.py index 9bdf401..644d5ae 100644 --- a/tests/data_structures/test_doubly_linked_list_node.py +++ b/tests/data_structures/test_doubly_linked_list_node.py @@ -2,11 +2,6 @@ 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 @@ -14,5 +9,5 @@ def test_doubly_linked_list_node(): 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