在python中实现二叉搜索树?
我试图在python中为BST执行insert函数,但我只是对如何正确访问公共方法感到困惑,这让我有些难过,现在当我测试它时,它只是在第一次测试时停止,并说nonetype对象没有属性数据,但当t=tree()时,我应该如何访问数据树没有数据构造函数在python中实现二叉搜索树?,python,class,binary-search-tree,public,Python,Class,Binary Search Tree,Public,我试图在python中为BST执行insert函数,但我只是对如何正确访问公共方法感到困惑,这让我有些难过,现在当我测试它时,它只是在第一次测试时停止,并说nonetype对象没有属性数据,但当t=tree()时,我应该如何访问数据树没有数据构造函数 class Node(object): def __init__(self, data): self.parent = None self.left = None self.right = None
class Node(object):
def __init__(self, data):
self.parent = None
self.left = None
self.right = None
self.data = data
class Tree(object):
# Binary Search Tree
# class constants
PREORDER = 1
INORDER = 2
POSTORDER = 3
def __init__(self):
# Do not create any other private variables.
# You may create more helper methods as needed.
self.root = None
def print(self):
# Print the data of all nodes in order
self.__print(self.root)
def __print(self, curr_node):
# Recursively print a subtree (in order), rooted at curr_node
if curr_node is not None:
self.__print(curr_node.left)
print(str(curr_node.data), end=' ') # save space
self.__print(curr_node.right)
def insert(self, data):
# Find the right spot in the tree for the new node
# Make sure to check if anything is in the tree
# Hint: if a node n is null, calling n.getData() will cause an error
root = Node(data)
print("this is my", self.root)
if self.root is None:
self.root = root
return Node(data)
else:
if root.data == data:
return root
elif root.data < data:
root.right = insert(root.right,data)
else:
root.left = insert(root.left, data)
return root
提供了两种选择
tree_insert_with_individual_check
this is my None
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
文件labe3.py
输出
tree_insert_with_individual_check
this is my None
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
tree\u insert\u带有单个检查
这是我的
这是我的
这是我的
这是我的
这是我的
这是我的
这是我的
.
----------------------------------------------------------------------
在0.001s内运行1次测试
好啊
您介意修复代码上的缩进吗。很难说什么是函数,什么是方法。肯定是编辑了它!不幸的是,这是家庭作业的一部分,我很确定我不允许像那样将insert函数切换到node中。有没有其他方法可以访问节点函数——测试用例表明应该如何访问它?@PatTheTrickster——添加了两个选项,将功能保留在树中。第一个选项类似于实现打印以遍历树的方式。
class Node(object):
def __init__(self, data):
self.parent = None
self.left = None
self.right = None
self.data = data
class Tree(object):
# Binary Search Tree
# class constants
PREORDER = 1
INORDER = 2
POSTORDER = 3
def __init__(self):
# Do not create any other private variables.
# You may create more helper methods as needed.
self.root = None
def print(self):
# Print the data of all nodes in order
self.__print(self.root)
def __print(self, curr_node):
# Recursively print a subtree (in order), rooted at curr_node
if curr_node is not None:
self.__print(curr_node.left)
print(str(curr_node.data), end=' ') # save space
self.__print(curr_node.right)
def insert(self, d):
print("this is my", self.root)
if self.root is None:
self.root = Node(d)
else:
# current node
current = self.root
# Finds node to add data
while True:
if current.data > d:
if current.left == None:
current.left = Node(d)
break
else:
current = current.left
elif current.data < d:
if current.right == None:
current.right = Node(d)
break
else:
current = current.right
else:
break
import lab3
import unittest
class T0_tree__insert(unittest.TestCase):
def test_balanced_binary_search_tree(self):
print("\n")
print("tree_insert_with_individual_check")
t = lab3.Tree()
t.insert(4)
t.insert(2)
t.insert(6)
t.insert(1)
t.insert(3)
t.insert(5)
t.insert(7)
#The following check is without using tree as an iterator (which uses inorder traversal)
#So this function also does not check the implementation of the traversal function
self.assertEqual(t.root.data, 4)
self.assertEqual(t.root.left.data, 2)
self.assertEqual(t.root.left.left.data, 1)
self.assertEqual(t.root.left.right.data, 3)
self.assertEqual(t.root.right.data, 6)
self.assertEqual(t.root.right.left.data, 5)
self.assertEqual(t.root.right.right.data, 7)
print("\n")
if __name__ == '__main__':
unittest.main()
tree_insert_with_individual_check
this is my None
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
this is my <lab3.Node object at 0x7fa4386b92e0>
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK