LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2014, 04:32 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
insert() in trie with erroneous count


I'm missing something in the following code, which is providing a counter that is +1 from correct count when I enter a pre-provided "dictionary" of 143091 entries, but which enters the correct count if I enter a simple, self written "dictionary" of 10 entries. Am I missing something related to EOF or something of that sort?
The calling code is:
Code:
word = malloc(LENGTH); 
while(fgets(word,LENGTH,fp)) 
    {
    len= strlen (word);
    if (len>0 && word[len-1]=='\n') word[--len]= '\0';
                                                        
    insert (&trie, word);
And the insert() function is as follows, where d_cnt is the # of entries:
Code:
void insert(trie_t *pTrie, char key[])
{
    //declarations
    int level;  
    int length = strlen(key);
    int index;
    trie_node_t *pCrawl;
    ptrTrie = pTrie;        
      pCrawl = pTrie->root;
       
    for( level = 0; level < length; level++ )   
    {                                               
        if(CHAR_TO_INDEX(key[level])>=0)
        {
        index = CHAR_TO_INDEX(key[level]);
        }
        if(CHAR_TO_INDEX(key[level])<0)
        {
        index=26;
        }
        
        if( !pCrawl->children[index] )      
        
        {
            pCrawl->children[index] = getNode();//ADDS ANOTHER LEVEL OF NODES
        }
 
        pCrawl = pCrawl->children[index];
    }
    // mark last node as leaf
    pCrawl->value = pTrie->dSize;
    dSize++;
}
Thanks for your attention.
richard

Last edited by atlantis43; 03-14-2014 at 04:35 PM.
 
Old 03-14-2014, 10:12 PM   #2
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
disregard additional info previously posted here.

Last edited by atlantis43; 03-14-2014 at 11:32 PM. Reason: correction
 
Old 03-15-2014, 12:37 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
There might be empty lines or lines that are longer than LENGTH.

Code:
word = malloc(LENGTH); 
while(fgets(word,LENGTH,fp)) {
    ++nline;
    len= strlen (word);
    if (len>0 && word[len-1]=='\n') {
        word[--len]= '\0';
    } else {
        fprintf (stderr, "*** Problematic line #%ld\n", (long)nline);
    }                                                
    if (len==0) continue;
    insert (&trie, word);
 
1 members found this post helpful.
Old 03-15-2014, 07:47 AM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
That was, indeed, exactly correct regarding the problem with my code. Thanks again for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unloading a trie atlantis43 Programming 5 02-19-2014 10:53 AM
[SOLVED] passing parameters to trie atlantis43 Programming 12 02-14-2014 03:12 PM
Warning: [fnn_insert] Column count doesn't match value count at row 1 in bondoq Programming 2 09-27-2011 04:11 PM
Data structure trie spank Programming 7 05-21-2006 07:21 AM
ok, i trie it once more saavik Programming 3 04-18-2002 11:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration