I have a tree with the following structure:
Which I have stored in a file, with the following content:
1 - 2 3 4 * 2 - 5 6 7 * 5 - 11 12 13 14 * 11 - * 12 - * 13 - * 14 - * 6 - * 7 - * 3 - * 4 - 8 9 10 * 8 - 15 16 * 15 - * 16 - * 9 - * 10 - *
The structure is as follows:
-
" * ": separates "node families" that is, composite structures of a parent and their children.
-
" - * ": indicates that a node has no children.
-
" - ": indicates the children of a node.
Next I specify its implementation in Python:
class node(object):
def __init__(self, data):
self.data = data
self.children = []
def add_child(self, obj):
self.children.append(obj)
And the code to generate the tree presented:
#A continuación defino todos los nodos
a = node(1)
b = node(2)
c = node(3)
d = node(4)
e = node(5)
f = node(6)
g = node(7)
h = node(8)
i = node(9)
j = node(10)
k = node(11)
l = node(12)
ll = node(13)
m = node(14)
n = node(15)
o = node(16)
# A continuación defino las relaciones de parentesco
a.add_child(b)
a.add_child(c)
a.add_child(d)
b.add_child(e)
b.add_child(f)
b.add_child(g)
d.add_child(h)
d.add_child(i)
d.add_child(j)
h.add_child(n)
h.add_child(o)
e.add_child(k)
e.add_child(l)
e.add_child(ll)
e.add_child(m)
I have functions that run through the tree implemented in python and I save it in a file in the format indicated at the beginning of this post. But now, what I want to do is load it from that format into a program, and for that I have to use the code of the node class presented above. Next I write the code that I am developing:
#1. Genero todos los nodos
#1.2 Reduzco la cadena a elementos que no se repitan
cadena2 = list(set(cadena.split()))
#print(cadena2)
#1.3 Elimino de la cadena los símbolos '*' y '-'
cadena2.remove("*")
cadena2.remove("-")
#print(cadena2)
#1.4 Genero todos los nodos
listanodos = []
i = 0
while i < len(cadena2):
listanodos = listanodos + [node(cadena2[i])]
i = i + 1
#2 Vinculo los nodos hijos a los padres
# ¿Cómo lo hago?
Since I can generate the nodes, keeping them in a list, now I just need to create kinship relationships between them, I guess you have to go through cadena
for it.