Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8768199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:00:46+00:00 2026-06-13T17:00:46+00:00

I have written a binary tree code for inserting elements into it in non-recursive

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:00:47+00:00Added an answer on June 13, 2026 at 5:00 pm
    node = ref->left;
    

    You want

    ref->left = node;
    

    and similar for the ref->right

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a code for inserting to the binary tree an element generic's
I have written the following code to check if a tree is a Binary
I have written a code for finding diameter of Binary Tree. Need suggestions for
I have written code for finding the diameter of binary tree. But I am
I have written the below code for recursively searching binary tree . Even though
I have written a raw binary image file into a buffer which is an
I have rewritten the Binary Search Tree code found in the following links to
I have written some custom R code that wraps a third-party binary. One of
I have written a code for finding least common ancestor of nodes in binary
I have written some code in C to read a binary file containing complex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.