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


    一、二叉树的定义

    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
    

      

  • 相关阅读:
    为了抓包某APP所做的尝试(to be continued)
    VirtualBox的使用的一些Tips 网络配置|硬盘扩充
    斜线和反斜线简要历史,为什么windows和unix采用不同的路径分隔符
    求出二维数组主对角线、次对角线以及周边元素之和
    C#计算两个时间的时间差,精确到年月日时分秒
    C#获取MP3,WMA信息
    C#窗体随意移动
    DEV GridControl小结
    DEV 皮肤的使用
    C#窗体阴影
  • 原文地址:https://www.cnblogs.com/luoahong/p/9705950.html
Copyright © 2020-2023  润新知