前缀树

2018-07-17  本文已影响0人  shoulda

前缀树原理

//节点定义
public static class TrieNode {
        public int path;  //相当于这个节点的子节点有几个
        public int end;//以这个节点结尾的有多少个
        public TrieNode[] map;

        public TrieNode() {
            path = 0;
            end = 0;
            map = 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;
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    node.map[index] = new TrieNode();
                }
                node = node.map[index];
                node.path++;
            }
            node.end++;
        }
       //删除元素
        public void delete(String word) {
            if (search(word)) {
                char[] chs = word.toCharArray();
                TrieNode node = root;
                int index = 0;
                for (int i = 0; i < chs.length; i++) {
                    index = chs[i] - 'a';
                    if (node.map[index].path-- == 1) {
                        node.map[index] = null;
                        return;
                    }
                    node = node.map[index];
                }
                node.end--;
            }
        }
//查询元素
        public boolean search(String word) {
            if (word == null) {
                return false;
            }
            char[] chs = word.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    return false;
                }
                node = node.map[index];
            }
            return node.end != 0;
        }

        public int prefixNumber(String pre) {
            if (pre == null) {
                return 0;
            }
            char[] chs = pre.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    return 0;
                }
                node = node.map[index];
            }
            return node.path;
        }
    }

    public static void main(String[] args) {
        Trie trie = new Trie();
        System.out.println(trie.search("xuda"));
        trie.insert("xuda");
        System.out.println(trie.search("xuda"));
        trie.delete("xuda");
        System.out.println(trie.search("xuda"));
        trie.insert("xuda");
        trie.insert("xuda");
        trie.delete("xuda");
        System.out.println(trie.search("xuda"));
        trie.delete("xuda");
        System.out.println(trie.search("xuda"));
        trie.insert("xudaa");
        trie.insert("xudac");
        trie.insert("xudad");
        trie.insert("xudaw");
        trie.delete("xudacc");
        System.out.println(trie.search("xudas"));
        System.out.println(trie.prefixNumber("xuda"));

    }
****题目:
1.arr2中有哪些字符, 是arr1中出现的? 请打印
2.arr2中有哪些字符, 是作为arr1中某个字符串前缀出现的? 请
打印

上一篇 下一篇

猜你喜欢

热点阅读