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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:39:13+00:00 2026-05-27T00:39:13+00:00

So for my assignment, I am supposed to implement a Node class that just

  • 0

So for my assignment, I am supposed to implement a Node class that just contains data and pointers to its two siblings and a BinaryTree that reads in these Nodes and creates a binary tree out of them. My problem is pointing to the root of the Tree does not seem to work. Any help you can provide would be appreciated!

Note: The error is found a few lines into the addNode method in the BinaryTree.cpp file which can be found at the end of the question. Also, I am not able to access the value of size either, so I believe this is some sort of weird scope issues I cannot resolve. I also cannot use the “this” keyword in the addNode function.

I am also not allowed to use structs, per my homeworks’ instruction.

Node.H

#include <iomanip>

using namespace std;

class Node
{

    public:
      int data;
      Node* leftChild;
      Node* rightChild;
      Node(int data, Node* leftChild, Node* rightChild);

};

Node.cpp

#include <iomanip>
#include <iostream>
#include "Node.h"

using namespace std;

Node::Node(int data, Node* leftChild, Node* rightChild)
{
    this->data = data;
    this->leftChild = leftChild;
    this->rightChild = rightChild;
}

BinaryTree.H

#include <iomanip>
#include "Node.h"

using namespace std;

class Tree
{

public:
    Tree(int data);
    void addNode(int data);
    void inOrder(Node* N);

protected:
    Node* root;
    int size;
    int data;

private:
    int printNode(Node* N);

};

BinaryTree.cpp

#include <iostream>
#include <iomanip>
#include "BinaryTree.h"

using namespace std;

//Tree constructor. Sets the values of data, size, and root. 

Tree::Tree(int data)
{
    this->data = data;
    this->size = 0;
    this->root = new Node(data, NULL, NULL);
}

//Adds a node to the current Tree.
void addNode(int data)
{

    Node* tempNode = new Node(data, NULL, NULL);
    Node* current = root; //THIS IS THE ERROR LINE.

    while(current!=NULL)
    {
        //If the data we are trying to add is already in the Tree
        if(current->data == tempNode->data)
        {
            cout << "Data already in the Tree.";
        }

        //If the data for the new node is larger than the old
        else if(current->data < tempNode->data)
        {
            //See if the right child is null. If so, add the tree node there.
            if(current->rightChild == NULL)
            {
                current->rightChild = tempNode;

                return;
            }

            //Otherwise, traverse down the right tree.
            else
            {
                current = current->rightChild;
            }
        }

        //The data is smaller than the current node
        else
        {
            //See if the left child is null. If so, add the tree node there.
            if(current->leftChild == NULL)
            {
                current->leftChild = tempNode;
                return;
            }

            //Otherwise, traverse down the left tree
            else
            {
                current = current->leftChild;
            }
        }//End of leftChild Else

    }//End of while

}//End of addNode
  • 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-27T00:39:14+00:00Added an answer on May 27, 2026 at 12:39 am
    void addNode(int data)
    

    should be:

    void Tree::addNode(int data)
    

    as it is a member function of class Tree

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

Sidebar

Related Questions

I have an assignment that I'm supposed to implement the MIPS processor in C++
For a homework assignment I was given a Card class that has enumerated types
I have an assignment for my architecture class which to implement a 31 backgammon
As a school assignment, I am supposed to create a program in C# that
I am working on an assignment that is giving me grief. I am supposed
Have a homework assignment in which I'm supposed to create a vector of pointers
This is part of an assignment. Everything that's supposed to work, already works, but
I'm having an interesting problem with an assignment for my class, we're supposed to
In a school assignment I'm supposed to complete a method that should return an
My class assignment is to write a program that has the user input a

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.