Quellcode durchsuchen

somehow the node I'm trying to pass to the rotate functions isn't getting through

Natalie Pueyo vor 8 Jahren
Ursprung
Commit
f1807d93ad
2 geänderte Dateien mit 49 neuen und 28 gelöschten Zeilen
  1. 48 27
      program3/AVLCommands.cpp
  2. 1 1
      program3/AVLCommands.h

+ 48 - 27
program3/AVLCommands.cpp

@@ -99,34 +99,57 @@ int AVLCommands::height(std::shared_ptr<BSTNode> currentNode) {
   return currentNode->height_;
 }
 
-int AVLCommands::getBalance(std::shared_ptr<BSTNode> currentNode) {
-	if (root_ == nullptr) {
-		return 0;
+void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
+	if (currentNode == nullptr) {
+		return;
 	}
-
-	int leftHeight, rightHeight, balanceFactor;
-	if (currentNode != nullptr) {
-		if (currentNode->left_ != nullptr) {
-			leftHeight = height(currentNode->left_);
-		} else {
-			leftHeight = -1;
-		}
-
-		if (currentNode->right_ != nullptr) {
-			rightHeight = height(currentNode->right_);
-		} else {
-			rightHeight = -1;
+	std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
+	if (parent != nullptr) {
+		std::cout << "parent node key: " << parent->key_ << std::endl;
+		std::cout << "Rotation to be preformed:" << std::endl;
+
+		if (parent->balance_ > 1) {
+			if (parent->key_ < currentNode->left_->key_) {
+				std::cout << "Left-Left" << std::endl;
+				rightRotate(parent);
+				return;
+			} else if (parent->key_ > currentNode->left_->key_) {
+				std::cout << "Left-Right" << std::endl;
+		    parent->left_ =  leftRotate(parent->left_);
+		    rightRotate(parent);
+				return;
+			}
+		} else if (parent->balance_ < -1) {
+			if (parent->key_ < currentNode->right_->key_) {
+				std::cout << "Right-Right" << std::endl;
+				std::cout << "Parent key input: " << parent->key_ << std::endl;
+				rightRotate(parent);
+				return;
+			} else if (parent->key_ > currentNode->right_->key_) {
+				std::cout << "Right-Left" << std::endl;
+		    parent->right_ = rightRotate(parent->right_);
+		    leftRotate(parent);
+				return;
+			}
 		}
-
-		balanceFactor = leftHeight - rightHeight;
+		balance(parent);
 	}
-  return balanceFactor;
+	return;
 }
 
 std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
-	std::cout << "Right Rotate!" << std::endl;
+	if (currentNode == nullptr) {
+		std::cout << "Can't rotate on an empty node!" << std::endl;
+		return currentNode;
+	}
+	std::cout << "First, Right Rotate!" << std::endl;
+	std::cout << "Top: " << currentNode;
+	std::cout << ", Middle: " << currentNode->left_;
+	std::cout << ", Last: " << currentNode->left_->right_ << std::endl;
 	std::shared_ptr<BSTNode> x = currentNode->left_;
+	std::cout << "Does the current node have a left node?" << std::endl;
 	std::shared_ptr<BSTNode> T2 = x->right_;
+	std::cout << "Does the current node's left node have a right node?" << std::endl;
 	// Perform rotation
 	x->right_ = currentNode;
 	currentNode->left_ = T2;
@@ -204,9 +227,9 @@ void AVLCommands::Insert(int key) {
 
 	// update height and balance of the tree
 	height(root_);
+	balance(lastNode);
 
-	std::cout << "Rotation to be preformed:" << std::endl;
-
+	/*
 	// Check if unbalanced
 	// Left Left Case
   if (lastNode->balance_ > 1 && key < lastNode->left_->key_) {
@@ -233,7 +256,7 @@ void AVLCommands::Insert(int key) {
     lastNode->right_ = rightRotate(lastNode->right_);
     leftRotate(lastNode);
   }
-
+	*/
 }
 
 bool AVLCommands::Delete(int key) {
@@ -269,10 +292,7 @@ bool AVLCommands::Delete(int key) {
 	std::cout << "delete height update attempt success" << std::endl;
 
 	// Update balance factor of this ancestor's node
-	std::cout << "delete balance update attempt..." << std::endl;
-	int currentNodeBalance = getBalance(currentNode);
-	std::cout << "delete balance update attempt success!" << std::endl;
-
+	/*
 	// Check if unbalanced
 	// Left Left Case
   if (currentNodeBalance > 1 && getBalance(currentNode->left_) >= 0)
@@ -293,6 +313,7 @@ bool AVLCommands::Delete(int key) {
       currentNode->right_ = rightRotate(currentNode->right_);
       leftRotate(currentNode);
   }
+	*/
 	return false;
 }
 

+ 1 - 1
program3/AVLCommands.h

@@ -46,7 +46,7 @@ class AVLCommands {
  private:
   int max(int a, int b);
   int height(std::shared_ptr<BSTNode> currentNode);
-  int getBalance(std::shared_ptr<BSTNode> currentNode);
+  void balance(std::shared_ptr<BSTNode> currentNode);
   std::shared_ptr<BSTNode> rightRotate(std::shared_ptr<BSTNode>);
   std::shared_ptr<BSTNode> leftRotate(std::shared_ptr<BSTNode>);
   void DeleteLeaf(std::shared_ptr<BSTNode> currentNode);