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 8576685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:58:51+00:00 2026-06-11T19:58:51+00:00

I came across some threads on StackOverflow but none of them quite cleared my

  • 0

I came across some threads on StackOverflow but none of them quite cleared my doubts.

So the problem is simple. I need to iteratively insert elements into a binary tree. And this is my code.

BST newNode(int x)
{
    BSTNodePtr node = (BSTNodePtr) malloc(sizeof(struct TreeNode));
    node->Element = x;
    node->Left = NULL;
    node->Right = NULL;

    return node;

}

BST Insert(int x, BST T)
{
    BST temp_node = T;
    while( T != NULL)   {
        if (x < T->Element)
            T = T->Left;
        else if (x >= T->Element)
            T = T->Right;
    }

    T = newNode(x);

    return temp_node;

}

However, when I’m finding the height of this tree I am always getting 0. The height code is

int Height(BST T)
{
    if (T == NULL)
        return 0;

    return 1+(max(Height(T->Left), Height(T->Right)));
}

and this works perfectly fine when I do insertion recursively (using a function with the exact same signature)

What am I missing?

  • 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-11T19:58:52+00:00Added an answer on June 11, 2026 at 7:58 pm

    Couldn’t solve the problem that way. This code, however, seems to work.

    BST Insert(int x, BST T)
        {
           BST temp=T;
           BST node=(BST)malloc(sizeof(struct TreeNode));
           node->Element=x;
           node->Left=NULL;
           node->Right=NULL;
           if (T==NULL)
           {
               T=node;
               return(T);
               //printf("%d\n",T->Element);
           }
           else
           {
               while(1)
               {
                   if (temp->Element>=node->Element && temp->Left==NULL)
                   {
                       temp->Left=node;
                       break;
                   }
                   else if (temp->Element>=node->Element && temp->Left!=NULL)
                   {
    
                       temp=temp->Left;
                   }
                   else if (temp->Element<node->Element && temp->Right==NULL)
                   {
                       temp->Right=node;
                       break;
                   }
                   else
                   {
                       temp=temp->Right;
                   }
               }   
                     return(T);
           }            
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I heard this term many times when I came across some protocol implementations, but
I came across some javascript at work today that used jQuery to fetch two
I came across some legacy code that contains a function like this: LPCTSTR returnString()
I came across some strange behavior of asp:PlaceHolder , it removes whitespaces between the
Context I came across some code, like this: if( Some_Condition ) throw 0; I
I was debugging the other day and came across some memory- and register-fills I
I have been reading over some code lately and came across some lines such
I'm reviewing a .NET project, and I came across some pretty heavy usage of
When I started to learn about WPF and MVVM recently, I came across some
I am a Java EE Programmer, I came across Jquery some where in Stack

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.