Java实现前缀树(字典树)

package Tree.TrieTree;

public class TrieNode {
    public static class trieNode {
        public int pass;//表示当前节点一共经过了多少次
        public int end;//表示有多少字符串是以该字符为结尾
        public trieNode[] nexts;//表示当前节点的下一个节点

        public trieNode() {
            pass = 0;
            end = 0;
            //nexts[0] == null 没有走向'a'的路
            //nexts[0] != null 由走向'a'的路
            //...
            //nexts[25] != null
            nexts = new trieNode[26];// 有走向'z'的路
        }
    }

    public static class Trie {
        private trieNode root;

        public Trie() {
            root = new trieNode();
        }

        public void insert(String word) {
            trieNode node = root; //前缀树的节点从根节点开始
            if (word == null) { //如果字符串为空
                node.pass++;
                node.end++;
                return;
            }
            char[] chs = word.toCharArray(); //先将字符串转化为字符数组,方便处理
            node.pass++; //首先根节点的经过次数++
            int index = 0; //标记当前字符应该在什么位置
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a'; //计算当前字符的下标
                if (node.nexts[index] == null) { //如果节点不存在就申请节点
                    node.nexts[index] = new trieNode();
                }
                node = node.nexts[index];
                node.pass++; //该节点的经过次数++
            }
            node.end++; //当字符串处理完毕之后,最后一个处理的字符就是字符串的结尾,所以处理完以后就end++
        }

        //word这个单词之前加入过几次
        public int search(String word) {
            if(word == null) {
                return 0;
            }
            char[] chs = word.toCharArray();
            trieNode node = root;
            int index = 0;
            for(int i = 0;i < chs.length;i++) {
                index = chs[i] - 'a'; //找到对应字符的下标
                if(node.nexts[index] == null) { //如果找到中间对应的节点还没有建出来就说明该字符串根本没有添加过
                    return 0;
                }
                node = node.nexts[index]; //如果该前缀树上有该字符的节点就继续往下走,直到字符串结束
            }
            //由于字符串每天加一次就会将其路径上的最后一个字符的end++,所以字符串添加过的次数就是对应路径最后一个字符的end值
            return node.end;
        }

        //删除字符串
        public void delete(String word) {
            if(search(word) != 0) { //如果该字符串从来没有添加过就没有必要删除
                char[] chs = word.toCharArray();
                trieNode node = root;
                node.pass--; //先对路径上的字符经过次数--
                int index = 0;
                for(int i = 0;i < chs.length;i++) {
                    index = chs[i] - 'a';
                    if(--node.nexts[index].pass == 0) { //如果字符串出现的次数为0就将内存释放掉
                        node.nexts[index] = null;
                        return;
                    }
                    //否则就是字符数组继续向下走
                    node = node.nexts[index];
                }
                node.end--; //当字符数组走完之后就对其end--
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
前缀树,也称为字典树或Trie树,是一种树形数据结构,用于有效地处理字符串查找问题。每个节点表示一个字符串前缀,节点可以有零个或多个子节点。这里提供一份用Java实现前缀树的代码示例: ```java class TrieNode { // 存储子节点 private TrieNode[] children; // 标识是否是一个单词的结束节点 private boolean isEndOfWord; public TrieNode() { children = new TrieNode[26]; isEndOfWord = false; } public void insert(String word) { // 从根节点开始遍历 TrieNode curNode = this; for(char c : word.toCharArray()) { int index = c - 'a'; if(curNode.children[index] == null) { // 如果子节点不存在,则新建一个 curNode.children[index] = new TrieNode(); } // 继续遍历下一个节点 curNode = curNode.children[index]; } // 最后一个节点标识为单词结束节点 curNode.isEndOfWord = true; } public boolean search(String word) { TrieNode curNode = this; for(char c : word.toCharArray()) { int index = c - 'a'; if(curNode.children[index] == null) { // 如果不存在该节点 return false; } curNode = curNode.children[index]; } // 如果最后一个节点标识为单词结束节点,则表示查找到该单词 return curNode.isEndOfWord; } public boolean startsWith(String prefix) { TrieNode curNode = this; for(char c : prefix.toCharArray()) { int index = c - 'a'; if(curNode.children[index] == null) { // 如果不存在该节点 return false; } curNode = curNode.children[index]; } // 如果遍历完前缀,最后一个节点不为null,则表示该前缀存在 return true; } } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { root.insert(word); } public boolean search(String word) { return root.search(word); } public boolean startsWith(String prefix) { return root.startsWith(prefix); } } ``` 使用方法: ```java public static void main(String[] args) { Trie trie = new Trie(); trie.insert("apple"); boolean result1 = trie.search("apple"); // 返回 true boolean result2 = trie.search("app"); // 返回 false boolean result3 = trie.startsWith("app"); // 返回 true trie.insert("app"); boolean result4 = trie.search("app"); // 返回 true System.out.println(result1 + " " + result2 + " " + result3 + " " + result4); } ``` 输出结果: ``` true false true true ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假若爱有天意

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值