How to implement a tree data-structure in Java?

前端 未结 24 2946
鱼传尺愫
鱼传尺愫 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:53

    You can use TreeSet class in java.util.*. It is working like Binary search tree, so it is already sorted. TreeSet class implements Iterable, Collection and Set interfaces. You can traverse through the tree with iterator like a set.

    TreeSet treeSet = new TreeSet();
    Iterator it  = treeSet.Iterator();
    while(it.hasNext()){
    ...
    }
    

    You can check, Java Doc and some other .

提交回复
热议问题