AVLCommands.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <cassert>
  2. #include <iostream>
  3. #include <string>
  4. #include <queue>
  5. #include "json.hpp"
  6. #include "AVLCommands.h"
  7. BSTNode::BSTNode(int key) :
  8. key_(key),
  9. parent_(std::weak_ptr<BSTNode>()),
  10. left_(nullptr),
  11. right_(nullptr),
  12. height_(1) {} // add a height_
  13. BSTNode::BSTNode(int key, std::weak_ptr<BSTNode> parent) :
  14. key_(key),
  15. parent_(parent),
  16. left_(nullptr),
  17. right_(nullptr),
  18. height_(1) {}
  19. bool BSTNode::IsLeaf() const {
  20. return left_ == nullptr && right_ == nullptr;
  21. }
  22. bool BSTNode::HasLeftChild() const {
  23. return left_ != nullptr;
  24. }
  25. bool BSTNode::HasRightChild() const {
  26. return right_ != nullptr;
  27. }
  28. void BSTNode::DeleteChild(std::shared_ptr<BSTNode> v) {
  29. if (left_ == v) {
  30. left_ = nullptr;
  31. } else if (right_ == v) {
  32. right_ = nullptr;
  33. } else {
  34. std::cerr << "BSTNode::DeleteChild Error: non-child passed as argument\n";
  35. exit(EXIT_FAILURE);
  36. }
  37. }
  38. void BSTNode::ReplaceChild(std::shared_ptr<BSTNode> v, std::shared_ptr<BSTNode> u) {
  39. if (left_ == u || right_ == u) {
  40. std::cerr << "BSTNode::ReplaceChild Error: child passed as replacement\n";
  41. }
  42. if (left_ == v) {
  43. left_ = u;
  44. u->parent_ = v->parent_;
  45. } else if (right_ == v) {
  46. right_ = u;
  47. u->parent_ = v->parent_;
  48. } else {
  49. std::cerr << "BSTNode::ReplaceChild Error: non-child passed as argument\n";
  50. exit(EXIT_FAILURE);
  51. }
  52. }
  53. AVLCommands::AVLCommands() : root_(nullptr), size_(0) {} // add height here?
  54. int AVLCommands::max(int a, int b) {
  55. return (a > b)? a : b;
  56. }
  57. int AVLCommands::height(std::shared_ptr<BSTNode> currentNode) {
  58. if (currentNode == nullptr)
  59. return 0;
  60. return currentNode->height_;
  61. }
  62. int AVLCommands::getBalance(std::shared_ptr<BSTNode> currentNode) {
  63. if (currentNode == nullptr)
  64. return 0;
  65. return height(currentNode->left_) - height(currentNode->right_);
  66. }
  67. std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
  68. std::shared_ptr<BSTNode> x = currentNode->left_;
  69. std::shared_ptr<BSTNode> T2 = x->right_;
  70. // Perform rotation
  71. x->right_ = currentNode;
  72. currentNode->left_ = T2;
  73. // Update heights
  74. currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
  75. x->height_ = max(height(x->left_), height(x->right_))+1;
  76. // Return new root
  77. return x;
  78. }
  79. std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> currentNode){
  80. std::shared_ptr<BSTNode> y = currentNode->right_;
  81. std::shared_ptr<BSTNode> T2 = y->left_;
  82. // Perform rotation
  83. y->left_ = currentNode;
  84. currentNode->right_ = T2;
  85. // Update heights
  86. currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
  87. y->height_ = max(height(y->left_), height(y->right_))+1;
  88. // Return new root
  89. return y;
  90. }
  91. void AVLCommands::Insert(int key) {
  92. // BST insertion
  93. if (root_ == nullptr) {
  94. root_ = std::make_shared<BSTNode>(key);
  95. size_++;
  96. return;
  97. }
  98. std::shared_ptr<BSTNode> currentNode = root_, lastNode = nullptr;
  99. while (currentNode != nullptr) {
  100. lastNode = currentNode;
  101. currentNode = (key < currentNode->key_) ?
  102. currentNode->left_ : currentNode->right_;
  103. }
  104. if (key < lastNode->key_) {
  105. lastNode->left_ = std::make_shared<BSTNode>(key, lastNode);
  106. } else {
  107. lastNode->right_ = std::make_shared<BSTNode>(key, lastNode);
  108. }
  109. size_++;
  110. // Update hight of this ancestor's node... need to check if this should be currNode or lastNode
  111. std::cout << "insert height update attempt..." << std::endl;
  112. lastNode->height_ = 1 + max(height(lastNode->left_), height(lastNode->right_));
  113. std::cout << "New height: " << lastNode->height_ << std::endl;
  114. std::cout << "insert height update attempt success!" << std::endl;
  115. // Update balance factor of this ancestor's node
  116. std::cout << "insert balance update attempt..." << std::endl;
  117. int lastNodeBalance = getBalance(lastNode);
  118. std::cout << "New balance: " << lastNodeBalance << std::endl;
  119. std::cout << "insert balance update attempt success!" << std::endl;
  120. // Check if unbalanced
  121. // Left Left Case
  122. if (lastNodeBalance > 1 && key < lastNode->left_->key_)
  123. rightRotate(lastNode);
  124. // Right Right Case
  125. if (lastNodeBalance < -1 && key > lastNode->right_->key_)
  126. leftRotate(lastNode);
  127. // Left Right Case
  128. if (lastNodeBalance > 1 && key > lastNode->left_->key_) {
  129. lastNode->left_ = leftRotate(lastNode->left_);
  130. rightRotate(lastNode);
  131. }
  132. // Right Left Case
  133. if (lastNodeBalance < -1 && key < lastNode->right_->key_) {
  134. lastNode->right_ = rightRotate(lastNode->right_);
  135. leftRotate(lastNode);
  136. }
  137. }
  138. bool AVLCommands::Delete(int key) {
  139. // base BST delete
  140. std::shared_ptr<BSTNode> currentNode = root_;
  141. while (currentNode != nullptr) {
  142. if (currentNode->key_ == key) {
  143. if (currentNode->IsLeaf()) {
  144. DeleteLeaf(currentNode);
  145. } else if (currentNode->left_ == nullptr) {
  146. assert(currentNode->right_ != nullptr);
  147. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  148. parent->ReplaceChild(currentNode, currentNode->right_);
  149. size_--; assert(size_ >= 0);
  150. } else if (currentNode->right_ == nullptr) {
  151. assert(currentNode->left_ != nullptr);
  152. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  153. parent->ReplaceChild(currentNode, currentNode->left_);
  154. size_--; assert(size_ >= 0);
  155. } else {
  156. currentNode->key_ = DeleteMin(currentNode);
  157. }
  158. }
  159. currentNode = (key < currentNode->key_) ?
  160. currentNode->left_ : currentNode->right_;
  161. }
  162. std::cout << "current node value: " << currentNode->key_ << std::endl;
  163. // update height of current node
  164. std::cout << "delete height update attempt..." << std::endl;
  165. currentNode->height_ = 1 + max(height(currentNode->left_), height(currentNode->right_));
  166. std::cout << "New height: " << currentNode->height_ << std::endl;
  167. std::cout << "delete height update attempt success" << std::endl;
  168. // Update balance factor of this ancestor's node
  169. std::cout << "delete balance update attempt..." << std::endl;
  170. int currentNodeBalance = getBalance(currentNode);
  171. std::cout << "delete balance update attempt success!" << std::endl;
  172. // Check if unbalanced
  173. // Left Left Case
  174. if (currentNodeBalance > 1 && getBalance(currentNode->left_) >= 0)
  175. rightRotate(currentNode);
  176. // Right Right Case
  177. if (currentNodeBalance < -1 && getBalance(currentNode->right_) <= 0)
  178. leftRotate(currentNode);
  179. // Left Right Case
  180. if (currentNodeBalance > 1 && getBalance(currentNode->left_) < 0) {
  181. currentNode->left_ = leftRotate(currentNode->left_);
  182. rightRotate(currentNode);
  183. }
  184. // Right Left Case
  185. if (currentNodeBalance < -1 && getBalance(currentNode->right_) > 0) {
  186. currentNode->right_ = rightRotate(currentNode->right_);
  187. leftRotate(currentNode);
  188. }
  189. return false;
  190. }
  191. int AVLCommands::DeleteMin() {
  192. return DeleteMin(root_);
  193. }
  194. void AVLCommands::DeleteLeaf(std::shared_ptr<BSTNode> currentNode) {
  195. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  196. if (parent == nullptr) {
  197. // Delete root
  198. root_ = nullptr;
  199. size_--; assert(size_ == 0);
  200. } else {
  201. if (parent->right_ == currentNode) {
  202. parent->right_ = nullptr;
  203. } else if (parent->left_ == currentNode) {
  204. parent->left_ = nullptr;
  205. } else {
  206. std::cerr << "BST::DeleteLeaf Error: inconsistent state\n";
  207. }
  208. size_--; assert(size_ >= 0);
  209. }
  210. }
  211. int AVLCommands::DeleteMin(std::shared_ptr<BSTNode> currentNode) {
  212. std::shared_ptr<BSTNode> lastNode = nullptr;
  213. while (currentNode != nullptr) {
  214. lastNode = currentNode;
  215. currentNode = currentNode->left_;
  216. }
  217. int result = lastNode->key_;
  218. std::shared_ptr<BSTNode> parent = lastNode->parent_.lock();
  219. if (parent == nullptr) {
  220. // lastNode is root
  221. if (lastNode->right_ != nullptr) {
  222. root_ = lastNode->right_;
  223. lastNode->right_->parent_.reset();
  224. } else {
  225. root_ = nullptr;
  226. }
  227. } else {
  228. // lastNode under the root
  229. if (lastNode->right_ != nullptr) {
  230. parent->left_ = lastNode->right_;
  231. lastNode->right_->parent_ = parent;
  232. } else {
  233. parent->left_ = nullptr;
  234. }
  235. }
  236. size_--; assert(size_ >= 0);
  237. return result;
  238. }
  239. size_t AVLCommands::size() const {
  240. return size_;
  241. }
  242. bool AVLCommands::empty() const {
  243. return size_ == 0;
  244. }
  245. bool AVLCommands::Find(int key) const {
  246. std::shared_ptr<BSTNode> currentNode = root_;
  247. while (currentNode != nullptr) {
  248. if (currentNode->key_ == key) {
  249. return true;
  250. }
  251. currentNode = (key < currentNode->key_) ?
  252. currentNode->left_ : currentNode->right_;
  253. }
  254. return false;
  255. }
  256. std::string AVLCommands::JSON() const {
  257. nlohmann::json result;
  258. std::queue< std::shared_ptr<BSTNode> > nodes;
  259. if (root_ != nullptr) {
  260. result["root"] = root_->key_;
  261. nodes.push(root_);
  262. while (!nodes.empty()) {
  263. auto v = nodes.front();
  264. nodes.pop();
  265. std::string key = std::to_string(v->key_);
  266. std::string height = std::to_string(v->height_);
  267. result[key]["height"] = height;
  268. result[key]["left"] = v->left_->key_;
  269. if (v->left_ != nullptr) {
  270. result[key]["left"] = v->left_->key_;
  271. nodes.push(v->left_);
  272. }
  273. if (v->right_ != nullptr) {
  274. result[key]["right"] = v->right_->key_;
  275. nodes.push(v->right_);
  276. }
  277. if (v->parent_.lock() != nullptr) {
  278. result[key]["parent"] = v->parent_.lock()->key_;
  279. } else {
  280. result[key]["root"] = true;
  281. }
  282. }
  283. }
  284. result["size"] = size_;
  285. return result.dump(2) + "\n";
  286. }