为了好玩,我一直在尝试用 C++ 实现二叉搜索树。我的问题是我的插入功能有问题。以下是我到目前为止的内容:
class TreeNode{
public:
int data;
TreeNode *left;
TreeNode *right;
void Insert(int value, TreeNode *x);
void TreeNode::Print(TreeNode *x);
TreeNode();
};
TreeNode::TreeNode(){
left = NULL;
right = NULL;
}
.
void TreeNode::Insert(int value, TreeNode *x){
if(x->left == NULL && x->right == NULL){
TreeNode *tree = new TreeNode();
tree->datavalue;
x->left = tree;
}
else if(x->left == NULL && x->right != NULL){
TreeNode *tree = new TreeNode();
tree->data = value;
x->left = tree;
}
else if(x->left != NULL && x->right == NULL){
TreeNode *tree = new TreeNode();
tree->data = value;
x->right = tree;
}
else if(x->left != NULL && x->right != NULL){
??????
}
}