二叉树是一种数据结构,其中每个节点或顶点最多有两个子节点。在Python中,二叉树可以通过不同的数据结构(字典、列表)和节点的类表示以不同的方式表示。然而,binarytree库有助于直接实现二叉树。它还支持堆和二叉搜索树(BST)。该模块未预装 Python 的标准实用程序模块。要安装它,请在终端中键入以下命令。
pip install binarytree
创建节点
节点类表示二叉树中特定节点的结构。这个类的属性是values,left,right。
用法: binarytree.Node(value, left=None, right=None)
参数:
value: Contains the data for a node. This value must be number.
left: Contains the details of left node child.
right: Contains details of the right node child.
注意:如果左或右子节点不是binarytree.Node类的实例,则引发binarytree.exceptions.NodeTypeError,如果节点值不是数字,则引发binarytree.exceptions.NodeValueError。
例子:
Python3
from binarytree import Node
root = Node(3)
root.left = Node(6)
root.right = Node(8)
# Getting binary tree
print('Binary tree :', root)
# Getting list of nodes
print('List of nodes :', list(root))
# Getting inorder of nodes
print('Inorder of nodes :', root.inorder)
# Checking tree properties
print('Size of tree :', root.size)
print('Height of tree :', root.height)
# Get all properties at once
print('Properties of tree : \n', root.properties)
输出:
Binary tree :
3
/ \
6 8
List of nodes : [Node(3), Node(6), Node(8)]
Inorder of nodes : [Node(6), Node(3), Node(8)]
Size of tree : 3
Height of tree : 1
Properties of tree :
{‘height’: 1, ‘size’: 3, ‘is_max_heap’: False, ‘is_min_heap’: True, ‘is_perfect’: True, ‘is_strict’: True, ‘is_complete’: True, ‘leaf_count’: 2, ‘min_node_value’: 3, ‘max_node_value’: 8, ‘min_leaf_depth’: 1, ‘max_leaf_depth’: 1, ‘is_bst’: False, ‘is_balanced’: True, ‘is_symmetric’: False}
从 List 构建二叉树:
我们可以使用build()方法将值列表转换为二叉树,而不是重复使用Node方法。
这里,给定列表包含树的节点,使得索引 i 处的元素的左子元素位于索引 2*i+1 处,右子元素位于索引 2*i+2 处,父元素位于 (i - 1)//2 。对于 j>len(list)//2,索引 j 处的元素是叶节点。 None 表示该索引处不存在节点。在使用 value 属性构建二叉树后,我们还可以获取节点列表。
语法:binarytree.build(值)
参数:
值:二叉树的列表表示。
返回: 二叉树的根。
例子:
Python3
# Creating binary tree
# from given list
from binarytree import build
# List of nodes
nodes =[3, 6, 8, 2, 11, None, 13]
# Building the binary tree
binary_tree = build(nodes)
print('Binary tree from list :\n',
binary_tree)
# Getting list of nodes from
# binarytree
print('\nList from binary tree :',
binary_tree.values)
输出:
Binary tree from list : ___3 / \ 6 8 / \ \ 2 11 13 List from binary tree : [3, 6, 8, 2, 11, None, 13]
构建随机二叉树:
tree()生成一个随机二叉树并返回其根节点。
语法:binarytree.tree(高度=3, is_perfect=False)
参数:
height:树的高度,其值可以在0-9之间(含)
is_perfect:如果设置为 True,则会创建完美的二进制文件。
返回: 二叉树的根节点。
例子:
Python3
from binarytree import tree
# Create a random binary
# tree of any height
root = tree()
print("Binary tree of any height :")
print(root)
# Create a random binary
# tree of given height
root2 = tree(height = 2)
print("Binary tree of given height :")
print(root2)
# Create a random perfect
# binary tree of given height
root3 = tree(height = 2,
is_perfect = True)
print("Perfect binary tree of given height :")
print(root3)
输出:
Binary tree of any height : 14____ / \ 2 5__ / / \ 6 1 13 / / / \ 7 9 4 8 Binary tree of given height : 1__ / \ 5 2 / \ 4 3 Perfect binary tree of given height : __3__ / \ 2 4 / \ / \ 6 0 1 5
构建 BST:
二叉搜索树是一种特殊类型的树数据结构,其顺序给出了节点或顶点的排序列表。在Python中,我们可以使用binarytree模块直接创建BST对象。 bst()生成一个随机二叉搜索树并返回其根节点。
语法:binarytree.bst(高度=3, is_perfect=False)
参数:
height:树的高度,其值可以在0-9之间(含)
is_perfect:如果设置为 True,则会创建完美的二进制文件。
返回: BST 的根节点。
例子:
Python3
from binarytree import bst
# Create a random BST
# of any height
root = bst()
print('BST of any height : \n',
root)
# Create a random BST of
# given height
root2 = bst(height = 2)
print('BST of given height : \n',
root2)
# Create a random perfect
# BST of given height
root3 = bst(height = 2,
is_perfect = True)
print('Perfect BST of given height : \n',
root3)
输出:
BST of any height : ____9______ / \ __5__ ____12___ / \ / \ 2 8 10 _14 / \ / \ / 1 4 7 11 13 BST of given height : 5 / \ 4 6 / 3 Perfect BST of given height : __3__ / \ 1 5 / \ / \ 0 2 4 6
导入堆:
堆是一种树形数据结构,可以有两种类型 -
- 最大堆
- 最小堆
使用二叉树库的heap()方法,我们可以生成一个随机最大堆并返回其根节点。要生成 minheap,我们需要将 is_max 属性设置为 False。
语法:binarytree.heap(高度=3, is_max=True, is_perfect=False)
参数:
height:树的高度,其值可以在0-9之间(含)
is_max:如果设置为 True,则生成最大堆,否则生成最小堆。
is_perfect:如果设置为 True,则会创建完美的二进制文件。
返回: 堆的根节点。
Python3
from binarytree import heap
# Create a random max-heap
root = heap()
print('Max-heap of any height : \n',
root)
# Create a random max-heap
# of given height
root2 = heap(height = 2)
print('Max-heap of given height : \n',
root2)
# Create a random perfect
# min-heap of given height
root3 = heap(height = 2,
is_max = False,
is_perfect = True)
print('Perfect min-heap of given height : \n',
root3)
输出:
Max-heap of any height : _______14______ / \ ___12__ __13__ / \ / \ 10 8 3 9 / \ / \ / \ / 1 5 4 6 0 2 7 Max-heap of given height : __6__ / \ 4 5 / \ / \ 2 0 1 3 Perfect min-heap of given height : __0__ / \ 1 3 / \ / \ 2 6 4 5
相关用法
- Python Binary转Hexadecimal用法及代码示例
- Python Bytes转Int用法及代码示例
- Python Bytes转String用法及代码示例
- Python BaseException.with_traceback用法及代码示例
- Python BeautifulSoup find_next方法用法及代码示例
- Python BeautifulSoup next_elements属性用法及代码示例
- Python BeautifulSoup Tag stripped_strings属性用法及代码示例
- Python BeautifulSoup Tag contents属性用法及代码示例
- Python BeautifulSoup parent属性用法及代码示例
- Python BeautifulSoup append方法用法及代码示例
- Python BeautifulSoup previous_siblings属性用法及代码示例
- Python BeautifulSoup previous_sibling属性用法及代码示例
- Python BeautifulSoup Tag get_text方法用法及代码示例
- Python BeautifulSoup find_parent方法用法及代码示例
- Python BeautifulSoup replace_with方法用法及代码示例
- Python BeautifulSoup find_all_next方法用法及代码示例
- Python BeautifulSoup Tag descendants属性用法及代码示例
- Python BeautifulSoup extract方法用法及代码示例
- Python BeautifulSoup insert方法用法及代码示例
- Python BeautifulSoup Tag children属性用法及代码示例
- Python BeautifulSoup find_all方法用法及代码示例
- Python BeautifulSoup parents属性用法及代码示例
- Python BeautifulSoup find_previous_sibling方法用法及代码示例
- Python BeautifulSoup find方法用法及代码示例
- Python BeautifulSoup insert_before方法用法及代码示例
注:本文由纯净天空筛选整理自naina024大神的英文原创作品 Binarytree Module in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。