写了一个字典树

上星期写了一个链表,写的很艰难,跌跌绊绊。昨天我又试着写了一个单词树。这次顺手多了,没遇到什么问题,非常顺利,我也放弃了自定义结构体类型的写法。将每一处结构体都用struct 写。

可能代码不是非常简洁高效,但是,目前测试功能正常。以后我会慢慢改进学习的。下面是完整代码。我准备放到我的期末作业里面用。

小愉悦,昨天晚上写着太高兴,半夜又爬起来修复了几个bug,今早测试,“完美!"

各项测试正常。。。。开心,这样也不枉我数学挂科了。。。。

 

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <stdbool.h>
#include <string.h>
#include  <windows.h>
#define  EnterAndCheck    while ( ( (c = getchar() ) == ' ') && c != '\n' )

int samewords;
enum dowhat {delete = 1 ,add,search};

struct wtree* word_tree_insert(char *str,char *exp,struct wtree *pt);
struct wtree* word_tree_search(char *str,struct wtree *pt);
bool word_tree_delete(char *str,struct wtree *pt);
bool word_tree_child_enum(struct wtree* pt);
int alpcompare(const char a,const char b);




struct wtree* creatchild();
struct wtree
{
        char c;
        char exp[201];
        bool self;
        struct wtree *child[45];
};

struct wtree defstr = {'0',{'\0'},FALSE,NULL} ;


struct wtree root = {'0',{'\0'},FALSE,NULL};
int main()
{
        int i =0;
        struct wtree root;
        root = defstr;
        char ch[100];
        char exp[200];
        while (i<4)
        {
                scanf("%s",ch);
                scanf("%s",exp);
                exchange(ch);
                struct wtree *ptemp = word_tree_insert(ch,exp,&root);
                printf("%c,%s\n",ptemp->c,ptemp->exp);
                i++;
        }
        char sear[20];
        while (i<8)
        {
                printf("\n SEARCH\n");
                scanf("%s",sear);
                char in[20];
                strcpy(in,sear);
                exchange(in);
                struct wtree* py = word_tree_search(in,&root);
                if(py != NULL)
                {
                         printf("\nword:%s\nexp:%s\n",sear,py->exp);
                } else
                {
                        printf("\nno found!\n");
                }

                i++;
        }
        while (i<10)
        {
                printf("\ndelete\n");
                scanf("%s",sear);
                exchange(sear);
                word_tree_delete(sear,&root);
                i++;
        }
        while(i  < 13)
        {
                printf("\n SEARCH\n");
                scanf("%s",sear);
                char in[20];
                strcpy(in,sear);
                exchange(in);
                struct wtree* py = word_tree_search(in,&root);
                if(py != NULL)
                {
                         printf("\nword:%s\nexp:%s\n",sear,py->exp);
                } else
                {
                        printf("\nno found!\n");
                }

        }

;}


bool word_tree_delete(char *str,struct wtree *pt)
{
                int len = strlen(str);
                int i = 0;
                while (1)
                {
                       if (pt->child[i] == NULL)
                       {

                                return FALSE;


                       } else if(alpcompare((pt->child[i])->c,str[len - 1]) < 0) /**** a < b **/
                       {
                               i++;
                               continue;
                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) == 0)
                       {
                               str[len - 1] = '\0';
                               if (len == 1)
                               {
                                        if ((pt->child[i])->self == TRUE)
                                        {
                                                (pt->child[i])->self = NULL;
                                                (pt->child[i])->exp[0] = '\0';

                                        }
                                        break;
                               } else
                               {
                                        word_tree_delete(str,pt->child[i]);
                                        break;

                               }

                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) > 0)
                       {
                                return FALSE;
                       }
                }
                if (!word_tree_child_enum(pt->child[i]))  /****无用节点删除 ***/
                        {

                                int templen1 = strlen(pt->child);
                                for (free(pt->child[i]);i < templen1 - 2;i++)
                                {
                                        pt->child[i] = pt->child[i + 1];
                                }
                                pt->child[templen1 - 1] = NULL;
                        }
                return TRUE;
}

bool word_tree_child_enum(struct wtree* pt)
{
                int i;
                int len = strlen(pt->child);
                if (len == 0)
                {
                        return FALSE;
                } else
                {
                       for (i = 0;i < len;i++)
                       {
                               if (pt->child[i] != NULL)
                               {
                                       return TRUE;
                               }
                       }
                       return FALSE;
                }
}

struct wtree* word_tree_search(char *str,struct wtree *pt)
{
                int len = strlen(str);
                int i = 0;
                while (1)
                {
                       if (pt->child[i] == NULL)
                       {

                                return NULL;


                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) < 0) /**** a < b **/
                       {
                               i++;
                               continue;
                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) == 0)
                       {
                               str[len - 1] = '\0';
                               if (len == 1)
                               {
                                        return (pt->child[i]);
                               } else
                               {
                                        return word_tree_search(str,pt->child[i]);
                               }

                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) > 0)
                       {
                                return NULL;
                       }
                }
}

struct wtree* word_tree_insert(char *str,char *exp,struct wtree *pt)
{
                int len = strlen(str);
                int i = 0;
                while (1)
                {
                       if (pt->child[i] == NULL)
                       {
                               pt->child[i] = creatchild();
                               *(pt->child[i]) = defstr;
                               (pt->child[i])->c = str[len - 1];
                               str[len - 1] = '\0';
                               if (len == 1)
                               {
                                       strcpy((pt->child[i])->exp,exp);
                                       (pt->child[i])->self = TRUE;
                                       printf("return %p",pt->child[i]);
                                       return (pt->child[i]);
                               } else
                               {
                                       return word_tree_insert(str,exp,pt->child[i]);
                               }

                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) < 0) /**** a < b **/
                       {
                               i++;
                               continue;
                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) == 0)
                       {
                               str[len - 1] = '\0';
                               if (len == 1)
                               {
                                        samewords++;
                                        strcpy((pt->child[i])->exp,exp);
                                        (pt->child[i])->self = TRUE;
                                        return (pt->child[i]);
                               } else
                               {
                                        return word_tree_insert(str,exp,pt->child[i]);
                               }

                       } else if (alpcompare((pt->child[i])->c,str[len - 1]) > 0)
                       {
                               int tempint;
                               int templen = strlen(pt->child);
                               for (tempint = templen - 1; tempint > i;tempint--)
                               {
                                       pt->child[tempint] = pt->child[tempint - 1];
                               }
                               pt->child[i] = creatchild();
                               (pt->child[i])->c = str[len - 1];
                               str[len - 1] = '\0';
                               if (len == 1)
                               {
                                       strcpy((pt->child[i])->exp,exp);
                                       (pt->child[i])->self = TRUE;
                                       return (pt->child[i]);
                               } else
                               {
                                       return word_tree_insert(str,exp,pt->child[i]);
                               }
                       }
                }
}


struct wtree* creatchild()
{
        struct wtree *p = (struct wtree*)malloc(sizeof(struct wtree));
        if (p == NULL)
        {
                printf("Error,out of memory!\n");
                return NULL;
        }
        *p = defstr;
        return p;
}

int alpcompare(const char a,const char b)
{
        if (toupper(a) == toupper(b))
        {
                if (a == b)
                {
                        return 0;
                } else if (a > b) /*****  A isLOWER  ********/
                {
                        return 1;
                } else if (a < b)
                {
                        return -1;
                } else if (a == b)
                {
                        return 0;
                }
        } else if (toupper(a) != toupper(b))
        {
                return (toupper(a) - toupper(b));
        }
}


int exchange(char str[])
{
    int left = 0;
    int right = strlen(str) - 1;
    char temp;
    while(1)
    {
        if ( left <= right)
        {
            temp = str[left];
            str[left++] = str[right] ;
            str[right--] = temp;
        } else{
            break;
        }
    }

}

 

posted @ 2019-12-29 13:15  哒啉  阅读(134)  评论(0编辑  收藏  举报