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)
5
  • You won't get any help without specifying the structure of the node class. Commented May 16, 2020 at 18:03
  • @thelawnmowerman, it's a simple { data, left, right } object, you can infer this from highlighting all instances of node Commented May 16, 2020 at 19:39
  • @thelawnmowerman it's a regular tree node with {leftchild,root,rightchild} Commented May 17, 2020 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? Commented May 17, 2020 at 18:54
  • Actually I found a pretty good answer in GitHub. Here is the link. Hope it helps someone. github.com/OmkarPathak/Data-Structures-using-Python/blob/master/… Commented May 20, 2020 at 16:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.