Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 递归地将1添加到BST中的所有节点,具有最小数据的节点除外_C++_Recursion_Binary Search Tree - Fatal编程技术网

C++ 递归地将1添加到BST中的所有节点,具有最小数据的节点除外

C++ 递归地将1添加到BST中的所有节点,具有最小数据的节点除外,c++,recursion,binary-search-tree,C++,Recursion,Binary Search Tree,我试图将+1添加到每个节点的数据中,除了数量最小的节点。到目前为止,我的实现没有正常进行,我在递归调用中迷失了方向。我的代码在某些情况下添加不正确,并且在必要时没有添加。我知道要找到最小的数据块,我们就去连接到一路左(8)的节点。在这种情况下,我是否缺少某些测试条件 Given a data set: 8, 14, 24, 29, 31, 35, 46, 58, 62,85, 95 Expected results: 8, 15, 25, 30, 32, 36, 47, 59, 63, 86,

我试图将+1添加到每个节点的数据中,除了数量最小的节点。到目前为止,我的实现没有正常进行,我在递归调用中迷失了方向。我的代码在某些情况下添加不正确,并且在必要时没有添加。我知道要找到最小的数据块,我们就去连接到一路左(8)的节点。在这种情况下,我是否缺少某些测试条件

Given a data set: 8, 14, 24, 29, 31, 35, 46, 58, 62,85, 95

Expected results: 8, 15, 25, 30, 32, 36, 47, 59, 63, 86, 96
Actual results: 9, 14, 25, 29, 32, 36, 46, 59, 63, 85, 96

struct node
{

 node * left;
 node * right;
 int data;

};

int add1(node * root) 
{

    if(!root) return 0;    
    add1(root->left); //go left

    if(!root->left)  { //if left is NULL
        if(root->right) //check if there is a right child
            add1(root->right); //go to that node
        else
            return 0;
    }

    root->data += 1;    //add 1 to node
    add1(root->right); //go right

return 1;
}

int main()
{
node * root = NULL;
build(root); //inserts data set into our tree

display(root);
add1(root);
display(root);

return 0;

}

您可以沿着树向下移动,跟踪您是否是最左侧的节点。如果您曾经右转到达某个节点,该节点不可能是最左边的。如果您可能是最左边的节点,并且没有左边的子节点,那么您就是最左边的节点。其他所有内容都添加了1

void add1(root* node, bool mightbeLeftmost=true)
{
    if(!root) return;
    if(!mightbeLeftmost || root->left != nullptr) ++(root->data);
    add1(root->left, mightbeLeftmost);
    add1(root->right, false);
}

int main()
{
    //define list
    ...
    add1(root, true);
}

您可以沿着树向下移动,跟踪您是否是最左侧的节点。如果您曾经右转到达某个节点,该节点不可能是最左边的。如果您可能是最左边的节点,并且没有左边的子节点,那么您就是最左边的节点。其他所有内容都添加了1

void add1(root* node, bool mightbeLeftmost=true)
{
    if(!root) return;
    if(!mightbeLeftmost || root->left != nullptr) ++(root->data);
    add1(root->left, mightbeLeftmost);
    add1(root->right, false);
}

int main()
{
    //define list
    ...
    add1(root, true);
}

函数的解决方案还有一个额外的好处:除了增加除最小值以外的所有值,它还返回最小BST值。如果最小值不是唯一的,它也可以工作

#include <limits.h>

...

int add1(struct node* root)
{
        static int min;

        if (root == NULL)
          return INT_MAX;

        int lval = add1(root->left);

        // Check if it's the leftmost node to set min
        if (lval == INT_MAX)
            min = root->data;

        add1(root->right);

        if (root->data != min)
            root->data++;

        return min;
}
#包括
...
int add1(结构节点*根)
{
静态int-min;
if(root==NULL)
返回INT_MAX;
int lval=add1(根->左);
//检查它是否是要设置最小值的最左侧节点
如果(lval==INT_MAX)
最小=根->数据;
add1(根->右);
如果(根->数据!=min)
root->data++;
返回最小值;
}

函数的解决方案还有一个额外的好处:除了增加除最小值以外的所有值,它还返回最小BST值。如果最小值不是唯一的,它也可以工作

#include <limits.h>

...

int add1(struct node* root)
{
        static int min;

        if (root == NULL)
          return INT_MAX;

        int lval = add1(root->left);

        // Check if it's the leftmost node to set min
        if (lval == INT_MAX)
            min = root->data;

        add1(root->right);

        if (root->data != min)
            root->data++;

        return min;
}
#包括
...
int add1(结构节点*根)
{
静态int-min;
if(root==NULL)
返回INT_MAX;
int lval=add1(根->左);
//检查它是否是要设置最小值的最左侧节点
如果(lval==INT_MAX)
最小=根->数据;
add1(根->右);
如果(根->数据!=min)
root->data++;
返回最小值;
}

对不起,你是想添加我节点的结构吗?是的,节点声明和初始化对不起,你是想添加我节点的结构吗?是的,节点声明和初始化这是一个二进制搜索树。您不需要遍历整个树来发现最低值。最低的值总是树中最左边的值。哦,是的,我错过了那部分。威尔:这是一个二叉搜索树。您不需要遍历整个树来发现最低值。最低的值总是树中最左边的值。哦,是的,我错过了那部分。将调整