Binary Search Tree search method

Viewed 56

Learning on Binary Search Trees trying to implement it myself, I managed to get the traversal methods working properly now I am wondering why this search method isn't working properly can you please help spot the bug is within the search method I can't seem to spot where I could have went wrong

Can I also get help with the delete method I fail to see where the bug could be too and worse I am not getting an error message to point me to the direction or and output

BOTH METHODS I GET NO OUTPUT

CODE BELOW

 def search(self, val):
    return self.__search(self.root, val) is not None


def __search(self, p, val):

    if p is None:
        return None

    if val < p.info:
        return self.__search(p.lchild, val)

    elif val > p.info:
        return self.__search(p.rchild, val)

    else:
     return p




tree = BinaryTree()
tree.insertion(59)


def delete(self, val):
    self.root = self.__delete(self.root, val)


def __delete(self, p, val):

    if p is None:
        print(f"{val} Was not found")


    elif val < p.info:
        p.lchild = self.__delete(p.lchild, val)

    elif val > p.info:
        p.rchild = self.__delete(p.rchild, val)

    
    else:

        if p.lchild is not None and p.rchild is not None:

            s = p.rchild
            while s.lchild is not None:
                s = s.lchild

            p.info = s.info
            p.rchild = self.__delete(p.rchild, s.info)


        else:

            if p.lchild is not None:
                ch = p.lchild

            else:
                ch = p.rchild

            p = ch
       
        return p


tree = BinaryTree()
tree.insertion(59)
tree.insertion(45)
tree.insertion(72)
tree.insertion(75)
tree.insertion(69)
tree.insertion(50)
tree.delete(69)
tree.level_order()
0 Answers
Related