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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:15:39+00:00 2026-06-15T11:15:39+00:00

I m new to coding and i m trying to create a binary tree

  • 0

I m new to coding and i m trying to create a binary tree obtianed form the array. When i try to run it , i get a runtime error. Can someone explain what is wrong with this code.
tnode is the struct for storing the node and construct_tree is called to build the tree.

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>         
        struct tnode{
            int value;
            struct tnode *parent ;
            struct tnode *left;
            struct tnode *right;
        };

        typedef struct tnode node;


    node* construct_tree(int *a,int n){
        printf("where");        
        node *root, *temp, *traverse, *traverse_parent;
        root = (node*)malloc(sizeof(node));

        root->value = a[0];
        root->parent = NULL;
        root->left = NULL;    
        root->right = NULL;    
        traverse = root;
        printf("where1");    
        printf("\n%d",a[0]);
        int i=0;
        for(i=1;i<n;i++){
            temp = (node*)malloc(sizeof(node));
            temp->value = a[i];
            temp->left = NULL;
            temp->right = NULL;

            while(traverse == NULL){
                traverse_parent = traverse;
                if(traverse->value < a[i]) traverse = traverse->right;
                else traverse = traverse->left;            
            }
            temp ->parent = traverse_parent;        
            printf("\n a[i]: %d and parent : %d",a[i],traverse_parent->value);
        }
        return root;
    }


    int main(){
        int a[] = {5,3,7,6,11,1,4};
        node* root;
        printf("where2");        
        root = construct_tree(a,6);
        //tree_traversal(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-06-15T11:15:40+00:00Added an answer on June 15, 2026 at 11:15 am

    Your inner while is never executed since it’s initialized to root and never nullified. I suppose you would want to replace while(traverse == NULL) with while(traverse != NULL) and also reset the traverse back to root on each iteration of the for loop (after the while completion).

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

Sidebar

Related Questions

I'm completely new to coding - I can do some basic HTML. I'm trying
I'm new to coding in bash. I'm trying to create something which will loop
I'm new to Delphi coding and struggeling in reading .txt files. I am trying
I’m new to web development but have coding experience (Java) and I’m trying to
I'm new to Python, coming from a C# background and I'm trying to get
I am starting a new project trying to create a web application for an
I'm new to coding. I'm trying new things to learn better coding techniques. Right
Currently I'm trying to create some coding short cuts for our website. Right now
I am new to Qt and PySyde. I trying to create a small app
hi i am a new java programmer. i am just trying to create a

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.