Просмотр исходного кода

started working on deleteMin, untested, fixed JSON and added balance factor and height

Natalie Pueyo 8 лет назад
Родитель
Сommit
d6fc85608c
2 измененных файлов с 23 добавлено и 6 удалено
  1. 17 4
      program3/AVLCommands.cpp
  2. 6 2
      program3/test.cpp

+ 17 - 4
program3/AVLCommands.cpp

@@ -327,7 +327,18 @@ bool AVLCommands::Delete(int key) {
 
 
 int AVLCommands::DeleteMin() {
-	return DeleteMin(root_);
+	int deletedKey = DeleteMin(root_);
+	height(root_);
+
+	std::shared_ptr<BSTNode> smallestNode = root_;
+	std::shared_ptr<BSTNode> lastNode = nullptr;
+	while (smallestNode != nullptr) {
+		lastNode = smallestNode;
+		smallestNode = smallestNode->left_;
+	}
+	balance(lastNode);
+
+	return deletedKey;
 }
 
 
@@ -417,9 +428,10 @@ std::string AVLCommands::JSON() const {
 			auto v = nodes.front();
 			nodes.pop();
 			std::string key = std::to_string(v->key_);
-			//std::string height = std::to_string(v->height_);
-			//result[key]["height"] = height;
-			result[key]["left"] = v->left_->key_;
+			std::string balance = std::to_string(v->balance_);
+			result[key]["balance factor"] = balance;
+			std::string height = std::to_string(v->height_);
+			result[key]["height"] = height;
 			if (v->left_ != nullptr) {
 				result[key]["left"] = v->left_->key_;
 				nodes.push(v->left_);
@@ -436,5 +448,6 @@ std::string AVLCommands::JSON() const {
 		}
 	}
 	result["size"] = size_;
+	std::cout << result << std::endl;
 	return result.dump(2) + "\n";
 }

+ 6 - 2
program3/test.cpp

@@ -28,12 +28,16 @@ int main()
     //std::cout << std::endl;
     avl.Insert(3);
     //avl.printTree();
-    std::cout << std::endl;
+    //std::cout << std::endl;
     avl.Insert(2);
+    //avl.printTree();
+    //std::cout << std::endl;
+    //avl.Delete(4);
     avl.printTree();
     std::cout << std::endl;
-    avl.Delete(4);
+    avl.DeleteMin();
     avl.printTree();
     std::cout << std::endl;
+    avl.JSON();
     return 0;
 }