Python樹(shù)的重建實(shí)現(xiàn)示例
樹(shù)的重建(Tree Reconstruction)是一種從給定的遍歷序列中恢復(fù)原樹(shù)結(jié)構(gòu)的算法。在本文中,我們將討論樹(shù)的重建問(wèn)題以及常見(jiàn)的重建算法,包括先序遍歷和中序遍歷序列重建二叉樹(shù),以及層序遍歷序列重建二叉樹(shù)。我們將提供Python代碼實(shí)現(xiàn),并詳細(xì)說(shuō)明每個(gè)算法的原理和步驟。
1. 先序遍歷和中序遍歷序列重建二叉樹(shù)
給定一個(gè)二叉樹(shù)的先序遍歷序列和中序遍歷序列,我們可以通過(guò)遞歸地進(jìn)行樹(shù)的重建。先序遍歷序列的第一個(gè)元素為根節(jié)點(diǎn),在中序遍歷序列中找到該元素,將其分為左子樹(shù)和右子樹(shù),然后遞歸對(duì)左右子樹(shù)進(jìn)行同樣的操作。
class TreeNode:
def __init__(self, value):
self.val = value
self.left = None
self.right = None
def build_tree(preorder, inorder):
if not preorder or not inorder:
return None
root_val = preorder[0]
root = TreeNode(root_val)
index = inorder.index(root_val)
root.left = build_tree(preorder[1:index + 1], inorder[:index])
root.right = build_tree(preorder[index + 1:], inorder[index + 1:])
return root
2. 層序遍歷序列重建二叉樹(shù)
給定一個(gè)二叉樹(shù)的層序遍歷序列,我們可以使用隊(duì)列來(lái)逐層構(gòu)建樹(shù)結(jié)構(gòu)。隊(duì)列中的每個(gè)元素代表一個(gè)樹(shù)節(jié)點(diǎn),我們按照層序遍歷的順序依次將節(jié)點(diǎn)加入隊(duì)列,并根據(jù)隊(duì)列中的順序建立樹(shù)的連接關(guān)系。
from collections import deque
def build_tree_level_order(level_order):
if not level_order:
return None
root = TreeNode(level_order[0])
queue = deque([root])
i = 1
while i < len(level_order):
current = queue.popleft()
left_val = level_order[i]
i += 1
if left_val is not None:
current.left = TreeNode(left_val)
queue.append(current.left)
right_val = level_order[i]
i += 1
if right_val is not None:
current.right = TreeNode(right_val)
queue.append(current.right)
return root
示例
示例1:先序遍歷和中序遍歷序列重建二叉樹(shù)
preorder = [3, 9, 20, 15, 7]
inorder = [9, 3, 15, 20, 7]
root = build_tree(preorder, inorder)
# 驗(yàn)證重建的樹(shù)
def inorder_traversal(root):
if root is not None:
inorder_traversal(root.left)
print(root.val, end=" ")
inorder_traversal(root.right)
print("Inorder Traversal of Reconstructed Tree:")
inorder_traversal(root)
輸出結(jié)果:
Inorder Traversal of Reconstructed Tree:
9 3 15 20 7
示例2:層序遍歷序列重建二叉樹(shù)
level_order = [3, 9, 20, None, None, 15, 7]
root_level_order = build_tree_level_order(level_order)
# 驗(yàn)證重建的樹(shù)
def inorder_traversal_level_order(root):
if root is not None:
inorder_traversal_level_order(root.left)
print(root.val, end=" ")
inorder_traversal_level_order(root.right)
print("Inorder Traversal of Reconstructed Tree from Level Order:")
inorder_traversal_level_order(root_level_order)
輸出結(jié)果:
Inorder Traversal of Reconstructed Tree from Level Order:
9 3 15 20 7
以上兩個(gè)示例演示了樹(shù)的重建算法的使用,分別使用先序遍歷和中序遍歷序列,以及層序遍歷序列重建二叉樹(shù)。這些算法在樹(shù)的序列化和反序列化中起到關(guān)鍵作用,通過(guò)理解其原理和實(shí)現(xiàn),您將能夠更好地處理樹(shù)結(jié)構(gòu)的相關(guān)問(wèn)題。
到此這篇關(guān)于Python樹(shù)的重建實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python樹(shù)的重建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何獲取HTTP請(qǐng)求的Response Body
這篇文章主要介紹了在Python中如何獲取HTTP請(qǐng)求的響應(yīng)體,包括使用內(nèi)置的urllib庫(kù)、第三方庫(kù)requests以及一些高級(jí)用法,有需要的小伙伴可以了解下2024-11-11
python可視化數(shù)據(jù)分析pyecharts初步嘗試
這篇文章主要為大家介紹了python可視化數(shù)據(jù)分析pyecharts初步嘗試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
python3實(shí)現(xiàn)繪制二維點(diǎn)圖
今天小編就為大家分享一篇python3實(shí)現(xiàn)繪制二維點(diǎn)圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python開(kāi)發(fā)實(shí)例分享bt種子爬蟲(chóng)程序和種子解析
最近瘋狂的研究DHT網(wǎng)絡(luò)技術(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2014-05-05

