为什么根值没有更新
why is the root value not being updated
这是在BST中查找ceil
和floor
的代码。当我尝试插入数据时。每次插入调用转到第一个 if 条件时。即虽然我传递了指针。主函数中的值未更新。有人能告诉我为什么会这样吗?
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;
}
它必须达到第一个条件(直接或间接通过递归调用)。
实际插入仅发生在第一个if
块中,其他块将递归到达第一个if
块。
相关文章:
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 为什么 c++ 映射值不针对同一键更新?
- 为什么数组变量正在更新
- 为什么 beginthreadex 线程参数变量在父线程中没有更新
- 为什么字符串没有更新并且最终没有打印任何内容
- 为什么我的数组值会更新为随机值?
- 为什么在功能内擦除元素后不列出更新?
- 为什么我的矢量不随我对其元素所做的更改而更新
- 难以理解为什么返回值不会更新值
- 为什么周一周一上午3点更新日历周的数字,但不是在午夜
- 为什么在 var 未更新的情况下使用,仅在启动 var 后打印
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 为什么在C 中进行循环后,该变量未更新
- 为什么我的函数只在我用 & 符号传递结构时才更新结构?
- 为什么我的终端没有运行更新的C++代码?
- 为什么根值没有更新
- 为什么我的阵列没有更新
- 为什么std::atomic_compare_exchange会更新预期值
- 限制C++更新速率.为什么此代码每秒更新一次,而不是每秒更新 60 次
- 为什么在更新数组时不能指定构造函数