Insert 4 or 5 numbers in a binary tree, but get only 3 numbers in the output

This is part of the laboratory for the school, which deals with recursion and the binary tree. If I go to insert 4 or 5 numbers and print the result, I get only 3 numbers. Here is the code to insert:

Node *insert(Node *t, int key) {
    Node *insertParent;
    Node *result=NULL;

    if (t!=NULL) {
        result=search(t,key,insertParent);
    } else {
        t=new Node;
        t->data=key;
        t->leftchild=NULL;
        t->rightchild=NULL;
        return t;
    }

    if (result==NULL) {
        if (insertParent->data>key) {
            insertParent->leftchild=new Node;
            insertParent->leftchild->data=key;
            insertParent->leftchild->leftchild=NULL;
            insertParent->leftchild->rightchild=NULL;
            return insertParent->leftchild;
        } else if (insertParent->data<key) {
            insertParent->rightchild=new Node;
            insertParent->rightchild->data=key;
            insertParent->rightchild->leftchild=NULL;
            insertParent->rightchild->rightchild=NULL;
            return insertParent->rightchild;
        }
    } else
        return NULL;
}

But I believe that the problem is in the search function, in particular, the node pointer at the parent link:

Node* search(Node *t, int key, Node *&parent) {
    if (t!=NULL) {
        parent=t;
        if (t->data==key)
            return t;
        else if (t->data>key)
            return search(t->leftchild,key,t);
        else 
            return search(t->rightchild,key,t);
    } else
        return NULL;
}

I have a function that outputs a tree and tested it against a tree that I created manually, and it works fine:

void inorder(Node *t)
{
    if (t!=NULL) {
        if (t->leftchild!=NULL)
            inorder(t->leftchild);

        cout << t->data << ", ";

        if (t->rightchild!=NULL)
            inorder(t->rightchild);                     
    }  
}

Not looking for an answer, just looking for the area I should look at.

+5
source share
3 answers

, , . node. else/ifelse, , , .

Node* search(Node *t, int key, Node *&parent) {
    if (t!=NULL) {
        if (t->data==key) {
            parent=NULL;
            return t;
        } else if (t->data>key && t->leftchild!=NULL) {
            return search(t->leftchild,key,parent); // think this needs returned
        } else if (t->data>key && t->leftchild==NULL) {
            parent=t;
            return NULL;
        } else if (t->data<key && t->rightchild!=NULL) {
            return search(t->rightchild,key,parent); //think this needs returned
        } else if (t->data<key && t->rightchild==NULL) {
            parent=t;
            return NULL;
        }
    } else {
        parent=NULL;
        return NULL;
    }
}

:

Node *insert(Node *t, int key) {
    Node *insertParent=NULL;
    Node *result=NULL;

    if (t!=NULL) {
        result=search(t,key,insertParent);
    } else {
        t=new Node;
        t->data=key;
        t->leftchild=NULL;
        t->rightchild=NULL;
        return t;  
    }

    if (insertParent==NULL && result!=NULL) {
        return NULL;
    } else if (insertParent!=NULL && result==NULL) {
        //key not found need to add
        if (insertParent->data>key) {
            insertParent->leftchild=new Node;
            insertParent->leftchild->data=key;
            insertParent->leftchild->leftchild=NULL;
            insertParent->leftchild->rightchild=NULL;
            return insertParent->leftchild;
        } else {
            insertParent->rightchild=new Node;
            insertParent->rightchild->data=key;
            insertParent->rightchild->leftchild=NULL;
            insertParent->rightchild->rightchild=NULL;
            return insertParent->rightchild;
        }
    }
}

, ...

(: . , ? , ..)

0

. , "" node.

+3
Node* search(Node *t, int key, Node *&parent)
{
    if(t!=NULL)
    {
    parent=t;
    if (t->data==key)
        return t;

    else if (t->data>key)
        return search(t->leftchild, key, parent); // use "parent" because you want to assign parent to this variable

    else 
        return search(t->rightchild,key, parent);
    }
    else     return NULL;

}
0
source

All Articles