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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:45:20+00:00 2026-05-29T21:45:20+00:00

I have been working on a simple binary search tree for a larger project.

  • 0

I have been working on a simple binary search tree for a larger project. I understand the concept of a binary search tree and am simply having troubles with syntax of implementing in C++. I am deliberately not using boost’s tree containers. My code for the tree is as follows.

  struct Tree{
int nodeValue;
Tree *nodeChild1;
Tree *nodeChild2;
Tree(int userProvidedValue){
    nodeValue = userProvidedValue;
    nodeChild1 = NULL;
    nodeChild2 = NULL;
}
static void placeValue(Tree &parent, int value);
static Tree findValue(Tree parent, int value);
static void crawl(Tree parent);
~Tree(){

    delete nodeChild1;
    delete nodeChild2;
}
};

 void Tree::placeValue(Tree &parent, int value){
Tree node = Tree(value);
cout<<"made node"<<endl;
if(value>parent.nodeValue){
    cout<<"eval node child 2"<<endl;
    if(parent.nodeChild2 ==NULL){
        cout<<"reaching this";
        parent.nodeChild2 = &node;
    }
    else{
        placeValue(*parent.nodeChild2, value);
    }
}
if(value<=parent.nodeValue){
    cout<<"eval node child 1"<<endl;
    if(!parent.nodeChild1){
                cout<<"assigning"<<endl;
                parent.nodeChild1 = &node;
    }
            else{
                        placeValue(*parent.nodeChild1, value);
            }
        }
}

However whenever I construct a tree with Tree parent = Tree(5) then add another node to it with Tree::placeValue(parent, 4) it compiles fine but a message pops up telling me that the exe has crashed.

Can anyone please help me understand where this crashing is coming from? Thanks in advance.

The code to crawl through the tree looks like this:

void Tree::crawl(Tree parent){
cout<<parent.nodeValue<<endl;
if(NULL!=parent.nodeChild1){
    crawl(*parent.nodeChild1);
}
if(NULL!=parent.nodeChild2){
    crawl(*parent.nodeChild2);
}
}

Bonus Question: When Tree::crawl takes the argument of Tree &parent instead of Tree parent it runs fine. However without the & however it fails. Can anyone please explain why this is so?

  • 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-29T21:45:21+00:00Added an answer on May 29, 2026 at 9:45 pm

    You have to allocate Tree(s) on the heap.

    Tree node = Tree(value);
    

    Here you are allocating a Tree on the stack. This variable’s address will be disposed after getting out of scope. In order to allocate it on the heap, just use the new operator:

    Tree *node = new Tree(value);
    

    And then assign it as a child of the parent:

    parent.nodeChild2 = node;
    

    Regarding the Tree::crawl error, it is based on the same error. You keep allocating Tree(s) on the stack, therefore once it goes out of scope its destructor is called, delete(ing) nodeChild1 and nodeChild2. You should manage these kinds of structures by either using pointers, or always using references, so that when a function ends, the Tree’s destructor is not called. Therefore:

    void Tree::crawl(const Tree &parent){
      cout<<parent.nodeValue<<endl;
      if(NULL!=parent.nodeChild1){
          crawl(*parent.nodeChild1);
      }
      if(NULL!=parent.nodeChild2){
          crawl(*parent.nodeChild2);
      }
    }
    

    This should do it. Remember to do the same on Tree::findValue, you should use this signature for that function:

    static Tree findValue(const Tree &parent, int value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, so I've been trying to implement a simple binary search tree that uses
So lately I have been working on writing a simple compiler to better understand
I have been working on a web services related project for about the last
I have been working with Struts for some time, but for a project I
I have been working on a simple text editor in Cocoa/Objective-C for a practice
I have been working on a project in scala, but I am getting some
I have been working on an iPhone project, where we created all the user
I'm new to Objective-C and I have been working with simple programs, and I
I have been working on a simple Java app. I had it compiling and
I just started learning Backbone.js, and have been working on (what else) a simple

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.