没啥事用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 != '\0')  
    {  
        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 != '\0')  
    {  
        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 == '\0' && 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("\n");  
    }  
  
    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-\n");  
    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("\n"); //  
    // lexicographicalPrint(trieTree, printUtil);  
  
    printf("\nEnter the Word to be search - ");  
    scanf("%s", word);  
    searchWord(trieTree, word);  
  
    return 0;  
}  

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

posted @ 2017-09-26 18:18  chinacloudy  阅读(516)  评论(0编辑  收藏  举报