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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:35:36+00:00 2026-06-17T03:35:36+00:00

I started learning about trees and tried writing the code for BST. Unfortunately i

  • 0

I started learning about trees and tried writing the code for BST. Unfortunately i am having trouble displaying the tree datas. I am trying to implement Depth-first traversal. But have no idea how to implement it. Below is my code

#include<iostream>
using namespace std;

class node
{
  public:
  int data;
  node *left,*right;
};

class btree
{
private:
node *root;
public:
btree(){root=NULL;}
void insert(int value)
{node *temp=new node;
    if(root == NULL)
        {
        temp->right=NULL;
        temp->data=value;
        temp->left=NULL;
        root=temp;
        }
    else
        insertHelper(root, value);
}

void insertHelper(node* Node, int value)
{
    if(value < Node->data)
    {
        if(Node->left == NULL)
            {
                Node->left=new node;
                Node->left->data=value;
                Node->left->left=NULL;
                Node->left->right=NULL;
                }
        else
            insertHelper(Node->left, value);
    }
    else
    {
        if(Node->right== NULL)
           {
               Node->right = new node;
               Node->right->data=value;
               Node->right->left=NULL;
               Node->right->right=NULL;
               }
        else
            insertHelper(Node->right, value);
    }

}


void disp()
{node*tmp=root;
if(tmp==NULL)
    cout<<"empty"<<endl;
else
    {
        cout<<tmp->data<<endl;
        disphelper(tmp);
    }
}

void disphelper(node *tmp)
{


    if(tmp->left!=NULL)
    {
        cout<<tmp->left->data<<endl;
        tmp=tmp->left;
        disphelper(tmp);}
    if(tmp->right!=NULL)
    {
       cout<<tmp->right->data<<endl;
        tmp=tmp->right;
        disphelper(tmp);
    }
}
};

int main()
{
    btree binarytree;
    binarytree.disp();
    binarytree.insert(10);
    binarytree.insert(5);
    binarytree.insert(30);
    binarytree.disp();
}

the output is

empty
10
5

Can anyone please tell me why 30 is not displayed?

  • 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-17T03:35:37+00:00Added an answer on June 17, 2026 at 3:35 am

    MODIFY your code to

    void disphelper(node *tmp)
    {
        if(tmp == NULL)
           return;
        cout<<tmp->data<<endl; // print data for current node
    
        if(tmp->left!=NULL) // traverse left branch
        {
            //cout<<tmp->left->data<<endl;
            //tmp=tmp->left;
            disphelper(tmp->left);}
        if(tmp->right!=NULL) // traverse right branch
        {
           //cout<<tmp->right->data<<endl;
            //tmp=tmp->right;
            disphelper(tmp->right);
        }
    }
    

    And it should work.

    void disp()
    {node*tmp=root;
    if(tmp==NULL)
        cout<<"empty"<<endl;
    else
        {   
            disphelper(tmp);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning about HMVC in CodeIgniter. So far I've been enjoying having
I just started learning about Kinect through some quick start videos and was trying
I am trying to embrace TDD and started learning about mocking. I need some
Started learning TDD and met some misunderstandings about different approaches for writing tests. In
I recently started learning about ASP.Net MVC and its various features MVC_3_MUSIC_STORE + CODE
I am new here and just started learning about Objective C. I am having
I started learning about templates and I copied the code from my book but
Just started learning NServiceBus and trying to understand the concept. When it talks about
I've just started learning about thread safety. This is making me code a lot
Recently I've started learning about Contexts in .NET (context-bound, context-agile, message sinks, etc.). Several

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.