Problem with Avl Tree

Hello all!
I decided to use C++ instead C because I wanted to learn about OOP so I decided to write all my university stuff using C++ from now on.

Anyway, I'm programming an AVL tree with words, it's very simple yet I'm struggling with C++ itself, the node has a field for the word itself (string) and a field showing which dictionary page I found that word, so for example:

Insert "Ball 1" the node will show Ball {1}
Insert "Ball 3" the node will show Ball {1,3}

Since I found ball at pages 1 and 3, the field containing the page number is a vector, and here's my problem:
Node Struct:
1
2
3
4
5
6
7
8
typedef struct node *Pnode;

struct node{
  std::string word;
  std::vector<int>pages;
  Pnode left, right;
  int height;
};

And the problem itself:
1
2
3
4
5
6
7
8
9
10
11
void AvlTree::set_insert(std::string &str, const std::vector<int> &v){
  if(root == NULL){
    root = new node;
    root->left = NULL;
    root->right = NULL;
    root->word = str;
    root->pages.push_back(v);
    root->height = 0;
  }
  insert(root, str, v);
}

"root->pages.push_back(v);" Here, I want to put the page number value inside the vector, I think the argument is correct right? since I need the whole vector to make the insertions but I only need to insert one field from vector v and the way I'm seeing its like in trying to put the whole vector inside "pages", what can I do ?

thks :)


Edit: found a way to fix it!

1
2
3
4
5
6
7
8
9
10
11
12
oid AvlTree::set_insert(std::string &str, const std::vector<int> &v, int page_number){
  
  if(root == NULL){
    root = new node;
    root->left = NULL;
    root->right = NULL;
    root->word = str;
    root->pages.push_back(page_number);
    root->height = 0;
  }
  insert(root, str, v, page_number);
}

Last edited on
Topic archived. No new replies allowed.