前缀树以及相关题目

一个字符串类型的数组arr1,另一个字符串类型的数组arr2。arr2中哪些字符,是arr1中出现的?请打印。arr2中哪些字符,是作为arr1中某个字符串前缀出现的?请打印。arr2中哪些字符,是作为arr1中某个字符串前缀出现的?请打印arr2中出现次数最大的前缀。

前缀树结构。

Java代码:

package com.company;

import java.util.TreeMap;

//前缀树
public class TrieTree {
    public static class TrieNode{
        public int pass;
        public int end;
        public TrieNode[] nexts;

        public TrieNode() {
            pass = 0;
            end = 0;
            nexts = new TrieNode[26];
        }
    }
    public static  class Trie{
        private TrieNode root;
        public  Trie(){
            root = new TrieNode();
        }
        public void insert(String word){
            if (word == null){
                return;
            }
            char[] chs = word.toCharArray();
            TrieNode node = root;
            node.pass++;
            int index = 0;
            for (int i =0; i<chs.length; i++){
                index = chs[i] - 'a';//a就是0,b就是1
                if (node.nexts[index] == null){//有走向a的路吗,没有就新建一条a的路
                    node.nexts[index] = new TrieNode();
                }
                node = node.nexts[index];//然后当前节点移动到下一个节点上(有就直接移动,没有就新建了再移动)
                node.pass++;//然后这个节点的pass++
            }
            node.end++;
        }
        //word这个单词之前加入过几次
        public int search(String word){
            if (word == null){
                return 0;
            }
            char[] chars = word.toCharArray();
            TrieNode node = root;
            int index=0;
            for (int i = 0 ;i<chars.length;i++){
                index = chars[i] - 'a';
                if (node.nexts[index] == null){//如果这个节点后面没有路了,但是循环还没有终止,说明根本没有这个单词。
                    return 0;
                }
                node = node.nexts[index];
            }
            return node.end;
        }
        //所有加入的字符串中,有几个是以pre这个字符作为前缀的。
        public int prefixNumber(String pre){
            if (pre == null){
                return 0;
            }
            char[] chars = pre.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i =0;i<chars.length;i++){
                index = chars[i] - 'a';
                if (node.nexts[index] == null){
                    return 0;
                }
                node = node.nexts[index];
            }
            return node.pass;
        }
        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){//当到节点的pass为0的时候,后面可以全部删除了
                        node.nexts[index] = null;
                        return;
                    }
                    node.end--; 
                }
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

失忆机器

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

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

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

打赏作者

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

抵扣说明:

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

余额充值