Python定義二叉樹及4種遍歷方法實(shí)例詳解
本文實(shí)例講述了Python定義二叉樹及4種遍歷方法。分享給大家供大家參考,具體如下:
Python & BinaryTree
1. BinaryTree (二叉樹)
二叉樹是有限個元素的集合,該集合或者為空、或者有一個稱為根節(jié)點(diǎn)(root)的元素及兩個互不相交的、分別被稱為左子樹和右子樹的二叉樹組成。
- 二叉樹的每個結(jié)點(diǎn)至多只有二棵子樹(不存在度大于2的結(jié)點(diǎn)),二叉樹的子樹有左右之分,次序不能顛倒。
- 二叉樹的第i層至多有2^{i-1}個結(jié)點(diǎn)
- 深度為k的二叉樹至多有2^k-1個結(jié)點(diǎn);
- 對任何一棵二叉樹T,如果其終端結(jié)點(diǎn)數(shù)為N0,度為2的結(jié)點(diǎn)數(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ìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
安裝python3.7編譯器后如何正確安裝opnecv的方法詳解
這篇文章主要介紹了安裝python3.7編譯器后如何正確安裝opnecv,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Python如何自動獲取目標(biāo)網(wǎng)站最新通知
這篇文章主要介紹了Python如何自動獲取目標(biāo)網(wǎng)站最新通知,本文給大家分享實(shí)現(xiàn)思路及示例代碼,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Python簡單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能示例
這篇文章主要介紹了Python簡單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能,涉及Python針對字符串與列表的遍歷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04Python實(shí)現(xiàn)實(shí)時跟隨微信窗口移動的GUI界面
Python寫一些簡單的GUI界面也是非常簡單的,并且Python有著豐富的庫,這些庫可以很方便我們?nèi)ゲ僮鱓indows系統(tǒng)。本文就來用Python編寫一個實(shí)時跟隨微信窗口移動的GUI界面吧2023-04-04pytorch+lstm實(shí)現(xiàn)的pos示例
今天小編就為大家分享一篇pytorch+lstm實(shí)現(xiàn)的pos示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01百分百成功的全網(wǎng)最簡約sklearn環(huán)境配置教程
這篇文章主要介紹了百分百成功的全網(wǎng)最簡約sklearn環(huán)境配置教程,圖文全流程講解包簡單易懂,百分百成功,需要的朋友可以參考下2023-03-03用map函數(shù)來完成Python并行任務(wù)的簡單示例
這篇文章主要介紹了用map函數(shù)來完成Python并行任務(wù)的簡單示例,多線程和多進(jìn)程編程的問題一直都是Python中的熱點(diǎn)和難點(diǎn),需要的朋友可以參考下2015-04-04