二叉搜索树:NameError

时间:2016-12-15 08:31:18

标签: python python-3.x class binary-search-tree nameerror

我正在尝试构建二进制搜索树类并获取名称错误:

children_num for this children_num == node.children_count()

这是使用children_num的代码:

def delete(self, value):
    '''delete node containing value'''
    node, parent = self.lookup(value)
    if node is not None:
        children_num == node.children_count()
    if children_num == 0:
        # first case- if there are no children you can just remove node
        if parent:
            if parent.leftChild is node:
                parent.leftChild = None
            else:
                parent.rightChild = None
            del node
        else:
            self.value = None
    elif children_num == 1:
        # case 2- if node has 1 child->replace node with its child
        if node.leftChild:
            n = node.leftChild

这是children_count函数:

def children_count(self):
    # returns the number of children: 0 , 1 , 2
    total=0
    if self.leftChild:
        total += 1
    if self.rightChild:
        total += 1
    return total

我尝试在children_num__init__)中输入self.children_num= None,将其设为全局变量(global children_num)并将其定义为children_num==0,没有这些都有效。

0 个答案:

没有答案