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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:15:32+00:00 2026-05-29T06:15:32+00:00

I am writing a binary tree, started getting errors, so I’ve removed all my

  • 0

I am writing a binary tree, started getting errors, so I’ve removed all my templating, but it still won’t compile, stumped by my final error!
Writing a recursive add function, but not sure how to add my new dataClass to my tree when it finds an empty node

#pragma once
#include <cstring>

typedef dataClass T;
//template <class T>
class treeNode
{
private:
treeNode* _greaterNode;
treeNode* _lessNode;
T _data;
public:
treeNode(T data);
void add(T data);
void del(T data);
};

//template <class T>
treeNode/*<T>*/::treeNode(T data)
{
_data = data;
_greaterNode = _lessNode = NULL;

}
//template <class T>
void treeNode/*<T>*/::add(T data)
{
if(_data == NULL)
{
    // add here
    this = new treeNode(data);
}
else if(data > _data)
{
    // data is bigger go to greater
    this->_greaterNode->add(data);
}
else if(data < _data)
{   
    // data is lower go to less than
    this->_lessNode->add(data);
}
else
{
    // data the same, throw exception
}
}

It’s breaking on:

if(_data == NULL)
{
    // add here
    this = new treeNode(data);
}
  • 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-29T06:15:33+00:00Added an answer on May 29, 2026 at 6:15 am

    You can not assign to this! You could assing to *this, like in *this = treenode(data);, but that could probably lead to other errors as the node pointer would be overwritten.

    Why not simply set _data to the parameter data?

    Also, when doing the recursive call you should create the links if they do not exist:

    else if(data > _data)
    {
        // data is bigger go to greater
        if (this->_greaterNode == NULL)
            this->_greaterNode = new treeNode(data);
        else
            this->_greaterNode->add(data);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an immutable binary tree class where all of the methods (Insert, Remove,
I'm writing an iterative function to search a binary tree for a certain value.
I've looked at binary reading and writing objects in c++ but are having some
I`m writing binary search tree template for two reasons - learning C++ and learning
I am writing a delete member function for a Binary Search Tree. I have
I'm getting data from a binary file, reading from file and writing in a
I'm writing a class for a binary tree, and in the tree interface this
I was just writing an inorder function to my Binary tree and I have
I'm writing a simple C program using fork() to create a binary tree of
I am writing a c# class that parses and writes binary plist files. All

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.