C++ 为什么根值没有被更新
这是当我试图插入数据时,在BST中查找C++ 为什么根值没有被更新,c++,pointers,tree,binary-tree,C++,Pointers,Tree,Binary Tree,这是当我试图插入数据时,在BST中查找天花板和地板的代码。每次insert调用转到第一个if条件时。i、 虽然我传递了指针。未在主函数中更新该值。有人能告诉我为什么会这样吗 using namespace std; struct Node { int key; struct Node* right; struct Node* left; }; struct Node* newNode(int key) { struct Node* newNode = (struct
天花板
和地板
的代码。每次insert调用转到第一个if条件时。i、 虽然我传递了指针。未在主函数中更新该值。有人能告诉我为什么会这样吗
using namespace std;
struct Node
{
int key;
struct Node* right;
struct Node* left;
};
struct Node* newNode(int key)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->right = NULL;
newNode->left = NULL;
newNode->key = key;
return newNode;
}
void insert(struct Node** root,int key) {
if((*root) == NULL){
(*root)= newNode(key);
cout<<"entered first if condition"<<endl;
}
else if( (*root)->key <= key)
insert(&((*root)->left),key);
else
insert (&((*root)->right),key);
}
int ceil( struct Node* root , int input)
{
if (root == NULL)
return -1;
if(root->key == input)
return root->key;
if(root->key < input)
return ceil( root->right , input);
else{
int ceilnum = ceil(root->left, input);
return (ceilnum >= input) ? ceilnum : root->key;
}
}
int main()
{
int size, temp, ceilfor;
struct Node* root = NULL;
cout<< "size" << endl;
cin >> size;
for( int i = 0; i< size; i++)
{
cin >> temp;
insert(&root,temp);
}
cout<< root->key;
cout<< root->left->key;
cout << root->right->key;
cout << "ceil for" << endl;
cin >> ceilfor;
cout<< ceil(root, ceilfor) <<endl;
}
使用名称空间std;
结构体类型
{
int键;
结构节点*右;
结构节点*左;
};
结构节点*newNode(int键)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
newNode->right=NULL;
newNode->left=NULL;
新建节点->键=键;
返回newNode;
}
无效插入(结构节点**根,int键){
if((*root)==NULL){
(*根)=新节点(键);
库特基;
如果(根->键<输入)
返回ceil(根->右,输入);
否则{
int ceilnum=ceil(根->左,输入);
返回(ceilnum>=输入)?ceilnum:根->键;
}
}
int main()
{
内部尺寸、温度、天花板;
结构节点*root=NULL;
cout大小;
对于(int i=0;i>温度;
插入(&root,temp);
}
库特基;
coutleft->key;
cout right->key;
不能接受;
cout它必须到达第一个条件(通过递归调用直接或间接)
实际插入仅发生在第一个if
块中,其他块将递归地到达第一个if
块。请尝试在main
和insert
函数中打印root
的值?