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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:24:46+00:00 2026-05-30T11:24:46+00:00

Here is a c++ function to create a BST tree from an array of

  • 0

Here is a c++ function to create a BST tree from an array of integers?
It’s simple.
Take first element ,make root.
Take next array element and insert it into the tree.
Why is the loop starting from i=2 and not i=1??

node* buildtree(int a[], int len)
{
    node* root=new node(a[0]);
    node* temp=root;
    for(int i=1;i<len;i++)
    {
        while(!(root->left==NULL && root->right==NULL))
        {
            cout<<"here"<<i<<" "<<a[i]<<"  " << root->val<<"\n";
            if(root->val>a[i])
                root=root->left;
            else
                root=root->right;
        }
        node* currnode=new node(a[i]);
        if(root->val>a[i]) 
            root->left=currnode;
        else
            root->right=currnode;  

        if(root==NULL)
            cout<<"error...never.here";
        root=temp;
    }
    return root;
}

Thanks a lot for explaining it.I tried it another way but it only finds the root.What’s the problem in it?

   node* buildtree(int a[],int len)
   { node*  root=new node(a[0]);
    node* curr;
    for(int i=1;i<len;i++)
     { curr=root;
       while(curr!=NULL)    
         {
         if(curr->val>a[i])
         curr=curr->left;
         else 
         curr=curr->right;
         }
     curr=new node(a[i]); 
     }  
 return root;             
  }
  • 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-05-30T11:24:47+00:00Added an answer on May 30, 2026 at 11:24 am

    When trying to find the point of insertion,

    while(!(root->left==NULL && root->right==NULL))
    {
        cout<<"here"<<i<<" "<<a[i]<<"  " << root->val<<"\n";
        if(root->val>a[i])
            root=root->left;
        else
            root=root->right;
    }
    

    you only stop if both children are NULL, so at some point or other, you will set root to NULL. Consider the array begins with [5, 3, 6, ... ]. You start with

    NULL <- node(5) -> NULL
    node(3) <- node(5) ->NULL
    

    and then try to insert the 3. Since not both children are NULL, the while loop runs

    if (5 > 7)  // false
        root = root->left;
    else
        root = root->right;  // now root == NULL, oops
    

    and the controlling condition is checked anew

    while(!(NULL->left == NULL && NULL->right == NULL))
    

    segfault likely here, undefined behaviour invoked.

    You should do something like

    while(true) {
        if (root->val > a[i]) {
            if (root->left == NULL) {
                root->left = new node(a[i]);
                break;
            } else {
                root = root->left;
            }
        } else {
            if (root->right == NULL) {
                root->right = new node(a[i]);
                break;
            } else {
                root = root->right;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this function from someone on here: create FUNCTION [dbo].[fnSplitString] (@s varchar(512),@sep char(1))
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I need to pass an url from asp.net load_page to flowplayer javascript function here:
Here is the the function and the globals: $note_instance = Array(); $note_count = 0;
I create a function to edit user password here the function code. function updateUser
I have a User defined function in SQL Server 2008. Here it is: CREATE
as title states. here's the function DELIMITER // CREATE FUNCTION GetCreateValue( table_name CHAR(64), id_field
Here is my problem. I have a function where I dynamically create <input />
Here is my function so far: function create { a=$3 while [ $a -lt
I was trying to create function to create a div and style it, here

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.