SHARE
TWEET

Untitled

a guest Oct 19th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node:
  2.  
  3.     def __init__(self, data):
  4.  
  5.         self.left = None
  6.         self.right = None
  7.         self.data = data
  8.        
  9.  
  10.     def insert(self, data, depth=1):
  11.         if self.data:
  12.             if data <= self.data:
  13.                 if self.left is None:
  14.                     self.left = Node(data)
  15.                     print(depth+1,end=" ")
  16.                 else:
  17.                     self.left.insert(data,depth+1)
  18.             elif data > self.data:
  19.                 if self.right is None:
  20.                     self.right = Node(data)
  21.                     print(depth+1,end=" ")
  22.                 else:
  23.                     self.right.insert(data,depth+1)
  24.                     depth+=1
  25.         else:
  26.             self.data = data
  27.             print(depth,end=" ")
  28. ls=[]
  29. N=int(input())
  30. ls=list((map(int, input().split())))
  31. root=Node(ls[0])
  32. print(1,end=" ")
  33. for i in range(1,len(ls)):
  34.     root.insert(ls[i])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top