Przeglądaj źródła

fixed balance to use currentNode, and added case for 0 balance of child

Abhinav Sinha 8 lat temu
rodzic
commit
fe7f319256
1 zmienionych plików z 20 dodań i 17 usunięć
  1. 20 17
      program3/AVLCommands.cpp

+ 20 - 17
program3/AVLCommands.cpp

@@ -105,33 +105,34 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
 	if (currentNode == nullptr) {
 		return;
 	}
+	std::cout << "balance called at key: " << currentNode->key_ << std::endl;
 	std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
-	if (parent != nullptr) {
-		//std::cout << "parent node key: " << parent->key_ << std::endl;
-		std::cout << "balance: " << parent->balance_ <<std::endl;
+	if (currentNode != nullptr) {
+		//std::cout << "currentNode node key: " << currentNode->key_ << std::endl;
+		std::cout << "balance: " << currentNode->balance_ <<std::endl;
 		//std::cout << "Rotation to be preformed:" << std::endl;
 
-		if (parent->balance_ > 1) {
-			if (parent->left_->balance_ > 0) {
+		if (currentNode->balance_ > 1) {
+			if (currentNode->left_->balance_ >= 0) {
 				std::cout << "Left-Left" << std::endl;
-				rightRotate(parent);
+				rightRotate(currentNode);
 				return;
-			} else if (parent->left_->balance_ < 0) {
+			} else if (currentNode->left_->balance_ < 0) {
 				std::cout << "Left-Right" << std::endl;
-		    parent->left_ =  leftRotate(parent->left_);
-		    rightRotate(parent);
+		    currentNode->left_ =  leftRotate(currentNode->left_);
+		    rightRotate(currentNode);
 				return;
 			}
-		} else if (parent->balance_ < -1) {
-			if (parent->right_->balance_ < 0) {
+		} else if (currentNode->balance_ < -1) {
+			if (currentNode->right_->balance_ <= 0) {
 				std::cout << "Right-Right" << std::endl;
-				std::cout << "Current Node key input: " << parent->key_ << std::endl;
-				leftRotate(parent);
+				std::cout << "Current Node key input: " << currentNode->key_ << std::endl;
+				leftRotate(currentNode);
 				return;
-			} else if (parent->right_->balance_ > 0) {
+			} else if (currentNode->right_->balance_ > 0) {
 				std::cout << "Right-Left" << std::endl;
-		    parent->right_ = rightRotate(parent->right_);
-		    leftRotate(parent);
+		    currentNode->right_ = rightRotate(currentNode->right_);
+		    leftRotate(currentNode);
 				return;
 			}
 		}
@@ -270,6 +271,7 @@ bool AVLCommands::Delete(int key) {
 	std::cout << "Deleting key: " << key << std::endl;
 	// base BST delete
 	std::shared_ptr<BSTNode> currentNode = root_;
+	std::shared_ptr<BSTNode> parent = nullptr;
 	while (currentNode != nullptr) {
 		if (currentNode->key_ == key) {
 			if (currentNode->IsLeaf()) {
@@ -288,12 +290,13 @@ bool AVLCommands::Delete(int key) {
 				currentNode->key_ = DeleteMin(currentNode->right_);
 			}
 		}
+		parent = currentNode->parent_.lock();
 		currentNode = (key < currentNode->key_) ?
 			currentNode->left_ : currentNode->right_;
 	}
 
 	height(root_);
-	balance(currentNode);
+	balance(parent);
 
 	// Update balance factor of this ancestor's node
 	/*