我正在實現一個Trie,它將在字符串中存儲子字符串和它們的出現次數。我的特里結構中的每個節點都有一個名爲children的Map,它將存儲主節點的任何子節點。Java:在Trie中存儲子字符串
我的問題是,最終,這些子節點將擁有自己的子節點,我不知道如何從「地圖內的地圖內的地圖...」中檢索數據。
這是我到目前爲止有:
private class TrieNode
{
private T data; //will hold the substring
int count; //how many occurrences of it were in the string
private Map<TrieNode, Integer> children; //will hold subnodes
private boolean isWord; //marks the end of a word if a substring is the last substring of a String
private TrieNode(T data)
{
this.data = data;
count = 1;
children = new HashMap<TrieNode, Integer>();
isWord = false;
}
}
如何從子節點檢索數據,誰自己有可能在他們之下的其他子節點?
P.S.我很抱歉,如果我不能夠清楚地解釋它 - 我有遞歸問題。謝謝。
爲了這篇文章的緣故,讓它更易於理解 - 我說它是一個字符串,但它確實是一個泛型類型。順便說一下,這個節點類是另一個名爲「Trie」的類中的私有類。我在標題中聲明瞭T。 – Haque1