C++ 如何用对象数据实现二叉搜索树中的deletenode?
我已经实现了delete方法,但是由于某种原因,即使在我为其调用delete方法之后,已删除的节点仍然存在于树中C++ 如何用对象数据实现二叉搜索树中的deletenode?,c++,binary-search-tree,C++,Binary Search Tree,我已经实现了delete方法,但是由于某种原因,即使在我为其调用delete方法之后,已删除的节点仍然存在于树中 class Leaf { Item* data; Leaf* left; Leaf* right; friend class binaryTree; Leaf(Item* data, Leaf* left = 0, Leaf* right = 0) : data(data), left(left), right(rig
class Leaf {
Item* data;
Leaf* left;
Leaf* right;
friend class binaryTree;
Leaf(Item* data, Leaf* left = 0, Leaf* right = 0) :
data(data), left(left), right(right) {
}
~Leaf() {
delete left;
delete right;
}
Leaf* remove(Item* data, Leaf* parent) {
if (data < this->data) {
if (left != 0)
return left->remove(data, this);
else
return 0;
} else if (data > this->data) {
if (right != 0)
return right->remove(data, this);
else
return 0;
} else {
if (left != 0 && right != 0) {
this->data = right->minValue();
return right->remove(this->data, this);
} else if (parent->left == this) {
parent->left = (left != 0) ? left : right;
return this;
} else if (parent->right == this) {
parent->right = (left != 0) ? left : right;
return this;
}
}
return 0;
}
Item* minValue() {
if (left == 0)
return data;
else
return left->minValue();
}
};
我不明白为什么删除的节点在这些之后仍然存在。
我的inorderprint函数首先检查数据是否存在(如果(节点)),但仍然打印内存垃圾。
提前谢谢
以下是Item类和inorderprint:
class Item {
string neptun;
string name;
double rating;
Item* prev;
Item* next;
friend class circList;
friend class binaryTree;
friend class Leaf;
Item(string neptun, string name, double rating, Item* prev = 0, Item* next =
0) :
neptun(neptun), name(name), rating(rating), prev(prev), next(next) {
}
bool operator<(Item* other) {
return ((this->neptun) < (other->neptun));
}
bool operator>(Item* other) {
return ((this->neptun) > (other->neptun));
}
};
void listForward() {
cout << "Listing in abc order:" << endl;
listForward(root);
cout << endl;
}
void listForward(Leaf* node) {
if (node) {
listForward(node->left);
cout << node->data->neptun << endl;
listForward(node->right);
}
}
类项目{
弦海王星;
字符串名;
双重评级;
项目*prev;
项目*下一步;
朋友级旋转器;
朋友类二叉树;
朋友类叶子;
项目(字符串neptun,字符串名称,双重评级,项目*prev=0,项目*next=
0) :
海王星(海王星),名字(名字),评级(评级),上一个(上一个),下一个(下一个){
}
bool OPERNOREPTUN)<(其他->海王星);
}
bool运算符>(项目*其他){
返回((本->海王星)>(其他->海王星));
}
};
void listForward(){
cout看起来还不错。你能给出一个有问题的树值的例子吗?Item
上的比较器工作吗?你能知道inorderprint是如何实现的吗?我有理由怀疑你的叶节点中的比较操作没有做你认为它是做的事情。特别是,考虑到这一点:leaf*remove(Item*data,Leaf*parent)
执行此操作:如果(datadata)
要比较指针值;而不是Item
类型的解引用值。此外,如果指针不具有相同的逻辑序列(即数组中的连续分配,包括lastnode+1)比较本身是未定义的行为,因为在这种情况下只定义了操作符==
和操作符!=
。我已将Item类添加到问题中。现在问题已经解决了,感谢WhozCraig,这确实是一个比较问题。
class Item {
string neptun;
string name;
double rating;
Item* prev;
Item* next;
friend class circList;
friend class binaryTree;
friend class Leaf;
Item(string neptun, string name, double rating, Item* prev = 0, Item* next =
0) :
neptun(neptun), name(name), rating(rating), prev(prev), next(next) {
}
bool operator<(Item* other) {
return ((this->neptun) < (other->neptun));
}
bool operator>(Item* other) {
return ((this->neptun) > (other->neptun));
}
};
void listForward() {
cout << "Listing in abc order:" << endl;
listForward(root);
cout << endl;
}
void listForward(Leaf* node) {
if (node) {
listForward(node->left);
cout << node->data->neptun << endl;
listForward(node->right);
}
}