gpt4 book ai didi

C++ BST 顺序问题

转载 作者:行者123 更新时间:2023-11-28 05:12:40 25 4
gpt4 key购买 nike

我在实现中序遍历时遇到了问题。这是我的代码。这行不通吗?由于某种原因,我遇到了段错误。请帮忙

BSTree.h

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

private:
Node * root;
};

BSTree.cpp

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

节点.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;
};

节点.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;
}

最佳答案

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

关于C++ BST 顺序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com