Explorar o código

rightRotate fixed

Natalie Pueyo %!s(int64=8) %!d(string=hai) anos
pai
achega
5eb9214993
Modificáronse 2 ficheiros con 29 adicións e 29 borrados
  1. 21 21
      program3/AVLCommands.cpp
  2. 8 8
      program3/test.cpp

+ 21 - 21
program3/AVLCommands.cpp

@@ -112,11 +112,11 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
 		//std::cout << "Rotation to be preformed:" << std::endl;
 
 		if (parent->balance_ > 1) {
-			if (parent->key_ < parent->left_->key_) {
+			if (parent->key_ > parent->left_->key_) {
 				std::cout << "Left-Left" << std::endl;
 				rightRotate(parent);
 				return;
-			} else if (parent->key_ > parent->left_->key_) {
+			} else if (parent->key_ < parent->left_->key_) {
 				std::cout << "Left-Right" << std::endl;
 		    parent->left_ =  leftRotate(parent->left_);
 		    rightRotate(parent);
@@ -141,7 +141,6 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
 }
 
 std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
-	printTree();
 	if (currentNode == nullptr) {
 		std::cout << "Can't rotate on an empty node!" << std::endl;
 		return currentNode;
@@ -149,25 +148,33 @@ std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> curre
 
 	std::cout << "Right Rotate at node " << currentNode->key_ << std::endl;
 
-	std::shared_ptr<BSTNode> x = currentNode->left_;
-	currentNode->left_ = x->right_;
-	x->right_ = currentNode;
+	std::shared_ptr<BSTNode> newSubRoot = currentNode->left_;
+	currentNode->left_ = newSubRoot->right_;
+	if (newSubRoot->right_ != nullptr) {
+		newSubRoot->right_->parent_ = currentNode;
+	}
+	newSubRoot->right_ = currentNode;
+
+	std::shared_ptr<BSTNode> oldParent = currentNode->parent_.lock();
 
-	std::shared_ptr<BSTNode> parentSubroot = currentNode->parent_.lock();
+	currentNode->parent_ = newSubRoot;
 
-	if (parentSubroot == nullptr) {
-		root_ = x;
-		x->parent_.reset();
+	if (oldParent == nullptr) {
+		root_ = newSubRoot;
+		newSubRoot->parent_.reset();
 	} else {
-		parentSubroot->left_ = x;
+		if (oldParent->right_== currentNode) {
+			oldParent->right_ = newSubRoot;
+		}else{
+			oldParent->left_ = newSubRoot;
+		}
+		newSubRoot->parent_ = oldParent;
 	}
 
-	currentNode = x;
-	std::cout << "hello" << std::endl;
 	// Update heights
 	height(root_);
 	// Return new root
-	return x;
+	return newSubRoot;
 }
 
 // Takes the root of the tree to be left rotated and rotates it to provide new root
@@ -189,10 +196,6 @@ std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> curren
 
 	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 (oldParent == nullptr) {
 		root_ = newSubRoot;
 		newSubRoot->parent_.reset();
@@ -205,10 +208,7 @@ std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> curren
 		newSubRoot->parent_ = oldParent;
 	}
 
-	std::cout << "hello" << std::endl;
-	//printTree();
   //  Update heights
-	//printTree();
 	height(root_);
   // Return new root
   return newSubRoot;

+ 8 - 8
program3/test.cpp

@@ -5,31 +5,31 @@
 int main()
 {
     AVLCommands avl;
-    avl.Insert(2);
+    avl.Insert(9);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(3);
+    avl.Insert(8);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(4);
+    avl.Insert(7);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(5);
+    avl.Insert(6);
     avl.printTree();
     std::cout << std::endl;
     avl.Insert(1);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(6);
+    avl.Insert(5);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(7);
+    avl.Insert(4);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(8);
+    avl.Insert(3);
     avl.printTree();
     std::cout << std::endl;
-    avl.Insert(9);
+    avl.Insert(2);
     //avl.Insert(50);
     avl.printTree();
     std::cout << std::endl;