This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
tree_process.py
68 lines (57 loc) · 2.11 KB
/
tree_process.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
63
64
65
66
67
68
from os.path import join, dirname
from textx import metamodel_from_file
from textx.export import metamodel_export, model_export
from rbtree import RBTree, RBTreeNode
from bstree import BSTree, BSTreeNode
class BinaryTreeNode(BSTreeNode):
def __init__(self, val, color="B"):
self.val = val
self.color = color
self.left = None
self.right = None
self.parent = None
class BinaryTree(BSTree):
def __init__(self):
self.root = None
self.index = -1
self.action = ""
self.ops = []
self.content = []
def interpret(self, model):
for t in model.BinaryTrees:
self.root = self.build(t)
break
for op in model.Operations:
if op.__class__.__name__ == "Insert":
self.ops.append(('i', op.val))
if op.__class__.__name__ == "Delete":
self.ops.append(('d', op.val))
print(self.content)
print(self.ops)
return
def build(self, t):
if t.__class__.__name__ == 'NoneType':
return None
if t.BinaryTreeNodes.__class__.__name__ == 'Node':
self.content.append(t.BinaryTreeNodes.val)
current = BinaryTreeNode(t.BinaryTreeNodes.val)
current.left = self.build(t.BinaryTreeNodes.left)
current.right = self.build(t.BinaryTreeNodes.right)
return current
if t.BinaryTreeNodes.__class__.__name__ == 'INTNode':
self.content.append(t.BinaryTreeNodes.val)
return BinaryTreeNode(t.BinaryTreeNodes.val)
if t.BinaryTreeNodes.__class__.__name__ == 'NULLNode':
return None
def getmodel(debug=False):
this_folder = dirname(__file__)
bt_mm = metamodel_from_file(
join(this_folder, 'BinaryTree.tx'), debug=False)
metamodel_export(bt_mm, join(this_folder, 'bt_meta.dot'))
bt_model = bt_mm.model_from_file(join(this_folder, 'temp.bt'))
model_export(bt_model, join(this_folder, 'temp.dot'))
binarytree = BinaryTree()
binarytree.interpret(bt_model)
return binarytree
if __name__ == "__main__":
getmodel()