【208】实现Trie(前缀树)

13 篇文章 0 订阅

题目(难度:中等):

实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();

trie.insert("apple");
trie.search("apple");   // 返回 true
trie.search("app");     // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");   
trie.search("app");     // 返回 true

代码思想:

前缀树的特点是每个字符串都是从头节点开始连接,包括查询、删除等操作也都是从头节点开始。前缀树的每个边可以看作是一个字符,每个节点带有这个边出现的次数信息,即添加了几次。叶子节点也带有叶子节点出现的次数信息。

代码实现:

 public class TrieNode{
        public int path;
        public int end;
        public TrieNode[] nexts;

        public TrieNode(){
            path = 0;
            end = 0;
            nexts = new TrieNode[26];  //按照字母
        }
    }

    private TrieNode root;
    public Solution208() {
        root = new TrieNode();
    }

    public void insert(String word) {
        if (word == null || word.length() == 0) return;
        char[] str = word.toCharArray();
        int index = 0;
        TrieNode head = root;
        for (int i = 0; i < str.length; i++) {
            index = str[i] - 'a';
            if (head.nexts[index] == null) {
                head.nexts[index] = new TrieNode();
            }
            head = head.nexts[index];
            head.path++;
        }
        head.end++;
    }

    public boolean search(String word) {
        if (word == null || word.length() == 0) return false;
        char[] str = word.toCharArray();
        int index = 0;
        TrieNode head = root;
        for (int i = 0; i < str.length; i++) {
            index = str[i] - 'a';
            if (head.nexts[index] == null) {
                return false;
            }
            head = head.nexts[index];
        }
        if(head.end != 0) return true;
        else return false;
    }

    public boolean startwith(String prefix) {
        if (prefix == null || prefix.length() == 0) return false;
        char[] str = prefix.toCharArray();
        int index = 0;
        TrieNode head = root;
        for (int i = 0; i < str.length; i++) {
            index = str[i] - 'a';
            if (head.nexts[index] == null) {
                return false;
            }
            head = head.nexts[index];
        }
        return true;
    }

    public void delete(String word) {
        if (word == null || word.length() == 0 || !search(word)) return;
        char[] str = word.toCharArray();
        int index = 0;
        TrieNode head = root;
        for (int i = 0; i < str.length; i++) {
            index = str[i] - 'a';
            if (--head.nexts[index].path == 0) {
                head.nexts[index] = null;
                return;
            }
            head = head.nexts[index];
        }
        head.nexts[index].end--;
    }

测试用例:

算法分析:

时间复杂度O(n),额外空间复杂度O(1)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值