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


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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