在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Python樹遍歷算法
Python樹遍歷算法
Python雙端隊列
Python隊列
Python回溯
Python棧
Python數(shù)據(jù)結(jié)構(gòu)開發(fā)環(huán)境
Python數(shù)據(jù)結(jié)構(gòu)簡介
Python算法分析
Python圖遍歷算法
Python搜索算法
Python圖
Python鏈表
Python集合
Python元組
Python字典
Python矩陣
Python高級鏈表(雙向鏈表)
Python搜索樹
Python二維數(shù)組
Python堆
Python節(jié)點
Python排序算法
Python數(shù)據(jù)結(jié)構(gòu)
Python遞歸
Python列表
Python數(shù)組
Python算法設計
Python哈希表

Python樹遍歷算法

遍歷是訪問樹的所有節(jié)點的過程,也可以打印它們的值。 因為所有節(jié)點都通過邊(鏈接)連接,所以始終從根(頭)節(jié)點開始。 也就是說,我們不能隨機訪問樹中的一個節(jié)點。 這里介紹三種方式來遍歷一棵樹 -

  • 順序遍歷
  • 前序遍歷
  • 后序遍歷

按順序遍歷

在這種遍歷方法中,首先訪問左側(cè)子樹,然后訪問根,然后訪問右側(cè)子樹。 我們應該永遠記住每個節(jié)點本身可能代表一個子樹。

在下面的python程序中,使用Node類為根節(jié)點以及左右節(jié)點創(chuàng)建占位符。 然后創(chuàng)建一個insert()函數(shù)來將數(shù)據(jù)添加到樹中。 最后,Inorder遍歷邏輯通過創(chuàng)建一個空列表,并首先添加添加根節(jié)點或父節(jié)點,然后左節(jié)點來實現(xiàn)。 最后添加左節(jié)點以完成Inorder遍歷。 請注意,對于每個子樹重復此過程,直到遍歷所有節(jié)點。

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data
# Insert Node
    def insert(self, data):

        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

# Print the Tree
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

# Inorder traversal
# Left -> Root -> Right
    def inorderTraversal(self, root):
        res = []
        if root:
            res = self.inorderTraversal(root.left)
            res.append(root.data)
            res = res + self.inorderTraversal(root.right)
        return res

root = Node(27)
root.insert(14)
root.insert(35)
root.insert(10)
root.insert(19)
root.insert(31)
root.insert(42)
print(root.inorderTraversal(root))

執(zhí)行上面示例代碼,得到以下結(jié)果 -

[10, 14, 19, 27, 31, 35, 42]

前序遍歷

在這種遍歷方法中,首先訪問根節(jié)點,然后訪問左邊的子樹,最后訪問右邊的子樹。

在下面的python程序中,使用Node類為根節(jié)點以及左右節(jié)點創(chuàng)建占位符。 然后創(chuàng)建一個insert()函數(shù)來將數(shù)據(jù)添加到樹中。 最后,前序遍歷遍歷邏輯通過創(chuàng)建一個空列表并首先添加根節(jié)點,然后添加左節(jié)點來實現(xiàn)。 最后添加右節(jié)點以完成前序遍歷。 請注意,對于每個子樹重復此過程,直到遍歷所有節(jié)點。

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data
# Insert Node
    def insert(self, data):

        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

# Print the Tree
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

# Preorder traversal
# Root -> Left ->Right
    def PreorderTraversal(self, root):
        res = []
        if root:
            res.append(root.data)
            res = res + self.PreorderTraversal(root.left)
            res = res + self.PreorderTraversal(root.right)
        return res

root = Node(27)
root.insert(14)
root.insert(35)
root.insert(10)
root.insert(19)
root.insert(31)
root.insert(42)
print(root.PreorderTraversal(root))

當上面的代碼被執(zhí)行時,它會產(chǎn)生以下結(jié)果 -

[27, 14, 10, 19, 35, 31, 42]

后序遍歷

在這個遍歷方法中,最后訪問根節(jié)點。 首先遍歷左子樹,然后遍歷右子樹,最后遍歷根節(jié)點。

在下面的python程序中,使用Node類為根節(jié)點以及左右節(jié)點創(chuàng)建占位符。 然后創(chuàng)建一個insert()函數(shù)來將數(shù)據(jù)添加到樹中。 最后,通過創(chuàng)建一個空列表并添加左節(jié)點,然后添加右節(jié)點來實現(xiàn)后序遍歷邏輯。 最后,添加根或父節(jié)點以完成后序遍歷。 請注意,對于每個子樹重復此過程,直到遍歷所有節(jié)點。參考以下代碼實現(xiàn) -

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data
# Insert Node
    def insert(self, data):

        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

# Print the Tree
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

# Postorder traversal
# Left ->Right -> Root
    def PostorderTraversal(self, root):
        res = []
        if root:
            res = self.PostorderTraversal(root.left)
            res = res + self.PostorderTraversal(root.right)
            res.append(root.data)
        return res

root = Node(27)
root.insert(14)
root.insert(35)
root.insert(10)
root.insert(19)
root.insert(31)
root.insert(42)
print(root.PostorderTraversal(root))

當上面的代碼被執(zhí)行時,它會產(chǎn)生以下結(jié)果 -

[10, 19, 14, 31, 42, 35, 27]

上一篇:Python哈希表下一篇:Python回溯