将二进制树转换为双向链表时python中的静态变量问题

时间:2014-08-20 16:40:41

标签: python binary-tree static-members doubly-linked-list

我已经用C ++实现了这个算法。但我开始学习python,所以我试图在python中实现它。但由于python中没有静态变量的概念,我无法成功完成它。

在方法" 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()

驱动程序:

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

2 个答案:

答案 0 :(得分:1)

headtail的值不会从None convert_to_doubly_ll更改,因为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()

答案 1 :(得分:0)

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

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