Python編程實(shí)現(xiàn)雙鏈表,棧,隊(duì)列及二叉樹的方法示例
本文實(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ì)有所幫助。
- python雙向鏈表實(shí)現(xiàn)實(shí)例代碼
- Python單向鏈表和雙向鏈表原理與用法實(shí)例詳解
- Python二叉搜索樹與雙向鏈表轉(zhuǎn)換實(shí)現(xiàn)方法
- Python二叉搜索樹與雙向鏈表轉(zhuǎn)換算法示例
- Python數(shù)據(jù)結(jié)構(gòu)之雙向鏈表的定義與使用方法示例
- Python雙向循環(huán)鏈表實(shí)現(xiàn)方法分析
- python雙向鏈表原理與實(shí)現(xiàn)方法詳解
- Python雙鏈表原理與實(shí)現(xiàn)方法詳解
- Python數(shù)據(jù)結(jié)構(gòu)之雙向鏈表詳解
- Python代碼實(shí)現(xiàn)雙鏈表
相關(guān)文章
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é)
本文將介紹如何在 web 框架 Flask 中使用可視化工具 pyecharts, 看完本教程你將掌握幾種動態(tài)展示可視化數(shù)據(jù)的方法。感興趣的朋友跟隨小編一起看看吧2019-09-09Python數(shù)學(xué)建模PuLP庫線性規(guī)劃入門示例詳解
這篇文章主要為大家介紹了Python數(shù)學(xué)建模PuLP庫線性規(guī)劃入門示例詳解,想學(xué)習(xí)關(guān)于Python建模的同學(xué)可以學(xué)習(xí)參考下,希望能夠有所幫助2021-10-10Python數(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-03Python3.5文件讀與寫操作經(jīng)典實(shí)例詳解
這篇文章主要介紹了Python3.5文件讀與寫操作,結(jié)合實(shí)例形式詳細(xì)分析了Python針對文件的讀寫操作常用技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05python fabric實(shí)現(xiàn)遠(yuǎn)程部署
這篇文章主要為大家詳細(xì)介紹了 python fabric實(shí)現(xiàn)遠(yuǎn)程部署,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運(yùn)算
這篇文章主要介紹了python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運(yùn)算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07