婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > 教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷

教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷

熱門標(biāo)簽:老人電話機(jī)器人 地圖標(biāo)注視頻廣告 北京電信外呼系統(tǒng)靠譜嗎 梅州外呼業(yè)務(wù)系統(tǒng) 百度地圖標(biāo)注位置怎么修改 高德地圖標(biāo)注是免費(fèi)的嗎 洪澤縣地圖標(biāo)注 大連crm外呼系統(tǒng) 無(wú)錫客服外呼系統(tǒng)一般多少錢

一:代碼實(shí)現(xiàn)

class TreeNode:
    """節(jié)點(diǎn)類"""
    def __init__(self, mid, left=None, right=None):
        self.mid = mid
        self.left = left
        self.right = right


# 樹(shù)類
class Tree:
    """樹(shù)類"""
    def __init__(self, root=None):
        self.root = root

    def add(self, item):
        # 將要添加的數(shù)據(jù)封裝成一個(gè)node結(jié)點(diǎn)
        node = TreeNode(item)
        if not self.root:
            self.root = node
            return
        queue = [self.root]
        while queue:
            cur = queue.pop(0)
            if not cur.left:
                cur.left = node
                return
            else:
                queue.append(cur.left)

            if not cur.right:
                cur.right = node
                return
            else:
                queue.append(cur.right)
               
tree = Tree()
tree.add(0)
tree.add(1)
tree.add(2)
tree.add(3)
tree.add(4)
tree.add(5)
tree.add(6)

二:遍歷

在上述樹(shù)類代碼基礎(chǔ)上加遍歷函數(shù),基于遞歸實(shí)現(xiàn)。

先序遍歷:

先序遍歷結(jié)果是:0 -> 1 -> 3 -> 4 -> 2 -> 5 -> 6

# 先序遍歷
    def preorder(self, root, result=[]):
        if not root:
            return
        result.append(root.mid)
        self.preorder(root.left, result)
        self.preorder(root.right, result)
        return result
        
print("先序遍歷")
print(tree.preorder(tree.root))
"""
先序遍歷
[0, 1, 3, 4, 2, 5, 6]
"""

中序遍歷:

中序遍歷結(jié)果是:3 -> 1 -> 4 -> 0 -> 5 -> 2 -> 6

# 中序遍歷
    def inorder(self, root, result=[]):
        if not root:
            return result
        self.inorder(root.left, result)
        result.append(root.mid)
        self.inorder(root.right, result)
        return result
        
print("中序遍歷")
print(tree.inorder(tree.root))
"""
中序遍歷
3, 1, 4, 0, 5, 2, 6]
"""

后續(xù)遍歷

后序遍歷結(jié)果是:3 -> 4 -> 1 -> 5 -> 6 -> 2 -> 0

# 后序遍歷
    def postorder(self, root, result=[]):
        if not root:
            return result
        self.postorder(root.left, result)
        self.postorder(root.right, result)
        result.append(root.mid)

        return result
        
print("后序遍歷")
print(tree.postorder(tree.root))
"""
后序遍歷
[3, 4, 1, 5, 6, 2, 0]
"""

到此這篇關(guān)于教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)結(jié)構(gòu)定義與遍歷方法詳解
  • Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)與進(jìn)行二叉樹(shù)遍歷的方法詳解

標(biāo)簽:清遠(yuǎn) 吉林 長(zhǎng)春 怒江 安慶 泉州 岳陽(yáng) 洛陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷》,本文關(guān)鍵詞  教你,如何,使用,Python,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 阿荣旗| 额尔古纳市| 浙江省| 汤原县| 漯河市| 涟源市| 襄城县| 黄陵县| 永康市| 新竹市| 临沭县| 杭锦旗| 平乡县| 湖口县| 永定县| 丰镇市| 舟山市| 秦安县| 太湖县| 阿合奇县| 漳浦县| 太康县| 项城市| 色达县| 甘泉县| 重庆市| 房产| 察雅县| 荥经县| 苗栗市| 久治县| 彭山县| 连城县| 高阳县| 肥城市| 鄂州市| 南乐县| 仁怀市| 峨眉山市| 斗六市| 三江|