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


    题目

    字典树(前缀树)的实现
    

    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];
        }
    }
    
    

    结果

  • 相关阅读:
    第一次做的破网页
    c++初学指针
    eskibana
    有关JSON以及JSON在PHP中的应用
    比较全的log4j 配置
    验证码类
    PHP 兼容 Curl/Socket/Stream 的 HTTP 操作类
    PHP缓存技术
    Fatal error: Call to undefined function curl_init()解决方案
    socket基础
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/8947640.html
Copyright © 2020-2023  润新知