我试图将节点列表组织到我当前项目的二叉树中。编写代码就像在书中一样,但当流程在while循环中即将初始化当前指针与右或左假时,我得到地址错误。这是为什么?
这里是insert()fn:
template <class elemType>
void bSearchTree<elemType>::insert(const elemType& insertItem){
binaryTreeNode<elemType> *current;
binaryTreeNode<elemType> *trailCurrent;
binaryTreeNode<elemType> *newNode;
newNode = new binaryTreeNode<elemType>;
assert(newNode != NULL);
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
if(root == NULL)
root = newNode;
else{
current = root;
while(current != NULL){
trailCurrent = current;
if(current->info == insertItem){
cout << "The insert item is already in the list-";
cout << "duplicates are not allowed." << insertItem << endl;
return;
}else if(current->info > insertItem)
current = current->lLink;
else
current = current->rLink;
}
if(trailCurrent->info > insertItem)
trailCurrent->lLink = newNode;
else
trailCurrent->rLink = newNode;
}
if(current->info != insertItem){
totalNodes++;
}
}