-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathworking_bfs_2nd.py
48 lines (38 loc) · 960 Bytes
/
working_bfs_2nd.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
class node:
def __init__(self,val=None):
self.val = val
self.l = None
self.r = None
class bst:
def __init__(self):
self.head = None
def lvl_order(self):
""""
level order queue:
1.)give q the starting pos
2.)as long as the q is full
3.) take first thing in q mark as visited
4.) check if that popped items has children
if they do put then in the queue
""""
vis = []
q = []
q.append(self.head)
while len(q) > 0:
cur = q.pop(0)
vis.append(cur)
if cur.l:
q.append(cur.l)
if cur.r:
q.append(cur.r)
for x in vis:
print(x.val)
t = bst()
t.head = node(4)
t.head.l = node(2)
t.head.r = node(8)
t.head.l.l = node(1)
t.head.l.r = node(3)
t.head.r.l = node(5)
t.head.r.r = node(10)
t.lvl_order()