请参阅下面的 BST 代码。它只输出“5”。我做错了什么?
#include <iostream>
class bst {
public:
bst(const int& numb) : root(new node(numb)) {}
void insert(const int& numb) {
root->insert(new node(numb), root);
}
void inorder() {
root->inorder(root);
}
private:
class node {
public:
node(const int& numb) : left(NULL), right(NULL) {
value = numb;
}
void insert(node* insertion, node* position) {
if (position == NULL) position = insertion;
else if (insertion->value > position->value)
insert(insertion, position->right);
else if (insertion->value < position->value)
insert(insertion, position->left);
}
void inorder(node* tree) {
if (tree == NULL)
return;
inorder(tree->left);
std::cout << tree->value << std::endl;
inorder(tree->right);
}
private:
node* left;
node* right;
int value;
};
node* root;
};
int main() {
bst tree(5);
tree.insert(4);
tree.insert(2);
tree.insert(10);
tree.insert(14);
tree.inorder();
return 0;
}