问题描述
我需要帮助来实现将新子项和兄弟项添加到 Trie 的插入方法。我看到了很多解决方案。但是,我的问题是不同的,因为类内部的类成员是最终的,所以当我遍历树时我无法引用下一个元素。我在互联网上搜索了很多,但似乎找不到任何帮助。我已经为此苦苦挣扎了两天。我将在下面分享我的代码。
以下是我尝试过的所有插入方法。
public class TrieChildren extends AbstractTrieChildren {
public TrieChildren(char c,AbstractTrie t,AbstractTrieChildren o) {
super(c,t,o);
}
@Override
public AbstractTrieChildren insertSorted(char character,AbstractTrie child) {
AbstractTrieChildren current = this;
AbstractTrieChildren newEntry = new TrieChildren(character,child,null);
if (current.child == null) {
current = newEntry;
}
while (current != null) {
if (current.sibling == null) {
current = newEntry;
}
current = current.sibling;
}
return current;
}
正如您在下面的代码中看到的那样,兄弟成员等成员是最终成员。
public abstract class AbstractTrieChildren implements Iterable<AbstractTrieChildren> {
/**
* The label of the outgoing edge.
*/
protected final char character;
/**
* The target of the outgoing edge.
*/
protected final AbstractTrie child;
/**
* The reference to the next sibling or {@code null} if none.
*/
protected final AbstractTrieChildren sibling;
public AbstractTrieChildren(char character,AbstractTrie child,AbstractTrieChildren sibling) {
this.character = character;
this.child = child;
this.sibling = sibling;
}
/**
* Adds a new labelled edge ({@link #character},{@link #child}) to the <b>sorted</b> traversable linked list of siblings iff there is no such edge.
* If an entry already exists,then this method leaves the list unchanged.
*
* @param character the label of the possibly new labelled edge to insert
* @param child the target trie of the possibly new labelled edge to insert
* @return the new head (left-most node) of the <b>sorted</b> traversable linked sibling list
*/
public abstract AbstractTrieChildren insertSorted(char character,AbstractTrie child);
}
任何帮助将不胜感激。也许我错过了一些愚蠢的东西
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)