-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
30 lines (29 loc) · 930 Bytes
/
helper.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
def print_quad_tree_recursive(node):
if node.isLeaf:
print(node.val)
return
print_quad_tree_recursive(node.topLeft)
print_quad_tree_recursive(node.topRight)
print_quad_tree_recursive(node.bottomLeft)
print_quad_tree_recursive(node.bottomRight)
def print_quad_tree_iterative(node):
if not node:
return
num_full_nodes = 0
total_nodes = 0
q = [node]
level = 0
while q:
cur_node = q.pop(0)
total_nodes += 1
temp = []
for child_pos, child_node in zip(["tl","tr","bl",'br'], cur_node.get_childs()):
if child_node.isLeaf:
temp.append("{}: {}".format(child_pos, child_node.val))
continue
q.append(child_node)
if len(temp) == 4:
num_full_nodes += 1
print("level:{}, nodes:{}".format(level, temp))
level += 1
return num_full_nodes, total_nodes