MCPcopy Index your code
hub / github.com/joowani/binarytree / _get_tree_properties

Function _get_tree_properties

binarytree/__init__.py:1980–2055  ·  view source on GitHub ↗

Inspect the binary tree and return its properties (e.g. height). :param root: Root node of the binary tree. :type root: binarytree.Node :return: Binary tree properties. :rtype: binarytree.NodeProperties

(root: Node)

Source from the content-addressed store, hash-verified

1978
1979
1980def _get_tree_properties(root: Node) -> NodeProperties:
1981 """Inspect the binary tree and return its properties (e.g. height).
1982
1983 :param root: Root node of the binary tree.
1984 :type root: binarytree.Node
1985 :return: Binary tree properties.
1986 :rtype: binarytree.NodeProperties
1987 """
1988 is_descending = True
1989 is_ascending = True
1990 min_node_value = root.val
1991 max_node_value = root.val
1992 size = 0
1993 leaf_count = 0
1994 min_leaf_depth = 0
1995 max_leaf_depth = -1
1996 is_strict = True
1997 is_complete = True
1998 current_nodes = [root]
1999 non_full_node_seen = False
2000
2001 while len(current_nodes) > 0:
2002 max_leaf_depth += 1
2003 next_nodes = []
2004
2005 for node in current_nodes:
2006 size += 1
2007 val = node.val
2008 min_node_value = min(val, min_node_value)
2009 max_node_value = max(val, max_node_value)
2010
2011 # Node is a leaf.
2012 if node.left is None and node.right is None:
2013 if min_leaf_depth == 0:
2014 min_leaf_depth = max_leaf_depth
2015 leaf_count += 1
2016
2017 if node.left is not None:
2018 if node.left.val > val:
2019 is_descending = False
2020 elif node.left.val < val:
2021 is_ascending = False
2022 next_nodes.append(node.left)
2023 is_complete = not non_full_node_seen
2024 else:
2025 non_full_node_seen = True
2026
2027 if node.right is not None:
2028 if node.right.val > val:
2029 is_descending = False
2030 elif node.right.val < val:
2031 is_ascending = False
2032 next_nodes.append(node.right)
2033 is_complete = not non_full_node_seen
2034 else:
2035 non_full_node_seen = True
2036
2037 # If we see a node with only one child, it is not strict

Callers 12

heightMethod · 0.85
leaf_countMethod · 0.85
is_max_heapMethod · 0.85
is_min_heapMethod · 0.85
is_perfectMethod · 0.85
is_strictMethod · 0.85
is_completeMethod · 0.85
min_node_valueMethod · 0.85
max_node_valueMethod · 0.85
max_leaf_depthMethod · 0.85
min_leaf_depthMethod · 0.85
propertiesMethod · 0.85

Calls 1

NodePropertiesClass · 0.85

Tested by

no test coverage detected