我正在尝试使用字符串数组构建具有根节点、子节点和孙节点的树。我有一个这样的数组
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/"
]
我想取这个数组并确定相应的节点。那些像
"/capacitor/", "/connector/", "/diode/"
是根节点。那些像
"/capacitor/non_polarized/", "/capacitor/polarized/", "/optical/detector/"
是子节点,最后是像
"/optical/detector/diode/", "/optical/emmision/laser/"
是孙节点。有两个/
和文本之间的字符串是根节点,三个/
是子节点,四个/
是孙节点。
想象一下我有电容器作为我的根节点,现在我会有root_node = "capacitor" child_node = "/capacitor/non_polarized/","/capacitor/polarized/" and grandchild_node = "/capacitor/non_polarized/ceramic/", "/capacitor/polarized/al/"
编辑:我希望以这样一种方式输出,即通过使用根节点,我可以确定子孙。