简体   繁体   中英

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).

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. 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. I did it based on Python Official Documentation , private functions use at least two leading underscores and at most one trailing underscore.

  • First line in insert_element, added 'self.__root= ' so that the returned root value will be stored as the new 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.
  • I did not modify anything much in __in_order_str, since I think the author only asked for the insertion (?)
  • commented assertEqual, since no function is provided in the question (?)
  • 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.

Note: I'm also just a novice, please tell me any mistakes that I have done and I will rectify it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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