Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Search Tree: Test root node with value == 0 is allowed #810

Open
annoj opened this issue Aug 7, 2022 · 2 comments
Open

Binary Search Tree: Test root node with value == 0 is allowed #810

annoj opened this issue Aug 7, 2022 · 2 comments

Comments

@annoj
Copy link

annoj commented Aug 7, 2022

In my attempt to solve the Binary Search Tree exercise, I first came up wit a solution that would have overwritte it's root node when building a tree if the value of the root node was 0. This behaviour was not cought by the provided tests, but by @siebenschlaefer, who did a great job providing mentoring and feedback.

I would suggest to add a test for the tree to allow zero as the value of the root node to the testsuite. I have already written such a test for the C track and am happy to open a PR to add it.

in case you deem this an issue applicable to all language tracks, I'm happy to also open a PR in https://github.com/exercism/problem-specifications.

The test I propose to add is as follows:

static void test_data_can_have_zero_as_first_node(void)
{
    int tree_data[] = { 0, 0, 1, 2, 3 };
    node_t *tree = build_tree(tree_data, ARRAY_SIZE(tree_data));

    TEST_ASSERT_NOT_NULL(tree);
    TEST_ASSERT_EQUAL_INT(0, tree->data);
    TEST_ASSERT_NOT_NULL(tree->left);
    TEST_ASSERT_NOT_NULL(tree->right);

    TEST_ASSERT_EQUAL_INT(0, tree->left->data);
    TEST_ASSERT_NULL(tree->left->left);
    TEST_ASSERT_NULL(tree->left->right);

    TEST_ASSERT_EQUAL_INT(1, tree->right->data);
    TEST_ASSERT_NULL(tree->right->left);
    TEST_ASSERT_NOT_NULL(tree->right->right);

    TEST_ASSERT_EQUAL_INT(2, tree->right->right->data);
    TEST_ASSERT_NULL(tree->right->right->left);
    TEST_ASSERT_NOT_NULL(tree->right->right->right);

    TEST_ASSERT_EQUAL_INT(3, tree->right->right->right->data);
    TEST_ASSERT_NULL(tree->right->right->right->left);
    TEST_ASSERT_NULL(tree->right->right->right->right);

    free_tree(tree);
}
@ryanplusplus
Copy link
Member

@annoj sorry for the delay in responding. Can you help me understand what's special about 0 as the first node? How did your implementation fail?

@annoj
Copy link
Author

annoj commented Aug 13, 2022

@ryanplusplus in my first implementation I used the following implementation for build_tree():

static void _insert_node(node_t* tree, int value)
{
    if (!tree->data) {
        tree->data = value;

        return;
    }

    if (value <= tree->data) {
        if (!tree->left) {
            tree->left = calloc(1, sizeof(node_t));
            tree->left->data = value;
        } else {
            _insert_node(tree->left, value);
        }
    } else {
        if (!tree->right) {
            tree->right = calloc(1, sizeof(node_t));
            tree->right->data = value;
        } else {
            _insert_node(tree->right, value);
        }
    }
}

node_t *build_tree(int *tree_data, size_t tree_data_len)
{
    node_t* tree = calloc(1, sizeof(node_t));

    for (size_t i = 0; i < tree_data_len; i++) {
        _insert_node(tree, tree_data[i]);
    }

    return tree;
}

In the first line of _insert_node() I would check whether tree->data is not null (which is obviously a mistake). This was not cought by any of the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants