2017-04-05 158 views
0

我遇到了實現Inorder遍歷的麻煩。這是我的代碼。這不工作嗎?由於某種原因,我收到了段錯誤。請幫助C++ BST Inorder Trouble

BSTree.h

class BSTree { 
public: 
.... 
// void traverseInorder(Node* root); 

private: 
     Node * root; 
}; 

BSTree.cpp

void BSTree::inOrderPrint() { 
root->traverseInorder(root); 
} 

Node.h

class Node { 
public: 
    void traverseInorder(Node* root); 
// void printInOrder(Node* root); 
    Node* returnLeft(Node* root); 
    Node* returnRight(Node* root); 
    void printC(); 

private: 
    Customer c; 
    Node* left; 
    Node* right; 
}; 

Node.cpp

void Node::traverseInorder(Node* root){ 
    if(root==NULL){ 
     return; 
    } 
    if(root->left!= NULL){ 
     traverseInorder(root->left); 
    } 
     printC(); 
    if(right != NULL){ 
     traverseInorder(root->right); 
    } 
} 

void Node::printC(){ 
    cout<<c; 
} 

我< <有過多的

ostream& operator<<(ostream & os, Customer & c) { 
    os << c.getLastName() << ", " << c.getInitial() << ". " << "(" << c.getAccountBalance() << ")"; 
    return os; 
} 

回答

1
void Node::traverseInorder(Node* root){ 
if(root==NULL){ 
    return; 
} 
if(root->left!= NULL){ 
    root->left->traverseInorder(root->left); //call traverse function on root->left 
} 
    printC(); 
if(right != NULL){ // it should be root->right here 
    root->right->traverseInorder(root->right); 
} 
}