Storage format for AND-OR tree in Python 2.7

I am doing some work with decision trees at the moment when I use AND-OR trees as a view. I am looking for a suitable storage format for these trees.

node starting with "t" is OR node, node starting with "c" is (ordered!) AND node. Leaves always start with "p".

Initially, each node contains two parts: the name of the node and the description of the node.

Images showing two different representations of the same decision tree. I basically need both views, as well as a simple and quick solution for converting views to each other. enter image description here enter image description here

My thoughts are still

Datatype:

Dict: You would have to use an ordered dict. In addition, it would be easier to keep the name and description.

, , , node .

Node :

, . , . (, "" "" ), ?

.

+4
1

, . - :

['t1', ['c1', 'p1', ['c2', 'p2', 'p3']], ['c3', ['c4', 'p4', 'p5'], ['t2', ['c5', 'p6', 'p7'], 'p8']]]

:

BASE := [node_name, left, right]

:

  • node_name ( )
  • left right - a BASE ( , c, t node) node name ( , )

OrderedDict, IMO:

In [26]: from collections import OrderedDict as OD

In [27]: tree = OD((('name', 't1'), ('left', 'c1'), ('right', OD((('name', '...'),)) )))

In [28]: tree
Out[28]: 
OrderedDict([('name', 't1'),
             ('left', 'c1'),
             ('right', OrderedDict([('name', '...')]))])

, :

class Node:
     type = ''
     left = None
     right = None
0

Source: https://habr.com/ru/post/1651768/


All Articles