|
@@ -1,14 +1,10 @@
|
|
|
-// written by Rob Gysel
|
|
|
|
|
-
|
|
|
|
|
-#include "AVLCommands.h"
|
|
|
|
|
-
|
|
|
|
|
#include <cassert>
|
|
#include <cassert>
|
|
|
#include <iostream>
|
|
#include <iostream>
|
|
|
#include <string>
|
|
#include <string>
|
|
|
#include <queue>
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "json.hpp"
|
|
#include "json.hpp"
|
|
|
-
|
|
|
|
|
|
|
+#include "AVLCommands.h"
|
|
|
|
|
|
|
|
BSTNode::BSTNode(int key) :
|
|
BSTNode::BSTNode(int key) :
|
|
|
key_(key),
|
|
key_(key),
|
|
@@ -65,49 +61,6 @@ void BSTNode::ReplaceChild(std::shared_ptr<BSTNode> v, std::shared_ptr<BSTNode>
|
|
|
|
|
|
|
|
AVLCommands::AVLCommands() : root_(nullptr), size_(0) {} // add height here?
|
|
AVLCommands::AVLCommands() : root_(nullptr), size_(0) {} // add height here?
|
|
|
|
|
|
|
|
-// searches through the node keys to get the parent of specified key
|
|
|
|
|
-int AVLCommands::getParent(int key) {
|
|
|
|
|
- int parent = 0;
|
|
|
|
|
- if (root_ == nullptr) {
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
- std::shared_ptr<BSTNode> currentNode = root_, lastNode = nullptr;
|
|
|
|
|
- while (currentNode->key_ != key) {
|
|
|
|
|
- lastNode = currentNode;
|
|
|
|
|
- currentNode = (key < currentNode->key_) ?
|
|
|
|
|
- currentNode->left_ : currentNode->right_;
|
|
|
|
|
- }
|
|
|
|
|
- parent = lastNode -> key_;
|
|
|
|
|
- return parent;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-int AVLCommands::getLeft(int key) {
|
|
|
|
|
- if (root_ == nullptr) {
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
- std::shared_ptr<BSTNode> currentNode = root_;
|
|
|
|
|
- while (currentNode->key_ != key) {
|
|
|
|
|
- currentNode = (key < currentNode->key_) ?
|
|
|
|
|
- currentNode->left_ : currentNode->right_;
|
|
|
|
|
- }
|
|
|
|
|
- currentNode = currentNode->left_;
|
|
|
|
|
- return currentNode->key_;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-int AVLCommands::getRight(int key) {
|
|
|
|
|
- if (root_ == nullptr) {
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
- std::shared_ptr<BSTNode> currentNode = root_;
|
|
|
|
|
- while (currentNode->key_ != key) {
|
|
|
|
|
- currentNode = (key < currentNode->key_) ?
|
|
|
|
|
- currentNode->left_ : currentNode->right_;
|
|
|
|
|
- }
|
|
|
|
|
- currentNode = currentNode->right_;
|
|
|
|
|
- return currentNode->key_;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
int AVLCommands::max(int a, int b) {
|
|
int AVLCommands::max(int a, int b) {
|
|
|
return (a > b)? a : b;
|
|
return (a > b)? a : b;
|
|
|
}
|
|
}
|
|
@@ -177,10 +130,16 @@ void AVLCommands::Insert(int key) {
|
|
|
size_++;
|
|
size_++;
|
|
|
|
|
|
|
|
// Update hight of this ancestor's node... need to check if this should be currNode or lastNode
|
|
// Update hight of this ancestor's node... need to check if this should be currNode or lastNode
|
|
|
|
|
+ std::cout << "insert height update attempt..." << std::endl;
|
|
|
lastNode->height_ = 1 + max(height(lastNode->left_), height(lastNode->right_));
|
|
lastNode->height_ = 1 + max(height(lastNode->left_), height(lastNode->right_));
|
|
|
|
|
+ std::cout << "New height: " << lastNode->height_ << std::endl;
|
|
|
|
|
+ std::cout << "insert height update attempt success!" << std::endl;
|
|
|
|
|
|
|
|
// Update balance factor of this ancestor's node
|
|
// Update balance factor of this ancestor's node
|
|
|
|
|
+ std::cout << "insert balance update attempt..." << std::endl;
|
|
|
int lastNodeBalance = getBalance(lastNode);
|
|
int lastNodeBalance = getBalance(lastNode);
|
|
|
|
|
+ std::cout << "New balance: " << lastNodeBalance << std::endl;
|
|
|
|
|
+ std::cout << "insert balance update attempt success!" << std::endl;
|
|
|
|
|
|
|
|
// Check if unbalanced
|
|
// Check if unbalanced
|
|
|
// Left Left Case
|
|
// Left Left Case
|
|
@@ -229,11 +188,17 @@ bool AVLCommands::Delete(int key) {
|
|
|
currentNode->left_ : currentNode->right_;
|
|
currentNode->left_ : currentNode->right_;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ std::cout << "current node value: " << currentNode->key_ << std::endl;
|
|
|
// update height of current node
|
|
// update height of current node
|
|
|
|
|
+ std::cout << "delete height update attempt..." << std::endl;
|
|
|
currentNode->height_ = 1 + max(height(currentNode->left_), height(currentNode->right_));
|
|
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;
|
|
|
|
|
|
|
|
// Update balance factor of this ancestor's node
|
|
// Update balance factor of this ancestor's node
|
|
|
|
|
+ std::cout << "delete balance update attempt..." << std::endl;
|
|
|
int currentNodeBalance = getBalance(currentNode);
|
|
int currentNodeBalance = getBalance(currentNode);
|
|
|
|
|
+ std::cout << "delete balance update attempt success!" << std::endl;
|
|
|
|
|
|
|
|
// Check if unbalanced
|
|
// Check if unbalanced
|
|
|
// Left Left Case
|
|
// Left Left Case
|
|
@@ -258,6 +223,7 @@ bool AVLCommands::Delete(int key) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
int AVLCommands::DeleteMin() {
|
|
int AVLCommands::DeleteMin() {
|
|
|
return DeleteMin(root_);
|
|
return DeleteMin(root_);
|
|
|
}
|
|
}
|
|
@@ -340,6 +306,9 @@ std::string AVLCommands::JSON() const {
|
|
|
auto v = nodes.front();
|
|
auto v = nodes.front();
|
|
|
nodes.pop();
|
|
nodes.pop();
|
|
|
std::string key = std::to_string(v->key_);
|
|
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_;
|
|
|
if (v->left_ != nullptr) {
|
|
if (v->left_ != nullptr) {
|
|
|
result[key]["left"] = v->left_->key_;
|
|
result[key]["left"] = v->left_->key_;
|
|
|
nodes.push(v->left_);
|
|
nodes.push(v->left_);
|