Thanks for watching my question. I just ran into a really fundamental problem when I implement BST, that is, "what's the difference in different approaches for assigning a pointer?" We all know that point assignment can use:
int *p, q; p = &q;
Or:
int *p, *q; p = q;
They must be the same. But in my case below they work in a completely different way:
template <typename T> void Tree<T>::_insert(TreeNode<T>*& tree, const T& value, const unsigned& key) { // TreeNode<T> node(value, key); // if(tree == nullptr) tree = &node; TreeNode<T> *node = new TreeNode<T>(value, key); if(tree == nullptr) tree = node; else if(key < tree->index) _insert(tree->left, value, key); else if(key > tree->index) _insert(tree->right, value, key); else if(key == tree->index) std::cerr << "_insert: repeating key" << std::endl; }
Using the first method (marked), the function will not assign a tree equal to node, while the second method works fine.
So, is this my mistake, or are they naturally different?
source share