1

I have written a binary tree code for inserting elements into it in non-recursive method. The code is not working as intended. Regardless of how many times I debug the code, nothing seems to be wrong but I am getting wrong results. I am hoping you guys can help. Thanks in advance.

void insert(int element){
    if(root == NULL){
        struct elemq *node;
        node = (struct elemq *)malloc(sizeof(struct elemq));
        node->ele = element;
        node->left = NULL;
        node->right = NULL;
        root = node;
        cout << root->ele << "\n";
    }
    else{
        struct elemq *ref;
        ref = root;
        while(ref != NULL){
            if(element <= ref->ele){
                if(ref->left == NULL){
                    struct elemq *node;
                    node = (struct elemq *)malloc(sizeof(struct elemq ));
                    node->ele = element;
                    node->left = NULL;
                    node->right = NULL;
                    ref->left = node;
                    break;
                }
                else{
                    ref = ref->left;
                }
            }
            else if(element > ref->ele){
                if(ref->right == NULL){
                    struct elemq *node;
                    node = (struct elemq *)malloc(sizeof(struct elemq ));
                    node->ele = element;
                    node->left = NULL;
                    node->right = NULL;
                    ref->right = node;
                    break;
                }
                else{
                    ref = ref->right;
                }
            }
        }
    }
}

Every time I try to insert an element, each element is being treated as root, not only the first time. So, every time, the condition if(root == NULL) is true. I declared root as global variable and initialized it to NULL in main(). I came to know this by putting cout << in the first if() condition. I modified my previous post to this new question.

2
  • 1
    Some code somewhere is trashing root. Insert cout << root << "\n"; all over the place and seen if it transitions back to zero. Then find where it happens... Commented Oct 28, 2012 at 5:05
  • its my mistake. sorry. each time i am inserting an element, i am making root as null in the main. thank you Commented Oct 28, 2012 at 5:13

2 Answers 2

2
node = ref->left;

You want

ref->left = node;

and similar for the ref->right

Sign up to request clarification or add additional context in comments.

1 Comment

I made the change. Still, every time i try to insert an element, each element is being treated as root, not only at first time. So, every time, the condition if(root == NULL) is being true. I declared root as global variable and initialized it to NULL in main().
1

I think you are mistakenly setting the node that you are adding to the references, instead of setting the left and right references to the node you are adding. Change node = ref->left to ref->left = node, and likewise for the right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.