python 將有序數(shù)組轉(zhuǎn)換為二叉樹(shù)的方法
題目:將[0,1,2,3,4,5,6,7,8,9,10]存儲(chǔ)到二叉樹(shù),原數(shù)組有序,轉(zhuǎn)換為二叉排序樹(shù)。
二叉排序樹(shù)的特點(diǎn):當(dāng)前節(jié)點(diǎn)的左子樹(shù)上的所有節(jié)點(diǎn)都小于該節(jié)點(diǎn),右子樹(shù)上的所有節(jié)點(diǎn)都小于該節(jié)點(diǎn)。
二叉排序也稱(chēng)為二叉查找樹(shù)。
我的實(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:
#將中間元素作為二叉樹(shù)的根
root=BiTNode(array[mid])
#如果左邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹(shù)
if len(array[:mid])>0:
root.left_child = arrayToBiTree(array[:mid])
#如果右邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹(shù)
if len(array[mid+1:])>0:
root.right_child = arrayToBiTree(array[mid+1:])
return root
我們調(diào)用前面寫(xiě)的三種遍歷方法看一看,我們構(gòu)造的樹(shù)是否正確:
#將[0,1,2,3,4,5,6,7,8,9,10]存儲(chǔ)到二叉樹(shù)
if __name__ == '__main__':
#先構(gòu)造一個(gè)有序數(shù)組、鏈表
arr=[]
for i in range(10):
arr.append(i)
print(arr)
#調(diào)用函數(shù)
BT=arrayToBiTree(arr)
#前序遍歷二叉樹(shù)
print("前序")
print_tree_pre_order(BT)
# 中序遍歷二叉樹(shù)
print("中序")
print_tree_mid_order(BT)
# 后序遍歷二叉樹(shù)
print("后序")
print_tree_after_order(BT)
輸出:

根據(jù)這三種遍歷結(jié)果可以判斷出二叉樹(shù)的結(jié)構(gòu),結(jié)果和前面的是一樣的,代碼如下:
#定義二叉樹(shù)結(jié)點(diǎn)類(lèi)型
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):
#先判斷二叉樹(shù)是否為空
#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)
#中序遍歷二叉樹(shù)
def print_tree_mid_order(root):
#先判斷二叉樹(shù)是否為空,當(dāng)左右節(jié)點(diǎn)都為空時(shí)
if root is None:
return
#中序遍歷 左根右
#遍歷左子樹(shù)
if root.left_child is not None:
print_tree_mid_order(root.left_child)
#遍歷根節(jié)點(diǎn)
print(root.data)
#遍歷右子樹(shù)
if root.right_child is not None:
print_tree_mid_order(root.right_child)
#后序遍歷
def print_tree_after_order(root):
#先判斷二叉樹(shù)是否為空
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:
#將中間元素作為二叉樹(shù)的根
root=BiTNode(array[mid])
#如果左邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹(shù)
if len(array[:mid])>0:
root.left_child = array_to_bitree(array[:mid])
#如果右邊的元素個(gè)數(shù)不為零,則遞歸調(diào)用函數(shù),生成左子樹(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ǔ)到二叉樹(shù)
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)
#前序遍歷二叉樹(shù)
print("前序")
print_tree_pre_order(BT)
# 中序遍歷二叉樹(shù)
print("中序")
print_tree_mid_order(BT)
# 后序遍歷二叉樹(shù)
print("后序")
print_tree_after_order(BT)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python爬蟲(chóng)beautifulsoup庫(kù)使用操作教程全解(python爬蟲(chóng)基礎(chǔ)入門(mén))
這篇文章主要介紹了python爬蟲(chóng)beautifulsoup庫(kù)使用操作全解(python爬蟲(chóng)基礎(chǔ)入門(mén)),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Python如何讀寫(xiě)字節(jié)數(shù)據(jù)
這篇文章主要介紹了Python如何讀寫(xiě)字節(jié)數(shù)據(jù),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08
介紹一款python類(lèi)型檢查工具pyright(推薦)
這篇文章主要介紹了介紹一款python類(lèi)型檢查工具pyright(推薦),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
在Django框架中運(yùn)行Python應(yīng)用全攻略
這篇文章主要介紹了在Django框架中運(yùn)行Python應(yīng)用全攻略,在這之前必須搭建好簡(jiǎn)單的視圖和模版,接下來(lái)便是本文中所述的核心內(nèi)容應(yīng)用配置,需要的朋友可以參考下2015-07-07
python爬蟲(chóng)反爬之圖片驗(yàn)證功能實(shí)現(xiàn)
這篇文章主要介紹了python爬蟲(chóng)反爬之圖片驗(yàn)證功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03
Python 反轉(zhuǎn)字符串(reverse)的方法小結(jié)
這篇文章主要介紹了Python 反轉(zhuǎn)字符串(reverse)的方法小結(jié),需要的朋友可以參考下2018-02-02
聽(tīng)歌識(shí)曲--用python實(shí)現(xiàn)一個(gè)音樂(lè)檢索器的功能
本篇文章中主要介紹了用python實(shí)現(xiàn)一個(gè)音樂(lè)檢索器,類(lèi)似于QQ音樂(lè)的搖一搖識(shí)曲,有興趣的同學(xué)可以了解一下。2016-11-11

