0

So I'm trying to delete a node from a tree by using these two functions inside the class.Unfortunately it just doesnt delete anything and i was wondering what is wrong about it! any help would be truly appreciated.

def Find_Min(self,node):
        current=node
        while current.left is None:
             current=current.left
        return current



    def deletenode(self,node,ntbd):  ##ntbd:node to be deleted  /// node: root node
        if node is None:
            return None
        elif node.data>ntbd:
            node.left=self.deletenode(node.left,ntbd)
        elif node.data<ntbd:
            node.right=self.deletenode(node.right,ntbd)
        else:  ##Found you bastard
            if node.left==None and node.right==None:
                node=None
            elif node.left==None:
                temp=node.right
                node=None
                print("----",temp)
            elif node.right==None:
                temp=node.left
                node=None
                print("----",temp)
            else:
                smallest=self.Find_Min(node.right)
                node.data=smallest.data
                node.right=self.deletenode(node.right,smallest.data)
  • You won't get any help without specifying the structure of the `node` class. – thelawnmowerman May 16 '20 at 18:03
  • @thelawnmowerman, it's a simple `{ data, left, right }` object, you can infer this from highlighting all instances of `node` – Mulan May 16 '20 at 19:39
  • @thelawnmowerman it's a regular tree node with {leftchild,root,rightchild} – Soheil_mtr May 17 '20 at 18:52
  • @Thankyou Thanks for your comment pal. I would like to know how i can make my code work with only a slight modification. Could you help me with it too? – Soheil_mtr May 17 '20 at 18:54
  • Actually I found a pretty good answer in GitHub. Here is the link. Hope it helps someone. https://github.com/OmkarPathak/Data-Structures-using-Python/blob/master/Trees/BinarySearchTree.py – Soheil_mtr May 20 '20 at 16:19

0 Answers0