-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArvoreBusca (BFS).py
55 lines (50 loc) · 1.3 KB
/
ArvoreBusca (BFS).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
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self, data=None, node=None):
if node:
self.root = node
elif data:
node = Node(data)
self.root = node
else:
self.root = None
def insert(self, value):
parent = None
x = self.root
while(x):
parent = x
if value < x.data:
x = x.left
else:
x = x.right
if parent is None:
self.root = Node(value)
elif value < parent.data:
parent.left = Node(value)
else:
parent.right = Node(value)
def bfs(self):
res = []
q = [self.root]
while q:
node = q[0]
q = q[1:]
res.append(str(node.data))
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return res
for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
bst = BinarySearchTree()
for val in l:
bst.insert(val)
print(f'Case {i+1}:')
print(bst.prefix())
print()