二进制搜索树 - 查找高度和深度

时间:2016-03-15 13:54:44

标签: python

这是我第一次编码二进制搜索树。 我从网上引用了很多,并尝试了一些代码。 想知道 当我尝试将其打印出来时,为什么这两个代码不起作用并给了我同样的错误

def height(self,node):
    if node is None:
        return 0
    else:
        return max(height(node.left), height(node.right)) + 1
def depth(self, count=0):
    if self.root is None:
        return count
    return max(depth(self.root.left, count+1),
               depth(self.root.right, count+1))

都有相同的错误,表示

"global name height is not define"
"global name depth is not define"

我想知道为什么,因为我正在调用相同的功能

完整代码:

class BST:
    root=None

    def put(self, key, val):
        self.root = self.put2(self.root, key, val)

    def put2(self, node, key, val):
        if node is None:
            #key is not in tree, create node and return node to parent
            return Node(key, val)
        if key < node.key:
            # key is in left subtree
            node.left = self.put2(node.left, key, val)
        elif key > node.key:
            # key is in right subtree
            node.right = self.put2(node.right, key, val)
        else:
            node.val = val
       # node.count = 1 + self.size2(node.left) + self.size2(node.right)
        return node




    # draw the graph
    def drawTree(self, filename):
        # create an empty undirected graph
        G=pgv.AGraph('graph myGraph {}')

        # create queue for breadth first search
        q = deque([self.root])
        # breadth first search traversal of the tree
        while len(q) <> 0:
            node = q.popleft()
            G.add_node(node, label=node.key+":"+str(node.val))
            if node.left is not None:
                # draw the left node and edge
                G.add_node(node.left, label=node.left.key+":"+str(node.left.val))
                G.add_edge(node, node.left)
                q.append(node.left)
            if node.right is not None:
                # draw the right node and edge
                G.add_node(node.right, label=node.right.key+":"+str(node.right.val))
                G.add_edge(node, node.right)
                q.append(node.right)

        # render graph into PNG file
        G.draw(filename,prog='dot')
        os.startfile(filename)

    def createTree(self):
        self.put("F",6)
        self.put("D",4)
        self.put("C",3)
        self.put("B",2)
        self.put("A",1)
        self.put("E",5)
        self.put("I",9)
        self.put("G",7)


     self.put("H",8)
        self.put("J",10)


    def height(self,node):
        if node is None:
            return 0
        else:
            return max(height(node.left), height(node.right)) + 1


    def depth(self, count=0):
        if self.root is None:
            return count
        return max(depth(self.root.left, count+1),
                   depth(self.root.right, count+1))

class Node:
    left = None
    right = None
    key = 0
    val = 0



    def __init__(self, key, val):
        self.key = key
        self.val = val




bst = BST()
bst.createTree()
bst.drawTree("demo.png")
print bst.get("B")
##print bst.size("D")
##print bst.size("F")
print bst.depth("B")

2 个答案:

答案 0 :(得分:1)

Python不会自动将代码范围扩展到本地类:

return max(self.height(node.left), self.height(node.right)) + 1

return max(self.depth(self.root.left, count+1),
           self.depth(self.root.right, count+1))

答案 1 :(得分:0)

您确定BST.height()BST.depth()方法确实属于BST范围吗?看起来它们没有缩进BST类。检查你的间距。