生成可能具有终止[Python]分支的树

时间:2016-05-31 21:39:18

标签: python tree

我试图创建一个脚本,它将按以下方式生成树:如果数字是永远的:分成3个数字:数字// 2,数字* 2,数字+ 15.如果数字是奇数:分为2个数字:数字+1,数字* 4.这些分支继续,直到分支为:大于100,等于6,一个完美的正方形(数字的平方根是一个整数)。我在为不同条件设置2个或3个分支时遇到问题。这是我的代码:

import numpy as np

class Node(object):
    def __init__(self,number,parent):
        self._parent = parent
        self._number = number
        self._satisfied = number > 100 or number == 6 or np.sqrt(number) % 1 == 0
        self._branch1 = None
        self._branch2 = None
        self._branch3 = None
        self._depth = parent.depth + 1 if parent != None else 1

    @property
    def parent(self):
        return self._parent

    @property
    def number(self):
        return self._number

    @property
    def satisfied(self):
        return self._satisfied

    @property
    def depth(self):
        return self._depth

    @property
    def branch1(self):
        return self._branch1

    @branch1.setter
    def branch1(self,value):
        self._branch1 = value

    @property
    def branch2(self):
        return self._branch2

    @branch2.setter
    def branch2(self,value):
        self._branch2 = value

    @property
    def branch3(self):
        return self._branch3

    @branch3.setter
    def branch3(self,value):
        self._branch3 = value

def print_all_chains(node,chain=[]):
    if node.branch1 is None:
        chain.append(node.number)
        print '{0}: {1}'.format(node.satisfied, chain)
    else:
        print_all_chains(node.branch1, chain[:] + [node.number])
        print_all_chains(node.branch2, chain[:] + [node.number])
        print_all_chains(node.branch3, chain[:] + [node.number])

def make_daughters(number):
    if number % 2 == 0: #even
        daughters = [number // 2, number * 2, number + 15]
    else:
        daughters = [number + 1, number * 4, None]
    return daughters

def build_tree(node, maxDepth):
    if not node.satisfied and node.depth<maxDepth:
        daughters = make_daughters(node.number)
        node.branch1 = Node(daughters[0], node)
        build_tree(node.branch1,maxDepth)
        node.branch2 = Node(daughters[1], node)
        build_tree(node.branch2,maxDepth)
        node.branch3 = Node(daughters[2], node)
        build_tree(node.branch3, maxDepth)

def find_decay(number):
    root = Node(number,None)
    build_tree(root,maxDepth=3)
    print_all_chains(root)

if __name__ == '__main__':
    find_decay(int(raw_input('Number: ')))

2 个答案:

答案 0 :(得分:0)

为什么不使用列表来保存分支信息?然后可以用这种方式重写它,而不在两个分支情况下添加类型元素。

daughters = make_daughters(node.number)
node.branch = [Node(d, node) for d in daughters]

无需担心类型元素

答案 1 :(得分:0)

当您make_daughters时,问题出现了奇怪的情况:您在第3个参数中添加None,之后又在创建新的Node并尝试在sqrt上运行None方法。

修改了以下部分以解决此问题:

def print_all_chains(node,chain=[]):
    if node.branch1 is None:
        chain.append(node.number)
        print '{0}: {1}'.format(node.satisfied, chain)
    else:
        if node.branch1:  # print only if it's a valid branch
            print_all_chains(node.branch1, chain[:] + [node.number])
        if node.branch2:  # print only if it's a valid branch
            print_all_chains(node.branch2, chain[:] + [node.number])
        if node.branch3:  # print only if it's a valid branch
            print_all_chains(node.branch3, chain[:] + [node.number])

def make_daughters(number):
    if number % 2 == 0: #even
        daughters = [number // 2, number * 2, number + 15]
    else:
        daughters = [number + 1, number * 4]  # don't send None
    return daughters

def build_tree(node, maxDepth):
    if not node.satisfied and node.depth<maxDepth:
        daughters = make_daughters(node.number)
        node.branch1 = Node(daughters[0], node)
        build_tree(node.branch1,maxDepth)
        node.branch2 = Node(daughters[1], node)
        build_tree(node.branch2,maxDepth)
        if len(daughters) > 2: # make sure you have the third element
            node.branch3 = Node(daughters[2], node)
            build_tree(node.branch3, maxDepth)