我正在构建一个红黑树,但可能是我的类RBTree的析构函数存在一些问题。我将10 ^ 7的值添加到树中,然后调用析构函数,但内存似乎没有被释放。 (我查看系统监视器,我的程序仍然使用200MB)。
你能告诉我我的析构函数有什么问题吗?这是我的源代码。
抱歉我的英语不好。
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
enum Color {RED, BLACK};
template<class Data> class RBNode;
template<class Data> class RBTree;
template<class Data> class RBNode {
Color color; RBNode *p, *left, *right;
public:
Data v;
RBNode(Color color, RBNode *p, RBNode *left, RBNode *right, Data v):
color(color), p(p), left(left), right(right), v(v) {}
RBNode() {}
friend class RBTree<Data>;
};
template<class Data> class RBTree {
typedef RBNode<Data> Node;
typedef Node * PNode;
PNode root, nil;
void LeftRotate(PNode x) {
PNode y = x->right; x->right = y->left;
if(y->left != nil) y->left->p = x;
y->p = x->p;
if(x->p == nil) root = y;
else if(x == x->p->left) x->p->left = y;
else x->p->right = y;
y->left = x; x->p = y;
}
void RightRotate(PNode y) {
PNode x = y->left; y->left = x->right;
if(x->right != nil) x->right->p = y;
x->p = y->p;
if(y->p == nil) root = x;
else if(y == y->p->left) y->p->left = x;
else y->p->right = x;
x->right = y; y->p = x;
}
void insertFixUp(PNode z) {
while(z->p->color == RED) {
if(z->p == z->p->p->left) {
PNode y = z->p->p->right;
if(y->color == RED) z->p->color = y->color = BLACK, z->p->p->color = RED, z = z->p->p;
else {
if(z == z->p->right) LeftRotate(z = z->p);
z->p->color = BLACK; z->p->p->color = RED; RightRotate(z->p->p);
}
} else {
PNode y = z->p->p->left;
if(y->color == RED) z->p->color = y->color = BLACK, z->p->p->color = RED, z = z->p->p;
else {
if(z == z->p->left) RightRotate(z = z->p);
z->p->color = BLACK; z->p->p->color = RED; LeftRotate(z->p->p);
}
}
}
root->color = BLACK;
}
public:
RBTree() {
nil = new Node;
nil->color = BLACK;
nil->p = nil->left = nil->right = nil;
nil->v = Data();
root = nil;
}
~RBTree() {
delete root;
delete nil;
}
void insert(Data v) {
PNode y = nil, x = root;
while(x != nil) {
y = x;
x = v < x->v ? x->left : x->right;
}
PNode z = new Node; *z = Node(RED, y, nil, nil, v);
if(y == nil) root = z;
else if(v < y->v) y->left = z;
else y->right = z;
insertFixUp(z);
}
};
int main() {
RBTree<int> tree;
for(int i = 0; i < 10000000; ++i) tree.insert(i);
tree.~RBTree();
getchar();
return 0;
}
答案 0 :(得分:1)
您需要向RBNode
添加析构函数,删除其子代:
template<class Data> class RBNode {
...
~RBNode() {
delete left;
delete right;
}
...
};
按原样,删除树时将删除根节点,但根节点本身不会释放其资源。因此,您将丢失对root的子节点及其所有子节点等的所有引用。因为您不再具有对这些节点的引用,所以无法删除它们,您有内存泄漏。
析构函数确保当我们即将失去对节点子节点的引用时,这些子节点会被释放(以及它们的子节点等)。
答案 1 :(得分:1)
首先,它的错误在于你没有使用智能指针。其次,您没有在Node类中使用智能指针,因此当删除根时,不会删除任何其他对象。
答案 2 :(得分:0)
您的树节点似乎不会以递归方式删除其子节点。你需要在节点中使用析构函数,然后当根被破坏时,所有内容都会级联。
答案 3 :(得分:0)
你的析构函数只释放两个元素:root和nil。为了释放树的其余部分,你应该以某种方式传播释放树中的元素,如:
~RBNode() {
if (left != nil ) delete left;
if (right != nil) delete right;
}
(这只是一个想法,当然这个代码在字面上不起作用,因为你在析构函数中看不到nil。)
答案 4 :(得分:0)
我找到了我的析构函数,但我有更多的属性,比如大小和父指针,但我认为它会有所帮助
~RBTree() {
RBNode *p(root);
while(size!=0) {
if(p==root && size==1) { delete root; size--;}
else if(p->right!=0) p=p->right;
else if(p->left!=0) p=p->left;
else {
RBNode *c(p);
p=p->parent;
if(p->left==c) {
delete c;
p->left=0;
}
else {
delete c;
p->right=0;
}
size--;
}
}
}