python 將有序數(shù)組轉(zhuǎn)換為二叉樹的方法
題目:將[0,1,2,3,4,5,6,7,8,9,10]存儲(chǔ)到二叉樹,原數(shù)組有序,轉(zhuǎn)換為二叉排序樹。
二叉排序樹的特點(diǎn):當(dāng)前節(jié)點(diǎn)的左子樹上的所有節(jié)點(diǎn)都小于該節(jié)點(diǎn),右子樹上的所有節(jié)點(diǎn)都小于該節(jié)點(diǎn)。
二叉排序也稱為二叉查找樹。
我的實(shí)現(xiàn)思路:
取有序數(shù)組的中間節(jié)點(diǎn)作為根節(jié)點(diǎn),將數(shù)組分為左右兩個(gè)部分,對(duì)左右兩個(gè)子數(shù)組做相同的操作,遞歸的實(shí)現(xiàn)。
圖示:
1
2
3
代碼實(shí)現(xiàn):
def array_to_bitree(array): #判斷arr是否為空 if len(array)==0: return BiTNode(array[0]) mid=len(array)//2 # 有序數(shù)組的中間元素的下標(biāo) #print(mid) #start=0 # 數(shù)組第一個(gè)元素的下標(biāo) #end=-1 # 數(shù)組最后一個(gè)元素的下標(biāo) if len(array)>0: #將中間元素作為二叉樹的根 root=BiTNode(array[mid]) #如果左邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹 if len(array[:mid])>0: root.left_child = arrayToBiTree(array[:mid]) #如果右邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹 if len(array[mid+1:])>0: root.right_child = arrayToBiTree(array[mid+1:]) return root
我們調(diào)用前面寫的三種遍歷方法看一看,我們構(gòu)造的樹是否正確:
#將[0,1,2,3,4,5,6,7,8,9,10]存儲(chǔ)到二叉樹 if __name__ == '__main__': #先構(gòu)造一個(gè)有序數(shù)組、鏈表 arr=[] for i in range(10): arr.append(i) print(arr) #調(diào)用函數(shù) BT=arrayToBiTree(arr) #前序遍歷二叉樹 print("前序") print_tree_pre_order(BT) # 中序遍歷二叉樹 print("中序") print_tree_mid_order(BT) # 后序遍歷二叉樹 print("后序") print_tree_after_order(BT)
輸出:
根據(jù)這三種遍歷結(jié)果可以判斷出二叉樹的結(jié)構(gòu),結(jié)果和前面的是一樣的,代碼如下:
#定義二叉樹結(jié)點(diǎn)類型 class BiTNode: """docstring for BiTNode""" def __init__(self,arg): self.data = arg self.left_child = None self.right_child = None #前序遍歷 def print_tree_pre_order(root): #先判斷二叉樹是否為空 #if root.left_child is None and root.right_child is None: if root is None: return root #先根 print(root.data) #再左 if root.left_child is not None: print_tree_pre_order(root.left_child) #再右 if root.right_child is not None: print_tree_pre_order(root.right_child) #中序遍歷二叉樹 def print_tree_mid_order(root): #先判斷二叉樹是否為空,當(dāng)左右節(jié)點(diǎn)都為空時(shí) if root is None: return #中序遍歷 左根右 #遍歷左子樹 if root.left_child is not None: print_tree_mid_order(root.left_child) #遍歷根節(jié)點(diǎn) print(root.data) #遍歷右子樹 if root.right_child is not None: print_tree_mid_order(root.right_child) #后序遍歷 def print_tree_after_order(root): #先判斷二叉樹是否為空 if root is None: return root #再左 if root.left_child is not None: print_tree_after_order(root.left_child) #再右 if root.right_child is not None: print_tree_after_order(root.right_child) #先根 print(root.data) def array_to_bitree(array): #判斷arr是否為空 if len(array)==0: return BiTNode(array[0]) mid=len(array)//2 # 有序數(shù)組的中間元素的下標(biāo) #print(mid) #start=0 # 數(shù)組第一個(gè)元素的下標(biāo) #end=-1 # 數(shù)組最后一個(gè)元素的下標(biāo) if len(array)>0: #將中間元素作為二叉樹的根 root=BiTNode(array[mid]) #如果左邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹 if len(array[:mid])>0: root.left_child = array_to_bitree(array[:mid]) #如果右邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹 if len(array[mid+1:])>0: root.right_child = array_to_bitree(array[mid+1:]) return root #將[0,1,2,3,4,5,6,7,8,9,10]存儲(chǔ)到二叉樹 if __name__ == '__main__': #先構(gòu)造一個(gè)有序數(shù)組、鏈表 arr=[] for i in range(9): arr.append(i) print(arr) #調(diào)用函數(shù) BT=array_to_bitree(arr) #前序遍歷二叉樹 print("前序") print_tree_pre_order(BT) # 中序遍歷二叉樹 print("中序") print_tree_mid_order(BT) # 后序遍歷二叉樹 print("后序") print_tree_after_order(BT)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python爬蟲beautifulsoup庫使用操作教程全解(python爬蟲基礎(chǔ)入門)
這篇文章主要介紹了python爬蟲beautifulsoup庫使用操作全解(python爬蟲基礎(chǔ)入門),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02在Django框架中運(yùn)行Python應(yīng)用全攻略
這篇文章主要介紹了在Django框架中運(yùn)行Python應(yīng)用全攻略,在這之前必須搭建好簡(jiǎn)單的視圖和模版,接下來便是本文中所述的核心內(nèi)容應(yīng)用配置,需要的朋友可以參考下2015-07-07python爬蟲反爬之圖片驗(yàn)證功能實(shí)現(xiàn)
這篇文章主要介紹了python爬蟲反爬之圖片驗(yàn)證功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03Python 反轉(zhuǎn)字符串(reverse)的方法小結(jié)
這篇文章主要介紹了Python 反轉(zhuǎn)字符串(reverse)的方法小結(jié),需要的朋友可以參考下2018-02-02聽歌識(shí)曲--用python實(shí)現(xiàn)一個(gè)音樂檢索器的功能
本篇文章中主要介紹了用python實(shí)現(xiàn)一個(gè)音樂檢索器,類似于QQ音樂的搖一搖識(shí)曲,有興趣的同學(xué)可以了解一下。2016-11-11