Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在树中定位或获取子级的父级_Java_Intellij Idea_Tree - Fatal编程技术网

Java 如何在树中定位或获取子级的父级

Java 如何在树中定位或获取子级的父级,java,intellij-idea,tree,Java,Intellij Idea,Tree,我正在从如下文本文件制作一棵树: Store, manager, employee manager, smith, Michael employee, steve, karen, litt, kwan reads first line; sets first string to parent and following to children reads second line, sets first string to parent and following to children

我正在从如下文本文件制作一棵树:

Store, manager, employee
manager, smith, Michael
employee, steve, karen, litt, kwan
reads first line;
sets first string to parent and following to children
reads second line, sets first string to parent and following to children
        Store
          /
    manager - employee
      /              \
  smith - Michael     steve - karen - litt - kwan
class Node{
   String value;
   Node parent;
   Node[] children;
}
我的代码是这样的:

Store, manager, employee
manager, smith, Michael
employee, steve, karen, litt, kwan
reads first line;
sets first string to parent and following to children
reads second line, sets first string to parent and following to children
        Store
          /
    manager - employee
      /              \
  smith - Michael     steve - karen - litt - kwan
class Node{
   String value;
   Node parent;
   Node[] children;
}
但我想读第二行,看看第一个字符串是否是上面字符串中的子字符串,并将第一个字符串(第二行的父字符串)的值赋给该子字符串,以实现如下树结构:

Store, manager, employee
manager, smith, Michael
employee, steve, karen, litt, kwan
reads first line;
sets first string to parent and following to children
reads second line, sets first string to parent and following to children
        Store
          /
    manager - employee
      /              \
  smith - Michael     steve - karen - litt - kwan
class Node{
   String value;
   Node parent;
   Node[] children;
}

我无法找出检查字符串的父项是否是以前任何字符串的子项并将其设置为该子项的部分

您可以使用HashMap。
假设您有这样一个节点类:

Store, manager, employee
manager, smith, Michael
employee, steve, karen, litt, kwan
reads first line;
sets first string to parent and following to children
reads second line, sets first string to parent and following to children
        Store
          /
    manager - employee
      /              \
  smith - Michael     steve - karen - litt - kwan
class Node{
   String value;
   Node parent;
   Node[] children;
}
构造树时,可以构造一个
HashMap
,将字符串值映射到节点对象

然后您可以检查
map.get(stringValue)==null
,查看stringValue是否是上述字符串的子级