#include
#include
using namespace std;
struct BSTree {
int data;
BSTree* left;
BSTree* right;
};
void insert_Node(BSTree*& root, int n) {
if (root == NULL) {
root = new BSTree();
root->data = n;
root->left = NULL;
root->right = NULL;
}
else {
if (n <= root->data) {
insert_Node(root->left, n);
}
else {
insert_Node(root->right, n);
}
}
}
void preOder(BSTree* root) {
if (root == NULL) {
return;
}
else {
cout << root->data << " ";
preOder(root->left);
preOder(root->right);
}
}
int main() {
BSTree* root = NULL;
int n;
cin >> n;
while (n != 0) {
insert_Node(root, n);
cin >> n;
}
preOder(root);
return 0;
}
代码没有问题,我就是不理解那一句void insert_Node(BSTree*& root, int n)
里面*&这个用法,有没有同学指导下
他那个已经是指针,为啥还要取地址符号
void insert_Node(BSTree*& root, int n)
这种写法一般是因为在函数内会修改root指针地址的原因,所以用指针的引用类型
因为如果只是指针参数的话,函数内只能修改指针指向的内存空间数据,实现外部传递指针变量指向的内存空间数据修改。而无法实现修改外部传递指针变量地址的修改。