Python定義二叉樹(shù)及4種遍歷方法實(shí)例詳解
本文實(shí)例講述了Python定義二叉樹(shù)及4種遍歷方法。分享給大家供大家參考,具體如下:
Python & BinaryTree
1. BinaryTree (二叉樹(shù))
二叉樹(shù)是有限個(gè)元素的集合,該集合或者為空、或者有一個(gè)稱為根節(jié)點(diǎn)(root)的元素及兩個(gè)互不相交的、分別被稱為左子樹(shù)和右子樹(shù)的二叉樹(shù)組成。
- 二叉樹(shù)的每個(gè)結(jié)點(diǎn)至多只有二棵子樹(shù)(不存在度大于2的結(jié)點(diǎn)),二叉樹(shù)的子樹(shù)有左右之分,次序不能顛倒。
- 二叉樹(shù)的第i層至多有2^{i-1}個(gè)結(jié)點(diǎn)
- 深度為k的二叉樹(shù)至多有2^k-1個(gè)結(jié)點(diǎn);
- 對(duì)任何一棵二叉樹(shù)T,如果其終端結(jié)點(diǎn)數(shù)為N0,度為2的結(jié)點(diǎn)數(shù)為N2,則N0=N2+1
2. 二叉樹(shù)

生成二叉樹(shù)
# init a tree
def InitBinaryTree(dataSource, length):
root = BTNode(dataSource[0])
for x in xrange(1,length):
node = BTNode(dataSource[x])
InsertElementBinaryTree(root, node)
return root
print 'Done...'
前序遍歷
# pre-order
def PreorderTraversalBinaryTree(root):
if root:
print '%d | ' % root.data,
PreorderTraversalBinaryTree(root.leftChild)
PreorderTraversalBinaryTree(root.rightChild)
中序遍歷
# in-order
def InorderTraversalBinaryTree(root):
if root:
InorderTraversalBinaryTree(root.leftChild)
print '%d | ' % root.data,
InorderTraversalBinaryTree(root.rightChild)
后序遍歷
# post-order
def PostorderTraversalBinaryTree(root):
if root:
PostorderTraversalBinaryTree(root.leftChild)
PostorderTraversalBinaryTree(root.rightChild)
print '%d | ' % root.data,
按層遍歷
# layer-order
def TraversalByLayer(root, length):
stack = []
stack.append(root)
for x in xrange(length):
node = stack[x]
print '%d | ' % node.data,
if node.leftChild:
stack.append(node.leftChild)
if node.rightChild:
stack.append(node.rightChild)
Result

二叉樹(shù)的思想重在“遞歸”, 并不是非要用遞歸處理,而是去理解二叉樹(shù)遞歸的思想
完整代碼段
# -*- coding:utf-8 -*-
#################
### implement Binary Tree using python
### Hongwing
### 2016-9-4
#################
import math
class BTNode(object):
"""docstring for BTNode"""
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
# insert element
def InsertElementBinaryTree(root, node):
if root:
if node.data < root.data:
if root.leftChild:
InsertElementBinaryTree(root.leftChild, node)
else:
root.leftChild = node
else:
if root.rightChild:
InsertElementBinaryTree(root.rightChild, node)
else:
root.rightChild = node
else:
return 0
# init a tree
def InitBinaryTree(dataSource, length):
root = BTNode(dataSource[0])
for x in xrange(1,length):
node = BTNode(dataSource[x])
InsertElementBinaryTree(root, node)
return root
print 'Done...'
# pre-order
def PreorderTraversalBinaryTree(root):
if root:
print '%d | ' % root.data,
PreorderTraversalBinaryTree(root.leftChild)
PreorderTraversalBinaryTree(root.rightChild)
# in-order
def InorderTraversalBinaryTree(root):
if root:
InorderTraversalBinaryTree(root.leftChild)
print '%d | ' % root.data,
InorderTraversalBinaryTree(root.rightChild)
# post-order
def PostorderTraversalBinaryTree(root):
if root:
PostorderTraversalBinaryTree(root.leftChild)
PostorderTraversalBinaryTree(root.rightChild)
print '%d | ' % root.data,
# layer-order
def TraversalByLayer(root, length):
stack = []
stack.append(root)
for x in xrange(length):
node = stack[x]
print '%d | ' % node.data,
if node.leftChild:
stack.append(node.leftChild)
if node.rightChild:
stack.append(node.rightChild)
if __name__ == '__main__':
dataSource = [3, 4, 2, 6, 7, 1, 8, 5]
length = len(dataSource)
BTree = InitBinaryTree(dataSource, length)
print '****NLR:'
PreorderTraversalBinaryTree(BTree)
print '\n****LNR'
InorderTraversalBinaryTree(BTree)
print '\n****LRN'
PostorderTraversalBinaryTree(BTree)
print '\n****LayerTraversal'
TraversalByLayer(BTree, length)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python編程把二叉樹(shù)打印成多行代碼
- Python 二叉樹(shù)的層序建立與三種遍歷實(shí)現(xiàn)詳解
- python3實(shí)現(xiàn)二叉樹(shù)的遍歷與遞歸算法解析(小結(jié))
- python使用遞歸的方式建立二叉樹(shù)
- Python二叉樹(shù)的鏡像轉(zhuǎn)換實(shí)現(xiàn)方法示例
- python 平衡二叉樹(shù)實(shí)現(xiàn)代碼示例
- Python實(shí)現(xiàn)重建二叉樹(shù)的三種方法詳解
- Python二叉樹(shù)定義與遍歷方法實(shí)例分析
- Python簡(jiǎn)單定義與使用二叉樹(shù)示例
- 基于python二叉樹(shù)的構(gòu)造和打印例子
相關(guān)文章
pycharm設(shè)置鼠標(biāo)懸停查看方法設(shè)置
在本文里小編給大家分享的是關(guān)于pycharm鼠標(biāo)懸停查看方法說(shuō)明怎么設(shè)置的相關(guān)知識(shí)點(diǎn),需要的朋友們參考學(xué)習(xí)下。2019-07-07
Python matplotlib的使用并自定義colormap的方法
今天小編就為大家分享一篇Python matplotlib的使用并自定義colormap的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
python matplotlib imshow熱圖坐標(biāo)替換/映射實(shí)例
這篇文章主要介紹了python matplotlib imshow熱圖坐標(biāo)替換/映射實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
pycharm激活碼2020最新分享適用pycharm2020最新版親測(cè)可用
這篇文章主要介紹了pycharm激活碼2020最新分享適用pycharm2020最新版親測(cè)可用,同時(shí)也支持Intellij IDEA激活碼,PHPStorm激活碼大家可以放心使用需要的朋友可以參考下2020-11-11
Python實(shí)現(xiàn)的樸素貝葉斯算法經(jīng)典示例【測(cè)試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)的樸素貝葉斯算法,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)與使用樸素貝葉斯算法的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法詳解
python中數(shù)據(jù)處理是比較方便的,經(jīng)常用的就是讀寫(xiě)文件,提取數(shù)據(jù)等,本文主要介紹其中的一些用法,這篇文章主要給大家介紹了關(guān)于Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法的相關(guān)資料,需要的朋友可以參考下2024-03-03
在Python中使用全局日志時(shí)需要注意的問(wèn)題
這篇文章主要介紹了在Python中使用全局日志時(shí)需要注意的問(wèn)題, 作者由uliweb使用時(shí)遇到的問(wèn)題分析全局日志出現(xiàn)錯(cuò)誤時(shí)的解決方法,需要的朋友可以參考下2015-05-05
Windows下將Python文件打包成.EXE可執(zhí)行文件的方法
這篇文章主要介紹了Windows下將Python文件打包成.EXE可執(zhí)行文件的方法,需要的朋友可以參考下2018-08-08

