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

Python編程實(shí)現(xiàn)雙鏈表,棧,隊(duì)列及二叉樹的方法示例

 更新時(shí)間:2017年11月01日 12:00:45   作者:王輝_Python  
這篇文章主要介紹了Python編程實(shí)現(xiàn)雙鏈表,棧,隊(duì)列及二叉樹的方法,結(jié)合具體實(shí)例形式分析了Python簡單實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙鏈表,棧,隊(duì)列及二叉樹相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python編程實(shí)現(xiàn)雙鏈表,棧,隊(duì)列及二叉樹的方法。分享給大家供大家參考,具體如下:

1.雙鏈表

class Node(object):
  def __init__(self, value=None):
    self._prev = None
    self.data = value
    self._next = None
  def __str__(self):
    return "Node(%s)"%self.data
class DoubleLinkedList(object):
  def __init__(self):
    self._head = Node()
  def insert(self, value):
    element = Node(value)
    element._next = self._head
    self._head._prev = element
    self._head = element
  def search(self, value):
    if not self._head._next:
      raise ValueError("the linked list is empty")
    temp = self._head
    while temp.data != value:
      temp = temp._next
    return temp
  def delete(self, value):
    element = self.search(value)
    if not element:
      raise ValueError('delete error: the value not found')
    element._prev._next = element._next
    element._next._prev = element._prev
    return element.data
  def __str__(self):
    values = []
    temp = self._head
    while temp and temp.data:
      values.append(temp.data)
      temp = temp._next
    return "DoubleLinkedList(%s)"%values

2. 棧

class Stack(object):
  def __init__(self):
    self._top = 0
    self._stack = []
  def put(self, data):
    self._stack.insert(self._top, data)
    self._top += 1
  def pop(self):
    if self.isEmpty():
      raise ValueError('stack 為空')
    self._top -= 1
    data = self._stack[self._top]
    return data
  def isEmpty(self):
    if self._top == 0:
      return True
    else:
      return False
  def __str__(self):
    return "Stack(%s)"%self._stack

3.隊(duì)列

class Queue(object):
  def __init__(self, max_size=float('inf')):
    self._max_size = max_size
    self._top = 0
    self._tail = 0
    self._queue = []
  def put(self, value):
    if self.isFull():
      raise ValueError("the queue is full")
    self._queue.insert(self._tail, value)
    self._tail += 1
  def pop(self):
    if self.isEmpty():
      raise ValueError("the queue is empty")
    data = self._queue.pop(self._top)
    self._top += 1
    return data
  def isEmpty(self):
    if self._top == self._tail:
      return True
    else:
      return False
  def isFull(self):
    if self._tail == self._max_size:
      return True
    else:
      return False
  def __str__(self):
    return "Queue(%s)"%self._queue

4. 二叉樹(定義與遍歷)

class Node:
  def __init__(self,item):
    self.item = item
    self.child1 = None
    self.child2 = None
class Tree:
  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.child1 is None:
          pop_node.child1 = node
          return
        elif pop_node.child2 is None:
          pop_node.child2 = node
          return
        else:
          q.append(pop_node.child1)
          q.append(pop_node.child2)
  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.child1 is not None:
        q.append(pop_node.child1)
        res.append(pop_node.child1.item)
      if pop_node.child2 is not None:
        q.append(pop_node.child2)
        res.append(pop_node.child2.item)
    return res
  def preorder(self,root): # 先序遍歷
    if root is None:
      return []
    result = [root.item]
    left_item = self.preorder(root.child1)
    right_item = self.preorder(root.child2)
    return result + left_item + right_item
  def inorder(self,root): # 中序序遍歷
    if root is None:
      return []
    result = [root.item]
    left_item = self.inorder(root.child1)
    right_item = self.inorder(root.child2)
    return left_item + result + right_item
  def postorder(self,root): # 后序遍歷
    if root is None:
      return []
    result = [root.item]
    left_item = self.postorder(root.child1)
    right_item = self.postorder(root.child2)
    return left_item + right_item + result
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]

更多關(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)典教程

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

相關(guān)文章

  • PyCharm安裝第三方庫如Requests的圖文教程

    PyCharm安裝第三方庫如Requests的圖文教程

    今天小編就為大家分享一篇PyCharm安裝第三方庫如Requests的圖文教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python實(shí)現(xiàn)維吉尼亞算法

    python實(shí)現(xiàn)維吉尼亞算法

    這篇文章主要為大家詳細(xì)介紹了python編程實(shí)現(xiàn)維吉尼亞算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Flask框架響應(yīng)、調(diào)度方法和藍(lán)圖操作實(shí)例分析

    Flask框架響應(yīng)、調(diào)度方法和藍(lán)圖操作實(shí)例分析

    這篇文章主要介紹了Flask框架響應(yīng)、調(diào)度方法和藍(lán)圖操作,結(jié)合實(shí)例形式分析了Flask框架中響應(yīng)、調(diào)度方法和藍(lán)圖相關(guān)功能、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • 利用 Flask 動態(tài)展示 Pyecharts 圖表數(shù)據(jù)方法小結(jié)

    利用 Flask 動態(tài)展示 Pyecharts 圖表數(shù)據(jù)方法小結(jié)

    本文將介紹如何在 web 框架 Flask 中使用可視化工具 pyecharts, 看完本教程你將掌握幾種動態(tài)展示可視化數(shù)據(jù)的方法。感興趣的朋友跟隨小編一起看看吧
    2019-09-09
  • Python數(shù)學(xué)建模PuLP庫線性規(guī)劃入門示例詳解

    Python數(shù)學(xué)建模PuLP庫線性規(guī)劃入門示例詳解

    這篇文章主要為大家介紹了Python數(shù)學(xué)建模PuLP庫線性規(guī)劃入門示例詳解,想學(xué)習(xí)關(guān)于Python建模的同學(xué)可以學(xué)習(xí)參考下,希望能夠有所幫助
    2021-10-10
  • Python 3 中使用 Memcached的示例詳解

    Python 3 中使用 Memcached的示例詳解

    pymemcache是另一個(gè)流行的、功能豐富的Python Memcached客戶端庫,比python-memcached提供了復(fù)雜的操作和性能,在 Python3 中,使用 Memcached,高性能的分布式內(nèi)存對象緩存系統(tǒng),可以通過多個(gè)第三方庫來實(shí)現(xiàn),本文介紹Python 使用 Memcached相關(guān)知識,感興趣的朋友一起看看吧
    2024-02-02
  • Python數(shù)據(jù)處理利器Slice函數(shù)用法詳解

    Python數(shù)據(jù)處理利器Slice函數(shù)用法詳解

    這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)處理利器Slice函數(shù)用法的相關(guān)資料,slice函數(shù)是Python中的一個(gè)內(nèi)置函數(shù),用于對序列進(jìn)行切片操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • Python3.5文件讀與寫操作經(jīng)典實(shí)例詳解

    Python3.5文件讀與寫操作經(jīng)典實(shí)例詳解

    這篇文章主要介紹了Python3.5文件讀與寫操作,結(jié)合實(shí)例形式詳細(xì)分析了Python針對文件的讀寫操作常用技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-05-05
  • python fabric實(shí)現(xiàn)遠(yuǎn)程部署

    python fabric實(shí)現(xiàn)遠(yuǎn)程部署

    這篇文章主要為大家詳細(xì)介紹了 python fabric實(shí)現(xiàn)遠(yuǎn)程部署,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運(yùn)算

    python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運(yùn)算

    這篇文章主要介紹了python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運(yùn)算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評論