python實(shí)現(xiàn)二叉排序樹(shù)
方法一(粗暴)
#二叉排序樹(shù) class BTree(): ? ? def __init__(self,data): ? ? ? ? self.left = None ? ? ? ? self.right = None ? ? ? ? if type(data) == list: ? ? ? ? ? ? self.data = data[0] ? ? ? ? ? ? for d in data[1:]: ? ? ? ? ? ? ? ? self.insert(d) ? ? ? ? else: ? ? ? ? ? ? self.data = data ? ? def insert(self,data): ? ? ? ? bt = self ? ? ? ? while True: ? ? ? ? ? ? if data <= bt.data: ? ? ? ? ? ? ? ? if bt.left == None: ? ? ? ? ? ? ? ? ? ? bt.left = BTree(data) ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? bt = bt.left ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? if bt.right == None: ? ? ? ? ? ? ? ? ? ? bt.right = BTree(data) ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? bt = bt.right ? ? def mid_order(self): ? ? ? ? res = [] ? ? ? ? stack = [] ? ? ? ? node = self? ? ? ? ? while node or stack: ? ? ? ? ? ? while node: ? ? ? ? ? ? ? ? stack.append(node)? ? ? ? ? ? ? ? ? node = node.left ? ? ? ? ? ? node = stack.pop() ? ? ? ? ? ? res.append(node.data) ? ? ? ? ? ? node = node.right ? ? ? ? return res data = [5,1,2,3,6,8,9] bt = BTree(data) print(bt.mid_order())

方法二(遞歸)
class TreeNode(object): ? ? def __init__(self,data): ? ? ? ? self.data = data ? ? ? ? self.left = None ? ? ? ? self.right = None class BinaryTree(object): ? ? def insert(self,root, node): ? ? ? ? if root is None: ? ? ? ? ? ? return node ? ? ? ? if node.data < root.data: ? ? ? ? ? ? root.left = self.insert(root.left, node) ? ? ? ? else: ? ? ? ? ? ? root.right = self.insert(root.right, node) ? ? ? ? return root ? ? def mid_order(self,root): ? ? ? ? node = root ? ? ? ? stack = [] ? ? ? ? res = [] ? ? ? ? while node or stack: ? ? ? ? ? ? while node: ? ? ? ? ? ? ? ? stack.append(node) ? ? ? ? ? ? ? ? node = node.left ? ? ? ? ? ? node = stack.pop() ? ? ? ? ? ? res.append(node.data) ? ? ? ? ? ? node = node.right ? ? ? ? return res ? ?? data = [5,1,2,3,6,8,9] root = TreeNode(data[0]) tree = BinaryTree() for i in data[1:]: ? ? tree.insert(root,TreeNode(i)) print(tree.mid_order(root))

到此這篇關(guān)于python實(shí)現(xiàn)二叉排序樹(shù)的文章就介紹到這了,更多相關(guān)python二叉排序樹(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
win32com操作word之Application&Documents接口學(xué)習(xí)
這篇文章主要為大家介紹了win32com操作word之Application&Documents接口學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例
下面小編就為大家分享一篇python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
10分鐘學(xué)會(huì)使用python實(shí)現(xiàn)人臉識(shí)別(附源碼)
這篇文章主要介紹了10分鐘學(xué)會(huì)使用python實(shí)現(xiàn)人臉識(shí)別(附源碼),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
Django JSONField的自動(dòng)轉(zhuǎn)換思路詳解(django自定義模型字段)
如果想實(shí)現(xiàn)JSONField的自動(dòng)轉(zhuǎn)換,可以使用Django REST framework的JSONField,或者自定義一個(gè)字段類(lèi)并覆蓋from_db_value()和get_prep_value()方法來(lái)實(shí)現(xiàn)這個(gè)功能,這篇文章主要介紹了Django JSONField的自動(dòng)轉(zhuǎn)換(django自定義模型字段)問(wèn)題,需要的朋友可以參考下2023-06-06
Python中if elif else及縮進(jìn)的使用簡(jiǎn)述
這篇文章主要介紹了Python中if elif else及縮進(jìn)的使用,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Python asyncio異步編程常見(jiàn)問(wèn)題小結(jié)
本文主要介紹了Python asyncio異步編程常見(jiàn)問(wèn)題小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Python模擬百度自動(dòng)輸入搜索功能的實(shí)例
今天小編就為大家分享一篇Python模擬百度自動(dòng)輸入搜索功能的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python使用BeautifulSoup4修改網(wǎng)頁(yè)內(nèi)容的實(shí)戰(zhàn)記錄
BeautifulSoup除了可以查找和定位網(wǎng)頁(yè)內(nèi)容,還可以修改網(wǎng)頁(yè),下面這篇文章主要給大家介紹了關(guān)于Python使用BeautifulSoup4修改網(wǎng)頁(yè)內(nèi)容的相關(guān)資料,需要的朋友可以參考下2022-05-05

