Python實(shí)現(xiàn)二叉樹(shù)前序、中序、后序及層次遍歷示例代碼
前言
樹(shù)是數(shù)據(jù)結(jié)構(gòu)中非常重要的一種,主要的用途是用來(lái)提高查找效率,對(duì)于要重復(fù)查找的情況效果更佳,如二叉排序樹(shù)、FP-樹(shù)。另外可以用來(lái)提高編碼效率,如哈弗曼樹(shù)。
用 Python 實(shí)現(xiàn)樹(shù)的構(gòu)造和幾種遍歷算法。實(shí)現(xiàn)功能如下:
- 樹(shù)的構(gòu)造
- 遞歸實(shí)現(xiàn)先序遍歷、中序遍歷、后序遍歷
- 堆棧實(shí)現(xiàn)先序遍歷、中序遍歷、后序遍歷
- 隊(duì)列實(shí)現(xiàn)層次遍歷
# -*- coding=utf-8 -*-
class Node(object):
"""節(jié)點(diǎn)類"""
def __init__(self, element=-1, l_child=None, r_child=None):
self.element = element
self.l_child = l_child
self.r_child = r_child
class Tree(object):
"""樹(shù)類"""
def __init__(self):
self.root = Node()
self.queue = []
def add_node(self, element):
"""為樹(shù)添加節(jié)點(diǎn)"""
node = Node(element)
# 如果樹(shù)是空的,則對(duì)根節(jié)點(diǎn)賦值
if self.root.element == -1:
self.root = node
self.queue.append(self.root)
else:
tree_node = self.queue[0]
# 此結(jié)點(diǎn)沒(méi)有左子樹(shù),則創(chuàng)建左子樹(shù)節(jié)點(diǎn)
if tree_node.l_child is None:
tree_node.l_child = node
self.queue.append(tree_node.l_child)
else:
tree_node.r_child = node
self.queue.append(tree_node.r_child)
# 如果該結(jié)點(diǎn)存在右子樹(shù),將此節(jié)點(diǎn)丟棄
self.queue.pop(0)
def front_recursion(self, root):
"""利用遞歸實(shí)現(xiàn)樹(shù)的前序遍歷"""
if root is None:
return
print root.element,
self.front_recursion(root.l_child)
self.front_recursion(root.r_child)
def middle_recursion(self, root):
"""利用遞歸實(shí)現(xiàn)樹(shù)的中序遍歷"""
if root is None:
return
self.middle_recursion(root.l_child)
print root.element,
self.middle_recursion(root.r_child)
def back_recursion(self, root):
"""利用遞歸實(shí)現(xiàn)樹(shù)的后序遍歷"""
if root is None:
return
self.back_recursion(root.l_child)
self.back_recursion(root.r_child)
print root.element,
@staticmethod
def front_stack(root):
"""利用堆棧實(shí)現(xiàn)樹(shù)的前序遍歷"""
if root is None:
return
stack = []
node = root
while node or stack:
# 從根節(jié)點(diǎn)開(kāi)始,一直找它的左子樹(shù)
while node:
print node.element,
stack.append(node)
node = node.l_child
# while結(jié)束表示當(dāng)前節(jié)點(diǎn)node為空,即前一個(gè)節(jié)點(diǎn)沒(méi)有左子樹(shù)了
node = stack.pop()
# 開(kāi)始查看它的右子樹(shù)
node = node.r_child
@staticmethod
def middle_stack(root):
"""利用堆棧實(shí)現(xiàn)樹(shù)的中序遍歷"""
if root is None:
return
stack = []
node = root
while node or stack:
# 從根節(jié)點(diǎn)開(kāi)始,一直找它的左子樹(shù)
while node:
stack.append(node)
node = node.l_child
# while結(jié)束表示當(dāng)前節(jié)點(diǎn)node為空,即前一個(gè)節(jié)點(diǎn)沒(méi)有左子樹(shù)了
node = stack.pop()
print node.element,
# 開(kāi)始查看它的右子樹(shù)
node = node.r_child
@staticmethod
def back_stack(root):
"""利用堆棧實(shí)現(xiàn)樹(shù)的后序遍歷"""
if root is None:
return
stack1 = []
stack2 = []
node = root
stack1.append(node)
# 這個(gè)while循環(huán)的功能是找出后序遍歷的逆序,存在stack2里面
while stack1:
node = stack1.pop()
if node.l_child:
stack1.append(node.l_child)
if node.r_child:
stack1.append(node.r_child)
stack2.append(node)
# 將stack2中的元素出棧,即為后序遍歷次序
while stack2:
print stack2.pop().element,
@staticmethod
def level_queue(root):
"""利用隊(duì)列實(shí)現(xiàn)樹(shù)的層次遍歷"""
if root is None:
return
queue = []
node = root
queue.append(node)
while queue:
node = queue.pop(0)
print node.element,
if node.l_child is not None:
queue.append(node.l_child)
if node.r_child is not None:
queue.append(node.r_child)
if __name__ == '__main__':
"""主函數(shù)"""
# 生成十個(gè)數(shù)據(jù)作為樹(shù)節(jié)點(diǎn)
elements = range(10)
tree = Tree()
for elem in elements:
tree.add_node(elem)
print '隊(duì)列實(shí)現(xiàn)層次遍歷:'
tree.level_queue(tree.root)
print '\n\n遞歸實(shí)現(xiàn)前序遍歷:'
tree.front_recursion(tree.root)
print '\n遞歸實(shí)現(xiàn)中序遍歷:'
tree.middle_recursion(tree.root)
print '\n遞歸實(shí)現(xiàn)后序遍歷:'
tree.back_recursion(tree.root)
print '\n\n堆棧實(shí)現(xiàn)前序遍歷:'
tree.front_stack(tree.root)
print '\n堆棧實(shí)現(xiàn)中序遍歷:'
tree.middle_stack(tree.root)
print '\n堆棧實(shí)現(xiàn)后序遍歷:'
tree.back_stack(tree.root)
需要源碼的小伙伴可自行下載:代碼傳送門
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Python matplotlib繪制實(shí)時(shí)數(shù)據(jù)動(dòng)畫(huà)
Matplotlib作為Python的2D繪圖庫(kù),它以各種硬拷貝格式和跨平臺(tái)的交互式環(huán)境生成出版質(zhì)量級(jí)別的圖形。本文將利用Matplotlib庫(kù)繪制實(shí)時(shí)數(shù)據(jù)動(dòng)畫(huà),感興趣的可以了解一下2022-03-03
正則表達(dá)式在Python中的應(yīng)用小結(jié)
正則表達(dá)式是一種強(qiáng)大的文本模式匹配工具,它可以幫助我們快速地檢索、替換或提取字符串中的特定模式,在本文中,我將通過(guò)一些示例代碼,詳細(xì)介紹正則表達(dá)式在Python中的應(yīng)用,感興趣的朋友一起看看吧2024-07-07
Pytorch中如何調(diào)用forward()函數(shù)
這篇文章主要介紹了Pytorch中如何調(diào)用forward()函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python 反編譯exe文件為py文件的實(shí)例代碼
這篇文章主要介紹了python 反編譯exe文件為py文件的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
python數(shù)據(jù)預(yù)處理 :數(shù)據(jù)抽樣解析
這篇文章主要介紹了python數(shù)據(jù)預(yù)處理 :數(shù)據(jù)抽樣解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

