Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:
- print("DUPLICATE")
- return
- else data < self.data:
- if self.left is None:
- self.left = Node(data)
- else:
- self.left.insert(data)
- elif data > self.data:
- if self.right is None:
- self.right = Node(data)
- else:
- self.right.insert(data)
- else:
- self.data = data
- def search(self, data):
- if data == self.data:
- print(self.data)
- return self.data
- elif data < self.data:
- if self.left is None:
- print("NO VALUE")
- return
- print("LEFT")
- return self.left.search(data)
- elif data > self.data:
- if self.right is None:
- print("NO VALUE")
- return
- print("RIGHT")
- return self.right.search(data)
- if __name__ == "__main__":
- target = int(input())
- root = Node(None)
- for value in map(int, input().split()):
- root.insert(value)
- root.search(target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement