二叉树:节点未被识别

时间:2015-05-21 07:52:41

标签: c++ binary-tree

尝试使用搜索,插入和删除功能对二叉树进行编程 但主要是编译器没有识别"发现"和" root",在标头中声明并在构造函数中初始化 这是代码

binarytree.cpp

#include <iostream>
#include <cstddef>
#include "Tree.h"
using namespace std;

int main() {
    Tree a;
    char c;
        while(c!='4'){
            cout<<"Inserisci 1 per inserire un valore nell'albero\n 2 per cercarne un valore\n";
            cout<<"3 per distruggere un valore e tutti i suoi rami\n 4 per uscire";
            cin>>c;
            if(c=='1'){
                                    int n;
                                    cout<<"Inserisci il valore numerico da immettere";
                                    cin>>n;
                                    a.insert(n,a.root);
                                }
            if(c=='2'){
                                    int n;
                                    cout<<"Inserisci il valore numerico da cercare";
                                    cin>>n;
                                    a.search(n,a.root,a.found);
                                    if(a.found==NULL)
                                        cout<<"Elemento non trovato";
                                }
            if(c=='3'){
                                    int n;
                                    cout<<"Inserisci il valore numerico da eliminare con tutti i suoi rami";
                                    cin>>n;
                                    a.search(n,a.root,a.found);
                                    if(a.found==NULL)
                                        cout<<"Elemento non trovato";
                                    a.destroy_tree(a.found);
                                }
        }
    return 0;
}

tree.h中

#ifndef TREE_H_
#define TREE_H_

#include <cstddef>
class Tree {
private:
         struct node{
            node *right;
            node *left;
            int data;
            };
public:
         Tree(){
            root = NULL;
            found = NULL;
         }
    void destroy_tree(node*);
    void insert(int, node*);
    void search(int, node*,node*);
    node *root;
    node *found;
};

#endif /* TREE_H_ */

Tree.cpp

#include "Tree.h"
#include <cstddef>

void Tree::destroy_tree(node *leaf){
    if(leaf!=NULL){
        Tree::destroy_tree(leaf->left);
        Tree::destroy_tree(leaf->right);
        delete leaf;
    }
}

void Tree::insert(int key, node *leaf){
   if(leaf==NULL)
    leaf->data=key;
   else{
  if(key<leaf->data)
  {
    if(leaf->left!=NULL)
     insert(key, leaf->left);
    else
    {
      leaf->left = new node;
      leaf->left->data=key;
      leaf->left->left=NULL;    //Sets the left child of the child node to null
      leaf->left->right=NULL;   //Sets the right child of the child node to null
    }
  }
  else if(key>=leaf->data)
  {
    if(leaf->right!=NULL)
      insert(key, leaf->right);
    else
    {
      leaf->right=new node;
      leaf->right->data=key;
      leaf->right->left=NULL;  //Sets the left child of the child node to null
      leaf->right->right=NULL; //Sets the right child of the child node to null
    }
  }
}
}
void Tree::search(int key, node *leaf, node* found){
  if(leaf!=NULL){
    if(key==leaf->data)
      found = leaf;
    if(key<leaf->data)
      search(key, leaf->left, found);
    else
      search(key, leaf->right, found);
  }
  else found = NULL;
}

提前致谢

1 个答案:

答案 0 :(得分:0)

&#34; root&#34; &安培; &#34;实测&#34;是类属性。 从main函数,因为它们是公共的,您必须像这样访问它们:     Tree a;     a.root;

当然,您应该避免将这两个属性用作公共属性。将它们设为私有,只能从Tree类访问它们。