-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Simplify is_bst.py #10627
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
Merged
Merged
Simplify is_bst.py #10627
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0dd29c3
Simplify is_bst.py
cclauss d47de46
updating DIRECTORY.md
ded2c3b
Update is_bst.py
cclauss 595c7a6
Rename is_bst.py to is_sorted.py
cclauss c6d76b6
updating DIRECTORY.md
40f86fe
Update data_structures/binary_tree/is_sorted.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,96 @@ | ||
""" | ||
Given the root of a binary tree, determine if it is a valid binary search tree (BST). | ||
|
||
A valid binary search tree is defined as follows: | ||
- The left subtree of a node contains only nodes with keys less than the node's key. | ||
- The right subtree of a node contains only nodes with keys greater than the node's key. | ||
- Both the left and right subtrees must also be binary search trees. | ||
|
||
leetcode: https://leetcode.com/problems/validate-binary-search-tree/ | ||
|
||
If n is the number of nodes in the tree then: | ||
Runtime: O(n) | ||
Space: O(1) | ||
""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
data: float | ||
left: Node | None = None | ||
right: Node | None = None | ||
|
||
def __iter__(self) -> Iterator[float]: | ||
""" | ||
>>> root = Node(data=2.1) | ||
>>> list(root) | ||
[2.1] | ||
>>> root.left=Node(data=2.0) | ||
>>> list(root) | ||
[2.0, 2.1] | ||
>>> root.right=Node(data=2.2) | ||
>>> list(root) | ||
[2.0, 2.1, 2.2] | ||
""" | ||
if self.left: | ||
yield from self.left | ||
yield self.data | ||
if self.right: | ||
yield from self.right | ||
|
||
@property | ||
def is_sorted(self) -> bool: | ||
""" | ||
>>> Node(data='abc').is_sorted | ||
True | ||
>>> Node(data=2, | ||
... left=Node(data=1.999), | ||
... right=Node(data=3)).is_sorted | ||
True | ||
>>> Node(data=0, | ||
... left=Node(data=0), | ||
... right=Node(data=0)).is_sorted | ||
True | ||
>>> Node(data=0, | ||
... left=Node(data=-11), | ||
... right=Node(data=3)).is_sorted | ||
True | ||
>>> Node(data=5, | ||
... left=Node(data=1), | ||
... right=Node(data=4, left=Node(data=3))).is_sorted | ||
False | ||
>>> Node(data='a', | ||
... left=Node(data=1), | ||
... right=Node(data=4, left=Node(data=3))).is_sorted | ||
Traceback (most recent call last): | ||
... | ||
TypeError: '<' not supported between instances of 'str' and 'int' | ||
>>> Node(data=2, | ||
... left=Node([]), | ||
... right=Node(data=4, left=Node(data=3))).is_sorted | ||
Traceback (most recent call last): | ||
... | ||
TypeError: '<' not supported between instances of 'int' and 'list' | ||
""" | ||
if self.left and (self.data < self.left.data or not self.left.is_sorted): | ||
return False | ||
if self.right and (self.data > self.right.data or not self.right.is_sorted): | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
tree = Node(data=2.1, left=Node(data=2.0), right=Node(data=2.2)) | ||
print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") | ||
assert tree.right | ||
tree.right.data = 2.0 | ||
print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") | ||
tree.right.data = 2.1 | ||
print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.