2009-07-02 27 views
6

Có cấu trúc dữ liệu tích hợp trong Java để biểu diễn cây Crit-bit không? Hoặc bất kỳ thư viện nào có thể cung cấp chức năng này? Tôi cũng sẽ chấp nhận mã ngắn gọn như một câu trả lời, nếu có thể được thực hiện một cách đơn giản.Cây Crit-bit Java

Trả lời

5

Bạn đã thử dự án java radixtree?

Bạn có thể tìm thấy trong đó cấu trúc bạn đang tìm kiếm, giống như:

  • RadixTree lớp

(trích):

/** 
* This interface represent the operation of a radix tree. A radix tree, 
* Patricia trie/tree, or crit bit tree is a specialized set data structure 
* based on the trie that is used to store a set of strings. In contrast with a 
* regular trie, the edges of a Patricia trie are labelled with sequences of 
* characters rather than with single characters. These can be strings of 
* characters, bit strings such as integers or IP addresses, or generally 
* arbitrary sequences of objects in lexicographical order. Sometimes the names 
* radix tree and crit bit tree are only applied to trees storing integers and 
* Patricia trie is retained for more general inputs, but the structure works 
* the same way in all cases. 
* 
* @author Tahseen Ur Rehman 
* email: tahseen.ur.rehman {at.spam.me.not} gmail.com 
*/ 
public interface RadixTree<T> { 
    /** 
    * Insert a new string key and its value to the tree. 
    * 
    * @param key 
    *   The string key of the object 
    * @param value 
    *   The value that need to be stored corresponding to the given 
    *   key. 
    * @throws DuplicateKeyException 
    */ 
    public void insert(String key, T value) throws DuplicateKeyException; 
  • RadixTreeNode lớp

(trích):

/** 
* Represents a node of a Radix tree {@link RadixTreeImpl} 
* 
* @author Tahseen Ur Rehman 
* @email tahseen.ur.rehman {at.spam.me.not} gmail.com 
* @param <T> 
*/ 
class RadixTreeNode<T> { 
    private String key; 

    private List<RadixTreeNode<T>> childern; 

    private boolean real; 

    private T value; 

    /** 
    * intailize the fields with default values to avoid null reference checks 
    * all over the places 
    */ 
     public RadixTreeNode() { 
     key = ""; 
     childern = new ArrayList<RadixTreeNode<T>>(); 
     real = false; 
    } 
2

lựa chọn khác là rkapsi của patricia-trie, hoặc nếu bạn đang tìm kiếm cái gì một chút ít phức tạp mà bạn có thể hack vào chính mình, bạn có thể thử simple-patricia-trie mình.

Tôi cũng có một triển khai functional-critbit có sẵn, tập trung vào hiệu quả không gian (mặc dù hiệu suất của nó cũng tốt). Nó có cả hương vị có thể thay đổi và bất biến.

+0

' T cast (Object o)' phiền mr. trình biên dịch. Tò mò nếu bạn xây dựng bằng Eclipse? Trình biên dịch của Eclipse cho phép bạn thoát khỏi những thứ như thế. – alphazero

+0

Vâng, nhật thực đôi khi cắn tôi trên đó, nhưng 0.0.4 lên đến đầu hiện tại nên biên dịch tốt với javac thẳng. Một phương pháp đúc như vậy là một kludge khá phổ biến để cô lập các cảnh báo cast không được kiểm soát đến một điểm, và tôi chưa bao giờ có bất cứ điều gì nhiều hơn vấn đề nhỏ như [this one] (https://github.com/jfager/functional-critbit/ cam kết/6bbc21fee45b0bef9fff1eae1945c4eec35dc517) khi tôi sử dụng nó. Nếu bạn có thể mở một vấn đề github với nhiều chi tiết hơn về những gì bạn đang thấy, tôi sẽ đánh giá cao nó. – jfager

+0

https://github.com/jfager/functional-critbit/issues/1 – alphazero

0

thử concurrent-trees

trên gradle:

compile 'com.googlecode.concurrent-trees:concurrent-trees:2.4.0'