C++ C++;BST内存错误-什么';我的头发怎么了?

C++ C++;BST内存错误-什么';我的头发怎么了?,c++,memory,binary-search-tree,crash-dumps,C++,Memory,Binary Search Tree,Crash Dumps,我遇到的问题是,我的树只能成功删除一个节点一次。当我试图让它删除更多,它将崩溃与分割错误(核心转储)错误。我似乎无法指出它的确切错误位置,但应该是我在内存分配和释放方面做错了什么,我就是看不到 这是我的插入、移除、移除辅助对象和叶(因为它被称为内部移除): 插入: /********************************************************* Function: insert Arguments: const T &x (a T variable

我遇到的问题是,我的树只能成功删除一个节点一次。当我试图让它删除更多,它将崩溃与分割错误(核心转储)错误。我似乎无法指出它的确切错误位置,但应该是我在内存分配和释放方面做错了什么,我就是看不到

这是我的插入、移除、移除辅助对象和叶(因为它被称为内部移除):

插入:

/*********************************************************
 Function: insert
 Arguments: const T &x (a T variable)
 Returns: void
 Notes: If the tree is empty, the function will set the node 
    as the root. Otherwise, it will look for the appropiate 
    place (in terms of a BST) to place it as long as its data 
    is not already existent inside the tree. 
*********************************************************/
template<class T> void binSTree<T>::insert(const T &x)
{
    BinTreeNode *newnode, *current, *parentOfCurrent; /* newnode will be the inserted node, current and parentOfCurrent will be traversing the while loop to find an appropiate space to insert */
    newnode = new BinTreeNode; /* allocates storage space */
    newnode->info = x; /* sets newnode's data equal to x */
    newnode->left = newnode->right = NULL; /* sets newnode's left and right children equal to NULL */

    if (root == NULL)   { /* if the tree is empty, it will place newnode on the root */
        root = newnode;
        cout << "added to root\n";
        return;         }
    current = root; /* sets current at the root */
    while(current != NULL)  { /* while current is not NULL, it will search for a place to insert the new node */
    parentOfCurrent = current; /* sets the parent equal to current */ 
        if(current->info == x) /* if the variable is found inside the tree, then it will error and exit */
        {
            cout << x << " already exists in tree. Duplicates not allowed!\n";
            return;
        }
        else if(current->info > x) /* otherwise, if the variable is less than the data on the tree currently, it will move left */ 
            current = current->left; 
        else                       /* otherwise, if the variable is greater than the data on the tree currently, it will move right */          current = current->right;
                            }
    /* the new node is placed where current would be in */ 
    if (parentOfCurrent->info > x) 
        parentOfCurrent->left = newnode;
    else
        parentOfCurrent->right = newnode;
}
/*********************************************************
功能:插入
参数:常量T&x(一个T变量)
退货:作废
注意:如果树为空,函数将设置节点
作为根。否则,它将寻找合适的方法
place(就BST而言)将其放置为与其数据相同的位置
在树中还不存在。
*********************************************************/
模板树::插入(常量T&x)
{
BinTreeNode*newnode、*current、*parentOfCurrent;/*newnode将是插入的节点,current和parentOfCurrent将遍历while循环以找到要插入的适当空间*/
newnode=newbintreenode;/*分配存储空间*/
newnode->info=x;/*将newnode的数据设置为x*/
newnode->left=newnode->right=NULL;/*将newnode的左、右子级设置为NULL*/
如果(root==NULL){/*如果树为空,它将在根上放置newnode*/
根=新节点;
cout info==x)/*如果在树中找到该变量,则它将出错并退出*/
{
cout x)/*否则,如果变量小于当前树上的数据,它将向左移动*/
当前=当前->左侧;
else/*否则,如果变量大于当前树上的数据,则将向右移动*/current=current->right;
}
/*新节点位于当前节点所在的位置*/
如果(当前的父项->信息>x)
parentOfCurrent->left=newnode;
其他的
parentOfCurrent->right=newnode;
}
删除和删除:

template <class T> bool binSTree<T>::remove(const T &x)
{
    BinTreeNode *current, *parentOfCurrent; /* current for tranversing the tree in the while loop and parentofCurrent to help */
    bool found = false; /* the booleans that is to be returned */
    if (root == NULL)   { /* Checks if the tree is empty, if it is, it returns found (false) and exits */
        return found;
                        }
    current = root; /* sets current equal to root */ 
    while(current != NULL) /* loops repeats until current is equal to NULL */
    {
        if(current->info == x)  { /* if the current data from the tree is equal tox, then it breaks the loop and sets found equal to true */
            found = true;
            break;
                                }
        else    { /* otherwise, it will search for the next place to go in the tree */
            parentOfCurrent = current; /* parentOfCurrent is set to current before current is set to one of its children */ 
            if(current->info > x) /* if x is less than the data on current, then it will move left */
                current = current->left;
            else   /* if x is greater than the data on current, then it will move right */
                current = current->right;
                }
    }
    if(found == true) { /* if it was found, it will pass current via the parent to the private remove function */ 
        if (current == root)
            privRemove(root);
        else if (parentOfCurrent->info > x) {  /* if current is to the left of the parent */
            found = leaf( parentOfCurrent->left );
            if(found == true)
                privRemove(parentOfCurrent->left);
                                            }
        else {  /* if current is to the right of the parent */
            found = leaf( parentOfCurrent->left );
            if(found == true)
                privRemove(parentOfCurrent->right);
             }
                      }
    return found;
}
/*********************************************************
 Function: privRemove
 Arguments: BinTreeNode* &node (the node that is to be deleted)
 Returns: void
 Notes: This private remove function will take in the node that 
    is provided by the public remove function, and, after ensuring
    that the node passed is not NULL, then it will delete it.
*********************************************************/
template <class T> void binSTree<T>::privRemove(BinTreeNode* &node) 
{
    BinTreeNode *temp; /* initializes temp in order to hold the information of node */ 
    if(root != NULL )  /* if the node is not NULL */
    {
        temp = node;
        delete node;
        node = temp;
    }
}
模板bool bin树::删除(常量T&x)
{
BinTreeNode*current,*parentOfCurrent;/*current用于转换while循环中的树,parentOfCurrent用于帮助*/
bool found=false;/*要返回的布尔值*/
如果(root==NULL){/*检查树是否为空,如果为空,则返回find(false)并退出*/
发现退货;
}
current=root;/*将current设置为root*/
while(current!=NULL)/*循环重复,直到current等于NULL*/
{
如果(current->info==x){/*如果树中的当前数据等于tox,则它将中断循环并将find设置为true*/
发现=真;
打破
}
否则{/*否则,它将在树中搜索下一个要去的地方*/
parentOfCurrent=current;/*parentOfCurrent在将current设置为其一个子项之前设置为current*/
如果(当前->信息>x)/*如果x小于当前数据,则它将向左移动*/
当前=当前->左侧;
else/*如果x大于当前数据,则它将向右移动*/
当前=当前->右侧;
}
}
如果(found==true){/*如果找到它,它将通过父函数将电流传递给私有删除函数*/
如果(当前==根)
移除(根);
else如果(parentOfCurrent->info>x){/*如果current在父对象的左侧*/
找到=叶(当前的父级->左);
if(find==true)
privRemove(当前的父级->左侧);
}
else{/*如果当前在父级的右侧*/
找到=叶(当前的父级->左);
if(find==true)
privRemove(当前的父对象->右侧);
}
}
发现退货;
}
/*********************************************************
功能:删除
参数:BinTreeNode*&节点(要删除的节点)
退货:作废
注意:此私有删除函数将接收
由公共移除功能提供,并且在确保
如果传递的节点不是NULL,那么它将删除它。
*********************************************************/
模板void bin树::privRemove(BinTreeNode*&节点)
{
BinTreeNode*temp;/*初始化temp以保存节点的信息*/
如果(root!=NULL)/*如果节点不是NULL*/
{
温度=节点;
删除节点;
节点=温度;
}
}
叶子:

/*********************************************************
功能:叶子
参数:BinTreeNode*节点
返回值:布尔值
注意:此函数将检查参数中的节点
是通过检查其两个子项是否都为空的叶。如果
如果是,则返回true,否则返回false。
*********************************************************/
模板bool bin树::叶(BinTreeNode*节点)常量
{
bool isLEaf=false;/*要返回的布尔变量*/
if(node->left==NULL&&node->right==NULL)/*检查节点是否没有子节点*/
isLEaf=true;/*如果是,则将isLEaf设置为true*/
return isLEaf;/*遍历if语句后,返回变量isLEaf*/
}
需要注意的是,我的节点是在类头中声明的结构,变量root以BinTreeNode*root的形式声明为受保护的变量;它在binSTree构造函数中初始化为NULL。此外,我只允许删除叶子<
/*********************************************************
 Function: leaf
 Arguments: BinTreeNode* node
 Returns: boolean
 Notes: This function will check if the node in the argument 
  is a leaf by checking if both of its children are NULL. If 
  they are, it returns true. Otherwise, it returns false. 
*********************************************************/
template <class T> bool binSTree<T>::leaf(BinTreeNode* node) const
{
    bool isLEaf = false; /* the boolean variable it is to return */ 
    if(node->left == NULL && node->right == NULL) /* checks if the node has no children */
        isLEaf = true;  /* if it does, it sets isLEaf equal to true */
    return isLEaf;  /* after going through the if statement, it returns the variable isLeaf */
}
delete node;
node=temp;