• 二叉树与链表


    二叉树

    1. 建立二叉树

    class Node:
        def __init__(self, data, left=None, right=None):
            self.data = data
            self.left = left
            self.right = right
    
    tree = Node(1, Node(3, Node(7, Node(0)), Node(6)), Node(2, Node(5)))

    2. 广度遍历与深度遍历

    广度遍历:

    def lookup(tree):
        if not tree:
            return []
        l = []
        stack = [tree, ]
        while stack:
            node = stack.pop(0)
            l.append(node.data)
            if node.left:
                stack.append(node.left)
            if node.right:
                stack.append(node.right)
        return l

    深度遍历:

    前序遍历:根,左子树,右子树

    def pre(tree):
        l = []
    
        def deep(tree):
            if not tree:
                return
            l.append(tree.data)
            deep(tree.left)
            deep(tree.right)
        deep(tree)
        return l

    中序遍历:左子树,根,右子树

    def mid(tree):
        l = []
    
        def deep(tree):
            if not tree:
                return
            deep(tree.left)
            l.append(tree.data)
            deep(tree.right)
        deep(tree)
        return l

    后序遍历:左子树,右子树,根

    def post(tree):
        l = []
    
        def deep(tree):
            if not tree:
                return
            deep(tree.left)
            deep(tree.right)
            l.append(tree.data)
        deep(tree)
        return l

    3. 已知前序,中序求后序

    def rebuild(pre, mid):
        if not pre:
            return
        data = pre[0]
        tree = Node(data)
        index = mid.index(data)
        tree.left = rebuild(pre[1:index+1], mid[:index])
        tree.right = rebuild(pre[index+1:], mid[index+1:])
        return tree
    
    def post(tree):
        l = []
    
        def deep(tree):
            if not tree:
                return
            deep(tree.left)
            deep(tree.right)
            l.append(tree.data)
        deep(tree)
        return l

    分析:

    前序:[1, 3, 7, 0, 6, 2, 5]

    中序:[0, 7, 3, 6, 1, 5, 2]

    得知:

    1、 根节点为1

    2、 3, 7, 0, 6为左子树的前序

    3、 0, 7, 3, 6为左子树的中序

    4、2, 5为右子树的前序

    5、5, 2为右子树的中序

    根据2和3依此规律可构建一棵左子树

    根据4和5依此规律可构建一棵右子树

    4. 二叉树的最大深度

    def max_depth(tree):
        if not tree:
            return 0
        return max(max_depth(tree.left), max_depth(tree.right)) + 1

    5. 判断两棵二叉树是否相同

    def is_same_tree(tree1, tree2):
        if tree1 and tree2:
            return tree1.data == tree2.data and is_same_tree(tree1.left, tree2.left) and is_same_tree(tree1.right, tree2.right)
        elif not tree1 and not tree2:
            return True
        else:
            return False

    6. 序列化与反序列化

    二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存。

    某种遍历方式是指 可以基于 按层、 先序、中序、后序 的二叉树遍历方式

    某种格式是指 用某种符号表示一个空节点(如#),用某种符号表示结点值的结束(如!)

    def serialize(tree):
        l = []
    
        def deep(tree):
            if not tree:
                l.append('#!')
                return
            l.append('%s!' % tree.data)
            deep(tree.left)
            deep(tree.right)
        deep(tree)
        return ''.join(l)
    
    def deserialize(str):
        l = str.split('!')
    
        def deep(l):
            if l[0] == '#':
                l.pop(0)
                return
            tree = Node(int(l.pop(0)))
            tree.left = deep(l)
            tree.right = deep(l)
            return tree
        return deep(l)

    7. 判断二叉树是否是平衡二叉树

    单链表

    1. 建立链表

    class Node:
        def __init__(self, data):
            self.data = data
            self.next = None
    
    def createLinkList(l):
        if not l:
            return
        head = Node(l.pop(0))
        cur = head
        for data in l:
            node = Node(data)
            cur.next = node
            cur = cur.next
        return head

    2. 遍历链表

    def travel(head):
        cur = head
        while cur:
            print(cur.data)
            cur = cur.next

    3. 单链表逆置

    def rev(head):
        if not head:
            return
        pre = head
        cur = head.next
        pre.next = None
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre

    4. 合并两个有序链表

    def mergeLinkList(head1, head2):
        if not head1:
            return head2
        if not head2:
            return head1
        head = None
        if head1.data < head2.data:
            head = Node(head1.data)
            head1 = head1.next
        else:
            head = Node(head2.data)
            head2 = head2.next
        head.next = mergeLinkList(head1, head2)
        return head
  • 相关阅读:
    2017级面向对象程序设计 作业三
    2017级面向对象程序设计 作业二
    2017级面向对象程序设计 作业一
    寒假作业之总结
    寒假第三次作业
    寒假第二次作业 与电梯有关的代码问题
    我印象中最深刻的三位老师
    Alpha冲刺Day5
    Alpha冲刺Day4
    Alpha冲刺Day3
  • 原文地址:https://www.cnblogs.com/liuxiaowei/p/8111646.html
Copyright © 2020-2023  润新知