二叉樹是一種數據結構,其中每個節點或頂點最多有兩個子節點。在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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。