Skip to content

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

@annoj

Description

@annoj

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions