|
|
@@ -112,11 +112,11 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
|
|
|
//std::cout << "Rotation to be preformed:" << std::endl;
|
|
|
|
|
|
if (parent->balance_ > 1) {
|
|
|
- if (parent->key_ < parent->left_->key_) {
|
|
|
+ if (parent->key_ > parent->left_->key_) {
|
|
|
std::cout << "Left-Left" << std::endl;
|
|
|
rightRotate(parent);
|
|
|
return;
|
|
|
- } else if (parent->key_ > parent->left_->key_) {
|
|
|
+ } else if (parent->key_ < parent->left_->key_) {
|
|
|
std::cout << "Left-Right" << std::endl;
|
|
|
parent->left_ = leftRotate(parent->left_);
|
|
|
rightRotate(parent);
|
|
|
@@ -141,7 +141,6 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
|
|
|
}
|
|
|
|
|
|
std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
|
|
|
- printTree();
|
|
|
if (currentNode == nullptr) {
|
|
|
std::cout << "Can't rotate on an empty node!" << std::endl;
|
|
|
return currentNode;
|
|
|
@@ -149,25 +148,33 @@ std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> curre
|
|
|
|
|
|
std::cout << "Right Rotate at node " << currentNode->key_ << std::endl;
|
|
|
|
|
|
- std::shared_ptr<BSTNode> x = currentNode->left_;
|
|
|
- currentNode->left_ = x->right_;
|
|
|
- x->right_ = currentNode;
|
|
|
+ std::shared_ptr<BSTNode> newSubRoot = currentNode->left_;
|
|
|
+ currentNode->left_ = newSubRoot->right_;
|
|
|
+ if (newSubRoot->right_ != nullptr) {
|
|
|
+ newSubRoot->right_->parent_ = currentNode;
|
|
|
+ }
|
|
|
+ newSubRoot->right_ = currentNode;
|
|
|
+
|
|
|
+ std::shared_ptr<BSTNode> oldParent = currentNode->parent_.lock();
|
|
|
|
|
|
- std::shared_ptr<BSTNode> parentSubroot = currentNode->parent_.lock();
|
|
|
+ currentNode->parent_ = newSubRoot;
|
|
|
|
|
|
- if (parentSubroot == nullptr) {
|
|
|
- root_ = x;
|
|
|
- x->parent_.reset();
|
|
|
+ if (oldParent == nullptr) {
|
|
|
+ root_ = newSubRoot;
|
|
|
+ newSubRoot->parent_.reset();
|
|
|
} else {
|
|
|
- parentSubroot->left_ = x;
|
|
|
+ if (oldParent->right_== currentNode) {
|
|
|
+ oldParent->right_ = newSubRoot;
|
|
|
+ }else{
|
|
|
+ oldParent->left_ = newSubRoot;
|
|
|
+ }
|
|
|
+ newSubRoot->parent_ = oldParent;
|
|
|
}
|
|
|
|
|
|
- currentNode = x;
|
|
|
- std::cout << "hello" << std::endl;
|
|
|
// Update heights
|
|
|
height(root_);
|
|
|
// Return new root
|
|
|
- return x;
|
|
|
+ return newSubRoot;
|
|
|
}
|
|
|
|
|
|
// Takes the root of the tree to be left rotated and rotates it to provide new root
|
|
|
@@ -189,10 +196,6 @@ std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> curren
|
|
|
|
|
|
currentNode->parent_ = newSubRoot;
|
|
|
|
|
|
- std::cout << "New subtree root: " << newSubRoot->key_ << std::endl;
|
|
|
- //std::cout << "New left: " << y->left_->key_ << std::endl;
|
|
|
- //std::cout << "New right: " << y->right_->key_ << std::endl;
|
|
|
-
|
|
|
if (oldParent == nullptr) {
|
|
|
root_ = newSubRoot;
|
|
|
newSubRoot->parent_.reset();
|
|
|
@@ -205,10 +208,7 @@ std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> curren
|
|
|
newSubRoot->parent_ = oldParent;
|
|
|
}
|
|
|
|
|
|
- std::cout << "hello" << std::endl;
|
|
|
- //printTree();
|
|
|
// Update heights
|
|
|
- //printTree();
|
|
|
height(root_);
|
|
|
// Return new root
|
|
|
return newSubRoot;
|