• 数据结构:二叉树(七)


    一、二叉树的定义

    1、二叉树的链式存储

    将二叉树的节点定义为一个对象,节点之间通过类似链表的链接方式来连接

    2、节点的定义

    class BittreeNode:
         def __init__(self,data):
             self.data = data
             self.lchild = None
             self.rchild = None

    3、二叉树图

    二、前序遍历

    1、实现代码

    class BittreeNode:
         def __init__(self,data):
             self.data = data
             self.lchild = None
             self.rchild = None
    
    
    a = BittreeNode('A')
    b = BittreeNode('B')
    c = BittreeNode('C')
    d = BittreeNode('D')
    e = BittreeNode('E')
    f = BittreeNode('F')
    g = BittreeNode('G')
    
    e.lchild = a
    e.rchild = g
    a.rchild = c
    c.lchild = b
    c.rchild = d
    g.rchild = f
    
    root = e
    
    def pre_order(root):
        if root:
            print(root.data)
            pre_order(root.lchild)
            pre_order(root.rchild)
    
    pre_order(root)

    2、输出

    "D:Program FilesPython35python3.exe" E:/test/bitree.py
    E
    A
    C
    B
    D
    G
    F
    
    Process finished with exit code 0
    

    实现代码

    def pre_order(root):
        if root:
            print(root.data,end='')
            pre_order(root.lchild)
            pre_order(root.rchild)
    		
    pre_order(root)

    输出

    "D:Program FilesPython35python3.exe" E:/test/bitree.py
    EACBDGF
    Process finished with exit code 0

    三、中序遍历

    1、实现代码

    class BittreeNode:
         def __init__(self,data):
             self.data = data
             self.lchild = None
             self.rchild = None
    
    
    a = BittreeNode('A')
    b = BittreeNode('B')
    c = BittreeNode('C')
    d = BittreeNode('D')
    e = BittreeNode('E')
    f = BittreeNode('F')
    g = BittreeNode('G')
    
    e.lchild = a
    e.rchild = g
    a.rchild = c
    c.lchild = b
    c.rchild = d
    g.rchild = f
    
    root = e
    
    def pre_order(root):
        if root:
            print(root.data,end='')
            pre_order(root.lchild)
            pre_order(root.rchild)
    def in_order(root):
        if root:
            in_order(root.lchild)
            print(root.data,end='')
            in_order(root.rchild)
    
    pre_order(root)
    print("")
    in_order(root)

    2、输出

    "D:Program FilesPython35python3.exe" E:/test/bitree.py
    EACBDGF
    ABCDEGF
    Process finished with exit code 0

    四、后序遍历

    1、实现代码

    class BittreeNode:
         def __init__(self,data):
             self.data = data
             self.lchild = None
             self.rchild = None
    
    
    a = BittreeNode('A')
    b = BittreeNode('B')
    c = BittreeNode('C')
    d = BittreeNode('D')
    e = BittreeNode('E')
    f = BittreeNode('F')
    g = BittreeNode('G')
    
    e.lchild = a
    e.rchild = g
    a.rchild = c
    c.lchild = b
    c.rchild = d
    g.rchild = f
    
    root = e
    
    def pre_order(root):
        if root:
            print(root.data,end='')
            pre_order(root.lchild)
            pre_order(root.rchild)
            
    def in_order(root):
        if root:
            in_order(root.lchild)
            print(root.data,end='')
            in_order(root.rchild)
            
            
    def post_order(root):
        if root:
            post_order(root.lchild)
            post_order(root.rchild)
            print(root.data, end='')
            
    
    pre_order(root)
    print("")
    in_order(root)
    print("")
    post_order(root)

    2、输出

    "D:Program FilesPython35python3.exe" E:/BaiduNetdiskDownload/0921/test/bitree.py
    EACBDGF
    ABCDEGF
    BDCAFGE
    Process finished with exit code 0

    五、层次遍历

    1、实现代码

    from collections import deque
    
    class BittreeNode:
        def __init__(self, data):
            self.data = data
            self.lchild = None
            self.rchild = None
    
    
    a = BittreeNode('A')
    b = BittreeNode('B')
    c = BittreeNode('C')
    d = BittreeNode('D')
    e = BittreeNode('E')
    f = BittreeNode('F')
    g = BittreeNode('G')
    
    e.lchild = a
    e.rchild = g
    a.rchild = c
    c.lchild = b
    c.rchild = d
    g.rchild = f
    
    root = e
    
    
    def pre_order(root):
        if root:
            print(root.data, end='')
            pre_order(root.lchild)
            pre_order(root.rchild)
    
    
    def in_order(root):
        if root:
            in_order(root.lchild)
            print(root.data, end='')
            in_order(root.rchild)
    
    
    def post_order(root):
        if root:
            post_order(root.lchild)
            post_order(root.rchild)
            print(root.data, end='')
    
    
    def level_order(root):
        queue = deque()
        queue.append(root)
        while len(queue) > 0:
            node = queue.popleft()
            print(node.data,end='')
            if node.lchild:
                queue.append(node.lchild)
            if node.rchild:
                queue.append(node.rchild)
    
    pre_order(root)
    print("")
    in_order(root)
    print("")
    post_order(root)
    print("")
    level_order(root)

    2、输出

    "C:Program FilesPython35python.exe" E:/test/btree.py
    EACBDGF
    ABCDEGF
    BDCAFGE
    EAGCFBD
    Process finished with exit code 0
    

      

  • 相关阅读:
    Linux各主要发行版的包管理命令对照
    JDK 生成数字证书
    AbatorForEclipse插件使用总结
    [转载]在rhel 6 x86_64 上安装oracle 11g xe
    【转载】PL/SQL配置连接ORACLE
    Archlive新年第一棒: 基于2.6.37稳定内核的archlive20110107
    基于Arch的live系统
    【转】MyEclipse 9.0正式版官网下载(附Win+Llinux激活方法、汉化包)
    Exception in thread main java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFacto
    MyEclipse 8.6插件下载
  • 原文地址:https://www.cnblogs.com/luoahong/p/9705950.html
Copyright © 2020-2023  润新知