我需要为一个大学项目实现一个Trie(Java)。Trie 应该能够添加和删除字符串(对于第 1 阶段)。
我每天(过去几天)都花了几个小时试图弄清楚如何做到这一点,但每次都惨遭失败。
我需要一些帮助,互联网上的示例和我的教科书(Adam Drozdek 的 Java 中的数据结构和算法)没有帮助。
信息
我正在使用的节点类:
class Node { public boolean isLeaf; } class internalNode extends Node { public String letters; //letter[0] = '$' always. //See image -> if letter[1] = 'A' then children[1] refers to child node "AMMO" //See image -> if letter[2] = 'B' then children[2] refers to internal node "#EU" public TrieNode[] children = new TrieNode[2]; public TrieInternalNode(char ch) { letters = "#" + String.valueOf(ch);//letter[0] = '$' always. isLeaf = false; } } class leafNode extends Node { public String word; public TrieLeafNode(String word) { this.word = new String(word); isLeaf = true; } }
这是我需要遵循的插入伪代码:(警告它非常模糊)
trieInsert(String K) { i = 0; p = the root; while (not inserted) { if the end of word k is reached set the end-of-word marker in p to true; else if (p.ptrs[K[i]] == 0) create a leaf containing K and put its address in p.ptrs[K[i]]; else if reference p.ptrs[K[i]] refers to a leaf { K_L = key in leaf p.ptrs[K[i]] do { create a nonleaf and put its address in p.ptrs[K[i]]; p = the new nonleaf; } while (K[i] == K_L[i++]); } create a leaf containing K and put its address in p.ptrs[K[--i]]; if the end of word k is reached set the end-of-word marker in p to true; else create a leaf containing K_L and put its address in p.ptrs[K_L[i]]; else p = p.ptrs[K[i++]]; } }
我需要实现以下方法。
public boolean add(String word){...}//adds word to trie structure should return true if successful and false otherwise public boolean remove(String word){...}//removes word from trie structure should return true if successful and false otherwise
我找不到删除的伪代码,但如果插入不起作用,删除不会帮助我。
这是我需要实现的 Trie 外观的图像。
我知道如果像这样实施,Trie 仍然会效率低下,但目前我不必担心这一点。
这本书提供了一个类似于我需要做的实现,但不使用单词 char ('$') 的结尾,并且只将没有前缀的单词存储在子节点中
http://mathcs.duq.edu/drozdek/DSinJava/SpellCheck.java
约束
- 我需要在 JAVA 中实现 trie。
- 我可能不会导入或使用任何 Java 的内置数据结构。(即没有 Map、HashMap、ArrayList 等)
- 我可以使用数组、Java 原始类型和 Java 字符串。
- Trie 必须使用
$
(美元)符号来表示词尾。(见下图)
- 我可以假设现在
$
将插入包含该符号的单词。 - 我需要以与本书相同的风格实现 Trie it。
- 单词的大小写无关紧要,即。所有单词都将被视为小写
- Trie 应该只存储词尾字符和适用于单词而不是整个字母表的字符(如某些实现)。
我不希望任何人为我执行实施(除非他们有一个躺在身边:P)我真的需要帮助。