Home > database >  Binary search tree lazy deletion Python
Binary search tree lazy deletion Python

Time:11-06

I've created a binary search tree class and I'm trying to get a grasp on how "lazy deletion" works. I've set a flag in order to show if a node has been removed. When I search for a value that I want to remove, self.removed would be marked as True. However, when I use the findValue method, it still says that the removed value is still there.

I've been doing some research on how lazy deletion works and all of them say that you need to set a flag and set it to True if the value is found. Is there anything else I would need to implement in order to get this to work? Or am I missing something?

Any help would be appreciated!

class Node:

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

    def setLeft(self, left):
        self.left = left

    def setRight(self, right):
        self.right = right

    def getLeft(self):
        return self.left

    def getRight(self):
        return self.right

    def getValue(self):
        return self.value


class Tree:

    def __init__(self):
        self.root = None

    def insertValue(self, value):
        """add a new node containing value to the tree """
        if self.root is None:
            temp = Node(value)
            self.root = temp
            return
        self.recInsertValue(value, self.root)

    def recInsertValue(self, value, ptr):
        if value < ptr.value:
            if ptr.getLeft() is None:
                temp = Node(value)
                ptr.left = temp
            else:
                self.recInsertValue(value, ptr.getLeft())
        else:
            if ptr.right is None:
                temp = Node(value)
                ptr.right = temp
            else:
                self.recInsertValue(value, ptr.right)

    def findValue(self, value):
        """return true if there is a node containing value, false otherwise"""
        ptr = self.root

        while ptr is not None:
            if ptr.value == value:
                return True
            if value < ptr.value:
                ptr = ptr.getLeft()
            else:
                ptr = ptr.getRight()
        return False

    def removeValue(self, value):
        ptr = self.root
        while ptr is not None:
            if ptr.value == value:
                ptr.removed = True
                return True
            if value < ptr.value:
                ptr = ptr.getLeft()
            else:
                ptr = ptr.getRight()
        return False


    def inOrder(self):
        return self.recInOrder(self.root)


    def recInOrder(self, ptr):
        buffer = ""
        if ptr is not None:
            buffer  = self.recInOrder(ptr.getLeft())
            buffer  = str(ptr.getValue())   " "
            buffer  = self.recInOrder(ptr.getRight())
            return buffer
        return ""

CodePudding user response:

You should also modify the search method, otherwise there is no way to utilize the removed flag.

    def findValue(self, value):
        """return true if there is a node containing value, false otherwise"""
        ptr = self.root

        while ptr is not None:
            if ptr.value == value and not ptr.removed:
                return True
            if value < ptr.value:
                ptr = ptr.getLeft()
            else:
                ptr = ptr.getRight()
        return False

Also don't forget recInOrder:

    def recInOrder(self, ptr):
        buffer = ""
        if ptr is not None:
            buffer  = self.recInOrder(ptr.getLeft())
            buffer  = str(ptr.getValue())   " " if not ptr.removed else ""
            buffer  = self.recInOrder(ptr.getRight())
            return buffer
        return ""
  • Related