AVLCommands.cpp 8.3 KB

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