
Python實(shí)現(xiàn)基于二叉樹(shù)存儲(chǔ)結(jié)構(gòu)的堆排序算法示例
本文實(shí)例講述了Python實(shí)現(xiàn)基于二叉樹(shù)存儲(chǔ)結(jié)構(gòu)的堆排序算法。分享給大家供大家參考,具體如下:
既然用Python實(shí)現(xiàn)了二叉樹(shù),當(dāng)然要寫(xiě)點(diǎn)東西練練手。
網(wǎng)絡(luò)上堆排序的教程很多,但是卻幾乎都是以數(shù)組存儲(chǔ)的數(shù),直接以下標(biāo)訪問(wèn)元素,當(dāng)然這樣是完全沒(méi)有問(wèn)題的,實(shí)現(xiàn)簡(jiǎn)單,訪問(wèn)速度快,也容易理解。
但是以練手的角度來(lái)看,我還是寫(xiě)了一個(gè)二叉樹(shù)存儲(chǔ)結(jié)構(gòu)的堆排序
其中最難的問(wèn)題就是交換二叉樹(shù)中兩個(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)建二叉樹(shù)
def FrontBuildTree(self):
temp = input('Please Input: ')
node = Tree(temp)
if(temp != '#'):
node.left = self.FrontBuildTree()
node.right = self.FrontBuildTree()
return node#因?yàn)闆](méi)有引用也沒(méi)有指針,所以就把新的節(jié)點(diǎn)給返回回去
#前序遍歷二叉樹(shù)
def VisitNode(self):
print(self.val)
if(self.left != None):
self.left.VisitNode()
if(self.right != None):
self.right.VisitNode()
#中序遍歷二叉樹(shù)
def MVisitTree(self):
if(self.left != None):
self.left.MVisitTree()
print(self.val)
if(self.right != None):
self.right.MVisitTree()
#獲取二叉樹(shù)的第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è)鏈子保存起來(lái)
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#將二叉樹(shù)最后一個(gè)葉子節(jié)點(diǎn)移到頭結(jié)點(diǎn)
p.left = self.left
p.right = self.right
while(1):#優(yōu)化是萬(wàn)惡之源
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é)果:
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類(lèi)型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類(lèi)型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門(mén)控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書(shū)考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開(kāi)啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03