How to implement a tree data-structure in Java?

前端 未结 24 2494
鱼传尺愫
鱼传尺愫 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 01:02

    public abstract class Node {
      List children;
    
      public List getChidren() {
        if (children == null) {
          children = new ArrayList<>();
        }
        return chidren;
      }
    }
    

    As simple as it gets and very easy to use. To use it, extend it:

    public class MenuItem extends Node {
      String label;
      String href;
      ...
    }
    

提交回复
热议问题