我是C ++的新手。我的背景来自PHP和C#。我在Visual Studio 2005中用VC ++实现二进制搜索树
所有操作都没问题,我在一个特定情况下面临删除问题,即当我尝试删除头部两次或更多次时。
建议的策略是
- 在右子树中找到最小值
- 用最少的
替换要删除的节点- 删除最低要求
醇>
在我的代码中8位于顶部,当我删除顶部时,第一次,11次成为右侧子树的顶部,如果我删除任何其他节点,代码工作正常,但如果我再次删除顶部(删除后)现在它是8)我得到了以下错误
Windows在BinarySearchTreeByList.exe中触发了断点。
这可能是由于堆的损坏,并指示> BinarySearchTreeByList.exe或其加载的任何DLL中的错误。
输出窗口可能包含更多诊断信息
以下是完整的代码
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <list>
#include <iostream>
typedef struct node
{
node* left;
node* right;
node* parent;
int val;
};
using namespace std;
void insert_node(node** iterate, int newVal, node* newParent);
void traverse(node* iterate);
void del(node** iterate, int newVal, char direction);
void traverse(node* iterate)
{
if(iterate != NULL)
{
traverse(iterate->left);
printf("%d ",iterate->val);
traverse(iterate->right);
}
}
void del(node** iterate, int newVal, char direction)
{
if((*iterate) == NULL)
return;
if((*iterate)->val == newVal)
{
if((*iterate)->left == NULL && (*iterate)->right == NULL)
{
if(direction == 't')
{
node* deleted = *iterate;
*iterate = NULL;
free(deleted);
}
if(direction == 'l')
{
node* deleted = (*iterate)->parent->left;
(*iterate)->parent->left = NULL;
free(deleted);
}
if(direction == 'r')
{
node* deleted = (*iterate)->parent->right;
(*iterate)->parent->right = NULL;
free(deleted);
}
return;
}
if((*iterate)->left == NULL)
{
if(direction == 't')
{
node* deleted = *iterate;
*iterate = (*iterate)->right;
(*iterate)->parent = NULL;
free(deleted);
}
if(direction == 'l')
{
node* deleted = *iterate;
(*iterate)->parent->left = (*iterate)->right;
free(deleted);
}
if(direction == 'r')
{
node* deleted = *iterate;
(*iterate)->parent->right = (*iterate)->right;
free(deleted);
}
return;
}
if((*iterate)->right == NULL)
{
if(direction == 't')
{
node* deleted = *iterate;
*iterate = (*iterate)->left;
(*iterate)->parent = NULL;
free(deleted);
}
if(direction == 'l')
{
node* deleted = *iterate;
(*iterate)->parent->left = (*iterate)->left;
free(deleted);
}
if(direction == 'r')
{
node* deleted = *iterate;
(*iterate)->parent->right = (*iterate)->left;
free(deleted);
}
return;
}
node* findmin = (*iterate)->right;
int minVal = 0;
while(findmin != NULL)
{
minVal = findmin->val;
findmin = findmin->left;
}
(*iterate)->val = minVal;
del(&((*iterate)->right), minVal, 'r');
return;
}
if(newVal < (*iterate)->val)
del(&((*iterate)->left) ,newVal, 'l');
else
del(&((*iterate)->right) ,newVal, 'r');
}
void insert_node(node** iterate, int newVal, node* newParent)
{
if(*iterate == NULL)
{
node* newNode = (node*)malloc(sizeof(node));
newNode->val = newVal;
newNode->left = NULL;
newNode->right = NULL;
newNode->parent = newParent;
*iterate = newNode;
return;
}
if(newVal < (*iterate)->val)
insert_node(&((*iterate)->left) , newVal, *iterate);
else
insert_node(&((*iterate)->right) , newVal, *iterate);
}
int main()
{
node* iterate = NULL;
insert_node(&iterate, 8, NULL);
insert_node(&iterate, 15, NULL);
insert_node(&iterate, 4, NULL);
insert_node(&iterate, 2, NULL);
insert_node(&iterate, 1, NULL);
insert_node(&iterate, 3, NULL);
insert_node(&iterate, 7, NULL);
insert_node(&iterate, 6, NULL);
insert_node(&iterate, 11, NULL);
insert_node(&iterate, 22, NULL);
insert_node(&iterate, 12, NULL);
insert_node(&iterate, 13, NULL);
traverse(iterate);
printf("\n\n");
del(&iterate, 8, 't');
del(&iterate, 22, 't');
del(&iterate, 7, 't');
del(&iterate, 11, 't');
printf("\n\n");
traverse(iterate);
cin.clear();
cin.ignore(255, '\n');
cin.get();
}
感谢您的帮助
答案 0 :(得分:1)
您的问题是当您删除一个节点时,您将已删除父节点的子节点指针设置为已删除节点的子节点,但您没有将已删除父节点的子指针设置为已删除节点的子节点。
例如:
if(direction == 'l')
{
node* deleted = *iterate;
(*iterate)->parent->left = (*iterate)->right;
deleted->right->parent = deleted->parent;
free(deleted);
}
你错过了行deleted->right->parent = deleted->parent;
,我添加了它。
您应该以相同的方式修复代码中的其他几个位置。
答案 1 :(得分:0)
您的问题是,在删除包含子节点的节点时,您不会更新父节点。 parent
字段仅在插入节点时分配,因此您有一个定义明确的树。你也可以在某些情况下设置它,例如当只有一个孩子且方向为't'
时。
当您开始在节点周围摇晃而不更新'l'
和'r'
案例中已删除节点的子节点的父节点时,您会中断树。