Thread: BST addword function

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    11

    BST addword function *edited*

    Hi!
    I'm building a Tree that reads words in from a text file and adds it to the tree in sibling or child branches. Everything is being scanned in ok, but something is wrong with Addaword. It seems to not be getting past the first word of the array, and just winds up repeating it. Does anyone know how I would increment it correctly?

    *added more code to clarify*
    Code:
    class Node     
    {
       private:
       Node *sibling;
       Node *child;
       int occurs;
       int letters;
    
       public:
       AddAWord(char word[], int i);
       Search(char request[]);
       Node(char l) {occurs=0; sibling=0; child=0; letters=l;}
    };
    
    void main(void)
    {        
            Node *root=new Node('-');
            Node* new_node;
            char* word[80];
            int len;
            FILE*input;
            char* request[10];
            char str[80];
            int i = 0, k=0, l=0;
            ifstream b_file("abook.txt", ios::in);
    
                    b_file>>str;
                    for(int j=0; j < strlen(str); j++)
                    {
                            str[j] = tolower((int)str[j]); 
    			}
                   len = strlen(str);
                    word[i] = new (char[len+1]);
    
                   strcpy(word[i++], str);
                    root->AddAWord(*word, l);
           while(b_file.eof()==0 || i<80)
            {
                    b_file>>str;
                    for(int j=0; j < strlen(str); j++)
                    {                                   
    					str[j] = tolower((int)str[j]);
                    }
                    len = strlen(str);
                    word[i] = new (char[len+1]);
                    strcpy(word[i++], str);
                    new_node->AddAWord(word[i], l);
            }
            cout<<"What word would you like to search for: "<<endl;
            cin>>str;
            len = strlen(str);            
      request[k] = new (char[len+1]);
            strcpy(request[k++], str);
    }
    {
     cout<<word;
    
      if(letters!=word[0])
      {
        if(sibling==0)
        {               
     sibling=new Node(*word);
              i++;
          sibling->AddAWord(word, i);
        }
            else
            {
                    sibling->AddAWord(word, i++);
            }
      }
      else if(word[i] != '\0')
      {
              if(child == 0)
              {           
    child=new Node(*word);
                      printf("%c", word[i]);
                      i++;
                      child->AddAWord(word, i);
              }
             else
             {
                      child->AddAWord(word, i++);
            }
    }
    }
    Last edited by raell; 12-14-2003 at 09:52 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It would help to see some more code. Like what is the line above that opening brace? And where is letters declared? And you shouldn't mix cout<< and printf...Just stick with one
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You should work on your data structure. A BST is meant to simplify the manipulation of data, not make it more complicated. A little more organization might help, too...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >A BST is meant to simplify the manipulation of data, not make it more complicated.
    Not really. A binary search tree is meant to improve the efficiency of operations that involve searching. If we wanted to simplify data manipulation, everything would either use arrays or simple linked lists. A basic binary search tree is simple, but definitely more complicated than a linked list, and when you introduce balancing to guarantee logarithmic performance, you lose that simplicity.
    My best code is written with the delete key.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Case in point: there is no memory management to speak of in the code posted by the OP. A data structure has to be stable at any given moment, birth through destruction, to be considered good enough for use - right? If memory leaks or pointers dangle - how useful is that? Just seems illogical to me - that's all.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Dude, post more of your code; afterall how are we supposed to help if we can't see the engine of the car?? (i.e. AddAWord itself)
    Or you can chechk one of my early posts with attached code that does something similar.. ('bout 10-15 pages down I thinks)
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >A data structure has to be stable at any given moment
    Different people will have different ideas of a stable data structure. I see a stable data structure as one that can have any legal operation performed on it without breaking or causing future legal operations to break. This definition says nothing about memory management because I often write data structures that do not manage their own memory.

    >If memory leaks or pointers dangle - how useful is that?
    Memory leakage isn't a problem with programs that don't run for long on systems that reclaim memory allocated to them. Dangling pointers are only a problem if they are referenced or actually break the data structure. This isn't to say that allowing either is good style, but it isn't actually wrong.

    >Just seems illogical to me
    Sadly, real-world programming is often illogical.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM