Prechádzať zdrojové kódy

Insert works now. Fixed balance to pick correct rotation

Natalie Pueyo 8 rokov pred
rodič
commit
41f9ff42f3
1 zmenil súbory, kde vykonal 5 pridanie a 5 odobranie
  1. 5 5
      program3/AVLCommands.cpp

+ 5 - 5
program3/AVLCommands.cpp

@@ -108,27 +108,27 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
 	std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
 	if (parent != nullptr) {
 		//std::cout << "parent node key: " << parent->key_ << std::endl;
-		std::cout << "balanceFactor at balance: " << parent->balance_ <<std::endl;
+		std::cout << "balance: " << parent->balance_ <<std::endl;
 		//std::cout << "Rotation to be preformed:" << std::endl;
 
 		if (parent->balance_ > 1) {
-			if (parent->key_ > parent->left_->key_) {
+			if (parent->left_->balance_ > 0) {
 				std::cout << "Left-Left" << std::endl;
 				rightRotate(parent);
 				return;
-			} else if (parent->key_ < parent->left_->key_) {
+			} else if (parent->left_->balance_ < 0) {
 				std::cout << "Left-Right" << std::endl;
 		    parent->left_ =  leftRotate(parent->left_);
 		    rightRotate(parent);
 				return;
 			}
 		} else if (parent->balance_ < -1) {
-			if (parent->key_ < parent->right_->key_) {
+			if (parent->right_->balance_ < 0) {
 				std::cout << "Right-Right" << std::endl;
 				std::cout << "Current Node key input: " << parent->key_ << std::endl;
 				leftRotate(parent);
 				return;
-			} else if (parent->key_ > parent->right_->key_) {
+			} else if (parent->right_->balance_ > 0) {
 				std::cout << "Right-Left" << std::endl;
 		    parent->right_ = rightRotate(parent->right_);
 		    leftRotate(parent);