Python二叉樹定義與遍歷方法實例分析
本文實例講述了Python二叉樹定義與遍歷方法。分享給大家供大家參考,具體如下:
二叉樹基本概述:
二叉樹是有限個元素的幾個,如果為空則為空二叉樹,或者有一個結(jié)點稱之為根節(jié)點,分列根節(jié)點兩側(cè)的為二叉樹的左右子節(jié)點,二叉樹有如下的性質(zhì):
1. 二叉樹的每個結(jié)點不存在度大于2的結(jié)點
2. 二叉樹的第i層至多有2^{i-1}個結(jié)點
3. 深度為k的二叉樹至多有2^k - 1個結(jié)點
4. 二叉樹中,度為0的結(jié)點數(shù)N0比度為2的結(jié)點數(shù)N2大1,即存在N2 + 1 = N0
Python代碼:
#coding:utf-8
'BiTree'
class Node(object):
'Node Defination'
def __init__(self,item):
self.item = item
self.left = None
self.right = None
class Tree(object):
'Bitree Defination'
def __init__(self):
self.root = None
def add(self,item):
node = Node(item)
if self.root is None:
self.root = node
else:
q = [self.root]
while True:
pop_node = q.pop(0)
if pop_node.left is None:
pop_node.left = node
return
elif pop_node.right is None:
pop_node.right = node
return
else:
q.append(pop_node.left)
q.append(pop_node.right)
def traverse(self):#層次遍歷
if self.root is None:
return None
q = [self.root]
res = [self.root.item]
while q != []:
pop_node = q.pop(0)
if pop_node.left is not None:
q.append(pop_node.left)
res.append(pop_node.left.item)
if pop_node.right is not None:
q.append(pop_node.right)
res.append(pop_node.right.item)
return res
def preorder(self,root): #先序遍歷
if root is None:
return []
result = [root.item]
left_item = self.preorder(root.left)
right_item = self.preorder(root.right)
return result + left_item + right_item
def inorder(self,root): #中序遍歷
if root is None:
return []
result = [root.item]
left_item = self.inorder(root.left)
right_item = self.inorder(root.right)
return left_item + result + right_item
def postorder(self,root): #后序遍歷
if root is None:
return []
result = [root.item]
left_item = self.postorder(root.left)
right_item = self.postorder(root.right)
return left_item + right_item + result
if __name__=='__main__':
t = Tree()
for i in range(10):
t.add(i)
print "層序遍歷:",t.traverse()
print "先序遍歷:",t.preorder(t.root)
print "中序遍歷:",t.inorder(t.root)
print "后序遍歷:",t.postorder(t.root)
輸出結(jié)果:
層序遍歷: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
先序遍歷: [0, 1, 3, 7, 8, 4, 9, 2, 5, 6]
中序遍歷: [7, 3, 8, 1, 9, 4, 0, 5, 2, 6]
后序遍歷: [7, 8, 3, 9, 4, 1, 5, 6, 2, 0]
這里對于
if __name__=='__main__': “Make a script both importable and executable”
意思就是說讓你寫的腳本模塊既可以導(dǎo)入到別的模塊中用,另外該模塊自己也可執(zhí)行。
這里通過一個示例進(jìn)行解釋:
#test.py def func(): print "we are in %s"%__name__ if __name__ == '__main__': func()
輸出結(jié)果:
we are in __main__
說明if語句中的內(nèi)容被執(zhí)行了,調(diào)用了 func()函數(shù),現(xiàn)在在另一個模塊中調(diào)用func函數(shù)
#testtest from test import func func()
輸出結(jié)果:
we are in moudle
也就是說 if 條件中的內(nèi)容沒有執(zhí)行。
總結(jié):
如果直接執(zhí)行某個*.py文件,該文件中 if __name__ == '__main__'是True,相當(dāng)于調(diào)式本模塊的代碼;如果是從另一個模塊(testtest.py)通過import導(dǎo)入該文件的時候,這時__name__就是這個模塊的名字(test)而不是__main__,總之在調(diào)式代碼的時候加上 if __name__ == '__main__'中加入調(diào)試代碼,可以讓步模塊調(diào)用的時候不執(zhí)行調(diào)式代碼,如果想排查本模塊代碼的問題時,直接進(jìn)行調(diào)試執(zhí)行
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python實現(xiàn)創(chuàng)建詞云的示例詳解
詞云一般是根據(jù)輸入的大量詞語生成的,如果某個詞語出現(xiàn)的次數(shù)越多,那么相應(yīng)的大小就會越大,本文將利用wordcloud模塊實現(xiàn)詞云生成,需要的可以參考下2023-10-10
Python實現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作示例
這篇文章主要介紹了Python實現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作,涉及Python文件讀取、數(shù)值運算及基于pylab庫的圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
Pygame游戲開發(fā)之太空射擊實戰(zhàn)圖像精靈下篇
相信大多數(shù)8090后都玩過太空射擊游戲,在過去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來自己動手實現(xiàn)它,在編寫學(xué)習(xí)中回顧過往展望未來,下面開始入門篇2022-08-08
利用Python自帶PIL庫擴(kuò)展圖片大小給圖片加文字描述的方法示例
最近的一個工程項目是講文字添加到圖像上,所以下面這篇文章主要給大家介紹了關(guān)于利用Python自帶PIL庫擴(kuò)展圖片大小給圖片加文字描述的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08

