C++实现字典树的增删查写前缀打印

举报
楚楚冻人玥玥仙女 发表于 2021/11/19 02:46:27 2021/11/19
【摘要】 C++实现字典树的增删查写前缀打印 // // Created by jal on 19-3-9. // #include <bits/stdc++.h> using namespace s...

C++实现字典树的增删查写前缀打印

//
// Created by jal on 19-3-9.
//
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 26;
struct Node{
    int sum;
    bool isEnd;
    char val;
    vector<Node*>son;

    Node(char val):
            sum(0),isEnd(false),val(val){
        son.resize(SIZE);
        for(auto& node : son){
            node = NULL;
        }
    }
};

struct Trie{//字典树,实现单词的增删查写
    Node* root;
    Trie(){
        root = new Node('a');
    }

    /*
     * 往字典树中插入字符串
     */
    void insert(string s){
        if(s.size() <= 0 || s == "")return;
        Node* node = root;
        for(auto& c: s){
            int p = c - 'a';
            if(NULL == node->son[p]){
                node->son[p] = new Node(c);
            }
            node->son[p]->sum++;
            node = node->son[p];
        }
        node->isEnd = true;
    }

    /*
     * 查找字符串是否在字典树中
     */
    bool search(string s){
        if(s.size() <= 0 || s == "")return false;
        Node* node = root;
        for(auto& c: s){
            int p = c - 'a';
            if(NULL == node->son[p]){
                return false;
            }
            node = node->son[p];
        }
        return node->isEnd;
    }

    /*
     * 统计以s为前缀的字符串的个数
     */
    int countPrefix(string s){
        if(s.size() <= 0 || s == "")return 0;
        Node* node = root;
        for(auto& c: s){
            int p = c - 'a';
            if(NULL == node->son[p]){
                return 0;
            }
            node = node->son[p];
        }
        return node->sum;
    }

    /*
     * 从字典树中删除s,成功返回true,失败返回false
     */
    void deleteString(string s){
        if(s.size() <= 0 || s == "")return;
        Node* node = root;
        for(auto& c: s){
            int p = c - 'a';
            if(NULL == node->son[p]){
                return;
            }
            if(node->son[p]->sum-- <= 1){
                node->son[p] = NULL;
                return;
            }
            node = node->son[p];
        }
        node->isEnd = false;
    }

    /*
     * 查找以s为前缀的字符串在字典中的节点指针
     */
    Node* getPrefixNode(string s){
        if(s.size() <= 0 || s == "")return NULL;
        Node* node = root;
        for(auto& c: s){
            int p = c - 'a';
            if(NULL == node->son[p]){
                return NULL;
            }
            node = node->son[p];
        }
        return node;
    }

    /*
     * 中序遍历打印以s为前缀,指针为root的字典树
     */
    void preTraverse(string s, Node* root){
        if(root->isEnd){
            cout << s << endl;
        }
        for (int i = 0; i < SIZE; ++i) {
            if(root->son[i] != NULL){
                preTraverse(s+root->son[i]->val, root->son[i]);
            }
        }
        return;
    }

    /*
     * 打印以s为前缀的字符串
     */
    void printStringByPrefix(string s){
        if(s.size() <= 0 || s == "")return;
        if(countPrefix(s) <= 0)return;
        if(search(s))cout << s << endl;
        Node* node = getPrefixNode(s);
        preTraverse(s,node);
    }
};

int main(){
    Trie trie;
    trie.insert("jal");
    trie.insert("jiailing");
    trie.insert("jiailingx");
    trie.insert("jisuanji");
    cout << trie.search("jiailing") << endl;
    cout << trie.countPrefix("iai") << endl;
    cout << trie.countPrefix("ji") << endl;
    cout << trie.countPrefix("j") << endl;
    cout << trie.search("jal") << endl;
    trie.deleteString("jal");
    cout << trie.search("jal") << endl;
    cout << trie.countPrefix("j") << endl;
    trie.printStringByPrefix("j");
}
  
 

文章来源: blog.csdn.net,作者:爱玲姐姐,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jal517486222/article/details/88365106

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。