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.
_end = _current;
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.
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?