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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:50+00:00 2026-05-27T06:58:50+00:00

I created a class binary search tree. but the problem is when i print

  • 0

I created a class binary search tree.
but the problem is when i print the tree it crashes.
i think it can be an endless recursion in function print().

Here is my code

struct node{
node *l,*r;
int data;
};

class BinTree
{
  private:  node *root;
  public:
   BinTree(){ root=NULL; }
   void add(int a){ add_node(a,root); };
   void add_node(int a, node *rot)
   { node *curr; curr=rot;
       if(curr==NULL)
       {
           curr=new node;
           curr->data=a;
           curr->l=NULL;
           curr->r=NULL;
           return;
       }
       if(a>=curr->data)   curr=curr->r,add_node(a,curr);
       if(a<curr->data)    curr=curr->l,add_node(a,curr);
   }
   void print(){ inorder(root); }
   void inorder(node *curr)
   {
     if(curr->l!=NULL)   inorder(curr->l);
     cout<<curr->data<<" ";
     if(curr->r!=NULL)   inorder(curr->r);
   }
};

Can anyone help me?

  • 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-27T06:58:51+00:00Added an answer on May 27, 2026 at 6:58 am

    Your add_node is broken. If curr is NULL, it creates a new node but it never actually adds it to the existing tree. Thus all additions you make are effectively ignored and the tree stays empty.

    The inorder function dereferences curr without checking whether it is NULL, and print calls it without checking whether root is NULL. Thus, your crash most likely is caused by tryin to print out an empty tree and then dereferencing a null pointer.

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

Sidebar

Related Questions

I have a binary tree class that is created with a root node and
I am developing a binary search tree in java. But i am facing certain
I created a class Entry to handle Dictionary entries, but in my main(), I
I have to implement a binary search tree using C++ for one of assignments.
I am trying to create a custom Binary Search Tree, and I have everything
Hey guys, I have created a tree which is not a binary tree. Now,
I'm trying to make a list of all items in a binary search tree.
I currently have a binary search tree setup, utilizing templates to allow me to
How would you implement in Java the binary tree node class and the binary
My assignment is a Binary Search Tree derived from a Binary Tree, in my

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.