📜  Python中的二叉树模块

📅  最后修改于: 2022-05-13 01:54:32.963000             🧑  作者: Mango

Python中的二叉树模块

二叉树是一种数据结构,其中每个节点或顶点最多有两个孩子。在Python中,二叉树可以用不同的数据结构(字典、列表)和节点的类表示以不同的方式表示。但是,二叉树库有助于直接实现二叉树。它还支持堆和二叉搜索树(BST)。这个模块没有预装 Python 的标准实用程序模块。要安装它,请在终端中键入以下命令。

pip install binarytree 

创建节点

节点类表示二叉树中特定节点的结构。这个类的属性是值,左,右。

注意:如果左或右子节点不是 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)


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)


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)


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)


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)


输出:

从 List 构建二叉树:

我们可以使用 build() 方法将值列表转换为二叉树,而不是重复使用 Node 方法。
这里,一个给定的列表包含树的节点,使得索引 i 处的元素在索引 2*i+1 处有左子节点,在索引 2*i+2 处有右子节点,在 (i – 1)//2 处有父节点. j>len(list)//2 的索引 j 处的元素是叶节点。 None 表示该索引处没有节点。我们还可以在使用values属性构建二叉树后获取节点列表。

例子:

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() 生成一棵随机二叉树并返回其根节点。

例子:

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() 生成一个随机二叉搜索树并返回其根节点。

例子:

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() 方法,我们可以生成一个随机的 maxheap 并返回它的根节点。要生成 minheap,我们需要将 is_max 属性设置为 False。

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