简体   繁体   中英

Root node not in scope of main function, but defined in public class

I've been working on a program to implement a binary search tree, but using recursion, I have to call the root pointer in the main function. I get the error that 'root was not declared in this scope', although I've declared it inside the bstree class in public. I've been having a really hard time for the past couple of days coming back to this problem and searching all over the inte.net, so some help would really be appreciated. My code is below (i've removed some functions)

#include <iostream>

using namespace std;

struct Node{
  int data;
  Node* left;
  Node* right;
};

class Bstree{
  public:

  **  Node* root = nullptr;
**
    Bstree(){
      root = new Node();
    }

    void insert(Node* root, int x){
      if (root==nullptr) {
        Node* n = new Node;
        n->data = x;
        n->right = n->left = nullptr;
      } else if (x<root->data) {
        insert(root->left, x);
      } else { 
        insert(root->right, x);
      }
    }

    void inorder(Node* root){
       if (root==NULL) return;
       inorder(root->left);
       cout << root->data << " ";
       inorder(root->right);
    }
    void postorder(Node* root){
       if (root==NULL) return;
       postorder(root->left);
       postorder(root->right);
       cout << root->data << " ";
    }
};

int main(){

  Bstree BST;

  int N; cin >> N;
  for (int i=0;i<N;i++){
    int x; cin >> x;
    BST.insert(root, x);
  }

  BST.inorder(root);
  BST.postorder(root); 

}


I tried to make all recursive functions itirative, but itirative traversal functions are way more complicated than recursive. I tried writing *root instead of root when calling the functions, but I got the same error. Isn't root a global variable anyways since I declared it in a public class?

root is a member of the Bstree class, not a variable that is available in main . In main you could access it with BST.root , however, I feel that the main program has no business with that member. It should be the class that deals with those details, while the main program should just rely on the methods and not have to know about the root member.

So, I would suggest defining public methods on your class that do not take a root parameter. The class knows what the root is, so there's no need for the caller to pass it. However, the recursive calls do need that argument, but those recursive functions could be private methods.

Another issue is that your constructor creates a dummy node and assigns it to the root. This is not good. An empty tree has no node, so you should just leave the root to its initial nullptr value.

Here is an update of your code that fixes these problems:

#include <iostream>

using namespace std;

struct Node{
  int data;
  Node* left;
  Node* right;
};

class Bstree{
  private:
    Node* root = nullptr;

    void insert(Node* &root, int x){
      if (root==nullptr) {
        root = new Node;
        root->data = x;
        root->right = root->left = nullptr;
      } else if (x<root->data) {
        insert(root->left, x);
      } else { 
        insert(root->right, x);
      }
    }

    void inorder(Node* root){
       if (root==NULL) return;
       inorder(root->left);
       cout << root->data << " ";
       inorder(root->right);
    }

    void postorder(Node* root){
       if (root==NULL) return;
       postorder(root->left);
       postorder(root->right);
       cout << root->data << " ";
    }

  public:
    // Define the public methods without root argument. 
    void insert(int x){
        // But use a recursive private method that does take that argument
        // and pass that argument by reference
        insert(root, x);
    }

    void inorder() {
        inorder(root);
    }

    void postorder() {
        postorder(root);
    }
  
};

int main(){

  Bstree BST;

  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    BST.insert(x);
  }

  BST.inorder();
  cout << "\n";
  BST.postorder(); 
  cout << "\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM