-
- Notifications
You must be signed in to change notification settings - Fork 49.2k
add algorithm to check binary search tree #7947
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
cclauss merged 30 commits into TheAlgorithms:master from alexpantyukhin:is_binary_search_tree Nov 3, 2022
Merged
Changes from 17 commits
Commits
Show all changes
30 commits Select commit Hold shift + click to select a range
e9f4b23 add algorithm to check binary search tree
alexpantyukhin e2693ee add tests
alexpantyukhin 948dc8b add leetcode link
alexpantyukhin 39ec691 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 068607d fix typehints
alexpantyukhin c721ca3 typehints fixes
alexpantyukhin 7c13235 Merge remote-tracking branch 'origin/is_binary_search_tree' into is_b…
alexpantyukhin 7e76f52 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c531f96 Update data_structures/binary_tree/is_bst.py
alexpantyukhin f821963 Update data_structures/binary_tree/is_bst.py
alexpantyukhin 73769ae Update data_structures/binary_tree/is_bst.py
alexpantyukhin dd0fbf8 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6b3eac4 fix flake8
alexpantyukhin a58f63f Merge branch 'is_binary_search_tree' of https://github.com/alexpantyu…
alexpantyukhin 385a5ab fix typehint
alexpantyukhin ef2e902 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a467c1f add TreeNode resolving
alexpantyukhin 7cffe20 Update data_structures/binary_tree/is_bst.py
alexpantyukhin 516cb2d Update data_structures/binary_tree/is_bst.py
alexpantyukhin 6ba1056 Update data_structures/binary_tree/is_bst.py
alexpantyukhin bdf43d4 Update data_structures/binary_tree/is_bst.py
alexpantyukhin c11b07d change func name
alexpantyukhin 459133c Update data_structures/binary_tree/is_bst.py
alexpantyukhin 365cceb review notes fixes.
alexpantyukhin bebfcd0 Merge branch 'is_binary_search_tree' of https://github.com/alexpantyu…
alexpantyukhin 99c1a5d [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 896ae3a fix flake8
alexpantyukhin f488438 fix flake 8
alexpantyukhin 4a4d578 fix doctest
alexpantyukhin 60bf506 Update data_structures/binary_tree/is_bst.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """ | ||
| Author : Alexander Pantyukhin | ||
| Date : November 2, 2022 | ||
| | ||
| Task: | ||
| Given the root of a binary tree, determine if it is a valid binary search | ||
| tree (BST). | ||
| | ||
| A valid BST 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. | ||
| | ||
| Implementation notes: | ||
| Depth-first search approach. | ||
| | ||
| leetcode: https://leetcode.com/problems/validate-binary-search-tree/ | ||
| | ||
| Let n is the number of nodes in tree | ||
| Runtime: O(n) | ||
| Space: O(1) | ||
alexpantyukhin marked this conversation as resolved. Show resolved Hide resolved | ||
| """ | ||
| | ||
| from __future__ import annotations | ||
| | ||
| | ||
| class TreeNode: | ||
alexpantyukhin marked this conversation as resolved. Show resolved Hide resolved | ||
| def __init__( | ||
alexpantyukhin marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| self, | ||
| data: float = 0, | ||
| left: TreeNode | None = None, | ||
| right: TreeNode | None = None, | ||
| ) -> None: | ||
| self.data = data | ||
| self.left = left | ||
| self.right = right | ||
| | ||
| | ||
| def is_bst(root: TreeNode | None) -> bool: | ||
alexpantyukhin marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| """ | ||
| >>> is_bst(TreeNode(2, TreeNode(1), TreeNode(3))) | ||
| True | ||
| | ||
alexpantyukhin marked this conversation as resolved. Show resolved Hide resolved | ||
| >>> is_bst(TreeNode(0, TreeNode(-11), TreeNode(3))) | ||
| True | ||
| | ||
alexpantyukhin marked this conversation as resolved. Show resolved Hide resolved | ||
| >>> is_bst(TreeNode(5, TreeNode(1), TreeNode(4, TreeNode(3)))) | ||
| False | ||
| | ||
| >>> is_bst(TreeNode('a', TreeNode(1), TreeNode(4, TreeNode(3)))) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Each node should be type of TreeNode and data should be float. | ||
| | ||
| >>> is_bst(TreeNode(2, TreeNode([]), TreeNode(4, TreeNode(3)))) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Each node should be type of TreeNode and data should be float. | ||
| """ | ||
| | ||
| # Validation | ||
| def is_valid_tree(node: TreeNode | None) -> bool: | ||
| """ | ||
| >>> is_valid_tree(None) | ||
| True | ||
| | ||
| >>> is_valid_tree('abc') | ||
| False | ||
| | ||
| >>> is_valid_tree(TreeNode('not a float')) | ||
| False | ||
| | ||
| >>> is_valid_tree(TreeNode(1, TreeNode('123'))) | ||
| False | ||
| """ | ||
| if node is None: | ||
| return True | ||
| | ||
| if not isinstance(node, TreeNode): | ||
| return False | ||
| | ||
| try: | ||
| float(node.data) | ||
| except ValueError: | ||
| return False | ||
| except TypeError: | ||
| return False | ||
alexpantyukhin marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| return is_valid_tree(node.left) and is_valid_tree(node.right) | ||
| | ||
| if not is_valid_tree(root): | ||
| raise ValueError( | ||
| "Each node should be type of TreeNode and data should be float." | ||
| ) | ||
| | ||
| def is_bst_internal( | ||
alexpantyukhin marked this conversation as resolved. Outdated Show resolved Hide resolved alexpantyukhin marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| node: TreeNode | None, left_bound: float, right_bound: float | ||
| ) -> bool: | ||
| """ | ||
| >>> is_bst_internal(None) | ||
| True | ||
alexpantyukhin marked this conversation as resolved. Show resolved Hide resolved | ||
| """ | ||
| | ||
| if node is None: | ||
| return True | ||
| | ||
| return ( | ||
| (node.data > left_bound) | ||
| and (node.data < right_bound) | ||
cclauss marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| and is_bst_internal(node.left, left_bound, node.data) | ||
| and is_bst_internal(node.right, node.data, right_bound) | ||
| ) | ||
| | ||
| return is_bst_internal(root, -float("inf"), float("inf")) | ||
| | ||
| | ||
| if __name__ == "__main__": | ||
| import doctest | ||
| | ||
| doctest.testmod() | ||
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.