Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. class Node:
  2.     def __init__(self, data):
  3.         self.left = None
  4.         self.right = None
  5.         self.data = data
  6.  
  7.     def insert(self, data):
  8.         if self.data:
  9.             if data == self.data:
  10.                 print("DUPLICATE")
  11.                 return
  12.  
  13.             else data < self.data:
  14.                 if self.left is None:
  15.                     self.left = Node(data)
  16.                 else:
  17.                     self.left.insert(data)
  18.  
  19.             elif data > self.data:
  20.                 if self.right is None:
  21.                     self.right = Node(data)
  22.                 else:
  23.                     self.right.insert(data)
  24.        
  25.         else:
  26.             self.data = data
  27.  
  28.     def search(self, data):
  29.         if data == self.data:
  30.             print(self.data)
  31.             return self.data
  32.  
  33.         elif data < self.data:
  34.             if self.left is None:
  35.                 print("NO VALUE")
  36.                 return
  37.  
  38.             print("LEFT")
  39.             return self.left.search(data)
  40.  
  41.         elif data > self.data:
  42.             if self.right is None:
  43.                 print("NO VALUE")
  44.                 return
  45.  
  46.             print("RIGHT")
  47.             return self.right.search(data)
  48.  
  49. if __name__ == "__main__":
  50.     target = int(input())
  51.  
  52.     root = Node(None)
  53.  
  54.     for value in map(int, input().split()):
  55.         root.insert(value)
  56.  
  57.     root.search(target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement