-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and test longest consecutive sequence algorithm for binary …
…trees
- Loading branch information
1 parent
8125338
commit 4a3ab9d
Showing
2 changed files
with
48 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tree/binary_tree/longest_consecutive_sequence/test_longest_consecutive_sequence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import unittest | ||
from longest_consecutive_sequence import Node, longest_consecutive | ||
|
||
class TestLongestConsecutiveSequence(unittest.TestCase): | ||
def test_example_case(self): | ||
root = Node(1) | ||
root.right = Node(3) | ||
root.right.left = Node(2) | ||
root.right.right = Node(4) | ||
root.right.right.right = Node(5) | ||
self.assertEqual(longest_consecutive(root), 3) | ||
|
||
def test_empty_tree(self): | ||
self.assertEqual(longest_consecutive(None), 0) | ||
|
||
def test_single_node(self): | ||
root = Node(1) | ||
self.assertEqual(longest_consecutive(root), 1) | ||
|
||
def test_no_consecutive_sequence(self): | ||
root = Node(1) | ||
root.left = Node(3) | ||
root.right = Node(5) | ||
self.assertEqual(longest_consecutive(root), 1) | ||
|
||
def test_multiple_consecutive_sequences(self): | ||
root = Node(1) | ||
root.left = Node(2) | ||
root.left.left = Node(3) | ||
root.right = Node(4) | ||
root.right.right = Node(5) | ||
root.right.right.right = Node(6) | ||
self.assertEqual(longest_consecutive(root), 3) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |