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

Python實(shí)現(xiàn)重建二叉樹的三種方法詳解

 更新時(shí)間:2018年06月23日 01:58:51   作者:fly_hawk  
這篇文章主要介紹了Python實(shí)現(xiàn)重建二叉樹的三種方法,結(jié)合實(shí)例形式分析了Python重建二叉樹的實(shí)現(xiàn)方法、操作技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)重建二叉樹的三種方法。分享給大家供大家參考,具體如下:

學(xué)習(xí)算法中,探尋重建二叉樹的方法:

  • 用input 前序遍歷順序輸入字符重建
  • 前序遍歷順序字符串遞歸解析重建
  • 前序遍歷順序字符串堆棧解析重建

如果懶得去看后面的內(nèi)容,可以直接點(diǎn)擊此處本站下載完整實(shí)例代碼。

思路

學(xué)習(xí)算法中,python 算法方面的資料相對(duì)較少,二叉樹解析重建更少,只能摸著石頭過河。

通過不同方式遍歷二叉樹,可以得出不同節(jié)點(diǎn)的排序。那么,在已知節(jié)點(diǎn)排序的前提下,通過某種遍歷方式,可以將排序進(jìn)行解析,從而構(gòu)建二叉樹。

應(yīng)用上來將,可以用來解析多項(xiàng)式、可以解析網(wǎng)頁、xml等。

本文采用前序遍歷方式的排列,對(duì)已知字符串進(jìn)行解析,并生成二叉樹。新手,以解題為目的,暫未優(yōu)化,未能體現(xiàn) Python 簡(jiǎn)潔、優(yōu)美。請(qǐng)大牛不吝指正。

首先采用 input 輸入

節(jié)點(diǎn)類

class treeNode:
 def __init__(self, rootObj = None, leftChild = None, rightChild = None):
  self.key = rootObj
  self.leftChild = None
  self.rightChild = None

input 方法重建二叉樹

 def createTreeByInput(self, root):
  tmpKey = raw_input("please input a key, input '#' for Null")
  if tmpKey == '#':
   root = None
  else:
   root = treeNode(rootObj=tmpKey)
   root.leftChild = self.createTreeByInput(root.leftChild)
   root.rightChild = self.createTreeByInput(root.rightChild)
  return root

以下兩種方法,使用預(yù)先編好的字符串,通過 list 方法轉(zhuǎn)換為 list 傳入進(jìn)行解析

myTree 為實(shí)例化一個(gè)空樹

調(diào)用遞歸方法重建二叉樹

 treeElementList = '124#8##5##369###7##'
 myTree = myTree.createTreeByListWithRecursion(list(treeElementList))
 printBTree(myTree, 0)

遞歸方法重建二叉樹

 def createTreeByListWithRecursion(self, preOrderList):
  """
  根據(jù)前序列表重建二叉樹
  :param preOrder: 輸入前序列表
  :return: 二叉樹
  """
  preOrder = preOrderList
  if preOrder is None or len(preOrder) <= 0:
   return None
  currentItem = preOrder.pop(0) # 模擬C語言指針移動(dòng)
  if currentItem is '#':
   root = None
  else:
   root = treeNode(currentItem)
   root.leftChild = self.createTreeByListWithRecursion(preOrder)
   root.rightChild = self.createTreeByListWithRecursion(preOrder)
  return root

調(diào)用堆棧方法重建二叉樹

 treeElementList = '124#8##5##369###7##'
 myTree = myTree.createTreeByListWithStack(list(treeElementList))
 printBTree(myTree, 0)

使用堆棧重建二叉樹

def createTreeByListWithStack(self, preOrderList):
 """
 根據(jù)前序列表重建二叉樹
 :param preOrder: 輸入前序列表
 :return: 二叉樹
 """
 preOrder = preOrderList
 pStack = SStack()
 # check
 if preOrder is None or len(preOrder) <= 0 or preOrder[0] is '#':
  return None
 # get the root
 tmpItem = preOrder.pop(0)
 root = treeNode(tmpItem)
 # push root
 pStack.push(root)
 currentRoot = root
 while preOrder:
  # get another item
  tmpItem = preOrder.pop(0)
  # has child
  if tmpItem is not '#':
   # does not has left child, insert one
   if currentRoot.leftChild is None:
    currentRoot = self.insertLeft(currentRoot, tmpItem)
    pStack.push(currentRoot.leftChild)
    currentRoot = currentRoot.leftChild
   # otherwise insert right child
   elif currentRoot.rightChild is None:
    currentRoot = self.insertRight(currentRoot, tmpItem)
    pStack.push(currentRoot.rightChild)
    currentRoot = currentRoot.rightChild
  # one child is null
  else:
   # if has no left child
   if currentRoot.leftChild is None:
    currentRoot.leftChild = None
    # get another item fill right child
    tmpItem = preOrder.pop(0)
    # has right child
    if tmpItem is not '#':
     currentRoot = self.insertRight(currentRoot, tmpItem)
     pStack.push(currentRoot.rightChild)
     currentRoot = currentRoot.rightChild
    # right child is null
    else:
     currentRoot.rightChild = None
     # pop itself
     parent = pStack.pop()
     # pos parent
     if not pStack.is_empty():
      parent = pStack.pop()
     # parent become current root
     currentRoot = parent
     # return from right child, so the parent has right child, go to parent's parent
     if currentRoot.rightChild is not None:
      if not pStack.is_empty():
       parent = pStack.pop()
       currentRoot = parent
   # there is a leftchild ,fill right child with null and return to parent
   else:
    currentRoot.rightChild = None
    # pop itself
    parent = pStack.pop()
    if not pStack.is_empty():
     parent = pStack.pop()
    currentRoot = parent
 return root

顯示二叉樹

def printBTree(bt, depth):
 '''''
 遞歸打印這棵二叉樹,#號(hào)表示該節(jié)點(diǎn)為NULL
 '''
 ch = bt.key if bt else '#'
 if depth > 0:
  print '%s%s%s' % ((depth - 1) * ' ', '--', ch)
 else:
  print ch
 if not bt:
  return
 printBTree(bt.leftChild, depth + 1)
 printBTree(bt.rightChild, depth + 1)

打印二叉樹的代碼,采用某仁兄代碼,在此感謝。

input 輸入及顯示二叉樹結(jié)果

 

解析字符串的結(jié)果

 

完整代碼參見:https://github.com/flyhawksz/study-algorithms/blob/master/Class_BinaryTree2.py

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

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

相關(guān)文章

  • 詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式

    詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式

    這篇文章主要介紹了詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python socket模塊創(chuàng)建和使用套接字示例詳解

    python socket模塊創(chuàng)建和使用套接字示例詳解

    這篇文章主要為大家介紹了python socket模塊來創(chuàng)建和使用套接字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Python如何篩選序列中的元素的方法實(shí)現(xiàn)

    Python如何篩選序列中的元素的方法實(shí)現(xiàn)

    這篇文章主要介紹了Python如何篩選序列中的元素的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python3.5 Pandas模塊缺失值處理和層次索引實(shí)例詳解

    Python3.5 Pandas模塊缺失值處理和層次索引實(shí)例詳解

    這篇文章主要介紹了Python3.5 Pandas模塊缺失值處理和層次索引,結(jié)合實(shí)例形式詳細(xì)分析了Python3.5 Pandas模塊缺失值處理和層次索引的原理、處理方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-04-04
  • 超級(jí)好用的4個(gè)Python命令行可視化庫

    超級(jí)好用的4個(gè)Python命令行可視化庫

    通常大家都是在自己的電腦上跑程序,直接是可以可視化相應(yīng)的結(jié)果.如果是在服務(wù)器上的話,使用終端,是不太方便查看結(jié)果. 今天,小F就給大家介紹4個(gè)可以在命令行中使用的Python庫. 分別是Bashplotlib、tqdm、PrettyTable、Colorama,需要的朋友可以參考下
    2021-06-06
  • Python pip安裝第三方庫的攻略分享

    Python pip安裝第三方庫的攻略分享

    pip 就是 Python 標(biāo)準(zhǔn)庫(The Python Standard Library)中的一個(gè)包,只是這個(gè)包比較特殊,用它可以來管理 Python 標(biāo)準(zhǔn)庫(The Python Standard Library)中其他的包。本文為大家介紹了pip安裝第三方庫的方法,需要的可以參考一下
    2022-11-11
  • Python實(shí)現(xiàn)繪制凸包的示例代碼

    Python實(shí)現(xiàn)繪制凸包的示例代碼

    凸包(Convex Hull)是一個(gè)計(jì)算幾何(圖形學(xué))中的概念。這篇文章主要為大家詳細(xì)介紹了Python繪制凸包的示例代碼,感興趣的小伙伴可以了解一下
    2023-05-05
  • Python pandas 重命名索引和列名稱的實(shí)現(xiàn)

    Python pandas 重命名索引和列名稱的實(shí)現(xiàn)

    本文主要介紹了Python pandas 重命名索引和列名稱的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • selenium+python實(shí)現(xiàn)基本自動(dòng)化測(cè)試的示例代碼

    selenium+python實(shí)現(xiàn)基本自動(dòng)化測(cè)試的示例代碼

    這篇文章主要介紹了selenium+python實(shí)現(xiàn)基本自動(dòng)化測(cè)試的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 跟老齊學(xué)Python之畫圈還不簡(jiǎn)單嗎?

    跟老齊學(xué)Python之畫圈還不簡(jiǎn)單嗎?

    畫圈?換一個(gè)說法就是循環(huán)。循環(huán),是高級(jí)語言編程中重要的工作?,F(xiàn)實(shí)生活中,很多事情都是在循環(huán),日月更迭,斗轉(zhuǎn)星移,無不是循環(huán);王朝更迭,尋常百姓,也都是循環(huán)。
    2014-09-09

最新評(píng)論

玉溪市| 聂荣县| 南部县| 双鸭山市| 天长市| 张掖市| 麻城市| 宜君县| 怀仁县| 灵璧县| 古田县| 荥经县| 乐至县| 长沙市| 图木舒克市| 运城市| 临猗县| 贡觉县| 当雄县| 鄂托克旗| 杨浦区| 浦东新区| 定州市| 买车| 玉树县| 疏附县| 景洪市| 阳朔县| 宜都市| 南通市| 蒲城县| 上思县| 连江县| 大城县| 墨竹工卡县| 武威市| 池州市| 双流县| 徐汇区| 三亚市| 突泉县|