Skip to content

Commit a0c700b

Browse files
authored
Update node code (#192)
1 parent 1e4c72a commit a0c700b

8 files changed

Lines changed: 73 additions & 98 deletions
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
1-
class BinaryTreeNode:
1+
class BinaryTreeNode[T]:
22
"""Binary Tree Node.
33
44
A node in a binary tree, holding data plus references to its left and right child nodes.
55
"""
66

7-
def __init__(self, data, left_node=None, right_node=None):
8-
self.__data = data
9-
self.__left_node = left_node
10-
self.__right_node = right_node
11-
12-
@property
13-
def data(self):
14-
return self.__data
15-
16-
@data.setter
17-
def data(self, data):
18-
self.__data = data
19-
20-
@property
21-
def left_node(self):
22-
return self.__left_node
23-
24-
@left_node.setter
25-
def left_node(self, node):
26-
self.__left_node = node
27-
28-
@property
29-
def right_node(self):
30-
return self.__right_node
31-
32-
@right_node.setter
33-
def right_node(self, node):
34-
self.__right_node = node
7+
def __init__(self, data: T, left: BinaryTreeNode[T] | None = None, right: BinaryTreeNode[T] | None = None):
8+
self.data = data
9+
self.left = left
10+
self.right = right

‎src/data_structures/circularly_linked_list.py‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode
22

33

4-
class CircularlyLinkedList:
4+
class CircularlyLinkedList[T]:
55
"""Circularly Linked List.
66
77
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:
1212
This is more of an example. It can be optimized in a variety of ways depending on its intended usage.
1313
"""
1414

15-
def __init__(self, node: SinglyLinkedListNode | None = None):
16-
self.__head: SinglyLinkedListNode = node
15+
def __init__(self, node: SinglyLinkedListNode[T] | None = None):
16+
self._head: SinglyLinkedListNode[T] | None = node
1717
if node:
18-
node.next = self.__head
18+
node.next = self._head
1919

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

2424
@head.setter
25-
def head(self, node: SinglyLinkedListNode):
26-
self.__head = node
25+
def head(self, node: SinglyLinkedListNode[T] | None):
26+
self._head = node
2727
if node:
28-
node.next = self.__head
28+
node.next = self._head
2929

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

42-
def append(self, node: SinglyLinkedListNode):
42+
def append(self, node: SinglyLinkedListNode[T]):
4343
"""Insert node just before head, so the list stays circular with head as the last-linked node."""
44-
previous_node = self.head
45-
46-
if not previous_node:
44+
if not self.head:
4745
self.head = node
4846
node.next = self.head
4947
return
5048

51-
while previous_node.next != self.head:
49+
previous_node = self.head
50+
51+
while previous_node and previous_node.next != self.head:
5252
previous_node = previous_node.next
5353

54+
if not previous_node:
55+
return
5456
next_node = previous_node.next
5557
previous_node.next = node
5658
node.next = next_node
@@ -66,17 +68,17 @@ def remove(self, index: int):
6668
self.head = None
6769
return
6870

69-
if index == 0:
71+
if index == 0 and self.head:
7072
previous_node.next = self.head.next
7173
self.head = previous_node.next
7274
return
7375

7476
list_index = 1
75-
while previous_node.next is not self.head and list_index < index:
77+
while previous_node and previous_node.next is not self.head and list_index < index:
7678
previous_node = previous_node.next
7779
list_index += 1
7880

79-
if list_index == index:
81+
if list_index == index and previous_node and previous_node.next:
8082
next_node = previous_node.next
8183
previous_node.next = next_node.next
8284
else:

‎src/data_structures/doubly_linked_list.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ class DoublyLinkedList[T]:
1111
"""
1212

1313
def __init__(self):
14-
self.__head: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
15-
self.__tail: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
14+
self._head: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
15+
self._tail: DoublyLinkedListNode[T] = DoublyLinkedListNode[T](None)
1616

17-
self.__head.next = self.__tail
18-
self.__tail.previous = self.__head
17+
self._head.next = self._tail
18+
self._tail.previous = self._head
1919

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

26-
while node and node is not self.__tail:
26+
while node and node is not self._tail:
2727
values.append(node.data)
2828
node = node.next
2929

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

3939
if last_node:
4040
last_node.next = node
4141
node.previous = last_node
4242

43-
node.next = self.__tail
44-
self.__tail.previous = node
43+
node.next = self._tail
44+
self._tail.previous = node
4545

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

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

7373
if not current_node and index == 0:
74-
self.__head.next = node
75-
node.previous = self.__head
76-
node.next = self.__tail
77-
self.__tail.previous = node
74+
self._head.next = node
75+
node.previous = self._head
76+
node.next = self._tail
77+
self._tail.previous = node
7878
return
7979

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

108-
while node and node != self.__tail:
108+
while node and node != self._tail:
109109
count += 1
110110
node = node.next
111111
return count

‎src/data_structures/doubly_linked_list_node.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class DoublyLinkedListNode[T]:
77

88
def __init__(
99
self,
10-
data: T | None = None,
10+
data: T,
1111
previous_node: "DoublyLinkedListNode[T] | None" = None,
1212
next_node: "DoublyLinkedListNode[T] | None" = None,
1313
):
14-
self.data: T | None = data
14+
self.data: T = data
1515
self.previous: "DoublyLinkedListNode[T] | None" = previous_node
1616
self.next: "DoublyLinkedListNode[T] | None" = next_node

‎src/data_structures/singly_linked_list.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class SinglyLinkedList[T]:
2121
"""
2222

2323
def __init__(self):
24-
self.__head: SinglyLinkedListNode[T] = SinglyLinkedListNode[T](None)
24+
self._head: SinglyLinkedListNode[T] = SinglyLinkedListNode[T](None)
2525

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

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

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

5555
while current_node:
@@ -64,7 +64,7 @@ def get(self, index: int) -> T | None:
6464
def insert(self, data: T, index: int) -> None:
6565
"""Insert a new node containing data before the node currently at index."""
6666
node = SinglyLinkedListNode(data)
67-
current_node = self.__head
67+
current_node = self._head
6868
count = 0
6969
while current_node:
7070
if count == index:
@@ -80,7 +80,7 @@ def insert(self, data: T, index: int) -> None:
8080
def remove(self, index: int) -> None:
8181
"""Remove the node at index by linking its predecessor directly to its successor."""
8282
count = 0
83-
current_node = self.__head
83+
current_node = self._head
8484

8585
while current_node:
8686
if count == index:
@@ -96,7 +96,7 @@ def remove(self, index: int) -> None:
9696
def size(self) -> int:
9797
"""Return the number of nodes in the list."""
9898
count = 0
99-
current_node = self.__head.next
99+
current_node = self._head.next
100100

101101
while current_node:
102102
count += 1

‎tests/data_structures/test_binary_tree_node.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33

44
def test_init():
5-
tree = BinaryTreeNode(1)
6-
left_node = BinaryTreeNode(2)
7-
right_node = BinaryTreeNode(3)
5+
root = BinaryTreeNode(1)
6+
left = BinaryTreeNode(2)
7+
right = BinaryTreeNode(3)
88

9-
assert tree.data == 1
10-
assert not tree.left_node
11-
assert not tree.right_node
9+
assert root.data == 1
10+
assert not root.left
11+
assert not root.right
1212

13-
tree = BinaryTreeNode(1, left_node, right_node)
14-
assert tree.left_node.data == 2
15-
assert tree.right_node.data == 3
13+
root = BinaryTreeNode(1, left, right)
14+
assert root.left and root.left.data == 2
15+
assert root.right and root.right.data == 3
1616

1717

1818
def test_left_node():
19-
left_node = BinaryTreeNode(2)
20-
tree = BinaryTreeNode(1)
21-
tree.left_node = left_node
22-
assert tree.left_node.data == 2
19+
left = BinaryTreeNode(2)
20+
root = BinaryTreeNode(1)
21+
root.left = left
22+
assert root.left and root.left.data == 2
2323

2424

2525
def test_right_node():
26-
right_node = BinaryTreeNode(2)
27-
tree = BinaryTreeNode(1)
28-
tree.right_node = right_node
29-
assert tree.right_node.data == 2
26+
right = BinaryTreeNode(2)
27+
root = BinaryTreeNode(1)
28+
root.right = right
29+
assert root.right.data == 2

‎tests/data_structures/test_doubly_linked_list.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def test_get_value():
4343
def test_get_node():
4444
doubly_linked_list = DoublyLinkedList()
4545
doubly_linked_list.insert(3, 0)
46-
assert doubly_linked_list.get_node(0).data == 3
46+
node = doubly_linked_list.get_node(0)
47+
assert node is not None
48+
assert node.data == 3
4749

4850

4951
def test_insert_empty():

‎tests/data_structures/test_doubly_linked_list_node.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33

44
def test_doubly_linked_list_node():
5-
node = DoublyLinkedListNode()
6-
assert node.data is None
7-
assert node.next is None
8-
assert node.previous is None
9-
105
node = DoublyLinkedListNode(1)
116
assert node.data == 1
127
assert node.previous is None
138
assert node.next is None
149

1510
node = DoublyLinkedListNode(2, DoublyLinkedListNode(1), DoublyLinkedListNode(3))
1611
assert node.data == 2
17-
assert node.previous.data == 1
18-
assert node.next.data == 3
12+
assert node.previous and node.previous.data == 1
13+
assert node.next and node.next.data == 3

0 commit comments

Comments
 (0)