Skip to content

Commit

Permalink
Merge pull request #1 from joaquingx/master
Browse files Browse the repository at this point in the history
Property `is_full` added to the inspect function
  • Loading branch information
joowani committed Oct 10, 2016
2 parents 9d51494 + 4d7b1ac commit 3a7074c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions binarytree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def inspect(bt):
else:
raise ValueError('Expecting a list or a node')

is_full = True
is_bst = True
is_descending = True
is_ascending = True
Expand All @@ -457,14 +458,15 @@ def inspect(bt):
min_leaf_depth = 0
current_depth = -1
current_nodes = [bt]

while current_nodes:

null_encountered = False
current_depth += 1
next_nodes = []

for node in current_nodes:
num_of_children = 0
node_count += 1
node_value = _value_of(node)
min_value = min(node_value, min_value)
Expand All @@ -478,7 +480,7 @@ def inspect(bt):
is_left_padded = False
elif child == _null and not null_encountered:
null_encountered = True

if left_child == _null and right_child == _null:
if min_leaf_depth == 0:
min_leaf_depth = current_depth
Expand All @@ -491,6 +493,7 @@ def inspect(bt):
elif _value_of(left_child) < node_value:
is_ascending = False
next_nodes.append(left_child)
num_of_children +=1

if right_child != _null:
if _value_of(right_child) > node_value:
Expand All @@ -499,6 +502,9 @@ def inspect(bt):
is_ascending = False
is_bst = False
next_nodes.append(right_child)
num_of_children += 1
if num_of_children == 1:
is_full = False

current_nodes = next_nodes

Expand All @@ -516,5 +522,6 @@ def inspect(bt):
'min_leaf_depth': min_leaf_depth,
'max_leaf_depth': current_depth,
'min_value': min_value,
'max_value': max_value
'max_value': max_value,
'is_full': is_full
}
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def self_inspect(target):
'is_max_heap': True,
'is_min_heap': True,
'is_bst': True,
'is_full': True,
'height': 0,
'max_value': 1,
'min_value': 1,
Expand All @@ -290,6 +291,7 @@ def self_inspect(target):
'is_max_heap': False,
'is_min_heap': True,
'is_bst': False,
'is_full' : False,
'height': 1,
'max_value': 2,
'min_value': 1,
Expand All @@ -305,6 +307,7 @@ def self_inspect(target):
'is_max_heap': False,
'is_min_heap': True,
'is_bst': False,
'is_full' : True,
'height': 1,
'max_value': 3,
'min_value': 1,
Expand All @@ -322,6 +325,7 @@ def self_inspect(target):
'is_max_heap': False,
'is_min_heap': False,
'is_bst': True,
'is_full' : True,
'height': 1,
'max_value': 3,
'min_value': 1,
Expand All @@ -340,6 +344,7 @@ def self_inspect(target):
'is_max_heap': False,
'is_min_heap': False,
'is_bst': False,
'is_full' : False,
'height': 2,
'max_value': 4,
'min_value': 1,
Expand All @@ -355,6 +360,7 @@ def self_inspect(target):
'is_max_heap': False,
'is_min_heap': True,
'is_bst': False,
'is_full' : True,
'height': 2,
'max_value': 5,
'min_value': 1,
Expand All @@ -370,6 +376,7 @@ def self_inspect(target):
'is_max_heap': False,
'is_min_heap': False,
'is_bst': False,
'is_full' : False,
'height': 2,
'max_value': 6,
'min_value': 1,
Expand Down

0 comments on commit 3a7074c

Please sign in to comment.