I'm working on a project that is supposed to parse through a text file (about the length of a book or small dictionary) and print out the index. The program does not need to keep track of the frequency of the word. The data structure I'm using is a binary search tree and an avl tree. I have two tree because the user has the option to switch between the trees if he/she wishes. My trees hold onto nodes and the nodes hold onto an string(the word) and a vector of ints(the page numbers that the word occurs on). The source code for the trees do not allow for the insert of duplicate words. My problem is that when the program runs across a duplicate word on a different page i dont know how to insert the page number into the vector of the already existing node for that particular word. Any suggestions???

Insert method in tree:

/**
         * Insert x into the tree; duplicates are ignored.
         */
        template <class Comparable>
        void AvlTree<Comparable>::insert( const Comparable & x )
        {
            insert( x, root );
        }

A portion of my program--> actual inserting:

if(c >=97 && c <= 122) {
            c = c-32;
   }
 if(c >= 65 && c <= 90) {
            word.append(1,c);
  }
  else {
           if(!word.empty()) {
           Node node(word, page);
           avl.insert(node);
           word = "";
  }

My Node.cpp:

#include "Node.h"
#include <vector>

Node::Node() {
  name = "";

}

Node::Node(string st, int p) {
  name = st;
  pages.push_back(p);
}

void Node::incPages(int p) {
        pages.push_back(p);
}

vector<int> Node::getPage() {
  return pages;
}


string Node::getName() {
  return name;
}

bool Node::operator< (const Node & rhs) const {
  if (this->name < rhs.name) {
    return true;
  }
  else {
    return false;
  }
}

ostream& operator << (ostream &out, Node &nd) {
        out << "Page Numbers: ";
        vector<int>::iterator i;
        for(i = nd.pages.begin(); i != nd.pages.end(); i++) {
                out << *i << " ";
        }
        out << "Name: " << nd.name << endl;
  return out;
}
if( !word.empty() ) 
{
   if( word does not exist in tree )
   {
           Node node(word, page);
           avl.insert(node);
    }
    else 
   {
       Node* node = get the existing node for this word from avl
      node->incPages(page) ;
   }
    word = "";
}

this part of the code is truly horrible

if(c >=97 && c <= 122) {
            c = c-32;
   }
 if(c >= 65 && c <= 90) {
            word.append(1,c);
  }

use std::islower , std::toupper , std::isupper instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.