没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过

#include <cstdio>
#include <cstdlib>
#include <vector>

#define ALPHABETS 2600000
#define CASE 0
#define MAX_WORD_SIZE 25

using namespace std;

struct node
{
    struct node *parent;
    struct node *children[ALPHABETS];
    vector<int> occurrences;
};

int IsGB(char *pText)
{
    unsigned char sqChar[200];
    sqChar[0] = *pText;
    if (sqChar[0] >= 0xa1)
        if (sqChar[0] == 0xa3)
            return 1; //全角字符
        else
            return 2; //汉字
    else
        return 0; //英文、数字、英文标点
}

void insertWord(struct node *trieTree, char *word, int index)
{
    struct node *traverse = trieTree;

    while (*word != '')
    {
        if (IsGB(word)!=1)
        {
            if (traverse->children[2600000-abs(*word)] == NULL)
            {
                traverse->children[2600000-abs(*word)] = (struct node *)calloc(2, sizeof(struct node));
                traverse->children[2600000-abs(*word)]->parent = traverse; // Assigning parent
            }
            traverse = traverse->children[2600000-abs(*word)];
            ++word;
            ++word;
        }
        else
        {
            if (traverse->children[*word - CASE] == NULL)
            {
                traverse->children[*word - CASE] = (struct node *)calloc(2, sizeof(struct node));
                traverse->children[*word - CASE]->parent = traverse; // Assigning parent
            }
            traverse = traverse->children[*word - CASE];
            ++word;
        }
    }

    traverse->occurrences.push_back(index); 
}


struct node *searchWord(struct node *treeNode, char *word)
{
    while (*word != '')
    {
        if (IsGB(word)!=1)
        {
            if (treeNode->children[2600000-abs(*word)] != NULL)
            {
                treeNode = treeNode->children[2600000-abs(*word)];
                ++word;
                ++word;
            }
            else
            {
                break;
            }
        }
        else
        {
            if (treeNode->children[*word - CASE] != NULL)
            {
                treeNode = treeNode->children[*word - CASE];
                ++word;
            }
            else
            {
                break;
            }
        }
    }

    if (*word == '' && treeNode->occurrences.size() != 0)
    {
        printf("Word found");
        return treeNode;
    }
    else
    {
        // Word not found
        printf("Word not found");
        return NULL;
    }
}

void removeWord(struct node *trieTree, char *word)
{
    struct node *trieNode = searchWord(trieTree, word);

    if (trieNode == NULL)
    {
        return;
    }

    trieNode->occurrences.pop_back(); 
    bool noChild = true;

    int childCount = 0;
    int i;

    for (i = 0; i < ALPHABETS; ++i)
    {
        if (trieNode->children[i] != NULL)
        {
            noChild = false;
            ++childCount;
        }
    }

    if (!noChild)
    {
        return;
    }

    struct node *parentNode;

    while (trieNode->occurrences.size() == 0 && trieNode->parent != NULL && childCount == 0)
    {
        childCount = 0;
        parentNode = trieNode->parent;

        for (i = 0; i < ALPHABETS; ++i)
        {
            if (parentNode->children[i] != NULL)
            {
                if (trieNode == parentNode->children[i])
                {
                    parentNode->children[i] = NULL;
                    free(trieNode);
                    trieNode = parentNode;
                }
                else
                {
                    ++childCount;
                }
            }
        }
    }
}

void lexicographicalPrint(struct node *trieTree, vector<char> word)
{
    int i;
    bool noChild = true;

    if (trieTree->occurrences.size() != 0)
    {
        vector<char>::iterator charItr = word.begin();

        while (charItr != word.end())
        {
            printf("%c", *charItr);
            ++charItr;
        }
        printf(" -> @ index -> ");

        vector<int>::iterator counter = trieTree->occurrences.begin();

        while (counter != trieTree->occurrences.end())
        {
            printf("%d, ", *counter);
            ++counter;
        }

        printf("
");
    }

    for (i = 0; i < ALPHABETS; ++i)
    {
        if (trieTree->children[i] != NULL)
        {
            noChild = false;
            word.push_back(CASE + i); 

            lexicographicalPrint(trieTree->children[i], word);
            word.pop_back();
        }
    }

}

int main()
{
    int n, i;
    vector<char> printUtil; 

    struct node *trieTree = (struct node *)calloc(1, sizeof(struct node));
    char word[MAX_WORD_SIZE];

   
    printf("Enter the number of words-
");
    scanf("%d", &n);

    for (i = 1; i <= n; ++i)
    {
        scanf("%s", word);
        insertWord(trieTree, word, i);
    }

    lexicographicalPrint(trieTree, printUtil);

    // scanf("%s", word);
    // removeWord(trieTree, word);

    // printf("
"); //
    // lexicographicalPrint(trieTree, printUtil);

    printf("
Enter the Word to be search - ");
    scanf("%s", word);
    searchWord(trieTree, word);

    return 0;
}


编译:g++ tr.cpp -o tr.exe

原文地址:https://www.cnblogs.com/bdccloudy/p/7665179.html