How to implement a tree data-structure in Java?

前端 未结 24 2854
鱼传尺愫
鱼传尺愫 2020-11-22 00:27

Is there any standard Java library class to represent a tree in Java?

Specifically I need to represent the following:

  • The sub-tree at any node can have
24条回答
  •  一个人的身影
    2020-11-22 00:54

    Since the question asks for an available data structure, a tree can be constructed from lists or arrays:

    Object[] tree = new Object[2];
    tree[0] = "Hello";
    {
      Object[] subtree = new Object[2];
      subtree[0] = "Goodbye";
      subtree[1] = "";
      tree[1] = subtree;
    }
    

    instanceof can be used to determine whether an element is a subtree or a terminal node.

提交回复
热议问题