【发布时间】:2019-12-07 03:15:57
【问题描述】:
我正在尝试在 C++ 中实现一个二叉搜索树,其中我实现了一个
deletenode()
方法使用双指针,但我的节点没有被删除使用预定义的方法,如free() 和delete。请帮帮我?
我一直在 youtube 上搜索视频或文章以及极客的极客,但他们都使用单个指针实现,因此在这种情况下我无法找到参考,尤其是在它附近也找不到参考。我从上周开始尝试解决这个问题,无法成功实施,一些节点被删除了一些不是我无法找到这背后的原因,还请告诉我为什么我的代码没有按预期工作
#include <bits/stdc++.h>
using namespace std;
typedef struct node {
struct node *left = NULL;
int key;
struct node *right = NULL;
} node;
void addNode(node **r, int k)
{
if (*r == NULL)
{
node *q = (struct node*)malloc(sizeof(struct node));
q->key = k;
*r = q;
(*r)->left = NULL;
(*r)->right = NULL;
return;
}
node *q = (struct node*)malloc(sizeof(struct node));
if (k > ((*r)->key))
{
addNode(&((*r)->right), k);
}
else if (k < ((*r)->key))
{
addNode(&((*r)->left), k);
}
}
void searchNode(node **r, int k)
{
if (*r == NULL)
{
cout << "\n NOT FOUND \n";
return;
}
if ((*r)->key == k)
{
cout << "\n FOUND \n";
return;
}
else if (k > ((*r)->key))
{
searchNode(&((*r)->right), k);
}
else if (k < ((*r)->key))
{
searchNode(&((*r)->left), k);
}
}
void del(node **r, int k)
{
if (*r == NULL)
return;
if ((*r)->key == k)
{
if ((*r)->left == NULL && (*r)->right == NULL)
{
(*r) = NULL;
(*r) = NULL;
free(r);
return;
}
if ((*r)->left == NULL)
{
node* q = (struct node*)malloc(sizeof(struct node));
q = (*r)->right;
(*r)->key = q->key;
(*r)->left = q->left;
(*r)->right = q->right;
free(q);
return;
}
if ((*r)->right == NULL)
{
node* q = (struct node*)malloc(sizeof(struct node));
q = (*r)->left;
(*r)->key = q->key;
(*r)->left = q->left;
(*r)->right = q->right;
free(q);
return;
}
node* q = (struct node*)malloc(sizeof(struct node));
q = (*r)->right;
while (q->left != NULL)
q = q->left;
(*r)->key = q->key;
if (((*r)->right) == q)
{
(*r)->right = NULL;
}
else
{
del(&q, q->key);
}
}
else if (k > ((*r)->key))
{
del(&((*r)->right), k);
}
else if (k < ((*r)->key))
{
del(&((*r)->left), k);
}
}
void print(node* r)
{
if (r == NULL)
return;
print(r->left);
cout << r->key << " ";
print(r->right);
}
int main()
{
node* root = NULL;
addNode(&root, 11);
addNode(&root, 5);
addNode(&root, 4);
addNode(&root, 8);
addNode(&root, 6);
addNode(&root, 10);
addNode(&root, 9);
addNode(&root, 19);
addNode(&root, 12);
addNode(&root, 30);
addNode(&root, 20);
addNode(&root, 50);
addNode(&root, 31);
addNode(&root, 37);
addNode(&root, 35);
addNode(&root, 38);
print(root);
del(&root, 9);
cout << "\n 9 should be missing" << endl;
print(root);
searchNode(&root, 9);
del(&root, 30);
cout << "\n 30 should be missing" << endl;
print(root);
searchNode(&root, 30);
del(&root, 8);
cout << "\n 8 should be missing" << endl;
print(root);
searchNode(&root, 8);
del(&root, 10);
cout << "\n 10 should be missing" << endl;
print(root);
searchNode(&root, 10);
del(&root, 11);
cout << "\n 11 should be missing" << endl;
print(root);
searchNode(&root, 11);
return 0;
}
当我删除根节点时,输出应该是
4 5 6 12 19 20 31 35 37 38 50
而它是4 5 6 12 12 19 20 31 35 37 38 50
【问题讨论】:
-
我已将您的代码复制到问题本身并进行了格式化。 (下次一定要自己做。)
-
用
new替换所有malloc调用,用delete替换free。 -
node* q = (struct node*)malloc(sizeof(struct node)); q = (*r)->right;这两行只相当于node* q = (*r)->right;(除了第一个选项也会泄漏一些内存)。 -
我一直在 youtube 上搜索视频或文章以及极客的极客 -- 这不是学习 C++ 的方式。是时候获取由同行评审作者撰写的实际书籍了。你真正要做的就是用
cout语句编写C代码。 -
顺便说一句,C++ 中不需要
typedef struct。struct足以创建一个类型。
标签: c++ pointers binary-tree