2015-05-21 189 views
0

我嘗試用他的搜索,插入和刪除功能來編程二叉樹 但主要是編譯器無法識別在頭文件中聲明並初始化的「found」和「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; 
} 

預先感謝

+0

對於**不**使用明確的'root'指針變量的問題(和答案)請參閱http://stackoverflow.com/questions/30326969/i-would-like-not-to-use-root-as -global/ – CiaPan

回答

0

「根」 & 「找到」 是類屬性。 從主要功能,因爲它們是公共的,必須像這樣訪問他們: Tree a; a.root;

是的,當然,你應該避免使用這些屬性2公衆。讓它們專用,只能從Tree類訪問它們。

+0

感謝您的幫助。現在它可以工作,但插入後插入功能會崩潰。我是由NULL根節點引起的,所以我修改了添加條件的代碼,但它不斷崩潰 – Xdroid

+0

我認爲您應該先修改代碼以刪除公共屬性。 使它們變爲私有並更新插入方法以直接修改Tree對象的根屬性。 另外你在這裏犯了幾個錯誤: if(leaf == NULL) leaf-> data = key; else {...} 1 /您忘記關閉}在else之前 2/leaf爲NULL! (你在if中做了測試),所以你不能寫葉 - >數據! – Ptiseb