【发布时间】:2016-07-31 23:11:27
【问题描述】:
我检查了其他类似的主题,但似乎没有一个对我有帮助。
我正在尝试为此特定的 BST 实现编写析构函数。每个节点都包含一个指向父节点的指针、一个指向左节点的指针、一个指向右节点的指针和一个指向它所包含的值的指针。 这是课程开头的样子:
#ifndef BST_H
#define BST_H
#include <iostream>
template <typename T>
class BST{
private:
BST<T> *left;
BST<T> *right;
BST<T> *parent;
T *value;
public:
BST() {
this->parent = NULL;
this->left = NULL;
this->right = NULL;
this->value = NULL;
}
~BST() {
removeRecursively(this);
}
void removeRecursively(BST<T>* node) {
if (node->left != NULL)
removeRecursively(node->left);
if (node->right != NULL)
removeRecursively(node->right);
if (node->left == NULL && node->right == NULL) {
if (node->parent->left == node)
node->parent->left = NULL;
if (node->parent->right == node)
node->parent->right = NULL;
node->parent = NULL;
node->value = NULL;
delete node->value;
delete node;
}
}
void add(T value) {
if (this->value == NULL) { // root-only case
this->value = new T;
*(this->value) = value;
}
else {
if (value < *(this->value)) {
if (this->left != NULL) // has left child
this->left->add(value);
else { // has no left child
this->left = new BST<T>;
this->left->value = new T;
this->left->parent = this;
*(this->left->value) = value;
}
}
else {
if (this->right != NULL) // has right child
this->right->add(value);
else { // has no right child
this->right = new BST<T>;
this->right->value = new T;
this->right->parent = this;
*(this->right->value) = value;
}
}
}
}
void inOrderDisplay() {
if (this->left != NULL)
this->left->inOrderDisplay();
std::cout << *(this->value) << " ";
if (this->right != NULL)
this->right->inOrderDisplay();
}
BST<T>* search(T value) {
if (*(this->value) == value)
return this;
else if (this->left != NULL && value < *(this->value))
return this->left->search(value);
else if (this->right != NULL && value > *(this->value))
return this->right->search(value);
else
return NULL;
}
BST<T>* remove(T value) {
BST<T>* node = search(value);
if (node != NULL) {
if (node->left == NULL && node->right == NULL) { // leaf node
delete node->value;
if (node->parent->left == node) // is left child
node->parent->left = NULL;
else // is right child
node->parent->right = NULL;
delete node;
}
// 1 child nodes
if (node->left != NULL && node->right == NULL) { // has left child
if (node->parent->left == node) // is left child
node->parent->left = node->left;
else // is right child
node->parent->right = node->left;
delete node->value;
node->parent = NULL;
delete node;
}
if (node->left == NULL && node->right != NULL) { // has right child
if (node->parent->left == node) // is left child
node->parent->left = node->right;
else // is right child
node->parent->right = node->right;
delete node->value;
node->parent = NULL;
delete node;
}
// 2 children nodes
if (node->left != NULL && node->right != NULL) {
T aux;
BST<T>* auxNode = node->right;
while (auxNode->left != NULL)
auxNode = auxNode->left;
aux = *(auxNode->value);
if (auxNode->right != NULL) {
*(auxNode->value) = *(auxNode->right->value);
auxNode->right->parent = NULL;
auxNode->right->value = NULL;
auxNode->right = NULL;
delete auxNode->right;
}
else {
if (auxNode->parent->left == auxNode) // is left child
auxNode->parent->left = NULL;
else // is right child
auxNode->parent->right = NULL;
auxNode->value = NULL;
delete auxNode;
}
*(node->value) = aux;
}
}
return this;
}
};
#endif // BST_H
BST 类的用法如下:
BST<int>* root = new BST<int>();
root->add(5);
root->add(2);
root->add(-17);
root->inOrderDisplay();
root->remove(5);
我提到所有方法都可以正常工作(我决定不发布它们,因为它们不是这个问题的主题)。问题是,当我使用 Valgrind 运行我的测试文件时,它会检测到一些内存泄漏,我确信它们的发生是因为缺少适当的析构函数(上述析构函数会产生分段错误)。
编辑:我添加了其他方法的代码
谢谢!
【问题讨论】:
-
您应该包含
add(和remove)的代码...您可能在这些代码中犯了错误... -
使用 RAII:
std::unique_ptr<BST<T>> left; std::unique_ptr<BST<T>> right; std::unique_ptr<T> value; BST<T>* parent; -
我看了两遍,但我找不到您的
removeRecursively()有什么问题,尽管我认为没有必要将您的指针归零。但这也不会造成伤害。 -
我提到所有方法都可以正常工作 -- 如果我每次听到这个代码都没有发布的地方都有一美元是问题,我会成为一个有钱人。您在
main程序中发布了对add函数的调用。你发帖,我们需要看看。 -
@RudyVelthuis:
removeRecursively()中存在几个问题,包括(但不仅限于)当前答案中指出的问题。
标签: c++ recursion binary-search-tree destructor