我是 gdb 和一般调试的新手,当然我不确定在没有 gdb 的情况下我是如何做到这一点的。gdb告诉我的程序调试是这个函数有段错误。我将在下面的评论中突出显示它:
template <class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *lLink;
nodeType<elemType> *rLink;
};
#define H_binarySearchTree
#include <iostream>
#include "binaryTree.h"
using namespace std;
template <class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
bool search(const elemType& searchItem) const;
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in
// the binary search tree; otherwise,
// returns false.
void insert(const elemType& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If there is no node in the binary search
// tree that has the same info as
// insertItem, a node with the info
// insertItem is created and inserted in the
// binary search tree.
void deleteNode(const elemType& deleteItem);
//Function to delete deleteItem from the binary search tree
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary
// search tree.
// If the binary tree is empty or deleteItem
// is not in the binary tree, an appropriate
// message is printed.
void printTree();
void printTree(nodeType<elemType> *p);
// void printTreeNode(nodeType<elemType> *p);
void swapSubtrees(nodeType<elemType> *root1);
void swapSubtrees();
nodeType<elemType> *root1;
template <class elemType>
void bSearchTreeType<elemType>::insert
(const elemType& insertItem)
{
nodeType<elemType> *current; //pointer to traverse the tree
nodeType<elemType> *trailCurrent; //pointer behind current
nodeType<elemType> *newNode; //pointer to create the node
newNode = new nodeType<elemType>;
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
if (root1 == NULL)
root1 = newNode;
else
{
current = root1;
while (current != NULL)
{
trailCurrent = current;
if (current->info == insertItem)//** This is where gdb says there is a seg fault
{
cout << "The item to be inserted is already ";
cout << "in the tree -- duplicates are not "
<< "allowed." << endl;
return;
}
else if (current->info > insertItem)
current = current->lLink;
else
current = current->rLink;
}//end while
if (trailCurrent->info > insertItem)
trailCurrent->lLink = newNode;
else
trailCurrent->rLink = newNode;
}
}//end insert
template<class elemType>
void bSearchTreeType<elemType>::swapSubtrees(nodeType<elemType> * root1)
{
if (root1 != NULL)
{
nodeType<elemType> *temp;
swapSubtrees(root1->lLink);//Seg Fault here as well
swapSubtrees(root1->rLink);
temp =root1->lLink;
root1->lLink = root1->rLink;
root1->rLink = temp;
}
}
}
有人可以帮助描述发生了什么,以及我需要做些什么来解决它吗?
*已编辑以包含更多细节非常感谢