字典树(前缀树)

package com.tank.practice.basicClass;


public class Code_31_Trie {//字典树(前缀树)

    public static class TrieNode {
        public int path;//记录多少单词公用这个节点
        public int end;//记录多少个单词以这个节点结尾
        public TrieNode[] map;//字符路径26种

        public TrieNode() {
            path=0;
            end=0;
            map = new TrieNode[26];//将26个字母包含进来
        }
    }

    public static class Trie {


        private TrieNode root;//根节点,没有字符路径

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

        public void insert(String word) {//添加word,可重复添加
            if (word == null) {
                return;
            }
            TrieNode node=root;
            char[] chs = word.toCharArray();
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';//字符路径key范围0~25
                if (node.map[index] == null) {
                    node.map[index] = new TrieNode();//字符路径指向的节点value
                }
                node = node.map[index];
                node.path++;
            }
            node.end++;
        }

        public void delete(String word) {//删除word,如果添加过多次,仅删除一个
            if (!search(word)) {
                return;
            }
            TrieNode node = root;
            char[] chs = word.toCharArray();
            int index=0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (--node.map[index].path==0) {
                    node.map[index] = null;
                    return;
                }
                node = node.map[index];
            }
            node.end--;
        }

        public boolean search(String word) {//查询word是否在字典树中
            if (word == null) {
                return false;
            }
            TrieNode node = root;
            char[] chs = word.toCharArray();
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    return false;
                }
                node = node.map[index];
            }
            return node.end != 0;
        }

        public int prefixNumber(String pre) {//返回以字符串pre为前缀的单词数量
            if (pre == null) {
                return 0;
            }
            TrieNode node = root;
            char[] chs = pre.toCharArray();
            int index=0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    return 0;
                }
                node = node.map[index];
            }
            return node.path;
        }



    }
    public static void main(String[] args) {
        Trie trie = new Trie();
        System.out.println(trie.search("tank"));
        trie.insert("tank");
        System.out.println(trie.search("tank"));
        trie.delete("tank");
        System.out.println(trie.search("tank"));
        trie.insert("tank");
        trie.insert("tank");
        trie.delete("tank");
        System.out.println(trie.search("tank"));
        trie.delete("tank");
        System.out.println(trie.search("tank"));
        trie.insert("tanka");
        trie.insert("tankac");
        trie.insert("tankab");
        trie.insert("tankad");
        trie.delete("tanka");
        System.out.println(trie.search("tanka"));
        System.out.println(trie.prefixNumber("tank"));
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值