字典树——动态&&静态

静态---时间快

/*************************************************************************
    > File Name: Trie.c
    > Author: 
    > Mail: 
    > Created Time: Tue 11 Dec 2018 03:46:05 PM CST
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int charmapping[256];
void init_charmapping()
{
    for(int i='a';i<='z';i++)
    {
        charmapping[i]=i-'a';
    }
}

const int maxn = 26;    //这里假设字符串只出现26个小写字母
const int maxm = 100000;

struct treenode
{
    bool end;
    struct treenode *next[maxn];
}head;

struct treenode memory[maxm];
int mallocp = 0;

void init()
{
    head.end = 1;
    for(int i = 0;i<maxn;i++) head.next[i] = NULL;
}

treenode* createnew()
{
    treenode *newnode;
    newnode = &memory[mallocp++];
    newnode->end = 0;
    for(int i=0;i<maxn;i++) newnode->next[i] = NULL;
    return newnode;
}

void update(char *s)
{
    int k = 0,temp;
    treenode *t = &head;
    while(s[k])
    {
        temp = charmapping[s[k]];     //找到c字符对应的标号2
        if(!t->next[temp]) t->next[temp] = createnew();
        t = t->next[temp];
        k++;
    }
    t->end = 1;
}

bool search(char *s)
{
    int k = 0,temp;
    treenode *t = &head;
    while(s[k])
    {
        temp = charmapping[s[k]];
        if(!t->next[temp]) return false;        //已经遍历到最后一个结点 
        t = t->next[temp];
        k++;
    }
    if(t->end) return true;
    return false;
}

int main(int argc,char **argv)
{
    //freopen("text.txt","r",stdin);
    init();
    char x[1000];
    char t;
    while(1)
    {
        fflush(stdin);
        scanf("%c",&t);
        getchar();
        if(t=='q')
        {
            scanf("%c",&x);
            getchar();
            if(search(x))     printf("匹配成功!
");
            else printf("匹配失败!
");
        }
        else if(t=='u')
        {
            scanf("%s",&x);
            getchar();
            update(x);
            printf("更新完毕!
");
        }
        else if(t=='e')
        {
            printf("退出ing……
");
            break;
        }
        else 
            printf("无效命令!
");
    }
    return 0;
}

动态方法

/*************************************************************************
    > File Name: Trie_dynamic.cpp
    > Author: 
    > Mail: 
    > Created Time: Tue 11 Dec 2018 05:23:37 PM CST
 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

int charmapping[256];
void init_charmapping()
{
    for(int i='a';i<'z';i++)            
    {
        charmapping[i] = i - 'a';
    }
}

const int maxn = 26;
const int maxm = 100000;
struct treenode
{
    int count;
    treenode *next[maxn];
}head;

void init_trie()
{
    head.count = 1;
    for(int i=0;i<maxm;i++) head.next[i] = NULL;
}

treenode* createnew()
{
    treenode *newnode;
    newnode = (treenode*)malloc(sizeof(treenode));
    newnode->count = 0;
    for(int i=0;i<maxn;i++) newnode->next[i] = NULL;
    return newnode;
}

void update(char *s,int num)
{
    int k=0,temp;
    treenode *t = &head;
    while(s[k])
    {
        t->count+=num;
        temp = charmapping[s[k]];
        if(!t->next[temp]) t->next[temp] = createnew();
        t = t->next[temp];
        k++;
    }
    t->count+=num;
}

bool search(char *s,int num)
{
    int k = 0,temp;
    treenode *t = &head;
    while(s[k])
    {
        temp = charmapping[s[k]];
        if(!t->next[temp] || t->next[temp]->count<num)    return false;
        t = t->next[temp];
        k++;    
    }
    int snum = t->count;
    for(int i=0;i<maxn;i++) if(t->next[i]) snum -= t->next[i]->count;
    if(snum>=num) return true;
    return false;
}

void erase(char *s,int num)
{
    int k = 0,temp;
    treenode *t = &head;
    treenode *t1;
    head.count -= num;
    while(s[k])
    {
        temp = charmapping[s[k]];
        t->next[temp]->count -= num;
        if(t->next[temp]->count==0)
        {
            t1 = t->next[temp];
            t->next[temp]=NULL;
            k++;
            break;
        }
        t = t->next[temp];
        k++;
    }
    while(s[k])
    {
        temp = charmapping[s[k]];
        t = t1->next[temp];
        free(t1);
        t1 = t;
        k++;
    }
}

char temp[1000];
void printall(treenode *tnode,int pos)
{
    int count = tnode->count;
    for(int i=0;i<maxn;i++)    if(tnode->next[i]) count -= tnode->next[i]->count;
    for(int i=0;i<count;i++) printf(""%s"
",temp);
    for(int i='a';i<='z';i++)
    {
        if(tnode->next[charmapping[i]])
        {
            temp[pos] = i;
            temp[++pos]='';
            printall(tnode->next[charmapping[i]],pos);
            temp[--pos]='';
        }
    }
}
原文地址:https://www.cnblogs.com/wzqstudy/p/10155535.html