Natalie Pueyo 8 سال پیش
والد
کامیت
0bedaaae7d
1فایلهای تغییر یافته به همراه26 افزوده شده و 11 حذف شده
  1. 26 11
      program3/AVLCommands.cpp

+ 26 - 11
program3/AVLCommands.cpp

@@ -178,28 +178,43 @@ std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> curren
 	}
 	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> newSubroot = currentNode->right_;
 
-	std::shared_ptr<BSTNode> parentSubroot = currentNode->parent_.lock();
-	std::cout << "New subtree root: " << y->key_ << std::endl;
+
+	currentNode->right_ = newSubroot->left_;
+	if (newSubroot->left_ != nullptr) {
+		newSubroot->left_->parent_ = currentNode;
+	}
+
+	newSubroot->left_ = currentNode;
+
+	std::shared_ptr<BSTNode> oldParent = 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;
 
-	if (parentSubroot == nullptr) {
-		root_ = y;
-		y->parent_.reset();
+	if (oldParent == nullptr) {
+		root_ = newSubroot;
+		newSubroot->parent_.reset();
 	} else {
-		parentSubroot->right_ = y;
+		if (oldParent->right_ == currentNode) {
+			oldParent->right_ = newSubroot;
+		} else {
+			oldParent->left_ = newSubroot;
+		}
+
+		newSubroot->parent_ = oldParent;
 	}
 
-	currentNode = y;
+	//currentNode = y;
+
 	std::cout << "hello" << std::endl;
+	//printTree();
   //  Update heights
 	height(root_);
   // Return new root
-  return y;
+  return newSubroot;
 }
 
 void AVLCommands::printTree(){