最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python二叉樹(shù)定義與遍歷方法實(shí)例分析

 更新時(shí)間:2018年05月25日 10:48:51   作者:止語(yǔ)---  
這篇文章主要介紹了Python二叉樹(shù)定義與遍歷方法,結(jié)合實(shí)例形式分析了二叉樹(shù)的概念、原理及Python定義、遍歷二叉樹(shù)相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python二叉樹(shù)定義與遍歷方法。分享給大家供大家參考,具體如下:

二叉樹(shù)基本概述:

二叉樹(shù)是有限個(gè)元素的幾個(gè),如果為空則為空二叉樹(shù),或者有一個(gè)結(jié)點(diǎn)稱之為根節(jié)點(diǎn),分列根節(jié)點(diǎn)兩側(cè)的為二叉樹(shù)的左右子節(jié)點(diǎn),二叉樹(shù)有如下的性質(zhì):

1. 二叉樹(shù)的每個(gè)結(jié)點(diǎn)不存在度大于2的結(jié)點(diǎn)
2. 二叉樹(shù)的第i層至多有2^{i-1}個(gè)結(jié)點(diǎn)
3. 深度為k的二叉樹(shù)至多有2^k - 1個(gè)結(jié)點(diǎn)
4. 二叉樹(shù)中,度為0的結(jié)點(diǎn)數(shù)N0比度為2的結(jié)點(diǎn)數(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]

這里對(duì)于

if __name__=='__main__':
“Make a script both importable and executable”

意思就是說(shuō)讓你寫(xiě)的腳本模塊既可以導(dǎo)入到別的模塊中用,另外該模塊自己也可執(zhí)行。

這里通過(guò)一個(gè)示例進(jìn)行解釋:

#test.py
def func():
 print "we are in %s"%__name__
if __name__ == '__main__':
 func()

輸出結(jié)果:

we are in __main__

說(shuō)明if語(yǔ)句中的內(nèi)容被執(zhí)行了,調(diào)用了 func()函數(shù),現(xiàn)在在另一個(gè)模塊中調(diào)用func函數(shù)

#testtest
from test import func
func()

輸出結(jié)果:

we are in moudle

也就是說(shuō) if 條件中的內(nèi)容沒(méi)有執(zhí)行。

總結(jié):

如果直接執(zhí)行某個(gè)*.py文件,該文件中 if __name__ == '__main__'是True,相當(dāng)于調(diào)式本模塊的代碼;如果是從另一個(gè)模塊(testtest.py)通過(guò)import導(dǎo)入該文件的時(shí)候,這時(shí)__name__就是這個(gè)模塊的名字(test)而不是__main__,總之在調(diào)式代碼的時(shí)候加上 if __name__ == '__main__'中加入調(diào)試代碼,可以讓步模塊調(diào)用的時(shí)候不執(zhí)行調(diào)式代碼,如果想排查本模塊代碼的問(wèn)題時(shí),直接進(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入門(mén)與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

玉林市| 沐川县| 耒阳市| 太湖县| 耒阳市| 雷波县| 大埔区| 延吉市| 德格县| 定陶县| 兴隆县| 余江县| 庆云县| 龙山县| 乐都县| 青田县| 阳东县| 琼海市| 玛沁县| 平舆县| 治县。| 双流县| 新宁县| 云梦县| 苍溪县| 聊城市| 武隆县| 周至县| 安陆市| 中阳县| 江川县| 汉沽区| 嘉义市| 鹰潭市| 南川市| 阳原县| 丹棱县| 富顺县| 确山县| 布拖县| 永顺县|