-
Notifications
You must be signed in to change notification settings - Fork 12
/
traverse_tree_sample.py
57 lines (41 loc) · 1.2 KB
/
traverse_tree_sample.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
import pickle
import sys
file_path = "sample_files/Java/Bubblesort.pkl"
def load_pickle(path):
"""Builds an AST from a script."""
with open(path, 'rb') as file_handler:
tree = pickle.load(file_handler)
return tree
return None
# return ast.parse(script)
def traverse_tree(root):
num_nodes = 1
queue = [root]
root_json = {
"node": str(root.kind),
"children": []
}
queue_json = [root_json]
while queue:
current_node = queue.pop(0)
num_nodes += 1
# print (_name(current_node))
current_node_json = queue_json.pop(0)
children = [x for x in current_node.child]
queue.extend(children)
for child in children:
print(child)
# print "##################"
#print child.kind
child_json = {
"node": str(child.kind),
"children": []
}
current_node_json['children'].append(child_json)
queue_json.append(child_json)
# print current_node_json
return root_json, num_nodes
data = load_pickle(file_path)
print(data)
# root = data.element
# traverse_tree(root)