Selaa lähdekoodia

Getting segmentation faults at rotations because the code doesn't take into account relationships to the parent nodes. Especially hard to deal with when dealing with the root

Natalie Pueyo 8 vuotta sitten
vanhempi
commit
eb49706b88
4 muutettua tiedostoa jossa 75 lisäystä ja 21 poistoa
  1. 41 20
      program3/AVLCommands.cpp
  2. 1 0
      program3/BST.cpp
  3. 4 1
      program3/Makefile
  4. 29 0
      program3/bstTest.cpp

+ 41 - 20
program3/AVLCommands.cpp

@@ -73,6 +73,8 @@ int AVLCommands::max(int a, int b) {
 }
 
 int AVLCommands::height(std::shared_ptr<BSTNode> currentNode) {
+	//std::cout << "Updating the height..." << std::endl;
+	//std::cout << "height: " << currentNode->height_ << std::endl;
 	if (root_ == nullptr) {
 		return 0;
 	}
@@ -105,27 +107,28 @@ void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
 	}
 	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;
+		//std::cout << "parent node key: " << parent->key_ << std::endl;
+		std::cout << "balanceFactor at balance: " << parent->balance_ <<std::endl;
+		//std::cout << "Rotation to be preformed:" << std::endl;
 
 		if (parent->balance_ > 1) {
-			if (parent->key_ < currentNode->left_->key_) {
+			if (parent->key_ < parent->left_->key_) {
 				std::cout << "Left-Left" << std::endl;
 				rightRotate(parent);
 				return;
-			} else if (parent->key_ > currentNode->left_->key_) {
+			} else if (parent->key_ > parent->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_) {
+			if (parent->key_ < parent->right_->key_) {
 				std::cout << "Right-Right" << std::endl;
-				std::cout << "Parent key input: " << parent->key_ << std::endl;
-				rightRotate(parent);
+				std::cout << "Current Node key input: " << parent->key_ << std::endl;
+				leftRotate(parent);
 				return;
-			} else if (parent->key_ > currentNode->right_->key_) {
+			} else if (parent->key_ > parent->right_->key_) {
 				std::cout << "Right-Left" << std::endl;
 		    parent->right_ = rightRotate(parent->right_);
 		    leftRotate(parent);
@@ -138,14 +141,15 @@ 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;
 	}
 	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::cout << "Top: " << currentNode->key_;
+	std::cout << ", Middle: " << currentNode->left_->key_;
+	std::cout << ", Last: " << currentNode->left_->right_->key_ << 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_;
@@ -160,23 +164,37 @@ std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> curre
 
 	// Return new root
 	return x;
-
-	return currentNode;
 }
 
+// Takes the root of the tree to be left rotated and rotates it to provide new root
 std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> currentNode){
+	if (currentNode == nullptr) {
+		std::cout << "Can't rotate on an empty node!" << std::endl;
+		return currentNode;
+	}
 	std::cout << "Left Rotate!" << std::endl;
+
 	std::shared_ptr<BSTNode> y = currentNode->right_;
-  std::shared_ptr<BSTNode> T2 = y->left_;
+	y->left_ = currentNode;
 
-  // Perform rotation
-  y->left_ = currentNode;
-  currentNode->right_ = T2;
+	std::shared_ptr<BSTNode> T2 = y->left_;
+	currentNode->right_ = T2;
 
-  //  Update heights
-  currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
-  y->height_ = max(height(y->left_), height(y->right_))+1;
 
+	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;
+	std::cout << "Old subtree root: " << currentNode->key_ << std::endl;
+	//std::cout << "Old left: " << currentNode->left_->key_ << std::endl;
+	std::cout << "Old right: " << currentNode->right_->key_ << std::endl;
+
+	std::cout << "New root: " << root_->key_ << std::endl;
+
+  //  Update heights
+  //currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
+  //y->height_ = max(height(y->left_), height(y->right_))+1;
+	//height(y);
+	printTree();
   // Return new root
   return y;
 }
@@ -193,6 +211,9 @@ void AVLCommands::printTree(std::shared_ptr<BSTNode> currentNode){
 		std::cout << "key: " << currentNode->key_ << std::endl;
 		std::cout << "height: " << currentNode->height_ << std::endl;
 		std::cout << "balance: " << currentNode->balance_ << std::endl;
+		if (currentNode->parent_.lock() != nullptr) {
+			std::cout << "parent: " << currentNode->parent_.lock()->key_ << std::endl;
+		}
 		if (currentNode->left_ != nullptr) {
 			std::cout << "left" << std::endl;
 			printTree(currentNode->left_);

+ 1 - 0
program3/BST.cpp

@@ -253,5 +253,6 @@ std::string BST::JSON() const {
 		}
 	}
 	result["size"] = size_;
+	std::cout << result << std::endl;
 	return result.dump(2) + "\n";
 }

+ 4 - 1
program3/Makefile

@@ -5,7 +5,7 @@ OPT=-O3 -std=c++14
 JSON=json.hpp
 
 .PHONY: all
-all: AVLSanityCheck Test
+all: AVLSanityCheck Test bstTest
 #all: BSTSanityCheck CreateData AVLSanityCheck Test
 
 #CreateData: CreateData.cxx json.hpp
@@ -20,6 +20,9 @@ AVLSanityCheck: AVLSanityCheck.cxx AVLCommands.o
 Test: test.cpp AVLCommands.o
 	$(CC) $(DEV) test.cpp AVLCommands.o -o Test.exe
 
+bstTest: bstTest.cpp BST.o
+	$(CC) $(DEV) bstTest.cpp BST.o -o bstTest.exe
+
 BST.o: BST.cpp BST.h
 	$(CC) $(DEV) -c BST.cpp
 

+ 29 - 0
program3/bstTest.cpp

@@ -0,0 +1,29 @@
+// Driver program to test above functions
+#include "BST.h"
+
+int main()
+{
+    BST avl;
+    avl.Insert(2);
+    //avl.printTree();
+    avl.Insert(3);
+    //avl.printTree();
+    avl.Insert(4);
+    //avl.printTree();
+    avl.Insert(5);
+    //avl.printTree();
+    avl.Insert(1);
+    //avl.printTree();
+    avl.Insert(6);
+    //avl.printTree();
+    avl.Insert(7);
+    //avl.printTree();
+    avl.Insert(8);
+    //avl.printTree();
+    avl.Insert(9);
+    //avl.Insert(50);
+    //avl.printTree();
+    avl.Delete(6);
+    avl.JSON();
+    return 0;
+}