Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:删除BST中的节点时,将获取seg故障或复制节点_C++_Tree_Binary Search Tree - Fatal编程技术网

C++ C++;:删除BST中的节点时,将获取seg故障或复制节点

C++ C++;:删除BST中的节点时,将获取seg故障或复制节点,c++,tree,binary-search-tree,C++,Tree,Binary Search Tree,所以我做了很多搜索,但是很多有删除问题的人的BST实现与我完全不同。在这个赋值中,我们得到了一个BST类,该类具有字段nodeContent,以及指向root、leftChild和rightChild的指针。本周的任务是创建一个删除树中指定节点的函数,然后我们应该能够遍历树以验证节点是否消失。我原以为我的过程很好,但当我测试代码时,它要么返回节点确实已被删除,要么返回我从中复制的节点已被复制。或者,当我试图删除一张有两个孩子的便笺时,我会遇到一个分段错误。我是第一次在SO上发帖,所以如果我做得不

所以我做了很多搜索,但是很多有删除问题的人的BST实现与我完全不同。在这个赋值中,我们得到了一个BST类,该类具有字段nodeContent,以及指向root、leftChild和rightChild的指针。本周的任务是创建一个删除树中指定节点的函数,然后我们应该能够遍历树以验证节点是否消失。我原以为我的过程很好,但当我测试代码时,它要么返回节点确实已被删除,要么返回我从中复制的节点已被复制。或者,当我试图删除一张有两个孩子的便笺时,我会遇到一个分段错误。我是第一次在SO上发帖,所以如果我做得不对,我道歉。我一直在绞尽脑汁想知道我哪里出了错!提前谢谢。哦,是的,我为这么多的评论道歉。。当我被困在试图通过这些步骤与自己交谈时,我会添加很多评论

            /*
            void BST::deleteNode(int el)
            Input: An integer that is to be deleted from the tree
            Output: Nothing
            Side Effect: Single node deleted and tree reordered
            */
            void BST::deleteNode(int el)
            {
              BSTNode *temp;
              BSTNode *prev;
              BSTNode *node = Root;

              while (node -> nodeContent != el && node != NULL) // start the search
              {
                // if the search is less than 
                if(el < node -> nodeContent)
                {
                  node = node -> leftChild;
                }
                else if (el > node -> nodeContent)
                {
                  node = node -> rightChild;
                }

                if (node == NULL)
                {
                  std::cout << "That item cannot be deleted, "
                              "because it doesn't exist" << std::endl;
                  return;
                }
              }
              // ok, so we found the node

              // this is if node has two children
              if (node -> leftChild != NULL && node -> rightChild != NULL)
              {
                // first, set temp to rightmost node in left subtree
                temp = node -> leftChild;
                while (temp -> rightChild != NULL)
                {
                  // set prev to the node above node (bad name resolution, I know..)
                  prev = temp;
                  temp = temp -> rightChild;
                }
                // now we have our node, temp, and prev set.
                // time to do some copying
                // first step: set prev's rightChild to NULL
                prev -> rightChild = NULL;
                // ok. now we need to check if temp has a left child
                if (temp -> leftChild != NULL)
                {
                  //if it does, set it to prev's rightChild
                  prev -> rightChild = temp -> leftChild;
                }
                // done. Now set nodes content to temps content
                node -> nodeContent = temp -> nodeContent;
                // good work. now delete temp
                delete temp;
                temp = NULL;
              }
              // this one is for deleting a node without a right child
              if (node -> rightChild == NULL && node -> leftChild != NULL)
              {
                // using temp this time as the leftChild of the node to be deleted
                temp = node -> leftChild;
                // copy the content from child to node's content
                node -> nodeContent = temp -> nodeContent;
                // george r.r. martin the heck out of temp, for his watch has ended
                delete temp;
                temp = NULL;
              }
              // now, if (soon-to-be) deleted node only has a right child
              if (node -> leftChild == NULL && node -> rightChild != NULL)
              {
                // set temp to be nodes rightChild
                temp = node -> rightChild;
                // copy content from temp to to node
                node -> nodeContent = temp -> nodeContent;
                // delete temp
                delete temp;
                temp = NULL;
              }
              // the last one should be the easiest, if the node has no children
              if (node -> leftChild == NULL && node -> rightChild == NULL)
              {
                delete node;
              } 
            }
/*
void BST::deleteNode(int-el)
输入:要从树中删除的整数
输出:无
副作用:删除单个节点并重新排序树
*/
void BST::deleteNode(int-el)
{
BSTNode*温度;
BSTNode*prev;
BSTNode*node=Root;
while(node->nodeContent!=el&&node!=NULL)//开始搜索
{
//如果搜索小于
if(elnodeContent)
{
node=node->leftChild;
}
else if(el>节点->节点内容)
{
节点=节点->右子节点;
}
if(node==NULL)
{
std::cout rightChild!=NULL)
{
//首先,将temp设置为左子树中最右边的节点
temp=node->leftChild;
while(temp->rightChild!=NULL)
{
//将prev设置为节点上方的节点(名称解析错误,我知道..)
prev=温度;
temp=temp->rightChild;
}
//现在我们有了节点、临时和上一个集合。
//是时候复印了
//第一步:将prev的rightChild设置为NULL
prev->rightChild=NULL;
//好的。现在我们需要检查一下temp是否有一个左撇子
if(temp->leftChild!=NULL)
{
//如果是,则将其设置为prev的rightChild
prev->rightChild=temp->leftChild;
}
//完成。现在将节点内容设置为临时内容
节点->节点内容=临时->节点内容;
//干得好。现在删除临时文件
删除临时文件;
温度=零;
}
//这个用于删除没有正确子节点的节点
if(node->rightChild==NULL&&node->leftChild!=NULL)
{
//这次使用temp作为要删除的节点的leftChild
temp=node->leftChild;
//将内容从子节点复制到节点的内容
节点->节点内容=临时->节点内容;
//乔治·r.r.马丁真是疯了,因为他的表快用完了
删除临时文件;
温度=零;
}
//现在,如果(即将)删除节点,则该节点只有一个正确的子节点
if(node->leftChild==NULL&&node->rightChild!=NULL)
{
//将临时节点设置为rightChild
temp=node->rightChild;
//将内容从临时复制到节点
节点->节点内容=临时->节点内容;
//删除临时文件
删除临时文件;
温度=零;
}
//如果节点没有子节点,那么最后一个应该是最简单的
if(node->leftChild==NULL&&node->rightChild==NULL)
{
删除节点;
} 
}

您正在使用三个
if
语句,并且没有从任何块返回。因此,假设您的第一个
if
语句得到满足,并且该节点只有一个正确的子节点。该子节点已被删除

现在,代码转到第二个
if
语句,然后执行第二个
if

我希望这能引导你找到答案。
另外,要找到分割错误发生的地方。如果你在C++中编码,这是一个必不可少的工具。

你在调试器中运行代码时观察到了什么?你是否偶然删除了两次节点?你正确地实现了拷贝和赋值操作符吗?这将是有帮助的,但是我首先注意到的是<代码>(节点->节点内容!=el&&node!=NULL)可能会导致未定义的行为。很好的一点..我很不好意思错过了!谢谢你的帮助!在开始上课之前,我只花了一秒钟检查它,但我在delete命令之后添加了return;希望它能修复它..但不幸的是没有运气..我不得不继续处理它,但这是一个好的开始,我想我从来没有使用过它以前使用过sed GDB,但它似乎非常有用。我一到家就会试试。谢谢!