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)文章
安裝python3.7編譯器后如何正確安裝opnecv的方法詳解
這篇文章主要介紹了安裝python3.7編譯器后如何正確安裝opnecv,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Python如何自動(dòng)獲取目標(biāo)網(wǎng)站最新通知
這篇文章主要介紹了Python如何自動(dòng)獲取目標(biāo)網(wǎng)站最新通知,本文給大家分享實(shí)現(xiàn)思路及示例代碼,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Python簡(jiǎn)單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能示例
這篇文章主要介紹了Python簡(jiǎn)單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能,涉及Python針對(duì)字符串與列表的遍歷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04pycharm中創(chuàng)建sql文件及模板的過(guò)程
很多小伙伴剛開(kāi)始使用pycharm時(shí)發(fā)現(xiàn)以前的老員工在使用pycharm創(chuàng)建sql文件時(shí)會(huì)自帶文件頭模板,例如時(shí)間、作者、版本、郵件等信息,這是怎么做到的呢,一起來(lái)看一下吧2022-07-07Python實(shí)現(xiàn)實(shí)時(shí)跟隨微信窗口移動(dòng)的GUI界面
Python寫(xiě)一些簡(jiǎn)單的GUI界面也是非常簡(jiǎn)單的,并且Python有著豐富的庫(kù),這些庫(kù)可以很方便我們?nèi)ゲ僮鱓indows系統(tǒng)。本文就來(lái)用Python編寫(xiě)一個(gè)實(shí)時(shí)跟隨微信窗口移動(dòng)的GUI界面吧2023-04-04pytorch+lstm實(shí)現(xiàn)的pos示例
今天小編就為大家分享一篇pytorch+lstm實(shí)現(xiàn)的pos示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01百分百成功的全網(wǎng)最簡(jiǎn)約sklearn環(huán)境配置教程
這篇文章主要介紹了百分百成功的全網(wǎng)最簡(jiǎn)約sklearn環(huán)境配置教程,圖文全流程講解包簡(jiǎn)單易懂,百分百成功,需要的朋友可以參考下2023-03-03用map函數(shù)來(lái)完成Python并行任務(wù)的簡(jiǎn)單示例
這篇文章主要介紹了用map函數(shù)來(lái)完成Python并行任務(wù)的簡(jiǎn)單示例,多線程和多進(jìn)程編程的問(wèn)題一直都是Python中的熱點(diǎn)和難點(diǎn),需要的朋友可以參考下2015-04-04