AVLCommands.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // balance and height functions based on code from
  2. // http://www.sanfoundry.com/cpp-program-implement-avl-trees/
  3. // Finding elements based on code from
  4. // http://www.cplusplus.com/forum/beginner/54835/
  5. #include <cassert>
  6. #include <iostream>
  7. #include <string>
  8. #include <queue>
  9. #include "json.hpp"
  10. #include "AVLCommands.h"
  11. BSTNode::BSTNode(int key) :
  12. key_(key),
  13. parent_(std::weak_ptr<BSTNode>()),
  14. left_(nullptr),
  15. right_(nullptr),
  16. height_(0),
  17. balance_(0) {} // add a height_
  18. BSTNode::BSTNode(int key, std::weak_ptr<BSTNode> parent) :
  19. key_(key),
  20. parent_(parent),
  21. left_(nullptr),
  22. right_(nullptr),
  23. height_(0),
  24. balance_(0) {}
  25. bool BSTNode::IsLeaf() const {
  26. return left_ == nullptr && right_ == nullptr;
  27. }
  28. bool BSTNode::HasLeftChild() const {
  29. return left_ != nullptr;
  30. }
  31. bool BSTNode::HasRightChild() const {
  32. return right_ != nullptr;
  33. }
  34. void BSTNode::DeleteChild(std::shared_ptr<BSTNode> v) {
  35. if (left_ == v) {
  36. left_ = nullptr;
  37. } else if (right_ == v) {
  38. right_ = nullptr;
  39. } else {
  40. std::cerr << "BSTNode::DeleteChild Error: non-child passed as argument\n";
  41. exit(EXIT_FAILURE);
  42. }
  43. }
  44. void BSTNode::ReplaceChild(std::shared_ptr<BSTNode> v, std::shared_ptr<BSTNode> u) {
  45. if (left_ == u || right_ == u) {
  46. std::cerr << "BSTNode::ReplaceChild Error: child passed as replacement\n";
  47. }
  48. if (left_ == v) {
  49. left_ = u;
  50. u->parent_ = v->parent_;
  51. } else if (right_ == v) {
  52. right_ = u;
  53. u->parent_ = v->parent_;
  54. } else {
  55. std::cerr << "BSTNode::ReplaceChild Error: non-child passed as argument\n";
  56. exit(EXIT_FAILURE);
  57. }
  58. }
  59. AVLCommands::AVLCommands() : root_(nullptr), size_(0) {} // add height here?
  60. int AVLCommands::max(int a, int b) {
  61. return (a > b)? a : b;
  62. }
  63. int AVLCommands::height(std::shared_ptr<BSTNode> currentNode) {
  64. if (root_ == nullptr) {
  65. return 0;
  66. }
  67. int leftHeight, rightHeight, heightMax;
  68. if (currentNode != nullptr) {
  69. if (currentNode->left_ != nullptr) {
  70. leftHeight = height(currentNode->left_);
  71. } else {
  72. leftHeight = -1;
  73. }
  74. if (currentNode->right_ != nullptr) {
  75. rightHeight = height(currentNode->right_);
  76. } else {
  77. rightHeight = -1;
  78. }
  79. heightMax = 1 + max(leftHeight, rightHeight);
  80. currentNode->height_ = heightMax;
  81. // update balance for each node
  82. currentNode->balance_ = leftHeight - rightHeight;
  83. }
  84. return currentNode->height_;
  85. }
  86. void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
  87. if (currentNode == nullptr) {
  88. return;
  89. }
  90. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  91. if (parent != nullptr) {
  92. std::cout << "parent node key: " << parent->key_ << std::endl;
  93. std::cout << "Rotation to be preformed:" << std::endl;
  94. if (parent->balance_ > 1) {
  95. if (parent->key_ < currentNode->left_->key_) {
  96. std::cout << "Left-Left" << std::endl;
  97. rightRotate(parent);
  98. return;
  99. } else if (parent->key_ > currentNode->left_->key_) {
  100. std::cout << "Left-Right" << std::endl;
  101. parent->left_ = leftRotate(parent->left_);
  102. rightRotate(parent);
  103. return;
  104. }
  105. } else if (parent->balance_ < -1) {
  106. if (parent->key_ < currentNode->right_->key_) {
  107. std::cout << "Right-Right" << std::endl;
  108. std::cout << "Parent key input: " << parent->key_ << std::endl;
  109. rightRotate(parent);
  110. return;
  111. } else if (parent->key_ > currentNode->right_->key_) {
  112. std::cout << "Right-Left" << std::endl;
  113. parent->right_ = rightRotate(parent->right_);
  114. leftRotate(parent);
  115. return;
  116. }
  117. }
  118. balance(parent);
  119. }
  120. return;
  121. }
  122. std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
  123. if (currentNode == nullptr) {
  124. std::cout << "Can't rotate on an empty node!" << std::endl;
  125. return currentNode;
  126. }
  127. std::cout << "First, Right Rotate!" << std::endl;
  128. std::cout << "Top: " << currentNode;
  129. std::cout << ", Middle: " << currentNode->left_;
  130. std::cout << ", Last: " << currentNode->left_->right_ << std::endl;
  131. std::shared_ptr<BSTNode> x = currentNode->left_;
  132. std::cout << "Does the current node have a left node?" << std::endl;
  133. std::shared_ptr<BSTNode> T2 = x->right_;
  134. std::cout << "Does the current node's left node have a right node?" << std::endl;
  135. // Perform rotation
  136. x->right_ = currentNode;
  137. currentNode->left_ = T2;
  138. // Update heights
  139. currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
  140. x->height_ = max(height(x->left_), height(x->right_))+1;
  141. // Return new root
  142. return x;
  143. return currentNode;
  144. }
  145. std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> currentNode){
  146. std::cout << "Left Rotate!" << std::endl;
  147. std::shared_ptr<BSTNode> y = currentNode->right_;
  148. std::shared_ptr<BSTNode> T2 = y->left_;
  149. // Perform rotation
  150. y->left_ = currentNode;
  151. currentNode->right_ = T2;
  152. // Update heights
  153. currentNode->height_ = max(height(currentNode->left_), height(currentNode->right_))+1;
  154. y->height_ = max(height(y->left_), height(y->right_))+1;
  155. // Return new root
  156. return y;
  157. }
  158. void AVLCommands::printTree(){
  159. printTree(root_);
  160. }
  161. void AVLCommands::printTree(std::shared_ptr<BSTNode> currentNode){
  162. if (root_ == nullptr) {
  163. std::cout << "tree is empty" << std::endl;
  164. return;
  165. } else {
  166. std::cout << "key: " << currentNode->key_ << std::endl;
  167. std::cout << "height: " << currentNode->height_ << std::endl;
  168. std::cout << "balance: " << currentNode->balance_ << std::endl;
  169. if (currentNode->left_ != nullptr) {
  170. std::cout << "left" << std::endl;
  171. printTree(currentNode->left_);
  172. }
  173. if (currentNode->right_ != nullptr) {
  174. std::cout << "right" << std::endl;
  175. printTree(currentNode->right_);
  176. }
  177. }
  178. }
  179. void AVLCommands::Insert(int key) {
  180. std::cout << "Inserting new key: " << key << std::endl;
  181. // BST insertion
  182. if (root_ == nullptr) {
  183. root_ = std::make_shared<BSTNode>(key);
  184. size_++;
  185. return;
  186. }
  187. std::shared_ptr<BSTNode> currentNode = root_, lastNode = nullptr;
  188. while (currentNode != nullptr) {
  189. lastNode = currentNode;
  190. currentNode = (key < currentNode->key_) ?
  191. currentNode->left_ : currentNode->right_;
  192. }
  193. if (key < lastNode->key_) {
  194. lastNode->left_ = std::make_shared<BSTNode>(key, lastNode);
  195. } else {
  196. lastNode->right_ = std::make_shared<BSTNode>(key, lastNode);
  197. }
  198. size_++;
  199. // update height and balance of the tree
  200. height(root_);
  201. balance(lastNode);
  202. /*
  203. // Check if unbalanced
  204. // Left Left Case
  205. if (lastNode->balance_ > 1 && key < lastNode->left_->key_) {
  206. std::cout << "Left-Left" << std::endl;
  207. rightRotate(lastNode);
  208. }
  209. // Right Right Case
  210. if (lastNode->balance_ < -1 && key > lastNode->right_->key_) {
  211. std::cout << "Right-Right" << std::endl;
  212. leftRotate(lastNode);
  213. }
  214. // Left Right Case
  215. if (lastNode->balance_ > 1 && key > lastNode->left_->key_) {
  216. std::cout << "Left-Right" << std::endl;
  217. lastNode->left_ = leftRotate(lastNode->left_);
  218. rightRotate(lastNode);
  219. }
  220. // Right Left Case
  221. if (lastNode->balance_ < -1 && key < lastNode->right_->key_) {
  222. std::cout << "Right-Left" << std::endl;
  223. lastNode->right_ = rightRotate(lastNode->right_);
  224. leftRotate(lastNode);
  225. }
  226. */
  227. }
  228. bool AVLCommands::Delete(int key) {
  229. // base BST delete
  230. std::shared_ptr<BSTNode> currentNode = root_;
  231. while (currentNode != nullptr) {
  232. if (currentNode->key_ == key) {
  233. if (currentNode->IsLeaf()) {
  234. DeleteLeaf(currentNode);
  235. } else if (currentNode->left_ == nullptr) {
  236. assert(currentNode->right_ != nullptr);
  237. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  238. parent->ReplaceChild(currentNode, currentNode->right_);
  239. size_--; assert(size_ >= 0);
  240. } else if (currentNode->right_ == nullptr) {
  241. assert(currentNode->left_ != nullptr);
  242. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  243. parent->ReplaceChild(currentNode, currentNode->left_);
  244. size_--; assert(size_ >= 0);
  245. } else {
  246. currentNode->key_ = DeleteMin(currentNode);
  247. }
  248. }
  249. currentNode = (key < currentNode->key_) ?
  250. currentNode->left_ : currentNode->right_;
  251. }
  252. std::cout << "current node value: " << currentNode->key_ << std::endl;
  253. // update height of current node
  254. std::cout << "delete height update attempt..." << std::endl;
  255. currentNode->height_ = 1 + max(height(currentNode->left_), height(currentNode->right_));
  256. std::cout << "New height: " << currentNode->height_ << std::endl;
  257. std::cout << "delete height update attempt success" << std::endl;
  258. // Update balance factor of this ancestor's node
  259. /*
  260. // Check if unbalanced
  261. // Left Left Case
  262. if (currentNodeBalance > 1 && getBalance(currentNode->left_) >= 0)
  263. rightRotate(currentNode);
  264. // Right Right Case
  265. if (currentNodeBalance < -1 && getBalance(currentNode->right_) <= 0)
  266. leftRotate(currentNode);
  267. // Left Right Case
  268. if (currentNodeBalance > 1 && getBalance(currentNode->left_) < 0) {
  269. currentNode->left_ = leftRotate(currentNode->left_);
  270. rightRotate(currentNode);
  271. }
  272. // Right Left Case
  273. if (currentNodeBalance < -1 && getBalance(currentNode->right_) > 0) {
  274. currentNode->right_ = rightRotate(currentNode->right_);
  275. leftRotate(currentNode);
  276. }
  277. */
  278. return false;
  279. }
  280. int AVLCommands::DeleteMin() {
  281. return DeleteMin(root_);
  282. }
  283. void AVLCommands::DeleteLeaf(std::shared_ptr<BSTNode> currentNode) {
  284. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  285. if (parent == nullptr) {
  286. // Delete root
  287. root_ = nullptr;
  288. size_--; assert(size_ == 0);
  289. } else {
  290. if (parent->right_ == currentNode) {
  291. parent->right_ = nullptr;
  292. } else if (parent->left_ == currentNode) {
  293. parent->left_ = nullptr;
  294. } else {
  295. std::cerr << "BST::DeleteLeaf Error: inconsistent state\n";
  296. }
  297. size_--; assert(size_ >= 0);
  298. }
  299. }
  300. int AVLCommands::DeleteMin(std::shared_ptr<BSTNode> currentNode) {
  301. std::shared_ptr<BSTNode> lastNode = nullptr;
  302. while (currentNode != nullptr) {
  303. lastNode = currentNode;
  304. currentNode = currentNode->left_;
  305. }
  306. int result = lastNode->key_;
  307. std::shared_ptr<BSTNode> parent = lastNode->parent_.lock();
  308. if (parent == nullptr) {
  309. // lastNode is root
  310. if (lastNode->right_ != nullptr) {
  311. root_ = lastNode->right_;
  312. lastNode->right_->parent_.reset();
  313. } else {
  314. root_ = nullptr;
  315. }
  316. } else {
  317. // lastNode under the root
  318. if (lastNode->right_ != nullptr) {
  319. parent->left_ = lastNode->right_;
  320. lastNode->right_->parent_ = parent;
  321. } else {
  322. parent->left_ = nullptr;
  323. }
  324. }
  325. size_--; assert(size_ >= 0);
  326. return result;
  327. }
  328. size_t AVLCommands::size() const {
  329. return size_;
  330. }
  331. bool AVLCommands::empty() const {
  332. return size_ == 0;
  333. }
  334. bool AVLCommands::Find(int key) const {
  335. std::shared_ptr<BSTNode> currentNode = root_;
  336. while (currentNode != nullptr) {
  337. if (currentNode->key_ == key) {
  338. return true;
  339. }
  340. currentNode = (key < currentNode->key_) ?
  341. currentNode->left_ : currentNode->right_;
  342. }
  343. return false;
  344. }
  345. std::string AVLCommands::JSON() const {
  346. nlohmann::json result;
  347. std::queue< std::shared_ptr<BSTNode> > nodes;
  348. if (root_ != nullptr) {
  349. result["root"] = root_->key_;
  350. nodes.push(root_);
  351. while (!nodes.empty()) {
  352. auto v = nodes.front();
  353. nodes.pop();
  354. std::string key = std::to_string(v->key_);
  355. //std::string height = std::to_string(v->height_);
  356. //result[key]["height"] = height;
  357. result[key]["left"] = v->left_->key_;
  358. if (v->left_ != nullptr) {
  359. result[key]["left"] = v->left_->key_;
  360. nodes.push(v->left_);
  361. }
  362. if (v->right_ != nullptr) {
  363. result[key]["right"] = v->right_->key_;
  364. nodes.push(v->right_);
  365. }
  366. if (v->parent_.lock() != nullptr) {
  367. result[key]["parent"] = v->parent_.lock()->key_;
  368. } else {
  369. result[key]["root"] = true;
  370. }
  371. }
  372. }
  373. result["size"] = size_;
  374. return result.dump(2) + "\n";
  375. }