将二叉树转换为双链表时python中的静态变量问题 我已经在C++中实现了这个算法。但是我开始学习python,所以我尝试用python实现它。但由于python中没有静态变量的概念,我无法成功地完成它

将二叉树转换为双链表时python中的静态变量问题 我已经在C++中实现了这个算法。但是我开始学习python,所以我尝试用python实现它。但由于python中没有静态变量的概念,我无法成功地完成它,python,binary-tree,static-members,doubly-linked-list,Python,Binary Tree,Static Members,Doubly Linked List,在“convert_to_doubly_ll”方法中,我无法决定如何模拟静态变量“prev” 我检查了许多在python中模拟静态变量的方法,如: 1.将变量声明为全局变量 2.将变量附加到类的方法 3.将变量定义为类变量 4.使函数成为生成器函数 还有一些。。。但我无法在这种情况下应用任何方法 我写的代码如下: import sys class Tree: def __init__(self, v, l=None, r=None): self.v = v

在“convert_to_doubly_ll”方法中,我无法决定如何模拟静态变量“prev”

我检查了许多在python中模拟静态变量的方法,如:
1.将变量声明为全局变量
2.将变量附加到类的方法
3.将变量定义为类变量
4.使函数成为生成器函数
还有一些。。。但我无法在这种情况下应用任何方法

我写的代码如下:

import sys

class Tree: 
    def __init__(self, v, l=None, r=None):
        self.v = v
        self.l = l
        self.r = r

    def convert_to_doubly_ll(self, head=None):  
        static prev = None    # it does not work

        if self.l is not None:
            self.l.convert_to_doubly_ll(head)

        if prev is None:
            head = self     
        self.l = prev
        if prev is not None:
            prev.r = self
        prev = self

        if self.r is not None:
            self.r.convert_to_doubly_ll(head)

        return head

    def print_doubly_ll(self):
        print self.v
        if self.r is not None:
            self.r.print_doubly_ll()
import sys

class Tree: 
    def __init__(self, v, l=None, r=None):
        self.v = v
        self.l = l
        self.r = r

    def get_inorder_tree(self):  
        if not self:
            return

        if self.l:
            for node in self.l.get_inorder_tree():
                yield node
        yield self
        if self.r:  
            for node in self.r.get_inorder_tree():
                yield node

    def convert_to_doubly_ll(self):
        prev = None

        for node in self.get_inorder_tree():
            node.l = prev
            if prev is None:
                head = node
            else:
                prev.r = node
            prev = node
        return head

    def print_doubly_ll(self):
        print self.v
        if self.r is not None:
            self.r.print_doubly_ll()


def main():
    a = Tree(1)
    b = Tree(4)
    c = Tree(6)
    d = Tree(8)
    e = Tree(2, a)
    f = Tree(3, e, b)
    g = Tree(7, c, d)
    h = Tree(5, f, g)

    head = h.convert_to_doubly_ll()
    head.print_doubly_ll()

if __name__ == "__main__":
    main()
驱动程序:

a = Tree(1)
b = Tree(4)
c = Tree(6)
d = Tree(8)
e = Tree(2, a)
f = Tree(3, e, b)
g = Tree(7, c, d)
h = Tree(5, f, g)

head = h.convert_to_doubly_ll()
head.print_doubly_ll()

convert_to doubly_ll
中的
head
tail
的值不会从
None
更改,因为
convert_to doubly_ll_util
不会返回其更新值。您可以重写代码:

class Tree: 
    def __init__(self, v, l=None, r=None):
        self.v = v
        self.l = l
        self.r = r

    def convert_to_doubly_ll_util(self, head, tail):
        if self.l is not None:
            self.l.convert_to_doubly_ll_util(head, tail)

        self.l = tail
        if tail is not None:
            tail.r = self
        tail = self
        if head is None:
            head = tail

        if self.r is not None:
            self.r.convert_to_doubly_ll_util(head, tail)
        return head, tail


    def convert_to_doubly_ll(self):
        head = None
        tail = None
        head, tail = self.convert_to_doubly_ll_util(head, tail)
        return head, tail

    def print_doubly_ll(self):
        print self.v
        if self.r is not None:
            self.r.print_doubly_ll()

经过一番努力,我明白了如何在没有静态变量的情况下以pythonic的方式(使用生成器)编写这个算法。我的代码如下:

import sys

class Tree: 
    def __init__(self, v, l=None, r=None):
        self.v = v
        self.l = l
        self.r = r

    def convert_to_doubly_ll(self, head=None):  
        static prev = None    # it does not work

        if self.l is not None:
            self.l.convert_to_doubly_ll(head)

        if prev is None:
            head = self     
        self.l = prev
        if prev is not None:
            prev.r = self
        prev = self

        if self.r is not None:
            self.r.convert_to_doubly_ll(head)

        return head

    def print_doubly_ll(self):
        print self.v
        if self.r is not None:
            self.r.print_doubly_ll()
import sys

class Tree: 
    def __init__(self, v, l=None, r=None):
        self.v = v
        self.l = l
        self.r = r

    def get_inorder_tree(self):  
        if not self:
            return

        if self.l:
            for node in self.l.get_inorder_tree():
                yield node
        yield self
        if self.r:  
            for node in self.r.get_inorder_tree():
                yield node

    def convert_to_doubly_ll(self):
        prev = None

        for node in self.get_inorder_tree():
            node.l = prev
            if prev is None:
                head = node
            else:
                prev.r = node
            prev = node
        return head

    def print_doubly_ll(self):
        print self.v
        if self.r is not None:
            self.r.print_doubly_ll()


def main():
    a = Tree(1)
    b = Tree(4)
    c = Tree(6)
    d = Tree(8)
    e = Tree(2, a)
    f = Tree(3, e, b)
    g = Tree(7, c, d)
    h = Tree(5, f, g)

    head = h.convert_to_doubly_ll()
    head.print_doubly_ll()

if __name__ == "__main__":
    main()

请粘贴完整的回溯。问题不在您指出的方法中,因为该方法从不访问任何对象的
.v
属性。在python中,所有内容都是通过引用传递的。。。除了不可变的对象。。。由于它们是不可变的……此代码中唯一引用
v
的部分是在
print\u doubly\u ll
方法中,并且仅当您设法将
None
传递给它时。