I want to create binary tree. With this program I wrote I have some problems. It seems the element I added to the tree is always added to the root place! Adding never happens to the left or the right subtree. This thing is prooved by result I always get when I add a new element to the tree -it's always just printing message "We are adding to the root or it's a new leaf!".
Because this part of the program is doing by a recursive procedure push_in_tree I gues there is a problem, but I can't find it.I'm using win7 (C-free professional 5.0).
Thanks.

/*binary tree*/

#include <stdio.h>

#define ERR_FLAG 1

struct node
{
    int id;
    struct node *left;
    struct node *right;
};

typedef struct node EL;

EL *root;
EL *current;

void error_exit(char *str);
void read_node(EL *current);
void push_in_tree(EL *new_ptr, EL *root);
void print_node(EL *current);
void visiting_nodes(EL *root);

int main()
{
    int ch;

    root = NULL;

    do
    {
        if ((current =(EL *)malloc(sizeof(EL))) == NULL)
            error_exit("malloc failed");    
        else
            {
            current->left = NULL;
            current->right = NULL;
            current->id = 0;
            }
        read_node(current);
        push_in_tree(current, root);
        printf("Is there any more nodes (yes/no)\n");
        rewind(stdin);
    }
    while ((ch=toupper(getchar())) !='N');

    visiting_nodes(root);
    printf("**All elements are visited**");

}
void error_exit(char *str)
{
    printf("%s\n", str);
    exit(ERR_FLAG);
}
void read_node(EL *current)
{
    printf("Please enter the id: ");
    scanf("%d", &(current->id));
}
void push_in_tree(EL *new_ptr, EL *root)
{
    if (root == NULL)
        {
            printf("We are adding to the root or it's a new leaf\n");
            root = new_ptr;
            root->left= NULL;
            root->right= NULL;
        }   
    else 
        if (new_ptr->id < root->id)
            {
            push_in_tree(new_ptr, root->left);
            printf("We are going left\n");
            }
        else
            {
            push_in_tree(new_ptr, root->right);
            printf("We are going right\n");
            }
}
void print_node(EL *current)
{
    printf("Current number is: %d\n", current->id);
}

void visiting_nodes(EL *root)
{
    if (root != NULL)
        {
            visiting_nodes(root->left);
            printf("Press <enter> for printing next node");
            getchar();
            rewind(stdin);
            print_node(root);
            visiting_nodes(root->right);
        }   
}

OOPS!These lines are standing reverse:
74 and 75 should be:

            push_in_tree(new_ptr, root->left);
            printf("We are going left\n");

and 79 and 80 too should be:

            push_in_tree(new_ptr, root->right);
            printf("We are going right\n");

Still needing help.Thanks.

Your push_in_tree() function has no way of telling the caller what changed. Consider this:

EL *push_in_tree(EL *new_ptr, EL *root)
{
    if (root == NULL)
    {
        root = new_ptr;
    }   
    else if (new_ptr->id < root->id)
    {
        root->left = push_in_tree(new_ptr, root->left);
    }
    else
    {
        root->right = push_in_tree(new_ptr, root->right);
    }

    return root;
}

Then it would be called like so:

root = push_in_tree(current, root);

This ensures that at all levels of recursion, you can propagate changes back up the tree.

Thanks Deceptikon! You were very quick. Now program really works. Once more: thanks!

This article has been dead for over six months. Start a new discussion instead.