White Session - isomorphic -python language tree implementation

Newer and more comprehensive "data structures and algorithms," the update site, more python, go, waiting for you teaching artificial intelligence: < https://www.cnblogs.com/nickchen121/p/11407287.html

First, understand the meaning of problems

Given two trees T1 and T2. If T1 can be interchanged by several times about the child becomes T2, the two trees we call a "homogeneous." Now given the two trees, you determine whether they are isomorphic.

Input format: Input information given binary 2:

  • First given node in the tree of the tree in a row, followed by N rows

  • I-th row corresponding to the i-th node number, the node gives letters stored in its left child node number, the right child node number
  • If the child node is empty, then given in the respective position "-"

As shown below, there are various representation methods, we list the following two:

Second, the idea of ​​solving

A search is to say this, but the thing I read was not fully used method way linked list, so study a little, write a method is entirely a one-way linked list:

In fact, there should be a more elegant way to delete the entire list of methods, such as head set to none, might improve next?

# 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()

Guess you like

Origin www.cnblogs.com/nickchen121/p/11518875.html