• 写了一个字典树


    上星期写了一个链表,写的很艰难,跌跌绊绊。昨天我又试着写了一个单词树。这次顺手多了,没遇到什么问题,非常顺利,我也放弃了自定义结构体类型的写法。将每一处结构体都用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 != '
    ' )
    
    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',{''},FALSE,NULL} ;
    
    
    struct wtree root = {'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
    ",ptemp->c,ptemp->exp);
                    i++;
            }
            char sear[20];
            while (i<8)
            {
                    printf("
     SEARCH
    ");
                    scanf("%s",sear);
                    char in[20];
                    strcpy(in,sear);
                    exchange(in);
                    struct wtree* py = word_tree_search(in,&root);
                    if(py != NULL)
                    {
                             printf("
    word:%s
    exp:%s
    ",sear,py->exp);
                    } else
                    {
                            printf("
    no found!
    ");
                    }
    
                    i++;
            }
            while (i<10)
            {
                    printf("
    delete
    ");
                    scanf("%s",sear);
                    exchange(sear);
                    word_tree_delete(sear,&root);
                    i++;
            }
            while(i  < 13)
            {
                    printf("
     SEARCH
    ");
                    scanf("%s",sear);
                    char in[20];
                    strcpy(in,sear);
                    exchange(in);
                    struct wtree* py = word_tree_search(in,&root);
                    if(py != NULL)
                    {
                             printf("
    word:%s
    exp:%s
    ",sear,py->exp);
                    } else
                    {
                            printf("
    no found!
    ");
                    }
    
            }
    
    ;}
    
    
    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] = '';
                                   if (len == 1)
                                   {
                                            if ((pt->child[i])->self == TRUE)
                                            {
                                                    (pt->child[i])->self = NULL;
                                                    (pt->child[i])->exp[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] = '';
                                   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] = '';
                                   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] = '';
                                   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] = '';
                                   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!
    ");
                    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;
            }
        }
    
    }

  • 相关阅读:
    PowerDesigner 12小技巧-pd小技巧-pd工具栏不见了-pd修改外键命名规则-pd添加外键
    支付宝支付开发——当面付条码支付和扫码支付
    C#开发微信门户及应用(41)--基于微信开放平台的扫码登录处理
    ***微信 该连接无法访问问题解决办法
    JQuery 导入导出 Excel
    用PowerDesigner对现有的数据库反向工程建立E-R图
    js 停止事件冒泡 阻止浏览器的默认行为(阻止超连接 #)
    微信网页第三方登录原理
    C# 添加Windows服务,定时任务
    微信公众平台开发(104) 自定义菜单扫一扫、发图片、发地理位置
  • 原文地址:https://www.cnblogs.com/dosu/p/12114754.html
Copyright © 2020-2023  润新知