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

生成二叉樹
# 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

二叉樹的思想重在“遞歸”, 并不是非要用遞歸處理,而是去理解二叉樹遞歸的思想
完整代碼段
# -*- 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入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python matplotlib的使用并自定義colormap的方法
今天小編就為大家分享一篇Python matplotlib的使用并自定義colormap的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python matplotlib imshow熱圖坐標替換/映射實例
這篇文章主要介紹了python matplotlib imshow熱圖坐標替換/映射實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
pycharm激活碼2020最新分享適用pycharm2020最新版親測可用
這篇文章主要介紹了pycharm激活碼2020最新分享適用pycharm2020最新版親測可用,同時也支持Intellij IDEA激活碼,PHPStorm激活碼大家可以放心使用需要的朋友可以參考下2020-11-11
Python實現(xiàn)的樸素貝葉斯算法經(jīng)典示例【測試可用】
這篇文章主要介紹了Python實現(xiàn)的樸素貝葉斯算法,結(jié)合實例形式詳細分析了Python實現(xiàn)與使用樸素貝葉斯算法的具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-06-06
Python進階學(xué)習(xí)之pandas中read_csv()用法詳解
python中數(shù)據(jù)處理是比較方便的,經(jīng)常用的就是讀寫文件,提取數(shù)據(jù)等,本文主要介紹其中的一些用法,這篇文章主要給大家介紹了關(guān)于Python進階學(xué)習(xí)之pandas中read_csv()用法的相關(guān)資料,需要的朋友可以參考下2024-03-03
Windows下將Python文件打包成.EXE可執(zhí)行文件的方法
這篇文章主要介紹了Windows下將Python文件打包成.EXE可執(zhí)行文件的方法,需要的朋友可以參考下2018-08-08

