亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的遍歷實(shí)例

 更新時(shí)間:2014年04月29日 09:06:02   作者:  
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的遞歸遍歷實(shí)例,需要的朋友可以參考下

遍歷方案
    從二叉樹(shù)的遞歸定義可知,一棵非空的二叉樹(shù)由根結(jié)點(diǎn)及左、右子樹(shù)這三個(gè)基本部分組成。因此,在任一給定結(jié)點(diǎn)上,可以按某種次序執(zhí)行三個(gè)操作:
    1).訪問(wèn)結(jié)點(diǎn)本身(N)
    2).遍歷該結(jié)點(diǎn)的左子樹(shù)(L)
    3).遍歷該結(jié)點(diǎn)的右子樹(shù)(R)

有次序:
    NLR、LNR、LRN

遍歷的命名

    根據(jù)訪問(wèn)結(jié)點(diǎn)操作發(fā)生位置命名:
NLR:前序遍歷(PreorderTraversal亦稱(先序遍歷))  ——訪問(wèn)結(jié)點(diǎn)的操作發(fā)生在遍歷其左右子樹(shù)之前。
LNR:中序遍歷(InorderTraversal)  ——訪問(wèn)結(jié)點(diǎn)的操作發(fā)生在遍歷其左右子樹(shù)之中(間)。
LRN:后序遍歷(PostorderTraversal)    ——訪問(wèn)結(jié)點(diǎn)的操作發(fā)生在遍歷其左右子樹(shù)之后。

注:由于被訪問(wèn)的結(jié)點(diǎn)必是某子樹(shù)的根,所以N(Node)、L(Left subtlee)和R(Right subtree)又可解釋為根、根的左子樹(shù)和根的右子樹(shù)。NLR、LNR和LRN分別又稱為先根遍歷、中根遍歷和后根遍歷。

遍歷算法

1).先序遍歷的遞歸算法定義:
若二叉樹(shù)非空,則依次執(zhí)行如下操作:
a.訪問(wèn)根結(jié)點(diǎn)
b.遍歷左子樹(shù)
c.遍歷右子樹(shù)

2).中序遍歷的遞歸算法定義:
若二叉樹(shù)非空,則依次執(zhí)行如下操作:
a.遍歷左子樹(shù)
b.訪問(wèn)根結(jié)點(diǎn)
c.遍歷右子樹(shù)

3).后序遍歷得遞歸算法定義:
若二叉樹(shù)非空,則依次執(zhí)行如下操作:
a.遍歷左子樹(shù)
b.遍歷右子樹(shù)
c.訪問(wèn)根結(jié)點(diǎn)

一、二叉樹(shù)的遞歸遍歷:

復(fù)制代碼 代碼如下:

# -*- coding: utf - 8 - *-

class TreeNode(object):

    def __init__(self, left=0, right=0, data=0):
        self.left = left
        self.right = right
        self.data = data

     
class BTree(object):

    def __init__(self, root=0):
        self.root = root

    def is_empty(self):
        if self.root is 0:
            return True
        else:
            return False

    def preorder(self, treenode):
        '前序(pre-order,NLR)遍歷'
        if treenode is 0:
            return
        print treenode.data
        self.preorder(treenode.left)
        self.preorder(treenode.right)

    def inorder(self, treenode):
        '中序(in-order,LNR'
        if treenode is 0:
            return
        self.inorder(treenode.left)
        print treenode.data
        self.inorder(treenode.right)

    def postorder(self, treenode):
        '后序(post-order,LRN)遍歷'
        if treenode is 0:
            return
        self.postorder(treenode.left)
        self.postorder(treenode.right)
        print treenode.data

     
node1 = TreeNode(data=1)
node2 = TreeNode(node1, 0, 2)
node3 = TreeNode(data=3)
node4 = TreeNode(data=4)
node5 = TreeNode(node3, node4, 5)
node6 = TreeNode(node2, node5, 6)
node7 = TreeNode(node6, 0, 7)
node8 = TreeNode(data=8)
root = TreeNode(node7, node8, 'root')

bt = BTree(root)

print u'''

#生成的二叉樹(shù)

# ------------------------
#          root
#       7        8
#     6
#   2   5
# 1    3 4
#
# -------------------------

'''
print '前序(pre-order,NLR)遍歷 :\n'
bt.preorder(bt.root)

print '中序(in-order,LNR) 遍歷 :\n'
bt.inorder(bt.root)

print '后序(post-order,LRN)遍歷 :\n'
bt.postorder(bt.root)


二、.二叉樹(shù)的非遞歸遍歷

下面就用非遞歸的方式實(shí)現(xiàn)一遍。主要用到了 stack 和 queue維護(hù)一些數(shù)據(jù)節(jié)點(diǎn):

復(fù)制代碼 代碼如下:

# -*- coding: utf - 8 - *-

     
class TreeNode(object):

    def __init__(self, left=0, right=0, data=0):
        self.left = left
        self.right = right
        self.data = data

     
class BTree(object):

    def __init__(self, root=0):
        self.root = root

    def is_empty(self):
        if self.root is 0:
            return True
        else:
            return False

    def preorder(self, treenode):
        '前序(pre-order,NLR)遍歷'
        stack = []
        while treenode or stack:
            if treenode is not 0:
                print treenode.data
                stack.append(treenode)
                treenode = treenode.left
            else:
                treenode = stack.pop()
                treenode = treenode.right

    def inorder(self, treenode):
        '中序(in-order,LNR) 遍歷'
        stack = []
        while treenode or stack:
            if treenode:
                stack.append(treenode)
                treenode = treenode.left
            else:
                treenode = stack.pop()
                print treenode.data
                treenode = treenode.right

    # def postorder(self, treenode):
    #     stack = []
    #     pre = 0
    #     while treenode or stack:
    #         if treenode:
    #             stack.append(treenode)
    #             treenode = treenode.left
    #         elif stack[-1].right != pre:
    #             treenode = stack[-1].right
    #             pre = 0
    #         else:
    #             pre = stack.pop()
    #             print pre.data

    def postorder(self, treenode):
        '后序(post-order,LRN)遍歷'
        stack = []
        queue = []
        queue.append(treenode)
        while queue:
            treenode = queue.pop()
            if treenode.left:
                queue.append(treenode.left)
            if treenode.right:
                queue.append(treenode.right)
            stack.append(treenode)
        while stack:
            print stack.pop().data

    def levelorder(self, treenode):
        from collections import deque
        if not treenode:
            return
        q = deque([treenode])
        while q:
            treenode = q.popleft()
            print treenode.data
            if treenode.left:
                q.append(treenode.left)
            if treenode.right:
                q.append(treenode.right)

     
node1 = TreeNode(data=1)
node2 = TreeNode(node1, 0, 2)
node3 = TreeNode(data=3)
node4 = TreeNode(data=4)
node5 = TreeNode(node3, node4, 5)
node6 = TreeNode(node2, node5, 6)
node7 = TreeNode(node6, 0, 7)
node8 = TreeNode(data=8)
root = TreeNode(node7, node8, 'root')

     
bt = BTree(root)

print u'''

#生成的二叉樹(shù)

# ------------------------
#          root
#       7        8
#     6
#   2   5
# 1    3 4
#
# -------------------------

'''
print '前序(pre-order,NLR)遍歷 :\n'
bt.preorder(bt.root)

print '中序(in-order,LNR) 遍歷 :\n'
bt.inorder(bt.root)

print '后序(post-order,LRN)遍歷 :\n'
bt.postorder(bt.root)

print '層序(level-order,LRN)遍歷 :\n'
bt.levelorder(bt.root)

相關(guān)文章

  • 使用python實(shí)現(xiàn)CGI環(huán)境搭建過(guò)程解析

    使用python實(shí)現(xiàn)CGI環(huán)境搭建過(guò)程解析

    這篇文章主要介紹了使用python實(shí)現(xiàn)CGI環(huán)境搭建過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • minconda安裝pytorch的詳細(xì)方法

    minconda安裝pytorch的詳細(xì)方法

    這篇文章主要介紹了minconda安裝pytorch的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Python3中urlopen()的用法解讀

    Python3中urlopen()的用法解讀

    這篇文章主要介紹了Python3中urlopen()的用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • python偏函數(shù)partial用法

    python偏函數(shù)partial用法

    這篇文章要給大家分享得是python偏函數(shù)partial用法,主要介紹什么是偏函數(shù)partial、偏函數(shù)的作用、偏函數(shù)的語(yǔ)法及案例詳情,需要的朋友可以參考一下文章得具體詳解,希望對(duì)你有所幫助
    2021-10-10
  • PyCharm 2020.2 安裝詳細(xì)教程

    PyCharm 2020.2 安裝詳細(xì)教程

    這篇文章主要介紹了PyCharm 2020.2 安裝詳細(xì)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Python機(jī)器學(xué)習(xí)入門(三)之Python數(shù)據(jù)準(zhǔn)備

    Python機(jī)器學(xué)習(xí)入門(三)之Python數(shù)據(jù)準(zhǔn)備

    這篇文章主要介紹了Python機(jī)器學(xué)習(xí)入門知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • python實(shí)現(xiàn)從ftp服務(wù)器下載文件的方法

    python實(shí)現(xiàn)從ftp服務(wù)器下載文件的方法

    這篇文章主要介紹了python實(shí)現(xiàn)從ftp服務(wù)器下載文件的方法,涉及Python操作FTP的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • PyTorch中Tensor的維度變換實(shí)現(xiàn)

    PyTorch中Tensor的維度變換實(shí)現(xiàn)

    這篇文章主要介紹了PyTorch中Tensor的維度變換實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)填充復(fù)制實(shí)現(xiàn)示例

    TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)填充復(fù)制實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)如何進(jìn)行數(shù)據(jù)填充復(fù)制的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Python利用PyPDF2庫(kù)實(shí)現(xiàn)輕松提取PDF文本

    Python利用PyPDF2庫(kù)實(shí)現(xiàn)輕松提取PDF文本

    ython中的PyPDF2庫(kù)是一個(gè)非常有用的工具,無(wú)論您是需要分析PDF文檔中的內(nèi)容還是需要在文檔中搜索特定的信息,PyPDF2都可以幫助您輕松實(shí)現(xiàn)這些任務(wù),下面我們就來(lái)學(xué)習(xí)一下如何利用PyPDF2提取PDF文本吧
    2023-09-09

最新評(píng)論