我试图打印出我的二叉树的预顺序形式,但我遇到这些错误。我还在学习蟒蛇,所以我不太清楚发生了什么。但我想我的打印功能不能正常工作。不太清楚为什么preorder_print会出现全局名称问题,不过=/
输出:
>>> from BST_tree import *
pre order:
<BST_tree.Node instance at 0x0000000002AA0C88>
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
有人能帮我解决这个问题吗?我在python中实现了一个二进制搜索树数据结构,并编写了一个BST_height()函数来计算树的高度。但是当我运行我的代码时,它给了我一个错误,它说‘自我不是定义的’。我知道为什么会出现错误,但您能建议一些其他方法来运行根节点的BST_height函数吗?
class Node:
def __init__(self, data=None):
self.data = data
self.left = None
self.right = None
class BinarySearchTree:
d
如何求和bst (二进制搜索树)中的值,我尝试此代码,但给我错误
def total(bst):
total = 0
bst = BST()
node = bst._root
if node == None:
total = 0
if node._right is not None and node._left is not None:
total = total + node._right + node._left
elif node._right is not None and node._left is No
这是我为不同的家庭作业写的辅助程序。- Full Disclosure -我不需要上交这段代码,但我正在使用有效的BST实现来验证我的顺序遍历、前序遍历和后序遍历以完成家庭作业。遍历工作得很好,但是当我在传递的值不存在时调用remove(int)时,我得到了一个完全不相关的错误。当传递的值存在时,一切都会正常工作。我的删除操作由三个辅助函数组成: remove、deleteNode和makeDeletion。我想让这个bst类具有完整的功能。谢谢。
// remove(int) is a private function called from main
// when num
我已经用Add()方法编写了一个二进制搜索树。但它不起作用。当我将数字添加到树中时,root仍然是空的。为什么?
编辑:粘贴箱上的代码,在这里我遇到了一些显示的问题
using System;
namespace bst
{
public class Node
{
public int value;
public Node Right = null;
public Node Left = null;
public Node(int value)
{
this.value
我对编程相当陌生,我想搞一些二进制搜索树。我想要创建一个递归计算树中节点数的函数,但是,当我运行我的函数时,它似乎不起作用,它一直返回'none‘,就好像我的树中什么都没有一样。有人能帮我找到这里的问题吗?
这是我的TreeNode课程:
class TreeNode(object):
def __init__(self, data = None, left=None, right=None):
self.item = data
self.left = left
self.right = right
def __str
我一直收到这个错误,我不知道我该如何去纠正它。我正在尝试在类BST中实现方法count_less。我在_BSTNode类中编写了一个帮助器方法,并在count_less中使用间接递归调用该帮助器。
谢谢。
class BST:
"""A Binary Search Tree."""
def __init__(self: 'BST', container: list =[]) -> None:
"""
Initialize this BST by ins
from random import randint
class Node:
def __init__(self, value=None, left_child=None, right_child=None):
self.value = value
self.left_child = left_child
self.right_child = right_child
class BinarySearchTree:
def __init__(self):
self.root = Node()
de
我的BST类建筑如下:
class BST:
"""A Binary Search Tree."""
def __init__(self: 'BST', container: list =None) -> None:
"""
Initialize this BST by inserting the items from container (default [])
one by one, in the order given.
"""
我正在尝试创建一个二进制搜索树,但它给了我一个TypeError。如何将第二个参数(node)作为对象本身传递给insert(),因为第一个参数(self)总是BSTNode的实例。
# Binary Search Tree
# Create a BST and insert elements and print Inorder traversal
class BSTNode(object):
def __init__(self, key, left=None, right=None):
self.left = left
self.right = r
我有一个函数,它计算少于二进制搜索tree.It中的项的数量。但我就是不明白为什么局部变量count能记住总数,因为每次递归调用,它都被重置为0。
def count_less(self, item):
"""(BST, object) -> int
Return the number of items in BST that less than item.
"""
return BST.count_less_helper(self.root, item)
# Recursive helper funct
我目前正在尝试检查树是否为BST,同时注意到这些值不能等于树中的任何其他值。我尝试计算每个值应该达到的时间间隔(考虑将最小值和最大值分别作为arg和arg1)。例如,如果我们在左子树上一直往下走,就不会有min,只有max。然而,当我们切换到右边时,我们也会有一个最小值(我们刚刚切换到右边的根节点的值)。但是,我的代码没有显示正确的答案,我不知道原因。你能帮帮我吗?这些是我的函数:(我在hackerrank上解决这个问题,因此这就是为什么我有两个函数而不是一个) """ Node is defined as
class node:
def __init__(sel
我试图获得一个二叉树的最大深度,但我相信树是错误的。
我的代码:
def BST_maxdepth(root):
curdepth = [1]
maxdepth = [1]
if root is None:
return -1
else:
curdepth[0] = 1
maxdepth[0] = 1
if root.left is not None or root.right is not None:
所以我一直在做这个类项目,实现一个二进制搜索树。教授希望我们使私有递归,而使公共递归简单。(就像when to insert_element(50)一样,它调用一个私有函数recursive_insert(50,self.__root)来求解)。
我的插入函数运行时没有错误,但是测试用例总是返回空的,下面是我的私有函数的代码:
class Binary_Search_Tree:
class __BST_Node:
def __init__(self, value):
self.value = value
self.left=None
self.right=None