Insert function in binary tree, stack overflow error

I am trying to implement the insert function of a binary tree. Atm when I try and insert 3 nodes, the program breaks and gives me a stack overflow error. The error points to a getter function for an identifier for the data in my node class.

void LinkedList::add(Product *myProduct)
{
if (_length==0)
{
_head = new Node(NULL, NULL, myProduct);
_end = _head;
_length=1;
_current = _head;
_head = _end;
}
else
{
if(myProduct->getId() > _current->getProductId())
{
if (_current->getNext() == NULL)
{
_current = _current->getNext();
Node* _current = new Node(NULL,_head->getNext(), myProduct);
}
else
{
_current = _current->getNext();
_head = _current;
add(myProduct);
}
}
else if (myProduct->getId() < _current->getProductId())
{
if (_current->getPrev() == NULL)
{
_current = _current->getPrev();
Node* _current = new Node(NULL,_head->getPrev(), myProduct);
}
else
{
_current = _current->getPrev();
_head = _current;
add(myProduct);
}

}

}
_end = _current;
_length++;
}
Here is my insert function, the error message is
"An unhandled exception of type 'System.NullReferenceException' occurred in SDI2.exe

Additional information: Object reference not set to an instance of an object.
"
In my main I have declared an instance of product, "productToAdd = new Product(id,idPrice);" so I'm a bit confused as to what I need to include..
Does anyone have any suggestions? If the information provided isn't enough to answer the question, please say and I can paste some of my other functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void LinkedList::add(Product *myProduct)
{
if (_length==0)
{
_head = new Node(NULL, NULL, myProduct);
_end = _head;
_length=1;
_current = _head;
_head = _end;
}
else
{
if(myProduct->getId() > _current->getProductId())
{
if (_current->getNext() == NULL)
{
_current = _current->getNext();
Node* _current = new Node(NULL,_head->getNext(), myProduct);
}
else
{
_current = _current->getNext();
_head = _current;
add(myProduct);
}
}
else if (myProduct->getId() < _current->getProductId())
{
if (_current->getPrev() == NULL)
{
_current = _current->getPrev();
Node* _current = new Node(NULL,_head->getPrev(), myProduct);
}
else
{
_current = _current->getPrev();
_head = _current;
add(myProduct);
}

}

}
_end = _current;
_length++;
}
do it like this:

1
2
3
4
5
if (_current->getNext() == NULL)
{
Node* temp = new Node(NULL,_head->getNext(), myProduct);
current->next = temp;
current = current->next;


...


change rest of the code accordingly.
There are a lot of potential problems with your add method, but the first problem I see given the code and your description of the problem is that a linked list is not a binary tree.

Trees typically have a "root" not a "head". Tree nodes usually have "left" and "right" links, not "prev" and "next" links, and I would not be at all surprised to learn that the inaccurate nomenclature has caused you some grief. So, the first thing I would do, if I were you, is to divorce the idea of a tree from that of a linked list.

Why is _current a member of the class? Why does add take a pointer to an object? What invariants exist for _head, _end and _length? What sense does an _end member make in a tree? Is it actually a binary tree as you said and not a linked list?
Last edited on
Topic archived. No new replies allowed.