我可以帮助我知道我在 BST 的插入和搜索操作中的错误吗?

问题描述

我正在用 Python 实现 BST。我已经编写了在 BST 中插入数据和搜索数据的函数。 问题是,我已经尝试了很多次来找出我犯了什么错误,但我一直无法正确地做。 我请求一些帮助。请帮帮我。

class Node:

    def __init__(self,data):
        self.data = data
        self.left_ptr = None
        self.right_ptr = None

class BinarySearchTree:

    def __init__(self):
        self.ptr_to_root = None


    def insert(self,data):
        '''
        This feature/function helps the users to insert data into a Binary 
        Search Tree.
        '''
        # To insert,the first thing we have to do is
        # creating a node :-
        node = Node(data)
        
        # We create a temperory pointer as we
        # won't move ptr_to_root.
        temp_ptr = self.ptr_to_root

        # CASE 1 > If ptr_to_root is None i.e. If BST
        # is empty :
        if temp_ptr == None:
            temp_ptr = node
        

        # CASE 2 > If BST is not empty already : 
        while temp_ptr != None:
             
            # Below is the pointer which will remain behind
            # temp_ptr by one step.
            r = temp_ptr
            
            # If we already have the data present in BST,we
            # cannot re-enter it. Hence,we simply exit the 
            # function.
            if data == temp_ptr.data:
                return
            
            # If the data is having lesser value then temp_ptr's data,# then we move the temp_ptr to its left,or else the right.
            if data < temp_ptr.data:
                temp_ptr = temp_ptr.left_ptr
            else:
                temp_ptr = temp_ptr.right_ptr
        
        # r is the tail pointer,which follows the temp_ptr. 
        # Eventually,we will compare if the value that we wanrt to insert
        # is less or more then r.data,and accordingly insert the data desired.
        if data < r.data:
            r.left_ptr = node
        else:
            r.right_ptr = node

    

    def search(self,data):
        
        # If the BST is empty,then we do not 
        # have the element present in the BST for sure:
        temp_ptr = self.ptr_to_root
        
        # If BST is empty,then :
        if temp_ptr == None:
            return None

        
        # If BST is not empty,we may or may not be 
        # finding the element in the tree :
        while temp_ptr != None:

            if data == temp_ptr.data:
                return temp_ptr
            
            elif data < temp_ptr.data:
                temp_ptr = temp_ptr.left_ptr
            
            else:
                temp_ptr = temp_ptr.right_ptr
        
        # If the element is not present in BST :
        return None

当我运行上面的代码来插入一个元素然后搜索它时;即使我搜索已插入的元素,它也会返回“无”。 请帮忙!

解决方法

您的问题实际上非常简单,使用调试器几分钟就可以告诉您。您永远不会创建根节点。当您尝试将某些内容插入空树时,您设置了节点,但不保存它,因此树始终为空。改变这个:

.PHONY

一切正常。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...