|
@@ -73,6 +73,8 @@ int AVLCommands::max(int a, int b) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int AVLCommands::height(std::shared_ptr<BSTNode> currentNode) {
|
|
int AVLCommands::height(std::shared_ptr<BSTNode> currentNode) {
|
|
|
|
|
+ //std::cout << "Updating the height..." << std::endl;
|
|
|
|
|
+ //std::cout << "height: " << currentNode->height_ << std::endl;
|
|
|
if (root_ == nullptr) {
|
|
if (root_ == nullptr) {
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
@@ -105,27 +107,28 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
|
|
|
}
|
|
}
|
|
|
std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
|
|
std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
|
|
|
if (parent != nullptr) {
|
|
if (parent != nullptr) {
|
|
|
- std::cout << "parent node key: " << parent->key_ << std::endl;
|
|
|
|
|
- std::cout << "Rotation to be preformed:" << std::endl;
|
|
|
|
|
|
|
+ //std::cout << "parent node key: " << parent->key_ << std::endl;
|
|
|
|
|
+ std::cout << "balanceFactor at balance: " << parent->balance_ <<std::endl;
|
|
|
|
|
+ //std::cout << "Rotation to be preformed:" << std::endl;
|
|
|
|
|
|
|
|
if (parent->balance_ > 1) {
|
|
if (parent->balance_ > 1) {
|
|
|
- if (parent->key_ < currentNode->left_->key_) {
|
|
|
|
|
|
|
+ if (parent->key_ < parent->left_->key_) {
|
|
|
std::cout << "Left-Left" << std::endl;
|
|
std::cout << "Left-Left" << std::endl;
|
|
|
rightRotate(parent);
|
|
rightRotate(parent);
|
|
|
return;
|
|
return;
|
|
|
- } else if (parent->key_ > currentNode->left_->key_) {
|
|
|
|
|
|
|
+ } else if (parent->key_ > parent->left_->key_) {
|
|
|
std::cout << "Left-Right" << std::endl;
|
|
std::cout << "Left-Right" << std::endl;
|
|
|
parent->left_ = leftRotate(parent->left_);
|
|
parent->left_ = leftRotate(parent->left_);
|
|
|
rightRotate(parent);
|
|
rightRotate(parent);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
} else if (parent->balance_ < -1) {
|
|
} else if (parent->balance_ < -1) {
|
|
|
- if (parent->key_ < currentNode->right_->key_) {
|
|
|
|
|
|
|
+ if (parent->key_ < parent->right_->key_) {
|
|
|
std::cout << "Right-Right" << std::endl;
|
|
std::cout << "Right-Right" << std::endl;
|
|
|
- std::cout << "Parent key input: " << parent->key_ << std::endl;
|
|
|
|
|
- rightRotate(parent);
|
|
|
|
|
|
|
+ std::cout << "Current Node key input: " << parent->key_ << std::endl;
|
|
|
|
|
+ leftRotate(parent);
|
|
|
return;
|
|
return;
|
|
|
- } else if (parent->key_ > currentNode->right_->key_) {
|
|
|
|
|
|
|
+ } else if (parent->key_ > parent->right_->key_) {
|
|
|
std::cout << "Right-Left" << std::endl;
|
|
std::cout << "Right-Left" << std::endl;
|
|
|
parent->right_ = rightRotate(parent->right_);
|
|
parent->right_ = rightRotate(parent->right_);
|
|
|
leftRotate(parent);
|
|
leftRotate(parent);
|
|
@@ -138,14 +141,15 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
|
|
std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
|
|
|
|
|
+ printTree();
|
|
|
if (currentNode == nullptr) {
|
|
if (currentNode == nullptr) {
|
|
|
std::cout << "Can't rotate on an empty node!" << std::endl;
|
|
std::cout << "Can't rotate on an empty node!" << std::endl;
|
|
|
return currentNode;
|
|
return currentNode;
|
|
|
}
|
|
}
|
|
|
std::cout << "First, Right Rotate!" << std::endl;
|
|
std::cout << "First, Right Rotate!" << std::endl;
|
|
|
- std::cout << "Top: " << currentNode;
|
|
|
|
|
- std::cout << ", Middle: " << currentNode->left_;
|
|
|
|
|
- std::cout << ", Last: " << currentNode->left_->right_ << std::endl;
|
|
|
|
|
|
|
+ std::cout << "Top: " << currentNode->key_;
|
|
|
|
|
+ std::cout << ", Middle: " << currentNode->left_->key_;
|
|
|
|
|
+ std::cout << ", Last: " << currentNode->left_->right_->key_ << std::endl;
|
|
|
std::shared_ptr<BSTNode> x = currentNode->left_;
|
|
std::shared_ptr<BSTNode> x = currentNode->left_;
|
|
|
std::cout << "Does the current node have a left node?" << std::endl;
|
|
std::cout << "Does the current node have a left node?" << std::endl;
|
|
|
std::shared_ptr<BSTNode> T2 = x->right_;
|
|
std::shared_ptr<BSTNode> T2 = x->right_;
|
|
@@ -160,23 +164,37 @@ std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> curre
|
|
|
|
|
|
|
|
// Return new root
|
|
// Return new root
|
|
|
return x;
|
|
return x;
|
|
|
-
|
|
|
|
|
- return currentNode;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Takes the root of the tree to be left rotated and rotates it to provide new root
|
|
|
std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> currentNode){
|
|
std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> currentNode){
|
|
|
|
|
+ if (currentNode == nullptr) {
|
|
|
|
|
+ std::cout << "Can't rotate on an empty node!" << std::endl;
|
|
|
|
|
+ return currentNode;
|
|
|
|
|
+ }
|
|
|
std::cout << "Left Rotate!" << std::endl;
|
|
std::cout << "Left Rotate!" << std::endl;
|
|
|
|
|
+
|
|
|
std::shared_ptr<BSTNode> y = currentNode->right_;
|
|
std::shared_ptr<BSTNode> y = currentNode->right_;
|
|
|
- std::shared_ptr<BSTNode> T2 = y->left_;
|
|
|
|
|
|
|
+ y->left_ = currentNode;
|
|
|
|
|
|
|
|
- // Perform rotation
|
|
|
|
|
- y->left_ = currentNode;
|
|
|
|
|
- currentNode->right_ = T2;
|
|
|
|
|
|
|
+ std::shared_ptr<BSTNode> T2 = y->left_;
|
|
|
|
|
+ currentNode->right_ = T2;
|
|
|
|
|
|
|
|
- // Update heights
|
|
|
|
|
- currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
|
|
|
|
|
- y->height_ = max(height(y->left_), height(y->right_))+1;
|
|
|
|
|
|
|
|
|
|
|
|
+ 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 root: " << root_->key_ << 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();
|
|
|
// Return new root
|
|
// Return new root
|
|
|
return y;
|
|
return y;
|
|
|
}
|
|
}
|
|
@@ -193,6 +211,9 @@ void AVLCommands::printTree(std::shared_ptr<BSTNode> currentNode){
|
|
|
std::cout << "key: " << currentNode->key_ << std::endl;
|
|
std::cout << "key: " << currentNode->key_ << std::endl;
|
|
|
std::cout << "height: " << currentNode->height_ << std::endl;
|
|
std::cout << "height: " << currentNode->height_ << std::endl;
|
|
|
std::cout << "balance: " << currentNode->balance_ << std::endl;
|
|
std::cout << "balance: " << currentNode->balance_ << std::endl;
|
|
|
|
|
+ if (currentNode->parent_.lock() != nullptr) {
|
|
|
|
|
+ std::cout << "parent: " << currentNode->parent_.lock()->key_ << std::endl;
|
|
|
|
|
+ }
|
|
|
if (currentNode->left_ != nullptr) {
|
|
if (currentNode->left_ != nullptr) {
|
|
|
std::cout << "left" << std::endl;
|
|
std::cout << "left" << std::endl;
|
|
|
printTree(currentNode->left_);
|
|
printTree(currentNode->left_);
|