2017-07-26 6 views
-4

einfach in C sprang ++ und versuchte, den folgenden Code auszuführen:nicht verstehen, die Ausgabe des gegebenen Code

#include <iostream> 

using namespace std; 


class BST_Node{ 
    private: 
     int val; 
     BST_Node* left; 
     BST_Node* right; 
    public: 
     void setVal(int); 
     int getVal(); 
     void setLeft(BST_Node*); 
     BST_Node* getLeft(); 
     void setRight(BST_Node*); 
     BST_Node* getRight(); 
     BST_Node(int val){this->val=val;this->left=nullptr;this->right=nullptr;} 
}; 

class BST{ 
    private: 
     BST_Node* root; 
    public: 
     void setBSTRoot(BST_Node* node){root=node;} 
     BST_Node* getBSTRoot(){return this->root;} 
}; 

BST createBST(int*, int); 

void placeNode(BST_Node*, int); 
void BST_Node::setVal(int val){ 
    this->val = val; 
    cout<<"11: "<< val << endl; 
} 

int BST_Node::getVal(){ 
    return this->val; 
} 

void BST_Node::setLeft(BST_Node* left){ 
    this->left = left; 
} 

void BST_Node::setRight(BST_Node* right){ 
    this->right = right; 
} 

BST_Node* BST_Node::getLeft(){ 
    return this->left; 
} 

BST_Node* BST_Node::getRight(){ 
    return this->right; 

} 

void print_inorder(BST_Node root){ 
    cout<<"working"<<endl; 
    if(root.getLeft()!=nullptr){ 
     print_inorder(*(root.getLeft())); 
    } 
    cout<<"*"<<root.getLeft()->getVal() <<"*" << endl; 
    cout<<"*"<<root.getVal() <<"*" << endl; 
    cout<<"*"<<root.getRight()->getVal() <<"*" << endl; 
} 


BST createBST(int* arr, int arr_size){ 
    BST obj_bst; 
    BST_Node* root; 
    cout<<"0 "<<endl; 
    root = obj_bst.getBSTRoot(); 
    cout<<"1: "<< (*arr) << endl; 
    //cout<<"create BST " << root->getVal() <<endl; 
    root->setVal(*arr); 
    cout<<"2 "<<endl; 
    cout<<"create BST " << root->getVal() <<endl; 
    for(int i=1;i<arr_size;i++){ 
     cout<<"create BST " << root->getVal() <<endl; 
     int curr_val = *(arr+i); 
     placeNode(root,curr_val); 
    } 
    cout<<"create BST " << root->getVal() <<endl; 
    return obj_bst; 
} 

void placeNode(BST_Node* root, int curr_val){ 
    int root_val = root->getVal(); 
    if((curr_val>root_val) && (root->getRight()!=nullptr)){ 
     placeNode(root->getRight(), curr_val); 
    }else if((curr_val<=root_val) && (root->getLeft()!=nullptr)){ 
     placeNode(root->getLeft(), curr_val); 
    }else if((curr_val>root_val) && (root->getRight()==nullptr)){ 


     BST_Node node(curr_val); 
     root->setRight(&node); 
    }else if((curr_val<=root_val) && (root->getLeft()==nullptr)){ 
     BST_Node node(curr_val); 
     root->setLeft(&node); 
    }else{ 
     cout<< "Placement denied" << endl; 
    } 

} 

//void createBST(int* arr, int arr_size); 
int main(){ 
    int arr[10] = {23,23,34,1,2,343,343,23,4343}; 
    cout<<"working"<<endl; 
    BST obj_bst = createBST(arr, 10);//sizeof(arr)/sizeof(int) 
    cout<<"working"<<endl; 
    BST_Node root = *(obj_bst.getBSTRoot()); 
    cout<<"working"<<endl; 
    print_inorder(root); 
    cout<<"working"<<endl; 
    return 0; 
} 

Der Ausgang I bekam: ": 23 1 arbeiten" Die Ausführung stoppt nach "root-> setVal (* arr);" Zeile in der "createBST" -Methode. Jede Erklärung würde sehr geschätzt werden. Vielen Dank!

Sorry über zu viel Code. Ich wusste nicht, was sonst noch hätte getan werden können, um die Frage zu stellen.

+3

Was meinen Sie mit "die Ausführung stoppt"? Gibt es einen Segfault? – bjhend

+0

Haben Sie es mit einem Debugger durchlaufen? Versuchen Sie, schrittweise durch die 'createBST'-Methode zu gehen, und Sie sollten die Ursache des Problems finden. – pstrjds

+0

Warum hast du nicht auf den Fehler geschaut, den du bekommen hast? – stark

Antwort

0

Nach der Erstellung von obj_bst wird kein Wert BST::root festgelegt. So gibt obj_bst.getBSTRoot() einen ungültigen Zeiger zurück, auf dem Sie setVal aufrufen, der einen Absturz verursacht.

Verwandte Themen