如何修复计数功能

时间:2017-02-27 02:35:24

标签: python recursion binary-tree

class TN:
    def __init__(self,value,left=None,right=None):
        self.value = value
        self.left  = left
        self.right = right

def list_to_tree(alist):
    if alist == None:
        return None
    else:
        return TN(alist[0],list_to_tree(alist[1]),list_to_tree(alist[2])) 

def str_tree(atree,indent_char ='.',indent_delta=2):
    def str_tree_1(indent,atree):
        if atree == None:
            return ''
        else:
            answer = ''
            answer += str_tree_1(indent+indent_delta,atree.right)
            answer += indent*indent_char+str(atree.value)+'\n'
            answer += str_tree_1(indent+indent_delta,atree.left)
            return answer
    return str_tree_1(0,atree) 

def count(t,value):
    nodes = []
    num = 0
    nodes.append(t)
    while len(nodes) > 0:
        if nodes[0] == value:
            num += 1
        next = nodes.pop(0)
        count(next,value)
    return num

我需要写一个递归函数计数(其他三个函数不能改变);它传递平衡二叉树和值作为参数。它返回值在树中的次数。在下面的二叉树中,count(tree,1)返回1,count(tree,2)返回2,count(tree,3)返回4

..1
....3
3
....3
..2
......2
....3

我调用了以下函数

tree = list_to_tree([3, [2, None, [3, None, None]], [1, [3, None, None], None]])
print('\nfor tree = \n',str_tree(tree))
for i in irange(1,3):
    print('count(tree,'+str(i)+') = ', count(tree,i))

但它显示错误" RecursionError:比较超过最大递归深度" 有人可以帮我修复计数功能吗?提前致谢。

2 个答案:

答案 0 :(得分:1)

如果你仔细查看你的代码,你会看到你设置了一个空节点列表,用t填充它,所以while循环总是输入你总是会弹出到下一个并且总是精确调用这个函数相同的参数。那当然是无限递归。

以下是一种正确设置的简单方法:

    [DllImport(obs-frontend-api.dll, CallingConvention = CallingConvention.Cdecl)]
    public static extern void obs_frontend_recording_start();

    [DllImport(obs-frontend-api.dll, CallingConvention = CallingConvention.Cdecl)]
    public static extern void obs_frontend_recording_stop();

答案 1 :(得分:0)

另一种方法是使用横向,在典型的树中有一个根和一个节点子类。你的树缺少那个结构,所以它看起来有点奇怪。要使用横向我使用全局变量来跟踪计数器。

class TN:
    def __init__(self,value,left=None,right=None):
        self.value = value
        self.left  = left
        self.right = right

def list_to_tree(alist):
    if alist == None:
        return None
    else:
        return TN(alist[0],list_to_tree(alist[1]),list_to_tree(alist[2])) 

def str_tree(atree,indent_char ='.',indent_delta=2):
    def str_tree_1(indent,atree):
        if atree == None:
            return ''
        else:
            answer = ''
            answer += str_tree_1(indent+indent_delta,atree.right)
            answer += indent*indent_char+str(atree.value)+'\n'
            answer += str_tree_1(indent+indent_delta,atree.left)
            return answer
    return str_tree_1(0,atree) 


NUM = 0
def count(t,value):
    global NUM
    NUM = 0
    if t != None:
      if t.left == None and t.right == None:
        if t.value == value:
          return 1
        else:
          return 0
      else:
        _count(t, value)
    return NUM


def _count(t, value):
    if t.left != None:
        _count(t.left, value)

    if t.value == value:
        global NUM
        NUM += 1

    if t.right != None:
        _count(t.right, value)


tree = list_to_tree([3, [2, None, [3, None, None]], [1, [3, None, None], None]])
print(str_tree(tree))
print("count 1", count(tree, 1))
print("count 2", count(tree, 2))
print("count 3", count(tree, 3))