Skip to content

Commit 32bf26a

Browse files
committed
Fix a bug in binary search tree check
1 parent 32d87ce commit 32bf26a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

binarytree/__init__.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,31 @@ def _is_balanced(node):
128128
return 0
129129

130130
left = _is_balanced(_left_of(node))
131-
right = _is_balanced(_right_of(node))
131+
if left < 0:
132+
return -1
132133

133-
if left < 0 or right < 0 or abs(left - right) > 1:
134+
right = _is_balanced(_right_of(node))
135+
if right < 0 or abs(left - right) > 1:
134136
return -1
137+
135138
return max(left, right) + 1
136139

137140

141+
def _is_bst(node, min_val=float('-inf'), max_val=float('inf')):
142+
"""Return True if and only if the tree is a binary search tree."""
143+
if node == _null:
144+
return True
145+
146+
if (min_val != _null and _value_of(node) <= min_val):
147+
return False
148+
149+
if (max_val != _null and _value_of(node) >= max_val):
150+
return False
151+
152+
return _is_bst(_left_of(node), min_val, _value_of(node)) and \
153+
_is_bst(_right_of(node), _value_of(node), max_val)
154+
155+
138156
def _build_list(root):
139157
"""Build a list from a tree and return it."""
140158
result = []
@@ -643,7 +661,6 @@ def inspect(bt):
643661
bt = _prepare_tree(bt)
644662

645663
is_full = True
646-
is_bst = True
647664
is_descending = True
648665
is_ascending = True
649666
is_left_padded = True
@@ -685,7 +702,6 @@ def inspect(bt):
685702
if left_child != _null:
686703
if _value_of(left_child) > node_value:
687704
is_descending = False
688-
is_bst = False
689705
elif _value_of(left_child) < node_value:
690706
is_ascending = False
691707
next_nodes.append(left_child)
@@ -696,7 +712,6 @@ def inspect(bt):
696712
is_descending = False
697713
elif _value_of(right_child) < node_value:
698714
is_ascending = False
699-
is_bst = False
700715
next_nodes.append(right_child)
701716
num_of_children += 1
702717
if num_of_children == 1:
@@ -711,7 +726,7 @@ def inspect(bt):
711726
'is_weight_balanced': is_balanced,
712727
'is_max_heap': is_descending and is_left_padded and is_balanced,
713728
'is_min_heap': is_ascending and is_left_padded and is_balanced,
714-
'is_bst': is_bst,
729+
'is_bst': _is_bst(bt),
715730
'height': current_depth,
716731
'leaf_count': leaf_count,
717732
'node_count': node_count,

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
setup(
44
name='binarytree',
55
description='Python Library for Learning Binary Trees',
6-
version='2.0.0',
6+
version='2.0.1',
77
author='Joohwan Oh',
88
author_email='[email protected]',
99
url='https://github.com/joowani/binarytree',
1010
packages=find_packages(),
1111
include_package_data=True,
12+
tests_require=['pytest'],
1213
classifiers=[
1314
'Intended Audience :: Developers',
1415
'Intended Audience :: End Users/Desktop',

0 commit comments

Comments
 (0)