Python實(shí)現(xiàn)基于二叉樹存儲(chǔ)結(jié)構(gòu)的堆排序算法示例
本文實(shí)例講述了Python實(shí)現(xiàn)基于二叉樹存儲(chǔ)結(jié)構(gòu)的堆排序算法。分享給大家供大家參考,具體如下:
既然用Python實(shí)現(xiàn)了二叉樹,當(dāng)然要寫點(diǎn)東西練練手。
網(wǎng)絡(luò)上堆排序的教程很多,但是卻幾乎都是以數(shù)組存儲(chǔ)的數(shù),直接以下標(biāo)訪問元素,當(dāng)然這樣是完全沒有問題的,實(shí)現(xiàn)簡(jiǎn)單,訪問速度快,也容易理解。
但是以練手的角度來看,我還是寫了一個(gè)二叉樹存儲(chǔ)結(jié)構(gòu)的堆排序
其中最難的問題就是交換二叉樹中兩個(gè)節(jié)點(diǎn)。
因?yàn)橐粋€(gè)節(jié)點(diǎn)最多與三個(gè)節(jié)點(diǎn)相連,那么兩個(gè)節(jié)點(diǎn)互換,就需要考慮到5個(gè)節(jié)點(diǎn)之間的關(guān)系,也需要判斷是左右孩子,這將是十分繁瑣的,也很容易出錯(cuò)。
class Tree: def __init__(self, val = '#', left = None, right = None): self.val = val self.left = left self.right = right self.ponit = None self.father = None self.counter = 0 #前序構(gòu)建二叉樹 def FrontBuildTree(self): temp = input('Please Input: ') node = Tree(temp) if(temp != '#'): node.left = self.FrontBuildTree() node.right = self.FrontBuildTree() return node#因?yàn)闆]有引用也沒有指針,所以就把新的節(jié)點(diǎn)給返回回去 #前序遍歷二叉樹 def VisitNode(self): print(self.val) if(self.left != None): self.left.VisitNode() if(self.right != None): self.right.VisitNode() #中序遍歷二叉樹 def MVisitTree(self): if(self.left != None): self.left.MVisitTree() print(self.val) if(self.right != None): self.right.MVisitTree() #獲取二叉樹的第dec個(gè)節(jié)點(diǎn) def GetPoint(self, dec): road = str(bin(dec))[3:] p = self for r in road: if (r == '0'): p = p.left else: p = p.right #print('p.val = ', p.val) return p #構(gòu)建第一個(gè)堆 def BuildHeadTree(self, List): for val in List: #print('val = ', val, 'self.counter = ', self.counter) self.ponit = self.GetPoint(int((self.counter + 1) / 2)) #print('self.ponit.val = ', self.ponit.val) if (self.counter == 0): self.val = val self.father = self else: temp = self.counter + 1 node = Tree(val) node.father = self.ponit if(temp % 2 == 0):#新增節(jié)點(diǎn)為左孩子 self.ponit.left = node else: self.ponit.right = node while(temp != 0): if (node.val < node.father.val):#如果新增節(jié)點(diǎn)比其父親節(jié)點(diǎn)值要大 p = node.father#先將其三個(gè)鏈子保存起來 LeftTemp = node.left RightTemp = node.right if (p.father != p):#判斷其不是頭結(jié)點(diǎn) if (int(temp / 2) % 2 == 0):#新增節(jié)點(diǎn)的父親為左孩子 p.father.left = node else: p.father.right = node node.father = p.father else: node.father = node#是頭結(jié)點(diǎn)則將其father連向自身 node.counter = self.counter self = node if(temp % 2 == 0):#新增節(jié)點(diǎn)為左孩子 node.left = p node.right = p.right if (p.right != None): p.right.father = node else: node.left = p.left node.right = p if (p.left != None): p.left.father = node p.left = LeftTemp p.right = RightTemp p.father = node temp = int(temp / 2) #print('node.val = ', node.val, 'node.father.val = ', node.father.val) #print('Tree = ') #self.VisitNode() else: break; self.counter += 1 return self #將頭結(jié)點(diǎn)取出后重新調(diào)整堆 def Adjust(self): #print('FrontSelfTree = ') #self.VisitNode() #print('MSelfTree = ') #self.MVisitTree() print('Get ', self.val) p = self.GetPoint(self.counter) #print('p.val = ', p.val) #print('p.father.val = ', p.father.val) root = p if (self.counter % 2 == 0): p.father.left = None else: p.father.right = None #print('self.left = ', self.left.val) #print('self.right = ', self.right.val) p.father = p#將二叉樹最后一個(gè)葉子節(jié)點(diǎn)移到頭結(jié)點(diǎn) p.left = self.left p.right = self.right while(1):#優(yōu)化是萬惡之源 LeftTemp = p.left RightTemp = p.right FatherTemp = p.father if (p.left != None and p.right !=None):#判斷此時(shí)正在處理的結(jié)點(diǎn)的左后孩子情況 if (p.left.val < p.right.val): next = p.left else: next = p.right if (p.val < next.val): break; elif (p.left == None and p.right != None and p.val > p.right.val): next = p.right elif (p.right == None and p.left != None and p.val > p.left.val): next = p.left else: break; p.left = next.left p.right = next.right p.father = next if (next.left != None):#之后就是一系列的交換節(jié)點(diǎn)的鏈的處理 next.left.father = p if (next.right != None): next.right.father = p if (FatherTemp == p): next.father = next root = next else: next.father == FatherTemp if (FatherTemp.left == p): FatherTemp.left = next else: FatherTemp.right = next if (next == LeftTemp): next.right = RightTemp next.left = p if (RightTemp != None): RightTemp.father = next else: next.left = LeftTemp next.right = p if (LeftTemp != None): LeftTemp.father = next #print('Tree = ') #root.VisitNode() root.counter = self.counter - 1 return root if __name__ == '__main__': print("腳本之家測(cè)試結(jié)果") root = Tree() number = [-1, -1, 0, 0, 0, 12, 22, 3, 5, 4, 3, 1, 6, 9] root = root.BuildHeadTree(number) while(root.counter != 0): root = root.Adjust()
運(yùn)行結(jié)果:
PS:這里再為大家推薦一款關(guān)于排序的演示工具供大家參考:
在線動(dòng)畫演示插入/選擇/冒泡/歸并/希爾/快速排序算法過程工具:
http://tools.jb51.net/aideddesign/paixu_ys
更多關(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)文章
python查找特定名稱文件并按序號(hào)、文件名分行打印輸出的方法
這篇文章主要介紹了python查找特定名稱文件并按序號(hào)、文件名分行打印輸出的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04python神經(jīng)網(wǎng)絡(luò)使用tensorflow實(shí)現(xiàn)自編碼Autoencoder
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)使用tensorflow實(shí)現(xiàn)自編碼Autoencoder,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python動(dòng)態(tài)網(wǎng)站爬蟲實(shí)戰(zhàn)(requests+xpath+demjson+redis)
本文主要介紹了python動(dòng)態(tài)網(wǎng)站爬蟲實(shí)戰(zhàn)(requests+xpath+demjson+redis),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09在Python上基于Markov鏈生成偽隨機(jī)文本的教程
這篇文章主要介紹了在Python上基于Markov鏈生成偽隨機(jī)文本的教程,是一個(gè)基于馬爾可夫算法的小實(shí)現(xiàn),充分體現(xiàn)了Python在科學(xué)計(jì)算中的用途,需要的朋友可以參考下2015-04-04Django中如何防范CSRF跨站點(diǎn)請(qǐng)求偽造攻擊的實(shí)現(xiàn)
這篇文章主要介紹了Django中如何防范CSRF跨站點(diǎn)請(qǐng)求偽造攻擊的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04