class Node: def __init__(self,val): self.left = None self.right = None self.data = val class BST: def __init__(self,data): self.root = Node(data) def insert(self,data,node=None): if node is
class Node: def __init__(self,val): self.left = None self.right = None self.data = val class BST: def __init__(self,data): self.root = Node(data)
def insert(self,data,node=None): if node is None: node = self.root if data > node.data: if node.right is None: node.right = Node(data) else: self.insert(data,node.right) else: if node.left is None: node.left = Node(data) else: self.insert(data,node.left)
def levelorderwithLevelInfo(self,root): queue = [root]
while queue != []: level_node = len(queue) while level_node > 0: node = queue.pop(0) if node.left: queue.append(node.left) if node.right: queue.append(node.right) print(node.data, end = " ") level_node -= 1 print("") if __name__=='__main__': bst = BST(10) bst.insert(6) bst.insert(14) bst.insert(4) bst.insert(8) bst.insert(12) bst.insert(16) bst.insert(6)
print('6 should not be added twice - it is a wrong BST ') bst.levelorderwithLevelInfo(bst.root)
#### suppose to modify the insert fucntion to manage duplicate keys
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started