Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
2
Indexable
Never
import time
class Node:
 def __init__(self,data):
 self.left=None
 self.right=None
 self.data=data
 def insert(self,data):
 if self.data:
 if data<self.data: #data less than root node
 if self.left is None:
 self.left=Node(data)
 else:
 self.left.insert(data)
 elif data>self.data: #data greater than root node
 if self.right is None:
 self.right=Node(data)
 else:
 self.right.insert(data)
 else: #if the root is empty
 self.data=data
 def print_tree(self): #printing the tree,inorder traversal
 if self.left:
 self.left.print_tree()
 print(self.data)
 if self.right:
 self.right.print_tree()
def main():
 start=time.time()
 start_time=time.ctime(start)
 root_node=int(input("Enter the value of root node: "))
 root=Node(root_node)
 while True:
 r=input("Do you want to insert an element(Y/N): ")
 if r.upper()=='Y':
 element=int(input("Enter the element: "))
 root.insert(element)
 elif r.upper()=='N':
 break
 else:
 print("Invalid Choice")
 print("Binary Tree:")
 root.print_tree()
 end=time.time()
 end_time=time.ctime(end)
 print("\nTime Complexity")
 print("The start time is: ", start_time)
 print("The end time is: ", end_time)