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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:32:24+00:00 2026-06-17T21:32:24+00:00

I am trying to implement the Binary tree in C.Firstly inserting the values and

  • 0

I am trying to implement the Binary tree in C.Firstly inserting the values and then traversing them into the Preorder.But when i call the function preorder() then it’s giving me infinite loop with only last value inserted.
I am using following code:

struct node* insert(struct node *root,int num);

void preorder(struct node *root);

struct node *root=NULL;

int count=1;

struct node {

    struct node *lchild;
    struct node *rchild; 
int data;
};

int main(){

    root=insert(root,1);
//root=insert(root,2);
preorder(root); 
return;
}

struct node* insert(struct node *root,int num){//insert a node into tree

   //struct node *q;
if(root==NULL)
{
    root=(struct node*)malloc(sizeof(struct node)); 
    root->data=num;
    root->lchild=NULL;
    root->rchild=NULL;
    //root=q;
    count++;
}
else{
    if(count % 2==0){
        root->lchild=insert(root->lchild,num);
    }
    else{
        root->rchild=insert(root->rchild,num);
    }
}
return(root);
}

void preorder(struct node *root){

    while(root!=NULL){
    printf("%d\t",root->data);
    preorder(root->lchild);
    preorder(root->rchild);     
}
}

here i am inserting only 1 value initially but the bug occurs.So in insert() there should not be any mistake,something correction should be done in preorder() or main()..what it can be?

  • 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-17T21:32:25+00:00Added an answer on June 17, 2026 at 9:32 pm

    I am not sure what preorder() should do, but this line causes the endless loop:

     while(root!=NULL){
    

    I guess you meant to write if not while

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

Sidebar

Related Questions

I've been trying to implement a delete function for a Binary Search Tree but
I was trying to implement binary search tree but I think I have made
I'm trying to implement a binary search tree class, but the compiler is throwing
I'm quite new to C and I'm trying to implement a binary tree in
I am trying to implement binary search tree operations and got stuck at deletion.
I'm trying to implement a method for a binary tree which returns a stream.
I've been trying to implement a binary search tree in C++ for fun. My
I am trying to implement a Binary Search Tree data structure in C, and
Im trying to implement a Phonegap CordovaWebView into my Fragment, but it doesn't work.
I am trying to implement a binary search tree. Here is the code I

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.