开发者

binary search tree - adding nodes

I built a function that needs to add a node to a binary search tree that is sorted by its ID (moviecode = id) but it doesn't work right. can you help me figure out whats wrong with the code?

Node *buildtree(Node *dataTree, char *name, int id, float rating, int numvote) {

    if ((dataTree == NULL)) {

        dataTree= make_node();
        strcpy(dataTree->data.movieName,name);
        dataTree->data.meanRaiting = rating;
        dataTree->data.numOfVoters = numvote;
        dataTree->开发者_如何学编程;data.movieCode = id;
        return;

    }

    else if (dataTree->data.movieCode > id) 
        (buildtree (dataTree->left, name, id, rating, numvote));    

    else 
        buildtree (dataTree->right, name, id, rating, numvote);     

    return dataTree;
}


At first glance, it looks to me like dataTree is getting instantiated, but it's not attached to anything. You should have a pointer point to the new object or it'll be lost at the end of the method.

What most people do in situations like this is pass a double pointer. When you need to modify the pointer, you simply do *dataTree = make_node();

Does that help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜