-
Notifications
You must be signed in to change notification settings - Fork 2
feat(datastructures, binary-tree): connect all siblings #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f663ab7
14fde9b
f0b6e02
41ab1db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import unittest | ||
| from parameterized import parameterized | ||
| from datastructures.trees.binary.utils import ( | ||
| lowest_common_ancestor, | ||
| lowest_common_ancestor_ptr, | ||
| connect_all_siblings, | ||
| connect_all_siblings_ptr, | ||
| ) | ||
| from datastructures.trees.binary.node import BinaryTreeNode | ||
|
|
||
|
|
@@ -182,5 +185,60 @@ def test_4(self): | |
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| CONNECT_ALL_SIBLINGS_TEST_CASES = [ | ||
| ( | ||
| BinaryTreeNode( | ||
| data=100, | ||
| left=BinaryTreeNode( | ||
| data=50, left=BinaryTreeNode(data=25), right=BinaryTreeNode(data=75) | ||
| ), | ||
| right=BinaryTreeNode( | ||
| data=200, left=BinaryTreeNode(data=300), right=BinaryTreeNode(data=10) | ||
| ), | ||
| ), | ||
| BinaryTreeNode( | ||
| data=100, | ||
| left=BinaryTreeNode( | ||
| data=50, left=BinaryTreeNode(data=25), right=BinaryTreeNode(data=75) | ||
| ), | ||
| right=BinaryTreeNode( | ||
| data=200, left=BinaryTreeNode(data=300), right=BinaryTreeNode(data=10) | ||
| ), | ||
| next=BinaryTreeNode( | ||
| data=50, | ||
| left=BinaryTreeNode(data=300), | ||
| right=BinaryTreeNode(data=10), | ||
| next=BinaryTreeNode( | ||
| data=200, | ||
| left=BinaryTreeNode(data=300), | ||
| right=BinaryTreeNode(data=10), | ||
| next=BinaryTreeNode( | ||
| data=25, | ||
| next=BinaryTreeNode( | ||
| data=75, | ||
| next=BinaryTreeNode(data=300, next=BinaryTreeNode(data=10)), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ] | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class ConnectAllSiblingsTestCase(unittest.TestCase): | ||
| @parameterized.expand(CONNECT_ALL_SIBLINGS_TEST_CASES) | ||
| def test_connect_all_siblings(self, root: BinaryTreeNode, expected: BinaryTreeNode): | ||
| actual = connect_all_siblings(root) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(CONNECT_ALL_SIBLINGS_TEST_CASES) | ||
| def test_connect_all_siblings_ptr( | ||
| self, root: BinaryTreeNode, expected: BinaryTreeNode | ||
| ): | ||
| actual = connect_all_siblings_ptr(root) | ||
| self.assertEqual(expected, actual) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test isolation issue: input tree is mutated between tests. Both Suggested fix: Create fresh tree instances for each test+def create_test_tree():
+ return BinaryTreeNode(
+ data=100,
+ left=BinaryTreeNode(
+ data=50, left=BinaryTreeNode(data=25), right=BinaryTreeNode(data=75)
+ ),
+ right=BinaryTreeNode(
+ data=200, left=BinaryTreeNode(data=300), right=BinaryTreeNode(data=10)
+ ),
+ )
+
class ConnectAllSiblingsTestCase(unittest.TestCase):
- @parameterized.expand(CONNECT_ALL_SIBLINGS_TEST_CASES)
- def test_connect_all_siblings(self, root: BinaryTreeNode, expected: BinaryTreeNode):
- actual = connect_all_siblings(root)
- self.assertEqual(expected, actual)
+ def test_connect_all_siblings(self):
+ root = create_test_tree()
+ result = connect_all_siblings(root)
+ # verify next chain...
- @parameterized.expand(CONNECT_ALL_SIBLINGS_TEST_CASES)
- def test_connect_all_siblings_ptr(
- self, root: BinaryTreeNode, expected: BinaryTreeNode
- ):
- actual = connect_all_siblings_ptr(root)
- self.assertEqual(expected, actual)
+ def test_connect_all_siblings_ptr(self):
+ root = create_test_tree()
+ result = connect_all_siblings_ptr(root)
+ # verify next chain...🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Uh oh!
There was an error while loading. Please reload this page.