python實現(xiàn)二叉排序樹
更新時間:2022年01月26日 11:44:51 作者:咕嘟咕嘟_?
這篇文章主要介紹了python實現(xiàn)二叉排序樹,
方法一(粗暴)
#二叉排序樹 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實現(xiàn)二叉排序樹的文章就介紹到這了,更多相關(guān)python二叉排序樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
win32com操作word之Application&Documents接口學(xué)習(xí)
這篇文章主要為大家介紹了win32com操作word之Application&Documents接口學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實例
下面小編就為大家分享一篇python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
10分鐘學(xué)會使用python實現(xiàn)人臉識別(附源碼)
這篇文章主要介紹了10分鐘學(xué)會使用python實現(xiàn)人臉識別(附源碼),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
Django JSONField的自動轉(zhuǎn)換思路詳解(django自定義模型字段)
如果想實現(xiàn)JSONField的自動轉(zhuǎn)換,可以使用Django REST framework的JSONField,或者自定義一個字段類并覆蓋from_db_value()和get_prep_value()方法來實現(xiàn)這個功能,這篇文章主要介紹了Django JSONField的自動轉(zhuǎn)換(django自定義模型字段)問題,需要的朋友可以參考下2023-06-06
Python使用BeautifulSoup4修改網(wǎng)頁內(nèi)容的實戰(zhàn)記錄
BeautifulSoup除了可以查找和定位網(wǎng)頁內(nèi)容,還可以修改網(wǎng)頁,下面這篇文章主要給大家介紹了關(guān)于Python使用BeautifulSoup4修改網(wǎng)頁內(nèi)容的相關(guān)資料,需要的朋友可以參考下2022-05-05

