Python之binarytree库函数使用详解

Python之binarytree库函数使用详解

本篇博客旨在大致介绍binarytree中部分库函数的作用,细节需要读者查阅相关帮助文档。

库函数介绍

函数名 输入参数 功能
_is_balanced(root) 二叉树的根结点(binarytree.Node类型) 如果二叉树是平衡的则返回树的高度,否则返回-1
_is_bst(root, min_value=float(’-inf’), max_value=float(‘inf’)) root:二叉树的根结点;min_value:最小的结点值;max_value:最大的结点值 如果这是一个二叉搜索树,返回True,否则返回False
_validate_tree_height(height) height:0-9的整数 检查二叉树的高度是否有效
_generate_perfect_bst(height) height:正整数 生成一棵完美的二叉树
_generate_random_node_values(height) height:树的高度 随机返回所有结点值中的某一个
_get_tree_properties(root) root:根结点 返回二叉树的属性
Node(value,left,right) 结点类的构造函数,value:结点值;left和right分别是左右子树,类型为结点类的实例 创建结点实例
len(root) root:根结点 返回树的结点数
root.validate - 检测二叉树是否畸形
root.value - 逐层从左到右返回结点值
root.leaves - 返回二叉树的叶子
root.height - 返回二叉树的高度
root.size - 返回所有结点的个数
root.leaf_count - 返回叶子的数目
root.is_balance - 返回布尔值,检测二叉树是否平衡
root.is_bst - 返回布尔值,检测二叉树是否为二叉搜索树
root.is_max_heap - 返回布尔值,检测二叉树是否为最大堆。关于最大堆和最小堆的含义参考 https://en.wikipedia.org/wiki/Min-max_heap
root.is_min_heap - 返回布尔值,检测二叉树是否为最小堆。
root.is_perfect - 返回布尔值,检测二叉树是否完美。一个二叉树是完美的如果其每一层的结点都被填满。
root.is_strict - 返回布尔值,检测二叉树是否严格。一个二叉树是严格的如果其非叶子结点既有左子结点,又有右子结点。
root,is_complete - 返回布尔值,检测二叉树是否完整。一个二叉树完整:除了最后一层以外,每一层的结点都被填满;最后一层的结点左对齐。
root.min_node_value - 返回所有结点中的最小值。
root.max_node_value - 返回所有结点中的最大值。
root.max_leaf_depth - 返回叶子结点中最大的深度。
root.min_leaf_depth - 返回叶子结点中最小的深度。
root.properties - 返回一个字典,包含二叉树的所有属性
root.inorder - 返回一个中序遍历列表,元素为Node类型。
root.preorder - 返回一个先序遍历列表,元素为Node类型。
root.postorder - 返回一个后序遍历列表,元素为Node类型。
root.levelorder - 返回一个逐层遍历列表
binarytree.build(values) values:列表 从列表元素逐层建立二叉树
binarytree.tree(height, is_perfect) height:高度,默认为3;is_perfect:是否完美,布尔值,默认为False 生成一个随机二叉树,并返回根结点
binarytree.bst(height, is_perfect) height:高度,默认为3;is_perfect:是否完美,布尔值,默认为False 生成一个随机二叉搜索树,并返回根结点
binarytree,heap(height=3, is_max=True, is_perfect=False) is_max:布尔值,是否为最大堆 生成一个堆,并返回根结点

例子

from binarytree import Node
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.right = Node(4)
print(root)

#输出
  __1
 /   \
2     3
 \
  4
#依次访问树的结点
from binarytree import Node
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root[0]
root[1]
root[2]
# 输出
Node(1)
Node(2)
Node(3)
>>> from binarytree import Node
>>>
>>> root = Node(1)       # index: 0, value: 1
>>> root.left = Node(2)  # index: 1, value: 2
>>> root.right = Node(3) # index: 2, value: 3
>>>
>>> root[0] = Node(4)  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
NodeModifyError: cannot modify the root node
>>> from binarytree import Node
>>>
>>> root = Node(1)       # index: 0, value: 1
>>> root.left = Node(2)  # index: 1, value: 2
>>> root.right = Node(3) # index: 2, value: 3
>>>
>>> root[1] = Node(4)
>>>
>>> root.left
Node(4)
>>> from binarytree import Node
>>>
>>> root = Node(1)       # index: 0, value: 1
>>> root.left = Node(2)  # index: 1, value: 2
>>> root.right = Node(3) # index: 2, value: 3
>>>
>>> root[11] = Node(4)  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
NodeNotFoundError: parent node missing at index 5
>>> from binarytree import Node
>>>
>>> root = Node(1)          # index: 0, value: 1
>>> root.left = Node(2)     # index: 1, value: 2
>>> root.right = Node(3)    # index: 2, value: 3
>>>
>>> del root[0]  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
NodeModifyError: cannot delete the root node
>>> from binarytree import Node
>>>
>>> root = Node(1)          # index: 0, value: 1
>>> root.left = Node(2)     # index: 1, value: 2
>>> root.right = Node(3)    # index: 2, value: 3
>>>
>>> del root[2]
>>>
>>> root[2]  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
NodeNotFoundError: node missing at index 2
>>> from binarytree import Node
>>>
>>> root = Node(1)              # index: 0, value: 1
>>> root.left = Node(2)         # index: 1, value: 2
>>> root.right = Node(3)        # index: 2, value: 3
>>> root.left.right = Node(4)   # index: 4, value: 4
>>>
>>> root.pprint()
<BLANKLINE>
__1
/   \\
2     3
\\
4
<BLANKLINE>
>>> root.pprint(index=True)     # Format: {index}-{value}
<BLANKLINE>
_____0-1_
/         \\
1-2_        2-3
\\
4-4
<BLANKLINE>
>>> from binarytree import Node
>>>
>>> root = Node(1)
>>> root.left = Node(2)
>>> root.right = Node(3)
>>> root.left.right = Node(4)
>>>
>>> root.values
[1, 2, 3, None, 4]
>>> from binarytree import Node
>>>
>>> root = Node(1)
>>> root.left = Node(2)
>>> root.right = Node(3)
>>> root.left.right = Node(4)
>>>
>>> print(root)
<BLANKLINE>
__1
/   \\
2     3
\\
4
<BLANKLINE>
>>> root.leaves
[Node(3), Node(4)]
>>> from binarytree import Node
>>>
>>> root = Node(1)
>>> root.left = Node(2)
>>> root.right = Node(3)
>>> root.left.left = Node(4)
>>> root.left.right = Node(5)
>>> props = root.properties
>>>
>>> props['height']         # equivalent to root.height
2
>>> props['size']           # equivalent to root.size
5
>>> props['max_leaf_depth'] # equivalent to root.max_leaf_depth
2
>>> props['min_leaf_depth'] # equivalent to root.min_leaf_depth
1
>>> props['max_node_value'] # equivalent to root.max_node_value
5
>>> props['min_node_value'] # equivalent to root.min_node_value
1
>>> props['leaf_count']     # equivalent to root.leaf_count
3
>>> props['is_balanced']    # equivalent to root.is_balanced
True
>>> props['is_bst']         # equivalent to root.is_bst
False
>>> props['is_complete']    # equivalent to root.is_complete
True
>>> props['is_max_heap']    # equivalent to root.is_max_heap
False
>>> props['is_min_heap']    # equivalent to root.is_min_heap
True
>>> props['is_perfect']     # equivalent to root.is_perfect
False
>>> props['is_strict']      # equivalent to root.is_strict
True
>>> from binarytree import build
>>>
>>> root = build([1, 2, 3, None, 4])
>>>
>>> print(root)
<BLANKLINE>
__1
/   \\
2     3
\\
4
<BLANKLINE>

猜你喜欢

转载自blog.csdn.net/qq7835144/article/details/87951881