关于前缀树的生成删除查询

前缀树节点的定义

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 Tire {
        public TrieNode root;

        public Tire() {
            root = new TrieNode();
        }
   }

前缀树的插入

public void insert(String word) {
            if (word == null) return;

            char[] chars = word.toCharArray();

            TrieNode node = root;
            node.pass++;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';

                if (node.nexts[index] == null) {
                    node.nexts[index] = new TrieNode();
                }

                node = node.nexts[index];
                node.pass++;
            }
            node.end++;
        }

前缀树的删除

public void delete(String word) {
            if (search(word) != 0) {
                char[] chars = word.toCharArray();

                TrieNode node = root;
                node.pass--;
                int index = 0;
                for (int i = 0; i < chars.length; i++) {
                    index = chars[i] - 'a';

                    if (--node.nexts[index].pass == 0) {
                        node.nexts[index] = null;
                        return;
                    }

                    node = node.nexts[index];

                }

                node.end--;
            }
        }

前缀树的字符串查找

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

前缀树带有某前缀的字符串查找

public int profixNumber(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;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前缀树(Trie树)是一种用于字典查找的数据结构,它可以高效地完成字符串的插入、删除和查找操作。下面是前缀树生成算法: 1. 定义一个Trie节点类,包含一个指向子节点的指针数组和一个布尔变量表示该节点是否为单词结尾。 ``` class TrieNode { public: TrieNode* children[26]; bool isEndOfWord; TrieNode() { for (int i = 0; i < 26; i++) children[i] = NULL; isEndOfWord = false; } }; ``` 2. 定义一个Trie类,包含一个根节点指针。在构造函数中初始化根节点。 ``` class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } }; ``` 3. 定义一个插入字符串的函数。从根节点开始,遍历待插入字符串的每个字符,如果当前字符对应的子节点不存在,则创建一个新的子节点,并将当前节点指向该子节点。最后将最后一个字符所对应的节点标记为单词结尾。 ``` void insert(string word) { TrieNode* node = root; for (char ch : word) { int index = ch - 'a'; if (node->children[index] == NULL) node->children[index] = new TrieNode(); node = node->children[index]; } node->isEndOfWord = true; } ``` 4. 定义一个查找字符串的函数。从根节点开始,遍历待查找字符串的每个字符,如果当前字符对应的子节点不存在,则返回false。如果查找完成后,最后一个字符所对应的节点为单词结尾,则返回true。 ``` bool search(string word) { TrieNode* node = root; for (char ch : word) { int index = ch - 'a'; if (node->children[index] == NULL) return false; node = node->children[index]; } return node != NULL && node->isEndOfWord; } ``` 5. 定义一个以某个前缀开头的所有单词的函数。从根节点开始,遍历前缀字符串的每个字符,如果当前字符对应的子节点不存在,则返回空vector。如果遍历完成后,最后一个字符所对应的节点存在,则从该节点开始深度优先遍历整个子树,将遇到的所有单词加入结果vector中。 ``` vector<string> startsWith(string prefix) { vector<string> result; TrieNode* node = root; for (char ch : prefix) { int index = ch - 'a'; if (node->children[index] == NULL) return result; node = node->children[index]; } collectWords(node, prefix, result); return result; } void collectWords(TrieNode* node, string prefix, vector<string>& result) { if (node->isEndOfWord) result.push_back(prefix); for (int i = 0; i < 26; i++) { if (node->children[i] != NULL) { char ch = 'a' + i; collectWords(node->children[i], prefix + ch, result); } } } ``` 这样,我们就完成了前缀树生成算法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值