亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python實(shí)現(xiàn)二叉樹前序、中序、后序及層次遍歷示例代碼

 更新時間:2019年05月18日 11:19:54   作者:yongxinz  
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)二叉樹前序、中序、后序及層次遍歷的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

樹是數(shù)據(jù)結(jié)構(gòu)中非常重要的一種,主要的用途是用來提高查找效率,對于要重復(fù)查找的情況效果更佳,如二叉排序樹、FP-樹。另外可以用來提高編碼效率,如哈弗曼樹。

用 Python 實(shí)現(xiàn)樹的構(gòu)造和幾種遍歷算法。實(shí)現(xiàn)功能如下:

  • 樹的構(gòu)造
  • 遞歸實(shí)現(xiàn)先序遍歷、中序遍歷、后序遍歷
  • 堆棧實(shí)現(xiàn)先序遍歷、中序遍歷、后序遍歷
  • 隊(duì)列實(shí)現(xiàn)層次遍歷
# -*- coding=utf-8 -*-


class Node(object):
 """節(jié)點(diǎn)類"""

 def __init__(self, element=-1, l_child=None, r_child=None):
  self.element = element
  self.l_child = l_child
  self.r_child = r_child


class Tree(object):
 """樹類"""

 def __init__(self):
  self.root = Node()
  self.queue = []

 def add_node(self, element):
  """為樹添加節(jié)點(diǎn)"""

  node = Node(element)
  # 如果樹是空的,則對根節(jié)點(diǎn)賦值
  if self.root.element == -1:
   self.root = node
   self.queue.append(self.root)
  else:
   tree_node = self.queue[0]
   # 此結(jié)點(diǎn)沒有左子樹,則創(chuàng)建左子樹節(jié)點(diǎn)
   if tree_node.l_child is None:
    tree_node.l_child = node
    self.queue.append(tree_node.l_child)
   else:
    tree_node.r_child = node
    self.queue.append(tree_node.r_child)
    # 如果該結(jié)點(diǎn)存在右子樹,將此節(jié)點(diǎn)丟棄
    self.queue.pop(0)

 def front_recursion(self, root):
  """利用遞歸實(shí)現(xiàn)樹的前序遍歷"""

  if root is None:
   return

  print root.element,
  self.front_recursion(root.l_child)
  self.front_recursion(root.r_child)

 def middle_recursion(self, root):
  """利用遞歸實(shí)現(xiàn)樹的中序遍歷"""

  if root is None:
   return

  self.middle_recursion(root.l_child)
  print root.element,
  self.middle_recursion(root.r_child)

 def back_recursion(self, root):
  """利用遞歸實(shí)現(xiàn)樹的后序遍歷"""

  if root is None:
   return

  self.back_recursion(root.l_child)
  self.back_recursion(root.r_child)
  print root.element,

 @staticmethod
 def front_stack(root):
  """利用堆棧實(shí)現(xiàn)樹的前序遍歷"""

  if root is None:
   return

  stack = []
  node = root
  while node or stack:
   # 從根節(jié)點(diǎn)開始,一直找它的左子樹
   while node:
    print node.element,
    stack.append(node)
    node = node.l_child
   # while結(jié)束表示當(dāng)前節(jié)點(diǎn)node為空,即前一個節(jié)點(diǎn)沒有左子樹了
   node = stack.pop()
   # 開始查看它的右子樹
   node = node.r_child

 @staticmethod
 def middle_stack(root):
  """利用堆棧實(shí)現(xiàn)樹的中序遍歷"""

  if root is None:
   return

  stack = []
  node = root
  while node or stack:
   # 從根節(jié)點(diǎn)開始,一直找它的左子樹
   while node:
    stack.append(node)
    node = node.l_child
   # while結(jié)束表示當(dāng)前節(jié)點(diǎn)node為空,即前一個節(jié)點(diǎn)沒有左子樹了
   node = stack.pop()
   print node.element,
   # 開始查看它的右子樹
   node = node.r_child

 @staticmethod
 def back_stack(root):
  """利用堆棧實(shí)現(xiàn)樹的后序遍歷"""

  if root is None:
   return

  stack1 = []
  stack2 = []
  node = root
  stack1.append(node)
  # 這個while循環(huán)的功能是找出后序遍歷的逆序,存在stack2里面
  while stack1:
   node = stack1.pop()
   if node.l_child:
    stack1.append(node.l_child)
   if node.r_child:
    stack1.append(node.r_child)
   stack2.append(node)
  # 將stack2中的元素出棧,即為后序遍歷次序
  while stack2:
   print stack2.pop().element,

 @staticmethod
 def level_queue(root):
  """利用隊(duì)列實(shí)現(xiàn)樹的層次遍歷"""

  if root is None:
   return

  queue = []
  node = root
  queue.append(node)
  while queue:
   node = queue.pop(0)
   print node.element,
   if node.l_child is not None:
    queue.append(node.l_child)
   if node.r_child is not None:
    queue.append(node.r_child)


if __name__ == '__main__':
 """主函數(shù)"""

 # 生成十個數(shù)據(jù)作為樹節(jié)點(diǎn)
 elements = range(10)
 tree = Tree()
 for elem in elements:
  tree.add_node(elem)

 print '隊(duì)列實(shí)現(xiàn)層次遍歷:'
 tree.level_queue(tree.root)

 print '\n\n遞歸實(shí)現(xiàn)前序遍歷:'
 tree.front_recursion(tree.root)
 print '\n遞歸實(shí)現(xiàn)中序遍歷:'
 tree.middle_recursion(tree.root)
 print '\n遞歸實(shí)現(xiàn)后序遍歷:'
 tree.back_recursion(tree.root)

 print '\n\n堆棧實(shí)現(xiàn)前序遍歷:'
 tree.front_stack(tree.root)
 print '\n堆棧實(shí)現(xiàn)中序遍歷:'
 tree.middle_stack(tree.root)
 print '\n堆棧實(shí)現(xiàn)后序遍歷:'
 tree.back_stack(tree.root)

需要源碼的小伙伴可自行下載:代碼傳送門

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Django 限制訪問頻率的思路詳解

    Django 限制訪問頻率的思路詳解

    這篇文章主要介紹了Django 限制訪問頻率的思路詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • 詳解Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化

    詳解Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化

    這篇文章主要介紹了Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 簡單談?wù)凱ython中的閉包

    簡單談?wù)凱ython中的閉包

    一般來說閉包這個概念在很多語言中都有涉及,簡單說,閉包就是根據(jù)不同的配置信息得到不同的結(jié)果,下面我們來專門講下在Python中的閉包
    2016-11-11
  • TensorFlow2.1.0最新版本安裝詳細(xì)教程

    TensorFlow2.1.0最新版本安裝詳細(xì)教程

    TensorFlow是一款優(yōu)秀的深度學(xué)習(xí)框架,支持多種常見的操作系統(tǒng),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,這篇文章主要介紹了TensorFlow2.1.0最新版本安裝詳細(xì)教程,需要的朋友可以參考下
    2020-04-04
  • Python requests模塊實(shí)例用法

    Python requests模塊實(shí)例用法

    在本篇文章中小編給大家分享了關(guān)于Python requests模塊實(shí)例用法,有需要的朋友們學(xué)習(xí)參考下。
    2019-02-02
  • Python離線安裝第三方庫詳細(xì)操作流程

    Python離線安裝第三方庫詳細(xì)操作流程

    在使用Python開發(fā)過程中,我們經(jīng)常需要使用各種第三方庫來擴(kuò)展Python的功能,這篇文章主要給大家介紹了關(guān)于Python離線安裝第三方庫的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法

    詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方

    這篇文章主要介紹了詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • appium測試之APP元素定位及基本工具介紹

    appium測試之APP元素定位及基本工具介紹

    看了我文章了相信都了解了web端的元素定位了,沒看的,既然進(jìn)來了那么肯定多多少少知道些,本文主要來介紹APP的元素定位有哪些定位方式,我們又怎么去連接APP,然后通過工具去獲取元素
    2021-09-09
  • Python基于whois模塊簡單識別網(wǎng)站域名及所有者的方法

    Python基于whois模塊簡單識別網(wǎng)站域名及所有者的方法

    這篇文章主要介紹了Python基于whois模塊簡單識別網(wǎng)站域名及所有者的方法,簡單分析了Python whois模塊的安裝及使用相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • Python 文件處理注意事項(xiàng)總結(jié)

    Python 文件處理注意事項(xiàng)總結(jié)

    這篇文章主要介紹了Python 文件處理注意事項(xiàng)總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論