Skip to content

Commit 26ffad9

Browse files
cclaussgithub-actionstianyizheng02
authored
Simplify is_bst.py (#10627)
* Simplify is_bst.py * updating DIRECTORY.md * Update is_bst.py * Rename is_bst.py to is_sorted.py * updating DIRECTORY.md * Update data_structures/binary_tree/is_sorted.py Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com> --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent 6987614 commit 26ffad9

File tree

3 files changed

+99
-132
lines changed

3 files changed

+99
-132
lines changed

DIRECTORY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@
199199
* [Flatten Binarytree To Linkedlist](data_structures/binary_tree/flatten_binarytree_to_linkedlist.py)
200200
* [Floor And Ceiling](data_structures/binary_tree/floor_and_ceiling.py)
201201
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
202-
* [Is Bst](data_structures/binary_tree/is_bst.py)
202+
* [Is Sorted](data_structures/binary_tree/is_sorted.py)
203+
* [Is Sum Tree](data_structures/binary_tree/is_sum_tree.py)
203204
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
204205
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
205206
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)

data_structures/binary_tree/is_bst.py

-131
This file was deleted.
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
3+
4+
A valid binary search tree is defined as follows:
5+
- The left subtree of a node contains only nodes with keys less than the node's key.
6+
- The right subtree of a node contains only nodes with keys greater than the node's key.
7+
- Both the left and right subtrees must also be binary search trees.
8+
9+
In effect, a binary tree is a valid BST if its nodes are sorted in ascending order.
10+
leetcode: https://leetcode.com/problems/validate-binary-search-tree/
11+
12+
If n is the number of nodes in the tree then:
13+
Runtime: O(n)
14+
Space: O(1)
15+
"""
16+
from __future__ import annotations
17+
18+
from collections.abc import Iterator
19+
from dataclasses import dataclass
20+
21+
22+
@dataclass
23+
class Node:
24+
data: float
25+
left: Node | None = None
26+
right: Node | None = None
27+
28+
def __iter__(self) -> Iterator[float]:
29+
"""
30+
>>> root = Node(data=2.1)
31+
>>> list(root)
32+
[2.1]
33+
>>> root.left=Node(data=2.0)
34+
>>> list(root)
35+
[2.0, 2.1]
36+
>>> root.right=Node(data=2.2)
37+
>>> list(root)
38+
[2.0, 2.1, 2.2]
39+
"""
40+
if self.left:
41+
yield from self.left
42+
yield self.data
43+
if self.right:
44+
yield from self.right
45+
46+
@property
47+
def is_sorted(self) -> bool:
48+
"""
49+
>>> Node(data='abc').is_sorted
50+
True
51+
>>> Node(data=2,
52+
... left=Node(data=1.999),
53+
... right=Node(data=3)).is_sorted
54+
True
55+
>>> Node(data=0,
56+
... left=Node(data=0),
57+
... right=Node(data=0)).is_sorted
58+
True
59+
>>> Node(data=0,
60+
... left=Node(data=-11),
61+
... right=Node(data=3)).is_sorted
62+
True
63+
>>> Node(data=5,
64+
... left=Node(data=1),
65+
... right=Node(data=4, left=Node(data=3))).is_sorted
66+
False
67+
>>> Node(data='a',
68+
... left=Node(data=1),
69+
... right=Node(data=4, left=Node(data=3))).is_sorted
70+
Traceback (most recent call last):
71+
...
72+
TypeError: '<' not supported between instances of 'str' and 'int'
73+
>>> Node(data=2,
74+
... left=Node([]),
75+
... right=Node(data=4, left=Node(data=3))).is_sorted
76+
Traceback (most recent call last):
77+
...
78+
TypeError: '<' not supported between instances of 'int' and 'list'
79+
"""
80+
if self.left and (self.data < self.left.data or not self.left.is_sorted):
81+
return False
82+
if self.right and (self.data > self.right.data or not self.right.is_sorted):
83+
return False
84+
return True
85+
86+
87+
if __name__ == "__main__":
88+
import doctest
89+
90+
doctest.testmod()
91+
tree = Node(data=2.1, left=Node(data=2.0), right=Node(data=2.2))
92+
print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.")
93+
assert tree.right
94+
tree.right.data = 2.0
95+
print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.")
96+
tree.right.data = 2.1
97+
print(f"Tree {list(tree)} is sorted: {tree.is_sorted = }.")

0 commit comments

Comments
 (0)