Probably a pointer problem
Just messing around and refreshing on C++ and I happen to run into a little problem.
Code:
template <class T> class TraverseTree{
public:
TraverseTree();
~TraverseTree();
static void preOrder(Node<T>*,Tree<T>*);
static void postOrder(Node<T>*,Tree<T>*);
static void inOrder(Node<T>*,Tree<T>*);
};
template <class T> void TraverseTree<T>::preOrder(Node<T>* l,Tree<T>* d){
if(d!=NULL){
l->v = d->v;
l->next = new Node<T>;
std::cout << l->v << std::endl;
l = l->next;
l->next = NULL;
preOrder(l,d->left);
preOrder(l,d->right);
}
}
The cout line prints out the correct stuff and works great. However I wanted to use the variable that I passed in to the function to print out the answers. When I do I get some of the right numbers but not in the right order and the others are from some random memory. Is my logic here wrong?
The first problem I had was that I lost the root while it generated the list so I made a temporary root node and passed that in for the list to be generated. Now I'm not too sure what the problem is.