AVLCommands.cpp 9.2 KB

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