Python數(shù)據(jù)結(jié)構(gòu)之棧、隊列及二叉樹定義與用法淺析
本文實(shí)例講述了Python數(shù)據(jù)結(jié)構(gòu)之棧、隊列及二叉樹定義與用法。分享給大家供大家參考,具體如下:
目前只實(shí)現(xiàn)了三種,棧、隊列和二叉樹,哪天得空繼續(xù)補(bǔ)吧~
1. 棧
#棧 class Stack: def __init__(self,size = 16): self.stack = [] self.size = size self.top = -1 def setSize(self, size): self.size = size def isEmpty(self): if self.top == -1: return True else: return False def isFull(self): if self.top +1 == self.size: return True else: return False def top(self): if self.isEmpty(): raise Exception("StackIsEmpty") else: return self.stack[self.top] def push(self,obj): if self.isFull(): raise Exception("StackOverFlow") else: self.stack.append(obj) self.top +=1 def pop(self): if self.isEmpty(): raise Exception("StackIsEmpty") else: self.top -= 1 return self.stack.pop() def show(self): print(self.stack) s = Stack(5) s.push(1) s.push(2) s.push(3) s.push(4) s.push(5) s.show() s.pop() s.show() s.push(6) s.show()
運(yùn)行結(jié)果:
2. 隊列
#隊列 class Queue: def __init__(self,size = 16): self.queue = [] self.size = size self.front = 0 self.rear = 0 def isEmpty(self): return self.rear == 0 def isFull(self): if (self.front - self.rear +1) == self.size: return True else: return False def first(self): if self.isEmpty(): raise Exception("QueueIsEmpty") else: return self.queue[self.front] def last(self): if self.isEmpty(): raise Exception("QueueIsEmpty") else: return self.queue[self.rear] def add(self,obj): if self.isFull(): raise Exception("QueueOverFlow") else: self.queue.append(obj) self.rear += 1 def delete(self): if self.isEmpty(): raise Exception("QueueIsEmpty") else: self.rear -=1 return self.queue.pop(0) def show(self): print(self.queue) q = Queue(3) q.add(1) q.add(2) q.show() q.delete() q.show()
運(yùn)行結(jié)果:
3. 二叉樹
#隊列 class Queue: def __init__(self,size = 16): self.queue = [] self.size = size self.front = 0 self.rear = 0 def isEmpty(self): return self.rear == 0 def isFull(self): if (self.front - self.rear +1) == self.size: return True else: return False def first(self): if self.isEmpty(): raise Exception("QueueIsEmpty") else: return self.queue[self.front] def last(self): if self.isEmpty(): raise Exception("QueueIsEmpty") else: return self.queue[self.rear] def add(self,obj): if self.isFull(): raise Exception("QueueOverFlow") else: self.queue.append(obj) self.rear += 1 def delete(self): if self.isEmpty(): raise Exception("QueueIsEmpty") else: self.rear -=1 return self.queue.pop(0) def show(self): print(self.queue) #二叉樹 class BinaryTreeNode: def __init__(self,data,left,right): self.left = left self.data = data self.right = right class BinaryTree: def __init__(self): self.root = None def makeTree(self,data,left,right): self.root = BinaryTreeNode(data,left,right) #left.root = right.root = None def isEmpty(self): if self.root is None: return True else: return False def preOrder(self,r): if r.root is not None: print(r.root.data) if r.root.left is not None: self.preOrder(r.root.left) if r.root.right is not None: self.preOrder(r.root.right) def inOrder(self,r): if r.root is not None: if r.root.left is not None: self.inOrder(r.root.left) print(r.root.data) if r.root.right is not None: self.inOrder(r.root.right) def postOrder(self,r): if r.root is not None: if r.root.left is not None: self.preOrder(r.root.left) if r.root.right is not None: self.preOrder(r.root.right) print(r.root.data) def levelOrder(self,a): q = Queue() r = a while r is not None: print(r.root.data) if r.root.left is not None: q.add(r.root.left) if r.root.right is not None: q.add(r.root.right) if q.isEmpty(): print("empty") r = None else: r = q.delete() r = BinaryTree() ra = BinaryTree() ra.makeTree(2,None,None) rb = BinaryTree() rb.makeTree(3,None,None) r.makeTree(1,ra,rb) print("前序遍歷") r.preOrder(r) print("中序遍歷") r.inOrder(r) print("后序遍歷") r.postOrder(r) print("層級遍歷") r.levelOrder(r)
運(yùn)行結(jié)果:
后續(xù)實(shí)現(xiàn)了會慢慢補(bǔ)上~~舊的也會不斷改進(jìn)~~
更多關(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è)計有所幫助。
- Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解
- Python數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的定義、查找、插入、構(gòu)造、刪除
- Python描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之哈夫曼樹篇
- Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實(shí)現(xiàn)方法示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之完全樹與最小堆實(shí)例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹結(jié)構(gòu)定義與遍歷方法詳解
- Python數(shù)據(jù)結(jié)構(gòu)之樹的全面解讀
相關(guān)文章
Python常用模塊logging——日志輸出功能(示例代碼)
logging模塊是Python的內(nèi)置模塊,主要用于輸出運(yùn)行日志,可以靈活配置輸出日志的各項信息。這篇文章主要介紹了Python常用模塊logging——日志輸出的實(shí)例代碼,需要的朋友可以參考下2019-11-11python使用pyecharts庫畫地圖數(shù)據(jù)可視化的實(shí)現(xiàn)
這篇文章主要介紹了python使用pyecharts庫畫地圖數(shù)據(jù)可視化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03python交互模式基礎(chǔ)知識點(diǎn)學(xué)習(xí)
在本篇內(nèi)容里小編給大家整理的是關(guān)于python交互模式是什么的相關(guān)基礎(chǔ)知識點(diǎn),需要的朋友們可以參考下。2020-06-06python pexpect ssh 遠(yuǎn)程登錄服務(wù)器的方法
今天小編就為大家分享一篇python pexpect ssh 遠(yuǎn)程登錄服務(wù)器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02