문제

트리 / 지시 된 acyclic 그래프 구현이 필요합니다.

public class TreeNode<K, V> {
    private K key; // 'key' for this node, always present
    private V value; // 'value' for this node, doesn't have to be set

    private TreeNode<K, V> parent;
    private Set<TreeNode<K, V>> children; 
}
  • 어떤 종류의 분류도 없습니다.
  • 그만큼 TreeNode 키 주위의 래퍼 일 뿐이며 가능한 값입니다 (노드는 값을 설정할 필요가 없습니다).
  • 부모와 자녀 모두에 대한 링크가 필요합니다.

표준 API 나 커먼즈 등에 나를 위해 이것을 할 수있는 것이 있습니까?

나는 그것을 직접 쓰지 않는다 (그리고 나는 확실히 나는 확실히 ~ 아니다 당신에게 사람들에게 물었다) 나는 단지 바퀴를 다시 발명하고 싶지 않다.

도움이 되었습니까?

해결책

그와 같은 것이없는 것 같습니다. 나는 물었다 비슷한 질문 지난주에 내 나무를 구현하게되었습니다. 내 구현은 당신이 제안한 것과 매우 유사했습니다.

public class TreeNode<T>
{
    private LinkedList<TreeNode<T>> children = new LinkedList<TreeNode<T>>();
    public T value { get; set; }

    public TreeNode(T value)
    {
        this.value = value;
    }
    public LinkedList<TreeNode<T>> GetChildren()
    {
        return children;
    }
}

부모에게 다시 링크를 추가해야합니다.

다른 팁

또한 있습니다 http://www.jgrapht.org, LGPL에 따라 소프트웨어가 라이센스가 부여되었습니다. 그래도 경고해야합니다. 자신의 구현은 위험에 처해 있습니다. 구조에 대한 재귀를 사용하여 (그래프 인), 아시 클릭인지 확인하거나 무한 루프 문제가 발생하는지 확인해야합니다. 이미 문제를 다루는 제 3 자 코드를 사용하는 것이 좋습니다.

I'd say it's better to roll out your own implementation (besides, you've already got the interface nicely thought out). What are the operations you are planning to perform on this tree anyway? You'd probably want to design your API around the things you want... direct access to individual nodes by key/value? types of traversals? add/remove operations?

If you're looking for additional graph capabilities, JDigraph's Digraph class should fit the bill.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top