C ++ BST Inorder Trouble

时间:2017-04-05 02:42:30

标签: c++ binary-search-tree inorder

我在实施Inorder遍历方面遇到了麻烦。这是我的代码。这不行吗?我出于某种原因得到了段故障。请帮忙

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

和我的&lt;&lt;

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

1 个答案:

答案 0 :(得分: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); 
}
}