当前位置: 移动技术网 > IT编程>脚本编程>Python > 小白专场-树的同构-python语言实现

小白专场-树的同构-python语言实现

2019年09月14日  | 移动技术网IT编程  | 我要评论

嘀动网,1024x768壁纸,计算机基础知识

目录

更新、更全的《数据结构与算法》的更新网站,更有python、go、人工智能教学等着你:<

一、题意理解

给定两棵树t1和t2。如果t1可以通过若干次左右孩子互换就变成t2,则我们称两棵树是“同构的”。现给定两棵树,请你判断它们是否是同构的。

输入格式:输入给出2棵二叉树的信息:

  • 先在一行中给出该树的结点树,随后n行

  • 第i行对应编号第i个结点,给出该结点中存储的字母、其左孩子结点的编号、右孩子结点的编号
  • 如果孩子结点为空,则在相应位置给出“-”

如下图所示,有多种表示的方式,我们列出以下两种:

二、求解思路

搜到一篇也是讲这个的,但是那篇并没有完全用到单向链表的方法,所以研究了一下,写了一个是完全用单向链表的方法:

其实应该有更优雅的删除整个单向列表的方法,比如头设为none,可能会改进下?

# python语言实现

l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))


# 节点
class node:
    def __init__(self, coef, exp):
        self.coef = coef
        self.exp = exp
        self.next = none


# 单链表
class list:
    def __init__(self, node=none):
        self.__head = node

    # 为了访问私有类
    def gethead(self):
        return self.__head

    def travel(self):
        cur1 = self.__head
        cur2 = self.__head
        if cur1.next != none:
            cur1 = cur1.next
        else:
            print(cur2.coef, cur2.exp, end="")
            return
        while cur1.next != none:
            print(cur2.coef, cur2.exp, end=" ")
            cur1 = cur1.next
            cur2 = cur2.next

        print(cur2.coef, cur2.exp, end=" ")
        cur2 = cur2.next
        print(cur2.coef, cur2.exp, end="")

    # add item in the tail
    def append(self, coef, exp):
        node = node(coef, exp)
        if self.__head == none:
            self.__head = node
        else:
            cur = self.__head
            while cur.next != none:
                cur = cur.next
            cur.next = node


def addl(l1, l2):
    p1 = l1.gethead()
    p2 = l2.gethead()
    l3 = list()
    while (p1 is not none) & (p2 is not none):
        if (p1.exp > p2.exp):
            l3.append(p1.coef, p1.exp)
            p1 = p1.next
        elif (p1.exp < p2.exp):
            l3.append(p2.coef, p2.exp)
            p2 = p2.next
        else:
            if (p1.coef + p2.coef == 0):
                p1 = p1.next
                p2 = p2.next
            else:
                l3.append(p2.coef + p1.coef, p1.exp)
                p2 = p2.next
                p1 = p1.next
    while p1 is not none:
        l3.append(p1.coef, p1.exp)
        p1 = p1.next
    while p2 is not none:
        l3.append(p2.coef, p2.exp)
        p2 = p2.next
    if l3.gethead() == none:
        l3.append(0, 0)
    return l3


def mull(l1, l2):
    p1 = l1.gethead()
    p2 = l2.gethead()
    l3 = list()
    l4 = list()
    if (p1 is not none) & (p2 is not none):
        while p1 is not none:
            while p2 is not none:
                l4.append(p1.coef * p2.coef, p1.exp + p2.exp)
                p2 = p2.next
            l3 = addl(l3, l4)
            l4 = list()
            p2 = l2.gethead()
            p1 = p1.next
    else:
        l3.append(0, 0)
    return l3


def l2l(l):
    l = list()
    l.pop(0)
    for i in range(0, len(l), 2):
        l.append(l[i], l[i + 1])
    return l


l1 = l2l(l1)
l2 = l2l(l2)
l3 = list()
l3 = mull(l1, l2)
l3.travel()
print("")
l3 = list()
l3 = addl(l1, l2)
l3.travel()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网