Skip to content

Commit 41ab1db

Browse files
committed
test(datastructures, binary-tree): connect all siblings, remove comparison of entire tree
1 parent f0b6e02 commit 41ab1db

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

datastructures/trees/binary/node.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ def height(self) -> int:
191191
pass
192192

193193
def __repr__(self):
194-
return f"BinaryTreeNode(data={self.data}, key={self.key}, left={self.left}, right={self.right}, parent={self.parent}, next={self.next})"
194+
parent_data = self.parent.data if self.parent else None
195+
next_data = self.next.data if self.next else None
196+
return f"BinaryTreeNode(data={self.data}, key={self.key}, left={self.left}, right={self.right}, parent={parent_data}, next={next_data})"
195197

196198
def __eq__(self, other: "BinaryTreeNode") -> bool:
197199
"""Checks if this node is equal to another node based on the data they contain

datastructures/trees/binary/test_utils.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from typing import List
23
from parameterized import parameterized
34
from datastructures.trees.binary.utils import (
45
lowest_common_ancestor,
@@ -196,48 +197,29 @@ def test_4(self):
196197
data=200, left=BinaryTreeNode(data=300), right=BinaryTreeNode(data=10)
197198
),
198199
),
199-
BinaryTreeNode(
200-
data=100,
201-
left=BinaryTreeNode(
202-
data=50, left=BinaryTreeNode(data=25), right=BinaryTreeNode(data=75)
203-
),
204-
right=BinaryTreeNode(
205-
data=200, left=BinaryTreeNode(data=300), right=BinaryTreeNode(data=10)
206-
),
207-
next=BinaryTreeNode(
208-
data=50,
209-
left=BinaryTreeNode(data=300),
210-
right=BinaryTreeNode(data=10),
211-
next=BinaryTreeNode(
212-
data=200,
213-
left=BinaryTreeNode(data=300),
214-
right=BinaryTreeNode(data=10),
215-
next=BinaryTreeNode(
216-
data=25,
217-
next=BinaryTreeNode(
218-
data=75,
219-
next=BinaryTreeNode(data=300, next=BinaryTreeNode(data=10)),
220-
),
221-
),
222-
),
223-
),
224-
),
200+
[100, 50, 200, 25, 75, 300, 10],
225201
),
226202
]
227203

228204

229205
class ConnectAllSiblingsTestCase(unittest.TestCase):
230206
@parameterized.expand(CONNECT_ALL_SIBLINGS_TEST_CASES)
231-
def test_connect_all_siblings(self, root: BinaryTreeNode, expected: BinaryTreeNode):
207+
def test_connect_all_siblings(self, root: BinaryTreeNode, expected: List[int]):
232208
actual = connect_all_siblings(root)
233-
self.assertEqual(expected, actual)
209+
current = actual
210+
for expected_val in expected:
211+
self.assertEqual(current.data, expected_val)
212+
current = current.next
213+
self.assertIsNone(current)
234214

235215
@parameterized.expand(CONNECT_ALL_SIBLINGS_TEST_CASES)
236-
def test_connect_all_siblings_ptr(
237-
self, root: BinaryTreeNode, expected: BinaryTreeNode
238-
):
216+
def test_connect_all_siblings_ptr(self, root: BinaryTreeNode, expected: List[int]):
239217
actual = connect_all_siblings_ptr(root)
240-
self.assertEqual(expected, actual)
218+
current = actual
219+
for expected_val in expected:
220+
self.assertEqual(current.data, expected_val)
221+
current = current.next
222+
self.assertIsNone(current)
241223

242224

243225
if __name__ == "__main__":

0 commit comments

Comments
 (0)