I am trying to build a tree with root, children, and grandchildren nodes using an array of strings. I have an array like this
array = [
"/capacitor/",
"/capacitor/non_polarized/",
"/capacitor/non_polarized/ceramic/",
"/capacitor/polarized/",
"/capacitor/polarized/al/",
"/connector/",
"/diode/",
"/diode/normal/",
"/optical/",
"/optical/emmision/diode/",
"/optical/emmision/laser/",
"/optical/detector/",
"/optical/detector/diode/"
]
I would like to take this array and determine the respective nodes. The ones which are like
"/capacitor/", "/connector/", "/diode/"
are the root nodes. The ones which are like
"/capacitor/non_polarized/", "/capacitor/polarized/", "/optical/detector/"
are children nodes, and finally the ones like
"/optical/detector/diode/", "/optical/emmision/laser/"
are grandchildren nodes. A strings which has two / and text in between is a root node, with three / is a child node, and with four / is a grandchildren node.
Imagine I had capacitor as my root node, now I would have root_node = "capacitor" child_node = "/capacitor/non_polarized/","/capacitor/polarized/" and grandchild_node = "/capacitor/non_polarized/ceramic/", "/capacitor/polarized/al/"
EDIT: I would like the output in such a way that the by using the root node I can determine the children and grandchildren.
present?. That is contradictory.