TRIES实施

时间:2015-12-21 19:27:50

标签: c algorithm data-structures

我最近学习了Tries算法,并且正在努力使用链表实现它。我已经完成了数组实现并且工作正常。

我期待树的话 (熊,床,公牛,买入,买入,卖出,股票,止损)将被创建如下:

 /
 |              
 b--------------------------------->s
 |                                  |
 e------->i--->u                    e---->t
 |        |    |                    |     |
 a--->d   d    l--->y               l     o
 |             |                    |     |
 r             l                    l     c--->p
                                          |
                                          k

但是我意识到我的插入功能没有做同样的事情,因此当我试图找到这些单词时,我无法找到它们。

例如:当我搜索(熊,公牛,买入,卖出,停止)时,结果是FOUND,但是当我搜索(床,买入,卖出)时,结果没有找到。所有插入的字符串的预期结果应该是FOUND。

我在下面发布我的代码。期待一些帮助而不是完全代码,只是指向我出错的地方或我错过的地方。

/* I am trying to add few words in the tree using Tries Algorithm and 
 * then trying to find them */

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

typedef struct node{
    char info;
    struct node *nextSibling;
    struct node *child;
    int count;
} node;

struct node* insert(struct node *ptr,char *string);

typedef enum Result {FALSE,TRUE} SEARCH;

SEARCH find(struct node *ptr,char *string);

void main()
{
    struct node *headptr = NULL;
    char string[256];

    headptr = insert(headptr,"bear");
    headptr = insert(headptr,"bed");
    headptr = insert(headptr,"bid");
    headptr = insert(headptr,"bull");
    headptr = insert(headptr,"buy");
    headptr = insert(headptr,"sell");
    headptr = insert(headptr,"stock");
    headptr = insert(headptr,"stop");

    printf("bear bed bid bull buy sell stock stop\n");
    printf("Enter a string to search:\n");
    scanf("%s",string);

    if(find(headptr,string)){
        printf("Found\n");
    }else{
        printf("Not Found\n");
    }

}

struct node* insert(struct node *ptr,char *string){

    struct node *temp = NULL;   
    struct node *p    = NULL;   
    char   info       = '\0';

    if(!string[0])
        return NULL;

    info = string[0];
    string++;

    temp = (struct node *)malloc(sizeof(struct node));
    temp->info    = info;
    temp->nextSibling = NULL;
    temp->child       = NULL;
    temp->count       = 1;

    if((ptr == NULL)||(ptr->info > info)){
        temp->nextSibling = ptr;
        ptr = temp;

        ptr->child = insert(ptr->child,string);
        return ptr;
    }
    else{
        p = ptr;
        while(p->nextSibling){
            if(p->info == info){
                p->count += 1;
                free(temp);
                p->child = insert(p->child,string);
                if ( ptr->nextSibling == p )
                    return ptr;
                else
                    return p;
            }

            if((p->nextSibling)->info > info){
                temp->nextSibling = p->nextSibling;
                p->nextSibling = temp;

                temp->child = insert(temp->child,string);
                if ( ptr->nextSibling == p )
                    return ptr;
                else
                    return p;
            }

            p = p->nextSibling;
        }

        if(!(p->nextSibling)){  
            if(p->info == info){
                p->count += 1;
                free(temp);
                temp = NULL;
                p->child = insert(p->child,string);
                if ( ptr->nextSibling == p )
                    return ptr;
                else
                    return p;
            }
            else{
                temp->nextSibling = p->nextSibling;
                p->nextSibling = temp;
                temp->child = insert(temp->child,string);
                if ( ptr->nextSibling == p )
                    return ptr;
                else
                    return p;
            }
        }
    }
}


SEARCH find(struct node *ptr,char *string){

    char info = '\0';
    struct node *p = NULL;
    SEARCH rc = FALSE;

    if(!string[0]){ 
        rc = TRUE;
        return rc;
    }

    info = string[0];
    string++;

    if(ptr == NULL){
        rc = FALSE;
    }
    else{
        p = ptr;
        while(p){
            if(p->info == info){
                rc =find(p->child,string);
                break;
            }
            else if(p->info > info){
                if (!string[0])
                    rc = FALSE;
                else
                    rc =find(p->child,string);  
                break;
            }
            else if ( p->info < info)
            {
                rc =find(p->child,string);
                break;
            }
            p = p->nextSibling;
        }
    }

    return rc;
}

1 个答案:

答案 0 :(得分:1)

我认为我只是弄乱了插入函数中的返回地址,这相当于错误的结果。我也不知道在写find函数时我在想什么。

我在这里发布我的工作代码,但我对此并不满意,因为我认为它可以做得更好。

请评论:)

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

typedef struct node{
        char info;
        struct node *nextSibling;
        struct node *child;
        int count;
    } node;

struct node* insert(struct node *ptr,char *string);

void print(struct node *ptr);

typedef enum Result {FALSE,TRUE} SEARCH;

SEARCH find(struct node *ptr,char *string);

void main()
{
    struct node *headptr = NULL;
    char string[256];

    headptr = insert(headptr,"bear");
    headptr = insert(headptr,"bid");
    headptr = insert(headptr,"bed");
    headptr = insert(headptr,"bull");
    headptr = insert(headptr,"buy");
    headptr = insert(headptr,"sell");
    headptr = insert(headptr,"stock");
    headptr = insert(headptr,"stop");

    printf("bear bed bid bull buy sell stock stop\n");
    printf("Enter a string to search:\n");
    scanf("%s",string);

    if(find(headptr,string)){
        printf("Found\n");
    }else{
        printf("Not Found\n");
    }
}

struct node* insert(struct node *ptr,char *string){

    struct node *temp = NULL;   
    struct node *p    = NULL;   
    char   info       = '\0';

    if(!string[0])
    return NULL;

    info = string[0];
    string++;

    temp = (struct node *)malloc(sizeof(struct node));
    temp->info    = info;
    temp->nextSibling = NULL;
    temp->child       = NULL;
    temp->count       = 1;

    if((ptr == NULL)||(ptr->info > info)){ /*Inserting 1st node or first node greater than the new node*/
        temp->nextSibling = ptr;
        ptr = temp;

        temp->child = insert(temp->child,string);
        return ptr;
    }
    else{
        p = ptr;
        while(p->nextSibling){
            if(p->info == info){
                p->count += 1;
                free(temp);
                p->child = insert(p->child,string);
                return ptr;
            }

            if((p->nextSibling)->info > info){
                temp->nextSibling = p->nextSibling;
                p->nextSibling = temp;

                temp->child = insert(temp->child,string);
                return ptr;
            }

            p = p->nextSibling;
        }

        if(!(p->nextSibling)){  
            if(p->info == info){ /*1st node or last node equal to the new node*/
                p->count += 1;
                free(temp);
                temp = NULL;
                p->child = insert(p->child,string);
                    return ptr;
            }
            else{  /*New node is to be inserted at the end*/
                temp->nextSibling = p->nextSibling;
                p->nextSibling = temp;
                temp->child = insert(temp->child,string);
                        return ptr;
            }
        }
    }

}

SEARCH find(struct node *ptr,char *string){

    char info = '\0';
    struct node *p = NULL;
    SEARCH rc = FALSE;

    if(!string[0]){ /* End of string case */
        rc = TRUE;
        return rc;
    }

    if(ptr == NULL){
        rc = FALSE;
    }
    else{
        p = ptr;
        while(p){
            if(p->info == string[0]){/*Checking the first node for match*/
                rc = find(p->child,++string);
                return rc;
            }
            p = p->nextSibling;
        }
    }
    return rc;
}