BST Insert And Delete With Successor (Python)
Implement a binary search tree supporting insert and delete using in-order successor for two-child nodes.
Code
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def insert(self, val):
if not self.root:
self.root = TreeNode(val)
return
cur = self.root
while True:
if val < cur.val:
if not cur.left:
cur.left = TreeNode(val)
return
cur = cur.left
else:
if not cur.right:
cur.right = TreeNode(val)
return
cur = cur.rightReport a problem with this exampleOpens a pre-filled issue on GitHub.