Преглед изворни кода

weird issues with basic BST Delete forming incorrect BST tree?

Natalie Pueyo пре 8 година
родитељ
комит
c2edf694f7
2 измењених фајлова са 21 додато и 52 уклоњено
  1. 3 35
      program3/AVLCommands.cpp
  2. 18 17
      program3/test.cpp

+ 3 - 35
program3/AVLCommands.cpp

@@ -264,38 +264,10 @@ void AVLCommands::Insert(int key) {
 	// update height and balance of the tree
 	height(root_);
 	balance(lastNode);
-
-	/*
-	// Check if unbalanced
-	// Left Left Case
-  if (lastNode->balance_ > 1 && key < lastNode->left_->key_) {
-		std::cout << "Left-Left" << std::endl;
-		rightRotate(lastNode);
-	}
-
-  // Right Right Case
-  if (lastNode->balance_ < -1 && key > lastNode->right_->key_) {
-		std::cout << "Right-Right" << std::endl;
-    leftRotate(lastNode);
-	}
-
-  // Left Right Case
-  if (lastNode->balance_ > 1 && key > lastNode->left_->key_) {
-		std::cout << "Left-Right" << std::endl;
-    lastNode->left_ =  leftRotate(lastNode->left_);
-    rightRotate(lastNode);
-  }
-
-  // Right Left Case
-  if (lastNode->balance_ < -1 && key < lastNode->right_->key_) {
-		std::cout << "Right-Left" << std::endl;
-    lastNode->right_ = rightRotate(lastNode->right_);
-    leftRotate(lastNode);
-  }
-	*/
 }
 
 bool AVLCommands::Delete(int key) {
+	std::cout << "Deleting key: " << key << std::endl;
 	// base BST delete
 	std::shared_ptr<BSTNode> currentNode = root_;
 	while (currentNode != nullptr) {
@@ -320,12 +292,8 @@ bool AVLCommands::Delete(int key) {
 			currentNode->left_ : currentNode->right_;
 	}
 
-	std::cout << "current node value: " << currentNode->key_ << std::endl;
-	// update height of current node
-	std::cout << "delete height update attempt..." << std::endl;
-	currentNode->height_ = 1 + max(height(currentNode->left_), height(currentNode->right_));
-	std::cout << "New height: " << currentNode->height_ << std::endl;
-	std::cout << "delete height update attempt success" << std::endl;
+	height(root_);
+	balance(currentNode);
 
 	// Update balance factor of this ancestor's node
 	/*

+ 18 - 17
program3/test.cpp

@@ -6,33 +6,34 @@ int main()
 {
     AVLCommands avl;
     avl.Insert(9);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(8);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(7);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(6);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(1);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(5);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(4);
-    avl.printTree();
-    std::cout << std::endl;
+    //avl.printTree();
+    //std::cout << std::endl;
     avl.Insert(3);
-    avl.printTree();
+    //avl.printTree();
     std::cout << std::endl;
     avl.Insert(2);
-    //avl.Insert(50);
     avl.printTree();
     std::cout << std::endl;
-    //avl.Delete(15);
+    avl.Delete(4);
+    avl.printTree();
+    std::cout << std::endl;
     return 0;
 }