Python 二叉搜索树中的节点计数器
基本上。。。我正在尝试使用我的“count”方法来计算树中有多少节点。。。但是递归不起作用。。我该怎么做Python 二叉搜索树中的节点计数器,python,Python,基本上。。。我正在尝试使用我的“count”方法来计算树中有多少节点。。。但是递归不起作用。。我该怎么做 ''' Created on Jul 11, 2013 To practice building native recursion things @author: bailey ''' class AllWords : def __init__(self): self.my_tree = Tree() def add(self, fresh_word): self.m
'''
Created on Jul 11, 2013
To practice building native recursion things
@author: bailey
'''
class AllWords :
def __init__(self):
self.my_tree = Tree()
def add(self, fresh_word):
self.my_tree.insert(fresh_word)
def __str__(self):
return str(self.my_tree)
class Tree :
def __init__(self):
self.root = Blob()
self.size = 0 # initialising size to be zero
self.tutti = "" # to hold all content data
self.left_edges = 0
self.right_edges = 0
self.x = 0
self.b = 0
def __str__(self):
if self.is_empty() :
return "This tree is empty"
else : # so the tree at least has something in the root
self.tutti += "This tree has depth = " + str(self.get_depth())
self.tutti += ", and contains the " + str(self.size) + " objects:\n"
self.tutti += ", and has " + str(self.x) + " nodes \n"
self.tutti += "This tree has " + str(self.left_edges) + " edges on left.\n"
self.tutti += "This tree has " + str(self.right_edges) + " edges on right.\n"
self.tutti += "This tree has " + str(self.edge_stats()) + " edges in total.\n"
self.grab_everything(self.root) # start at the root
return self.tutti
def grab_everything(self, my_blob):
if not my_blob.left_is_empty() : # if there's something on the left
self.grab_everything(my_blob.left)
self.tutti = self.tutti + str(my_blob.data) + ", " # update tutti
if not my_blob.right_is_empty() : # if there's something on the right
self.grab_everything(my_blob.right)
def is_empty(self):
return self.size == 0
def insert(self, something):
if self.is_empty() : # put the something at the root
self.root = Blob(something)
self.size = 1
else : # find where to put it by starting search at the root
self.insert_at_blob(something, self.root)
self.size += 1
def insert_at_blob(self, something, blob):
if something < blob.data : # look left
if blob.left_is_empty() :
blob.set_left( Blob(something) )
else : # keep looking to the left
self.insert_at_blob(something, blob.left)
else : # look right
if blob.right_is_empty() :
blob.set_right( Blob(something) )
else : # keep looking to the right
self.insert_at_blob(something, blob.right)
def get_depth(self): # depth is max number of edges from root outwards
if self.is_empty() :
return -1 # my choice of answer if there's nothing there
else : # note: will define a root-only tree to have depth 0
return self.get_subdepth(self.root)
def get_subdepth(self, blob):
if not blob.left_is_empty() :
left_depth = self.get_subdepth(blob.left)
else :
left_depth = -1 # since that node is empty
if not blob.right_is_empty() :
right_depth = self.get_subdepth(blob.right)
else :
right_depth = -1 # since that node is empty
return max(left_depth, right_depth) + 1
def count_left_only(self):
if not self.root.left_is_empty():
self._count_left_only(self.root.left)
else :
print("There are no left edges.")
def _count_left_only(self, blob):
if not blob.left_is_empty():
self._count_left_only(blob.left)
self.left_edges += 1
def count_right_only(self):
if not self.root.right_is_empty():
self._count_right_only(self.root.right)
else :
print("There are no right edges.")
def _count_right_only(self, blob):
if not blob.right_is_empty():
self._count_right_only(blob.right)
self.right_edges += 1
def edge_stats(self):
return self.left_edges + self.right_edges
def count(self, blob):
if blob == None:
return(0)
if not blob.left_is_empty()and not blob.right_is_empty():
self.x = self.x + 1
else:
return (1 + self.count(blob.left) + self.count(blob.right))
class Blob : # a node class to hold data in a binary tree
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
def set_data(self, thing):
self.data = thing
def set_left(self, blob):
self.left = blob
def set_right(self, blob):
self.right = blob
def left_is_empty(self):
return self.left is None
def right_is_empty(self):
return self.right is None
def __str__(self):
return str(self.data)
import Searching
tout = Searching.AllWords()
tout.add(20)
tout.add(15)
tout.add(35)
tout.add(17)
tout.add(33)
tout.add(12)
tout.add(43)
tout.my_tree.count(tout)
tout.my_tree.count_right_only()
tout.my_tree.count_left_only()
print( str(tout) )
“”
创建于2013年7月11日
练习构建本机递归
@作者:贝利
'''
课堂用语:
定义初始化(自):
self.my_tree=tree()
def添加(自身、新单词):
self.my_tree.insert(新单词)
定义(自我):
return str(self.my_树)
类树:
定义初始化(自):
self.root=Blob()
self.size=0#初始化大小为零
self.tutti=“#保存所有内容数据
self.left_边=0
self.right_边=0
self.x=0
self.b=0
定义(自我):
如果self.is_为空():
return“此树为空”
否则:#那么这棵树的根里至少有东西
self.tutti+=“此树具有深度=“+str(self.get_depth())
self.tutti+=”,并包含“+str(self.size)+”对象:\n
self.tutti+=”,并具有“+str(self.x)+”节点\n
self.tutti+=“此树的左侧有“+str(self.left_边)+”边。\n”
self.tutti+=“此树的右侧有“+str(self.right_边)+”边。\n”
self.tutti+=“此树总共有“+str(self.edge_stats())+”条边。\n”
self.grab_一切(self.root)#从根开始
返回self.tutti
def抓取所有东西(自我,我的斑点):
如果不是我的_blob.left _是_empty():#如果左边有什么东西
赛尔夫。抓住所有东西(我的小滴。左)
self.tutti=self.tutti+str(my_blob.data)+“,”更新tutti
如果不是我的,右边是空的:,如果右边有什么东西
赛尔夫,抓住所有东西(我的右图)
def为空(自身):
返回self.size==0
def插入(自我,某物):
如果self.is_empty():#将某物放在根上
self.root=Blob(某物)
self.size=1
else:#通过从根目录开始搜索,找到放置它的位置
self.insert_at_blob(something,self.root)
self.size+=1
def insert_at_blob(自我、某物、blob):
如果有什么东西
我得到0,但我应该得到7,树有这个方法,但所有单词都没有。您正在
Tree.count()
的tout.my_Tree.count(tout)
行上以blob
的形式传递AllWords
对象,因为您已声明tout=search.AllWords()
。你可能想把它做成一棵树
tout.my_tree.count(tout)
您正在传递AllWords的一个实例“tout”,但count调用righty\u isempty和left\u is\u empty方法,这些方法在AllWords上不存在,因此出现错误。请尝试:
tout.my_tree.count(tout.my_tree.root)
因为
tout
不是Blob
Tree
的实例,所以AllWords
没有这个方法。你在树.count()
中将AllWords
对象作为blob
传递给Tree.count()
。也不要通过==
与singletonNone
进行比较,但在count
方法中使用是的。它现在起作用了,但我的方法没有达到预期的效果…:@好吧,现在是你编辑你的文章的好时机