简体   繁体   English

Python 3:二进制搜索树无法设置节点

[英]Python 3: Binary Search Tree failed to set nodes

so I have been working on this class project implementing a binary search tree. 因此,我一直在从事实施二叉搜索树的此类项目。 The professor wants us to make the private recursive while make the public one simple. 教授希望我们使私有递归同时使公众变得简单。 (like when to insert_element(50), it calls a private function recursive_insert(50, self.__root) to solve). (就像何时插入元素(50)一样,它调用私有函数recursive_insert(50,self .__ root)来解决)。

My insertion function runs for no error yet the test case always return empty, and here are my codes for the private functions: 我的插入函数运行没有错误,但是测试用例总是返回空,这是我的私有函数代码:

class Binary_Search_Tree:


 class __BST_Node:

 def __init__(self, value):
  self.value = value
  self.left=None   
  self.right=None

def __init__(self):
  self.__root = None
  self.__height=0
  self.__size=0

def _in_order_str(self, root):
  if root is None:
    outcome= "[ ]"
  elif self.__size==1:
    outcome = "[ " + str(root.value) + " ]"
  else:
    outcome = "[ "
    self._in_order_str(root.left)
    outcome += str(root.value) +", "
    self._in_order_str(root.right)
    outcome+= " ]"
  return outcome

def _recur_ins(self, val,root):
  if root is None:
    root=Binary_Search_Tree.__BST_Node(val)
  elif root.value>val:
    root.left = _recur_ins(val,root.left) #do I need self here?
  elif root.value <val:
    root.right = _recur_ins(val,root.right)
  return root

And this one is for the public: 这是面向公众的:

def insert_element(self, value):
  self._recur_ins(value,self.__root)
  self.__size+=1 

My Test Case: 我的测试用例:

  def test_insertion_from_empty(self):
    root=None
    self.__bst.insert_element(50)
    self.__bst.insert_element(30)
    self.__bst.insert_element(70)
    self.assertEqual('[ 30, 50, 70 ]', self.__bst.in_order())

UPDATE: I think the problem comes from my _in_order_str(self, root): method. 更新:我认为问题来自我的_in_order_str(self, root):方法。 The general case I found online is: 我在网上发现的一般情况是:

def inorder(root):
    if root is not None:
        inorder(root.left)
        print root.key
        inorder(root.right)

I know this could be a very silly question, but I really failed to figure it our by myself. 我知道这可能是一个非常愚蠢的问题,但是我真的没有自己一个人来解决。 Any help will be appreciated so thank you so much!!! 任何帮助将不胜感激,非常感谢您!!!

After changing your code as little as possible, I think I have managed to get it working. 在尽可能少地更改您的代码之后,我想我已经设法使其正常工作。

from pprint import pprint  # For debugging

class Binary_Search_Tree:
    class __BST_Node:
        def __init__(self, value):
            self.value = value
            self.left = None
            self.right = None

    def __init__(self):
        self.__root = None
        self.__height = 0
        self.__size = 0

    def __in_order_str(self, root):
        if root is None:
            outcome = "[ ]"
        elif self.__size == 1:
            outcome = "[ " + str(root.value) + " ]"
        else:
            outcome = "[ "
            self.__in_order_str(root.left)
            outcome += str(root.value) + ", "
            self.__in_order_str(root.right)
            outcome += " ]"
        return outcome

    def __recur_ins(self, val, root):
        if root is None:
            root = Binary_Search_Tree.__BST_Node(val)
        elif root.value > val:
            root.left = self.__recur_ins(val, root.left)
        elif root.value < val:
            root.right = self.__recur_ins(val, root.right)
        return root

    def insert_element(self, value):
        self.__root = self.__recur_ins(value, self.__root)
        self.__size += 1

    def test_insertion_from_empty(self):
        self.insert_element(50)
        self.insert_element(60)
        self.insert_element(70)
        # self.assertEqual('[ 30, 50, 70 ]', self.__bst.in_order())


test = Binary_Search_Tree()
test.test_insertion_from_empty()
pprint(vars(test))

Notes on the changes: 更改说明:

  • changed some functions(_recur_ins, _in_order_str) from using '_' to '__' to make them private functions. 将某些函数(_recur_ins,_in_order_str)从使用“ _”更改为“ __”以使其成为私有函数。 I did it based on Python Official Documentation , private functions use at least two leading underscores and at most one trailing underscore. 我是基于Python官方文档编写的 ,私有函数至少使用两个下划线,最多使用一个下划线。

  • First line in insert_element, added 'self.__root= ' so that the returned root value will be stored as the new root 在insert_element的第一行中,添加了'self .__ root =',以便将返回的根值存储为新的根

  • Added 'self.' 添加了“自我”。 in front of '__recur_ins', since as far as I know, you must use self whenever you need to call a function which is located at the same class. 就'__recur_ins'而言,因为据我所知,每当需要调用位于同一类的函数时,都必须使用self。
  • I did not modify anything much in __in_order_str, since I think the author only asked for the insertion (?) 我没有对__in_order_str进行任何修改,因为我认为作者只要求插入(?)
  • commented assertEqual, since no function is provided in the question (?) 评论了assertEqual,因为问题(?)中没有提供任何功能
  • Modified the spaces so that it can be more readable 修改了空格,使其更具可读性

If I put the debug mode right before I dumped the variable, this is what I get: 如果在转储变量之前将调试模式设置为正确,这就是我得到的: 调试结果

Which I think should be correct. 我认为应该是正确的。 50 is inserted first, thus it is used as the root, then 60 is inserted on the right child of 50, and 70 is put on the right child of 60. 首先插入50,因此将其用作根,然后将60插入50的右子元素上,然后将70插入60的右子元素上。

Note: I'm also just a novice, please tell me any mistakes that I have done and I will rectify it :) 注意:我也是新手,请告诉我我犯的任何错误,我将予以纠正:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM