Giter Club home page Giter Club logo

Comments (2)

ryanplusplus avatar ryanplusplus commented on May 26, 2024

@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?

from c.

annoj avatar annoj commented on May 26, 2024

@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.

from c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.