-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreebinins.py
62 lines (50 loc) · 1.18 KB
/
treebinins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env python3
class Node:
def __init__(self, info):
self.info = info
self.left = None
self.right = None
self.level = None
def __str__(self):
return str(self.info)
def preOrder(root):
if root == None:
return
print (root.info, end=" ")
preOrder(root.left)
preOrder(root.right)
class BinarySearchTree:
def __init__(self):
self.root = None
def insert_at(self, pos, val):
if val < pos.info:
if pos.left:
self.insert_at(pos.left, val)
else:
pos.left = Node(val)
elif val > pos.info:
if pos.right:
self.insert_at(pos.right, val)
else:
pos.right = Node(val)
else:
raise ValueError(f'Duplicate value "{val}"')
def insert(self, val):
if not self.root:
self.root = Node(val)
else:
self.insert_at(self.root, val)
'''
Test:
# Input
6
4 2 3 1 7 6
# Output
4 2 1 3 7 6
'''
tree = BinarySearchTree()
t = int(input())
arr = list(map(int, input().split()))
for i in range(t):
tree.insert(arr[i])
preOrder(tree.root)