I have recently learned Tries algorithm and am struggling hard to implement it using linked list. I have done the array implementation and is working fine.
I am expecting the Tree for the words (bear,bed, bull,bid,buy,sell,stock,stop) to be created like this:
 /
 |              
 b--------------------------------->s
 |                                  |
 e------->i--->u                    e---->t
 |        |    |                    |     |
 a--->d   d    l--->y               l     o
 |             |                    |     |
 r             l                    l     c--->p
                                          |
                                          k
But I realize that my insert function is not doing the same and hence when I try to find these words I am not able to find them.
For example: when i search for (bear,bull,buy,sell,stop) the result is FOUND but when i search for (bed,bid,stock) the result is NOT FOUND. The expected result for all the inserted strings should have been FOUND.
I am posting my code below. Looking forward to some help and not exactly the code but just a pointer to where I m going wrong or what I am missing.
/* 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;
}
 
    