C++ C++;AVLtree插入法
所以我遇到的问题是AVLtree的insert方法。我的avltree存储城市对象,我使用城市的名称来决定它们应该去哪里 我的城市.cpp课程:C++ C++;AVLtree插入法,c++,pointers,rotation,insertion,avl-tree,C++,Pointers,Rotation,Insertion,Avl Tree,所以我遇到的问题是AVLtree的insert方法。我的avltree存储城市对象,我使用城市的名称来决定它们应该去哪里 我的城市.cpp课程: #include "City.h" #include <iostream> using namespace std; City::City(string name, string country, double lat, double lon){ this->name =
#include "City.h"
#include <iostream>
using namespace std;
City::City(string name, string country, double lat, double lon){
this->name = name;
this->country = country;
this->latitude = lat;
this->longtitude = lon;
}
double City::getLatOfCity(void){
return this->latitude;
}
double City::getLonOfCity(void){
return this->longtitude;
}
string City::getName(void){
return this->name;
}
string City::getCountry(void){
return this->country;
}
右转法:
Node* AVLtree::rotateRight(Node* node){
Node* tempParent = node->getParent();
if(tempParent!=0){cout<<"IHN";cout<<"temp parent: " << tempParent->getCity()->getName();}
Node* tempNode = node->getChildR();
node->setChildR(tempNode->getChildL());
tempNode->setChildL(node);
if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;}
else{tempParent->setChildR(tempNode);}
return tempNode;
}
我知道这很混乱,但它似乎正确地旋转了节点
Node* AVLtree::rotateTwoRights(Node* node){
Node* tempParent = node;
Node* tempNode = node->getChildR();
Node* tempTempNode = tempNode->getChildL();
tempNode->setChildL(tempTempNode->getChildL());
tempTempNode->setChildR(tempNode);
tempParent->setChildR(tempTempNode);
Node* tempTempParent = tempParent->getParent();
tempTempParent->setChildR(tempParent->getChildL());
tempParent->setChildL(tempTempParent);
Node* newTempParent = tempParent->getParent();
if(newTempParent==0){tempParent->removeParent();this->rootNode=tempParent;}
else{newTempParent->setChildR(tempParent);}
return tempParent;
}
与我的第一个旋转双右方法相同,但用于左侧。同样的问题:
Node* AVLtree::rotateTwoLefts(Node* node){
node = rotateRight(node->getChildL());
node = rotateLeft(node);
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);
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);
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;
}
插入“CA”后,应遵循以下步骤:
(一)
(二)
(三)
错误是,当我对它运行测试时,树的根仍然是B。
我相信这是由于parent
,因为在递归调用后重新平衡树时,它不会得到更新。但是,当我将parent
赋值给方法的返回值时,我甚至不能将“C”添加到树中
我将非常感激任何形式的帮助。我在这上面花了很多时间,真的很想把这件事做完
我为这篇冗长的帖子和混乱的代码道歉,并提前表示感谢 假设节点
C
是第一个左子树深度(0)和右子树深度(2)相差2的节点,我认为应该只进行简单的左旋转,从而
B
/ \
A CA
/ \
C D
当不平衡的修补达到
B
时,树已充分平衡。我没有详细查看您的代码,但您假设的结果似乎不正确,也就是说,代码有可能是正确的。啊,是的,应该是这样的。然而,代码仍然存在问题:/刚刚意识到它不应该这样,因为它需要遵循通用的右-右旋转,这将导致我的原始帖子
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);
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);
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;
}
B
/ \
A C
\
D
B
/ \
A C
\
D
/
CA
B
/ \
A C
\
CA
\
D
C
/ \
B CA
/ \
A D
B
/ \
A CA
/ \
C D