簡體   English   中英

根節點不在main function的scope中,而是定義在public class中

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

我一直在研究一個實現二叉搜索樹的程序,但是使用遞歸,我必須調用主 function 中的根指針。我得到了“根未在此范圍內聲明”的錯誤,盡管我已經在 bstree class 中公開聲明它。 在過去的幾天里,我一直很難回到這個問題並在整個 inte.net 上進行搜索,因此非常感謝您的幫助。 我的代碼在下面(我刪除了一些功能)

#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); 

}


我試圖使所有遞歸函數都具有迭代性,但迭代遍歷函數比遞歸復雜得多。 我嘗試在調用函數時寫 *root 而不是 root,但我得到了同樣的錯誤。 自從我在公共 class 中聲明它以來,root 不是一個全局變量嗎?

rootBstree class 的成員,而不是main中可用的變量。 main中,您可以使用BST.root訪問它,但是,我覺得主程序與該成員無關。 處理這些細節的應該是 class,而主程序應該只依賴這些方法,而不必知道根成員。

因此,我建議在您的 class 上定義不采用root參數的公共方法。 class 知道根是什么,因此調用者無需傳遞它。 但是,遞歸調用確實需要該參數,但那些遞歸函數可以是私有方法。

另一個問題是您的構造函數創建了一個虛擬節點並將其分配給根。 這個不好。 空樹沒有節點,因此您應該將root保留為其初始nullptr值。

這是修復這些問題的代碼更新:

#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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM