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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:38:33+00:00 2026-06-18T06:38:33+00:00

Trying to teach myself C++ (I normally use Python) and wrote this code. #include

  • 0

Trying to teach myself C++ (I normally use Python) and wrote this code.

#include <iostream>
using namespace std;


class TreeNode {
    public:
        TreeNode *left;
        TreeNode *right;
        int value;

        TreeNode(int _value);
        void add_node(TreeNode node);
};

TreeNode::TreeNode(int _value) {
    left = 0;
    right = 0;
    value = _value;
    cout << "Creating node with value: " << value << endl;
}

void TreeNode::add_node(TreeNode node) {
    cout << "Adding " << node.value << " to " << value << endl;
    if (node.value < value) {
        cout << node.value << " < " << value << endl;
        if (left) {
            cout << "Left node of " << value << " exists " << left->value << endl;
            left->add_node(node);
        } else {
            cout << "Left node of " << value << " does not exist" << endl;
            left = &node;
        }
    }
    if (node.value > value) {
        cout << node.value << " > " << value << endl;
        if (right) {
            cout << "Right node of " << value << " exists " << right->value << endl;
            right->add_node(node);
        } else {
            cout << "Right node of " << value << " does not exist" << endl;
            right = &node;
        }
    }
}

int main ()
{
    TreeNode root(25);
    TreeNode n1(15);
    TreeNode n2(30);
    TreeNode n3(20);

    root.add_node(n1);
    root.add_node(n2);
    root.add_node(n3);

    cout << root.left->value << endl;
    cout << root.right->value << endl;

    return 0;
}

The program compiles but runs with results that I don’t understand.

Creating node with value: 25
Creating node with value: 15
Creating node with value: 30
Creating node with value: 20
Adding 15 to 25
15 < 25
Left node of 25 does not exist
Adding 30 to 25
30 > 25
Right node of 25 does not exist
Adding 20 to 25
20 < 25
Left node of 25 exists 20
Adding 20 to 20
20
20

I was expecting the last bit to be different.

Adding 20 to 25
20 < 25
Left node of 25 exists 15
Adding 20 to 15
20 > 15
Right node of 15 does not exist
15
30

Can someone explain what is happening here?

  • 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-18T06:38:34+00:00Added an answer on June 18, 2026 at 6:38 am

    You are copying the address of a copy of TreeNode, not the address of the TreeNode in main.
    Take note of the commented out function calls, and read this:
    How to pass objects to functions in C++?

    //Output with TreeNode node as arg
    //Creating node with value: 25
    //Creating node with value: 15
    //Creating node with value: 30
    //Creating node with value: 20
    //Adding 15 to 25
    //15 < 25
    //Left node of 25 does not exist
    //Adding 30 to 25
    //30 > 25
    //Right node of 25 does not exist
    //Adding 20 to 25
    //20 < 25
    //Left node of 25 exists 20
    //Adding 20 to 20
    //20
    //20
    
    //Output with TreeNode & node as arg
    //Creating node with value: 25
    //Creating node with value: 15
    //Creating node with value: 30
    //Creating node with value: 20
    //Adding 15 to 25
    //15 < 25
    //Left node of 25 does not exist
    //Adding 30 to 25
    //30 > 25
    //Right node of 25 does not exist
    //Adding 20 to 25
    //20 < 25
    //Left node of 25 exists 15
    //Adding 20 to 15
    //20 > 15
    //Right node of 15 does not exist
    //15
    //30
    
    #include <iostream>
    using namespace std;
    
    
    class TreeNode {
        public:
            TreeNode *left;
            TreeNode *right;
            int value;
    
            TreeNode(int _value);
            //void add_node(TreeNode  node);
            void add_node(TreeNode & node);
    };
    
    TreeNode::TreeNode(int _value) {
        left = 0;
        right = 0;
        value = _value;
        cout << "Creating node with value: " << value << endl;
    }
    
    //void TreeNode::add_node(TreeNode node) {
    void TreeNode::add_node(TreeNode & node) {
        cout << "Adding " << node.value << " to " << value << endl;
        if (node.value < value) {
            cout << node.value << " < " << value << endl;
            if (left) {
                cout << "Left node of " << value << " exists " << left->value << endl;
                left->add_node(node);
            } else {
                cout << "Left node of " << value << " does not exist" << endl;
                left = &node;
            }
        }
        if (node.value > value) {
            cout << node.value << " > " << value << endl;
            if (right) {
                cout << "Right node of " << value << " exists " << right->value << endl;
                right->add_node(node);
            } else {
                cout << "Right node of " << value << " does not exist" << endl;
                right = &node;
            }
        }
    }
    
    int main ()
    {
        TreeNode root(25);
        TreeNode n1(15);
        TreeNode n2(30);
        TreeNode n3(20);
    
        root.add_node(n1);
        root.add_node(n2);
        root.add_node(n3);
    
        cout << root.left->value << endl;
        cout << root.right->value << endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to teach myself python using Google's AppEngine , and I can't get
I'm just trying to teach myself how to use Linq. This is what I
I'm trying to teach myself how to use the QStyledItemDelegate class correctly. Qt's got
I'm trying to teach myself python right now, and I'm using exercises from Learn
I am trying to teach myself LRU algorithm using this youtube video . In
I am trying to teach myself MySQL/PHP from the very beginning. The following code
I am trying to teach myself a bit of Javascript and made this collection
I have been trying to teach myself MEF, starting with this tutorial: http://blogs.msdn.com/b/brada/archive/2008/09/29/simple-introduction-to-composite-applications-with-the-managed-extensions-framework.aspx There
I am trying to teach myself C from a python background. My current mini-problem
I am trying to teach myself Python, and I have realized that the only

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.