• 《程序员代码面试指南》第五章 字符串问题 字典树(前缀树)的实现


    题目

    字典树(前缀树)的实现
    

    java代码

    package com.lizhouwei.chapter5;
    
    /**
     * @Description: 字典树(前缀树)的实现
     * @Author: lizhouwei
     * @CreateDate: 2018/4/25 21:34
     * @Modify by:
     * @ModifyDate:
     */
    public class Chapter5_23 {
        public TrieNode root;
    
        public Chapter5_23() {
            root = new TrieNode();
        }
    
        public void insert(String str) {
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                if (node.map[index] == null) {
                    node.map[index] = new TrieNode();
                }
                node = node.map[index];
                node.pathNum++;
            }
        }
    
        public void delete(String str) {
            if (!search(str)) {
                return;
            }
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
    
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                node = node.map[index];
                if (node.pathNum-- == 1) {
                    node.map[index] = null;
                    return;
                }
            }
            node.endNum--;
        }
    
        public boolean search(String str) {
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                node = node.map[index];
                if (node == null) {
                    return false;
                }
            }
            return node.endNum != 0;
        }
    
        public int prefixNumber(String str) {
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
    
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                node = node.map[index];
                if (node == null) {
                    return 0;
                }
            }
            return node.pathNum;
        }
    
        //测试
        public static void main(String[] args) {
            Chapter5_23 chapter = new Chapter5_23();
            String[] strs = {"abc", "abcd", "abd", "b", "bcd", "efg", "hik"};
            for (String str : strs) {
                chapter.insert(str);
            }
            boolean exist = chapter.search("abc");
            System.out.println("abc是否在字典树中:" + exist);
            System.out.println("字典树中删除abc");
            chapter.delete("abc");
            exist = chapter.search("abc");
            System.out.println("abc是否在字典树中:" + exist);
            int result = chapter.prefixNumber("abc");
            System.out.println("以abc作为前缀的字符串的个数:" + result);
        }
    }
    
    class TrieNode {
        public int pathNum;
        public int endNum;
        public TrieNode[] map;
    
        public TrieNode() {
            pathNum = 0;
            endNum = 0;
            map = new TrieNode[26];
        }
    }
    
    

    结果

  • 相关阅读:
    1058 A+B in Hogwarts (20分)
    我的Vue之小功能统计
    H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报
    微信小程序之特殊效果及功能
    移动端H5适配方法(盒子+图片+文字)
    5分钟教你3种实现验证码功能
    微信小程序动态生成保存二维码
    微信授权获取code(微信支付)
    H5微信自定义分享链接(设置标题+简介+图片)
    带你走近WebSocket协议
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/8947640.html
Copyright © 2020-2023  润新知