你好,游客 登录 注册 搜索
背景:
阅读新闻

字典树编写笔记

[日期:2014-02-05] 来源:Linux社区  作者:minghe_uestc [字体:大 中 小]

源文件

#include "trie.h"

Trie::Trie() {
    root = NULL;
}

Trie::~Trie() {
    clear();
}

void Trie::insert(char *word) {
    if (word == NULL) return;
    this->root = insertR(this->root, word, 0);
}

void Trie::printTrie() {
    printTrieR(this->root);
}

bool Trie::contains(char *word) {
    if (word == NULL) return false;
    return (findR(this->root, word, 0) == NULL) ? false : true;
}

const char *Trie::find(char *word) {
    if (word == NULL) return NULL;
    TrieNode *n = findR(this->root, word, 0);
    return (n == NULL) ? NULL : n->word;
}

int Trie::match(char *word, vector<char *> &ret) {
    return (word == NULL) ? 0 : matchR(this->root, word, 0, ret);
}

void Trie::remove(char *word) {
    removeR(this->root, word, 0);
}

void Trie::clear() {
    clearR(this->root);
    this->root = NULL;
}

bool Trie::empty() {
    return (root == NULL) ? true : false;
}

int Trie::match(char *word, int n, vector<char *> &ret) {
    return (word == NULL) ? 0 : matchR(root, word, 0, n, ret);
}

TrieNode *Trie::insertR(TrieNode *root, char *word, int w) {
    if (root == NULL) {
        return new TrieNode(word);
    }

    // 为叶子节点的情况
    if (root->word != NULL) {
        return splitR(root, new TrieNode(word), w);
    }

    // 不为叶子节点的情况
    int idx = TrieNode::digit(word, w);
    if (root->word == NULL) {
        root->next[idx] = insertR(root->next[idx], word, w+1);
    }
    return root;
}

TrieNode *Trie::splitR(TrieNode *p, TrieNode *q, int w) {
    // 相同单词情况
    if (p->word[w] == '\0' && q->word[w] == '\0') {
        delete q;
        return p;
    }

    TrieNode *t = new TrieNode(NULL);
    int pIdx = TrieNode::digit(p->word, w);
    int qIdx = TrieNode::digit(q->word, w);
    if (pIdx != qIdx) {
        // 字符不同
        t->next[pIdx] = p;
        t->next[qIdx] = q;
    } else {
        // 字符相同
        t->next[pIdx] = splitR(p, q, w+1);
    }
    return t;
}

void Trie::printTrieR(TrieNode *root) {
    if (root == NULL)
        return;

    if (root->word != NULL)
        cout << root->word << endl;
    for (int i = 0; i < MAX; i++) {
        printTrieR(root->next[i]);
    }
}

TrieNode *Trie::findR(TrieNode *root, char *word, int w) {
    if (root == NULL || (word[w] == '\0' && root->word == NULL))
        return NULL;

    // 找到单词
    if (root->word != NULL && strcmp(root->word+w-1, word+w-1) == 0) {
        return root;
    }
    return findR(root->next[TrieNode::digit(word, w)], word, w+1);
}

int Trie::matchR(TrieNode *root, char *word, int w, vector<char *> &ret) {
    // 找到前缀匹配的子树
    TrieNode *subTree = matchPrefixR(root, word, w);
    // 构建结果
    return (subTree == NULL) ? 0 : getWords(subTree, ret);
}

int Trie::getWords(TrieNode *root, vector<char *> &ret) {
    if (root == NULL) return 0;

    // 叶子节点
    if (root->word != NULL) {
        ret.push_back(root->word);
        return 1;
    }

    int count = 0;
    for (int i = 0; i < MAX; i++) {
        if (root->next[i] != NULL)
            count += getWords(root->next[i], ret);
    }
    return count;
}

TrieNode *Trie::matchPrefixR(TrieNode *root, char *word, int w) {
    if (root == NULL) return NULL;

    if (word[w] == '\0' || word[w+1] == '\0') return root;

    return matchPrefixR(root->next[TrieNode::digit(word, w)], word, w+1);
}

bool Trie::removeR(TrieNode *root, char *word, int w) {
    if (root == NULL) return false;

    if (root->word != NULL && strcmp(root->word+w-1, word+w-1) == 0) {
        delete root;
        return true;
    }

    int idx = TrieNode::digit(word, w);
    if (removeR(root->next[idx], word, w+1) != true)
        return false;
    root->next[idx] = NULL;

    for (int i = 0; i < MAX; i++) {
        if (root->next[i] != NULL) {
            return false;
        }
    }

    delete root;
    return true;
}

void Trie::clearR(TrieNode *root) {
    if (root == NULL) return;
    for (int i = 0; i < MAX; i++) {
        if (root->next[i] != NULL) {
            clearR(root->next[i]);
        }
    }
    delete root;
}

int Trie::getWords(TrieNode *root, int n, vector<char *> &ret) {
    if (root == NULL || n <= 0) return 0;

    if (root->word != 0) {
        ret.push_back(root->word);
        return 1;
    }

    int count = 0;
    for (int i = 0; i < MAX && n > 0; i++) {
        int nWords = getWords(root->next[i], n, ret);
        n -= nWords;
        count += nWords;
    }
    return count;
}

int Trie::matchR(TrieNode *root, char *word, int w, int n, vector<char *> &ret) {
    TrieNode *subTree = matchPrefixR(root, word, w);
    return (subTree == NULL) ? 0 : getWords(subTree, n, ret);
}

linux
相关资讯       字典树 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款