二进制搜索树 - 查找大小

时间:2016-03-15 23:54:07

标签: python binary-tree

我想找到具有给定节点的树的大小,该节点将如下所述

print bst.get("B")

然而,当我试图打印时,它一直声明“它只接受1个参数,但是给出了2个” 对不起,有人可以帮助我,因为我对此很陌生。

简要代码是:

def size(self,key):
    temp = self.root
    if (temp == 0):
        return 0
    return 1 + self.size(temp.left) + self.size(temp.right)

def size2(self,n):
    if n is None:
        return 0
    else:
        return 1 + self.size2(n.left) + self.size2(n.right)

完整代码:

import os
import pygraphviz as pgv
from collections import deque

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 size(self,key):
        temp = self.root
        if (temp == 0):
            return 0
        return 1 + self.size(temp.left) + self.size(temp.right)

    def size2(self,n):
        if n is None:
            return 0
        else:
            return 1 + self.size2(n.left) + self.size2(n.right)

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.size("D")

1 个答案:

答案 0 :(得分:0)

我的代码堆栈溢出。我认为您需要在size2方法中使用size

def size(self,key):
    temp = self.root
    if (temp == 0):
        return 0
    return 1 + self.size2(temp.left) + self.size2(temp.right)

就个人而言,我可能不会称之为方法size2,但那是品味(和风格)的问题。此外,key似乎未使用?