Abhinav Sinha 8 роки тому
батько
коміт
fd32ae61ac
1 змінених файлів з 22 додано та 11 видалено
  1. 22 11
      program3/AVLCommands.cpp

+ 22 - 11
program3/AVLCommands.cpp

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