|
|
@@ -172,29 +172,30 @@ std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> curren
|
|
|
std::cout << "Can't rotate on an empty node!" << std::endl;
|
|
|
return currentNode;
|
|
|
}
|
|
|
- std::cout << "Left Rotate!" << std::endl;
|
|
|
+ std::cout << "Left Rotate at node " << currentNode->key_ << std::endl;
|
|
|
|
|
|
std::shared_ptr<BSTNode> y = currentNode->right_;
|
|
|
+ currentNode->right_ = y->left_;
|
|
|
y->left_ = currentNode;
|
|
|
|
|
|
- std::shared_ptr<BSTNode> T2 = y->left_;
|
|
|
- currentNode->right_ = T2;
|
|
|
-
|
|
|
-
|
|
|
+ std::shared_ptr<BSTNode> parentSubroot = currentNode->parent_.lock();
|
|
|
std::cout << "New subtree root: " << y->key_ << std::endl;
|
|
|
- std::cout << "New left: " << y->left_->key_ << std::endl;
|
|
|
- std::cout << "New right: " << y->right_->key_ << std::endl;
|
|
|
- std::cout << "Old subtree root: " << currentNode->key_ << std::endl;
|
|
|
- //std::cout << "Old left: " << currentNode->left_->key_ << std::endl;
|
|
|
- std::cout << "Old right: " << currentNode->right_->key_ << std::endl;
|
|
|
+ //std::cout << "New left: " << y->left_->key_ << std::endl;
|
|
|
+ //std::cout << "New right: " << y->right_->key_ << std::endl;
|
|
|
|
|
|
- std::cout << "New root: " << root_->key_ << std::endl;
|
|
|
+ if (parentSubroot == nullptr) {
|
|
|
+ root_ = y;
|
|
|
+ y->parent_.reset();
|
|
|
+ } else {
|
|
|
+ parentSubroot->right_ = y;
|
|
|
+ }
|
|
|
|
|
|
+ currentNode = y;
|
|
|
+ std::cout << "hello" << std::endl;
|
|
|
// Update heights
|
|
|
//currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
|
|
|
//y->height_ = max(height(y->left_), height(y->right_))+1;
|
|
|
- //height(y);
|
|
|
- printTree();
|
|
|
+ height(root_);
|
|
|
// Return new root
|
|
|
return y;
|
|
|
}
|