简体   繁体   English

无法删除二叉搜索树中的根节点

[英]Cannot Delete the root node in a Binary search Tree

I have recently started learning and Implementing some data structures in Python and was trying Binary Search tree and completed the code. 我最近开始学习并在Python中实现一些数据结构,并尝试使用Binary Search树并完成了代码。 Everything is running fine except the deletion of root node. 除删除根节点外,其他一切运行正常。 I have divided my code into 3 modules 我已将代码分为3个模块

Here is my Code : 这是我的代码:

Node.py Node.py

class Node:

def __init__(self, data):
    self.data = data
    self.leftChild = None
    self.rightChild = None


def insert(self, data):
    if data < self.data:
        if not self.leftChild:
            self.leftChild = Node(data)
        else:
            self.leftChild.insert(data)
    else:
        if not self.rightChild:
            self.rightChild = Node(data)
        else:
            self.rightChild.insert(data)


def remove(self, data, parentNode): 
    if data < self.data:
        if self.leftChild is not None:
            self.leftChild.remove(data, self)

    elif data > self.data:
        if self.rightChild is not None:
            self.rightChild.remove(data, self)

    else:
        if self.leftChild is not None and self.rightChild is not None:
            self.data = self.rightChild.getMin()
            self.rightChild.remove(self.data, self)

        elif parentNode.leftChild == self:
            if self.leftChild is not None:
                tempNode = self.leftChild
            else:
                tempNode = self.rightChild



            parentNode.leftChild = tempNode


        elif parentNode.rightChild == self:
            if self.leftChild is not None:
                tempNode = self.leftChild
            else:
                tempNode = self.rightChild

            parentNode.rightChild = tempNode


def getMin(self):
    if self.leftChild is None:
        return self.data
    else:
        self.leftChild.getMin()


def getMax(self):
    if self.rightChild is None:
        return self.data
    else:
        self.rightChild.getMax()


def traverseInOrder(self):
    if self.leftChild is not None:
        self.leftChild.traverseInOrder()    
    print(self.data)

    if self.rightChild is not None:
        self.rightChild.traverseInOrder()

BinarySearchTree.py BinarySearchTree.py

from BinarySearchTrees.Node import Node


class BST:

def __init__(self):
    self.rootNode = None;


def insert(self, data):
    if self.rootNode is None:
        self.rootNode = Node(data)

    else:
        self.rootNode.insert(data)


def removal(self, dataToRemove):
    if self.rootNode:
        if self.rootNode.data == dataToRemove:
            tempNode = Node(None)
            tempNode.leftChild = self.rootNode
            print("The value of dataToRemove : " + str(dataToRemove))
            self.rootNode.remove(dataToRemove, tempNode)
        else:
            self.rootNode.remove(dataToRemove, None)


def getMin(self):
    if self.rootNode:
        return self.rootNode.getMin()

def getMax(self):
    if self.rootNode:
        return self.rootNode.getMax()

def traverseInOrder(self):
    if self.rootNode:
        self.rootNode.traverseInOrder()

Output.py Output.py

from BinarySearchTrees.BinarySearchTree import BST

bst = BST()

bst.insert(5)
bst.insert(8)
bst.insert(3)
bst.insert(7)

bst.insert(9)
bst.insert(4)
bst.insert(2)



bst.traverseInOrder()
bst.removal(5)
bst.traverseInOrder()

and the Error with the Removal command is : 并且“删除”命令的错误为:

    Traceback (most recent call last):
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Output.py", line 16, in <module>
    bst.removal(5)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\BinarySearchTree.py", line 24, in removal
    self.rootNode.remove(dataToRemove, tempNode)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 34, in remove
    self.rightChild.remove(self.data, self)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 23, in remove
    if data < self.data:
TypeError: '<' not supported between instances of 'NoneType' and 'int'

It's like the data parameter passed in remove function in Node is returning a None value despite of giving a value in the BinarySearchTree removal function. 就像Node中的remove函数中传递的data参数返回了None值,尽管在BinarySearchTree删除函数中提供了一个值。

If anyone could find the error in my code please tell me the solution. 如果有人在我的代码中找到错误,请告诉我解决方案。 It would be a great help.. 这将是一个很大的帮助。

Both instances of if data < self.data: should be : if data is not None and (data < self.data): if data < self.data:两个实例都应该是: if data is not None and (data < self.data):

This will short circuit the check when data is not None data is not None时,这将使检查短路

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

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