C++AVLtree删除方法
C++ AVLtree deletion method
我真的很纠结于如何在C++中实现avltree的删除方法。我真的不知道从哪里开始。我有找到节点的方法、旋转方法和插入方法,但我真的不确定如何启动这种删除方法。我知道这是一个模糊的,网上有资源,但我发现他们不是很清楚。有人能解释一下这个过程吗?
如果你想看到任何代码,请询问:)
如有任何建议,我们将不胜感激。非常感谢!
-----编辑-----
查找节点方法:
Node* AVLtree::findNode(string cityID){
Node *thisNode = this->rootNode;
while(thisNode!=0){
int location = thisNode->getCity()->getName().compare(cityID);
if(location==0){return thisNode;
}else if(location<0){//cout<<"NODE: " << thisNode->getCity()->getName()<<endl;
thisNode = thisNode->getChildR();
}else{thisNode = thisNode->getChildL();}
}
return thisNode;
}
向左旋转:
Node* AVLtree::rotateLeft(Node* node){
Node* tempParent= node->getParent();
Node* tempNode = node->getChildL();
node->setChildL(tempNode->getChildR());
tempNode->setChildR(node);
if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;tempNode->getNewHeight();}
else{tempParent->setChildR(tempNode);
}
return tempNode;
}
向右旋转:
Node* AVLtree::rotateRight(Node* node){
Node* tempParent = node->getParent();
Node* tempNode = node->getChildR();
node->setChildR(tempNode->getChildL());
tempNode->setChildL(node);
if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;tempNode->getNewHeight();}
else{tempParent->setChildR(tempNode);}
return tempNode;
}
双重旋转,用于插入不平衡节点右侧子项的左侧子树:
Node* AVLtree::rotateTwoRights(Node* node){
node = rotateLeft(node->getChildR());
node = rotateRight(node->getParent());
return node;
}
左右双旋转条件:
Node* AVLtree::rotateTwoLefts(Node* node){
node = rotateRight(node->getChildL());
node = rotateLeft(node->getParent());
return node;
}
插入方式:
Node* AVLtree::insertNode(Node* parent, Node* node, City *city, int side){
if(node == 0){
if(side==0){
node = parent->setChildL(new Node(city));
}else if(side==1){
node = parent->setChildR(new Node(city));
}
}else if(node->getCity()->getName().compare(city->getName())<0){ //Right case
parent = node;
node = insertNode(parent, node->getChildR(), city, 1);
parent->getNewHeight();
if(parent->getBalance()==2){
if(parent->getChildR()->getCity()->getName().compare(city->getName())<0){
node = rotateRight(parent);
}else{
node = rotateTwoRights(parent);
}
}
}else if(node->getCity()->getName().compare(city->getName())>0){ //Left case
parent = node;
node = insertNode(parent, node->getChildL(), city, 0);
parent->getNewHeight();
if(parent->getBalance()==-2){
if(parent->getChildL()->getCity()->getName().compare(city->getName())>0){
node = rotateLeft(parent);
}else{
node = rotateTwoLefts(parent);
}
}
}else{
node=0;
}
return node;
}
互联网上有许多抽象解释的此类算法的例子。
如果没有源代码,很难提供特定的帮助,因为我不知道你称你的方法为什么。
相关文章:
- 检测win32服务创建和删除的最佳方法
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 一种从内存中删除 UTF 字节的方法?
- 从数组中删除非唯一值、保持顺序和不使用向量的最佳方法?
- 在析构函数中删除单链表的正确方法是什么?
- 处理从列表中删除指向对象的指针的"healthy"方法是什么?
- 删除类成员的动态分配内存的最佳方法是什么
- 从浮点数中删除小数部分但保留类型的有效方法
- 如果方法在类中定义,则阻止编译器删除方法
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 有没有一种预处理器的方法可以从调试符号中删除代码段
- 删除父/子窗口层次结构的最佳方法
- 工厂方法创建的对象应该在哪里删除?
- 如何通过最快的方法删除指定的标签组件
- 无法使用擦除方法删除矢量的最后一个元素
- 尝试使用窗口注册表中的路径和 DeleteFile() 方法删除.exe文件
- 通过此方法删除链表中的数据会发生什么情况
- 如何使用c++使用流的方法删除文件
- 如何通过析构函数方法删除对象(类)
- 标准的c++11方法删除一个类型的所有指针