-1

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);
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
lex
  • 174
  • 1
  • 9
  • 3
    `this` is a `treeNode* const` which can't be modified. You should probably just do `_data = data`. – Seth Carnegie Feb 04 '12 at 19:24
  • 2
    And "it's breaking" is not an error description. – Lightness Races in Orbit Feb 04 '12 at 19:28
  • Woh! You have too many errors in your code. First, `_data == NULL` means that `data` should be a pointer, but what in the world does `data > _data` mean then? What is the reason to compare _pointers_? Also, you never set `_greaterNode` to smth, so you get `segfault` very quickly... Well, I think, you should mm... rebuild your class completely – Lol4t0 Feb 04 '12 at 19:36
  • @Lol4t0: You *can* apply the `<` operator to pointers. It compares the addresses they point to -- and has undefined behavior if they don't point into the same object (or just past the end of it). The `data < _data` is legal, so the compiler probably won't complain about it; it's just not useful here. – Keith Thompson Feb 04 '12 at 19:46
  • @KeithThompson, of cause you can.... but what the reason? Something tells me, that comparing pointers is not the thing, that author actually whants – Lol4t0 Feb 04 '12 at 19:58
  • @Lol4t0: That's exactly my point. Probably the OP wants to compare the objects that the pointers point to, but I haven't looked at the code closely enough to be sure. My point is that this is a logical error, not something you can expect the compiler to diagnose. – Keith Thompson Feb 04 '12 at 21:12

1 Answers1

0

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);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621