The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform
bst = BSTRee()
bst.insert(5)
bst.insert(11)
bst.insert(3)
bst.insert(4)
bst.insert(12)
bst.insert(2)
bst.delete(3)
when I call delete method, it did nothing. Can someone help me to fix it. The link below is my code on github. Thank you so much for your help. https://github.com/hly189/sort/blob/master/tree/BST.py
class BSTreeNodedef ____init__(self, value): self.value = value self.left = None self.right = Nonedef insert(self,key): if self.value == key: print ("the node already exists")return False elif self.value > key: if self.left is not None: return self.left.insert(key)else: self.left = BSTreeNode(key)return Trueelse: if self.right is not None: return self.right.insert(key)else: self.right = BSTreeNode(key)return Falsedef delete(self, node, k):if node == None: return Noneelif node.value == k: if node.left is None and node.right is None: return Noneelif node.left is None: return node.rightelif node.right is None: return node.left else: node.value = get_min(node.right)node.right.delete(node.right,node.value)elif k < node.value: node.left.delete(node.left,k)else: node.right.delete(node.right,k)return nodeclass BSTree: def __init__(self): self.root = None def delete(self,key): self.root.delete(self.root,key)def insert(self,data): if self.root: self.root.insert(data)else: self.root = BSTreeNode(data)return True def find_min(self,node):current_node = nodewhile current_node.left: current_node = current_node.leftreturn current_nodedef get_min(node): current_node = nodewhile current_node.left: current_node = current_node.leftreturn str(current_node.value)def print_helper(root, indent):if root is not None:print_helper(root.right, indent + " ")print (indent + str(root.value))print_helper(root.left, indent + " ")def print_tree(root):print_helper(root, "")