java 实现字典树(Trie)

TreeNode.java

package Trie;

public class TreeNode {

    public TreeNode[] slot = new TreeNode[26];

    public char c;

    public boolean isWord;

    public int prefix;

    public String word;

    public String explain;

}

Trie.java

package Trie;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Trie {

    public static final TreeNode wordsTree = new TreeNode();

    public void insert(String words, String explain) {
        var root = wordsTree;
        var chars = words.toCharArray();
        for (var c : chars) {
            var idx = c - 'a';
            if (root.slot[idx] == null) {
                root.slot[idx] = new TreeNode();
            }
            root = root.slot[idx];
            root.c = c;
            root.prefix++;
        }
        root.explain = explain;
        root.isWord = true;
    }

    public List<String> searchPrefix(String prefix){
        var root = wordsTree;
        var chars = prefix.toCharArray();
        var cache = new StringBuilder();
        for (var c : chars) {
            var idx = c - 'a';
            if (idx > root.slot.length || idx < 0 || root.slot[idx] == null) {
                return Collections.emptyList();
            }
            cache.append(c);
            root = root.slot[idx];
        }
        var list = new ArrayList<String>();
        if (root.prefix != 0) {
            for (int i = 0; i < root.slot.length; i++) {
                if (root.slot[i] != null) {
                    var c = (char) (i + 'a');
                    collect(root.slot[i], String.valueOf(cache) + c, list, 15);
                    if (list.size() >= 15) {
                        return list;
                    }
                }
            }
        }
        return list;
    }

    protected void collect(TreeNode treeNode, String pre, List<String> queue, int resultLimit) {
        if (treeNode.isWord) {
            treeNode.word = pre;
            queue.add(treeNode.word + "->" + treeNode.explain);
            if (queue.size() >= resultLimit) {
                return;
            }
        }
        for (int i = 0; i < treeNode.slot.length; i++) {
            var c = (char) ('a' + i);
            if (treeNode.slot[i] != null) {
                collect(treeNode.slot[i], pre + c, queue, resultLimit);
            }
        }
    }

}

测试代码

    public static void main(String[] args) {
        var trie = new Trie();
        trie.insert("bat", "大厂");
        trie.insert("batch", "批量");
        trie.insert("bitch", "彪子");
        trie.insert("battle", "战斗");
        System.out.println(trie);
        List<String> trieNodes = trie.searchPrefix("ba");
        System.out.println(trieNodes);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ApiChain

扔个包子砸我一下吧~

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

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

打赏作者

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

抵扣说明:

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

余额充值