AVLCommands.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. //std::cout << "Updating the height..." << std::endl;
  65. //std::cout << "height: " << currentNode->height_ << std::endl;
  66. if (root_ == nullptr) {
  67. return 0;
  68. }
  69. int leftHeight, rightHeight, heightMax;
  70. if (currentNode != nullptr) {
  71. if (currentNode->left_ != nullptr) {
  72. leftHeight = height(currentNode->left_);
  73. } else {
  74. leftHeight = -1;
  75. }
  76. if (currentNode->right_ != nullptr) {
  77. rightHeight = height(currentNode->right_);
  78. } else {
  79. rightHeight = -1;
  80. }
  81. heightMax = 1 + max(leftHeight, rightHeight);
  82. currentNode->height_ = heightMax;
  83. // update balance for each node
  84. currentNode->balance_ = leftHeight - rightHeight;
  85. }
  86. return currentNode->height_;
  87. }
  88. void AVLCommands::balance(std::shared_ptr<BSTNode> currentNode) {
  89. if (currentNode == nullptr) {
  90. return;
  91. }
  92. std::cout << "balance called at key: " << currentNode->key_ << std::endl;
  93. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  94. if (currentNode != nullptr) {
  95. //std::cout << "currentNode node key: " << currentNode->key_ << std::endl;
  96. std::cout << "balance: " << currentNode->balance_ <<std::endl;
  97. //std::cout << "Rotation to be preformed:" << std::endl;
  98. if (currentNode->balance_ > 1) {
  99. if (currentNode->left_->balance_ >= 0) {
  100. std::cout << "Left-Left" << std::endl;
  101. rightRotate(currentNode);
  102. return;
  103. } else if (currentNode->left_->balance_ < 0) {
  104. std::cout << "Left-Right" << std::endl;
  105. currentNode->left_ = leftRotate(currentNode->left_);
  106. rightRotate(currentNode);
  107. return;
  108. }
  109. } else if (currentNode->balance_ < -1) {
  110. if (currentNode->right_->balance_ <= 0) {
  111. std::cout << "Right-Right" << std::endl;
  112. std::cout << "Current Node key input: " << currentNode->key_ << std::endl;
  113. leftRotate(currentNode);
  114. return;
  115. } else if (currentNode->right_->balance_ > 0) {
  116. std::cout << "Right-Left" << std::endl;
  117. currentNode->right_ = rightRotate(currentNode->right_);
  118. leftRotate(currentNode);
  119. return;
  120. }
  121. }
  122. balance(parent);
  123. }
  124. return;
  125. }
  126. std::shared_ptr<BSTNode> AVLCommands::rightRotate(std::shared_ptr<BSTNode> currentNode){
  127. if (currentNode == nullptr) {
  128. std::cout << "Can't rotate on an empty node!" << std::endl;
  129. return currentNode;
  130. }
  131. std::cout << "Right Rotate at node " << currentNode->key_ << std::endl;
  132. std::shared_ptr<BSTNode> newSubRoot = currentNode->left_;
  133. currentNode->left_ = newSubRoot->right_;
  134. if (newSubRoot->right_ != nullptr) {
  135. newSubRoot->right_->parent_ = currentNode;
  136. }
  137. newSubRoot->right_ = currentNode;
  138. std::shared_ptr<BSTNode> oldParent = currentNode->parent_.lock();
  139. currentNode->parent_ = newSubRoot;
  140. if (oldParent == nullptr) {
  141. root_ = newSubRoot;
  142. newSubRoot->parent_.reset();
  143. } else {
  144. if (oldParent->right_== currentNode) {
  145. oldParent->right_ = newSubRoot;
  146. }else{
  147. oldParent->left_ = newSubRoot;
  148. }
  149. newSubRoot->parent_ = oldParent;
  150. }
  151. // Update heights
  152. height(root_);
  153. // Return new root
  154. return newSubRoot;
  155. }
  156. // Takes the root of the tree to be left rotated and rotates it to provide new root
  157. std::shared_ptr<BSTNode> AVLCommands::leftRotate(std::shared_ptr<BSTNode> currentNode){
  158. if (currentNode == nullptr) {
  159. std::cout << "Can't rotate on an empty node!" << std::endl;
  160. return currentNode;
  161. }
  162. std::cout << "Left Rotate at node " << currentNode->key_ << std::endl;
  163. std::shared_ptr<BSTNode> newSubRoot = currentNode->right_;
  164. currentNode->right_ = newSubRoot->left_;
  165. if (newSubRoot->left_ != nullptr) {
  166. newSubRoot->left_->parent_ = currentNode;
  167. }
  168. newSubRoot->left_ = currentNode;
  169. std::shared_ptr<BSTNode> oldParent = currentNode->parent_.lock();
  170. currentNode->parent_ = newSubRoot;
  171. if (oldParent == nullptr) {
  172. root_ = newSubRoot;
  173. newSubRoot->parent_.reset();
  174. } else {
  175. if (oldParent->right_== currentNode) {
  176. oldParent->right_ = newSubRoot;
  177. }else{
  178. oldParent->left_ = newSubRoot;
  179. }
  180. newSubRoot->parent_ = oldParent;
  181. }
  182. // Update heights
  183. height(root_);
  184. // Return new root
  185. return newSubRoot;
  186. }
  187. void AVLCommands::printTree(){
  188. printTree(root_);
  189. }
  190. void AVLCommands::printTree(std::shared_ptr<BSTNode> currentNode){
  191. if (root_ == nullptr) {
  192. std::cout << "tree is empty" << std::endl;
  193. return;
  194. } else {
  195. std::cout << "key: " << currentNode->key_ << std::endl;
  196. std::cout << "height: " << currentNode->height_ << std::endl;
  197. std::cout << "balance: " << currentNode->balance_ << std::endl;
  198. if (currentNode->parent_.lock() != nullptr) {
  199. std::cout << "parent: " << currentNode->parent_.lock()->key_ << std::endl;
  200. }
  201. if (currentNode->left_ != nullptr) {
  202. std::cout << "left" << std::endl;
  203. printTree(currentNode->left_);
  204. }
  205. if (currentNode->right_ != nullptr) {
  206. std::cout << "right" << std::endl;
  207. printTree(currentNode->right_);
  208. }
  209. }
  210. }
  211. void AVLCommands::Insert(int key) {
  212. std::cout << "Inserting new key: " << key << std::endl;
  213. // BST insertion
  214. if (root_ == nullptr) {
  215. root_ = std::make_shared<BSTNode>(key);
  216. size_++;
  217. return;
  218. }
  219. std::shared_ptr<BSTNode> currentNode = root_, lastNode = nullptr;
  220. while (currentNode != nullptr) {
  221. lastNode = currentNode;
  222. currentNode = (key < currentNode->key_) ?
  223. currentNode->left_ : currentNode->right_;
  224. }
  225. if (key < lastNode->key_) {
  226. lastNode->left_ = std::make_shared<BSTNode>(key, lastNode);
  227. } else {
  228. lastNode->right_ = std::make_shared<BSTNode>(key, lastNode);
  229. }
  230. size_++;
  231. // update height and balance of the tree
  232. height(root_);
  233. balance(lastNode);
  234. }
  235. bool AVLCommands::Delete(int key) {
  236. std::cout << "Deleting key: " << key << std::endl;
  237. // base BST delete
  238. std::shared_ptr<BSTNode> currentNode = root_;
  239. std::shared_ptr<BSTNode> parent = nullptr;
  240. while (currentNode != nullptr) {
  241. if (currentNode->key_ == key) {
  242. if (currentNode->IsLeaf()) {
  243. DeleteLeaf(currentNode);
  244. } else if (currentNode->left_ == nullptr) {
  245. assert(currentNode->right_ != nullptr);
  246. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  247. parent->ReplaceChild(currentNode, currentNode->right_);
  248. size_--; assert(size_ >= 0);
  249. } else if (currentNode->right_ == nullptr) {
  250. assert(currentNode->left_ != nullptr);
  251. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  252. parent->ReplaceChild(currentNode, currentNode->left_);
  253. size_--; assert(size_ >= 0);
  254. } else {
  255. currentNode->key_ = DeleteMin(currentNode->right_);
  256. }
  257. }
  258. parent = currentNode->parent_.lock();
  259. currentNode = (key < currentNode->key_) ?
  260. currentNode->left_ : currentNode->right_;
  261. }
  262. height(root_);
  263. balance(parent);
  264. // Update balance factor of this ancestor's node
  265. /*
  266. // Check if unbalanced
  267. // Left Left Case
  268. if (currentNodeBalance > 1 && getBalance(currentNode->left_) >= 0)
  269. rightRotate(currentNode);
  270. // Right Right Case
  271. if (currentNodeBalance < -1 && getBalance(currentNode->right_) <= 0)
  272. leftRotate(currentNode);
  273. // Left Right Case
  274. if (currentNodeBalance > 1 && getBalance(currentNode->left_) < 0) {
  275. currentNode->left_ = leftRotate(currentNode->left_);
  276. rightRotate(currentNode);
  277. }
  278. // Right Left Case
  279. if (currentNodeBalance < -1 && getBalance(currentNode->right_) > 0) {
  280. currentNode->right_ = rightRotate(currentNode->right_);
  281. leftRotate(currentNode);
  282. }
  283. */
  284. return false;
  285. }
  286. int AVLCommands::DeleteMin() {
  287. return DeleteMin(root_);
  288. }
  289. void AVLCommands::DeleteLeaf(std::shared_ptr<BSTNode> currentNode) {
  290. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  291. if (parent == nullptr) {
  292. // Delete root
  293. root_ = nullptr;
  294. size_--; assert(size_ == 0);
  295. } else {
  296. if (parent->right_ == currentNode) {
  297. parent->right_ = nullptr;
  298. } else if (parent->left_ == currentNode) {
  299. parent->left_ = nullptr;
  300. } else {
  301. std::cerr << "BST::DeleteLeaf Error: inconsistent state\n";
  302. }
  303. size_--; assert(size_ >= 0);
  304. }
  305. }
  306. int AVLCommands::DeleteMin(std::shared_ptr<BSTNode> currentNode) {
  307. std::shared_ptr<BSTNode> lastNode = nullptr;
  308. while (currentNode != nullptr) {
  309. lastNode = currentNode;
  310. currentNode = currentNode->left_;
  311. }
  312. int result = lastNode->key_;
  313. std::shared_ptr<BSTNode> parent = lastNode->parent_.lock();
  314. if (parent == nullptr) {
  315. // lastNode is root
  316. if (lastNode->right_ != nullptr) {
  317. root_ = lastNode->right_;
  318. lastNode->right_->parent_.reset();
  319. } else {
  320. root_ = nullptr;
  321. }
  322. } else {
  323. // lastNode under the root
  324. if (lastNode->right_ != nullptr) {
  325. if (parent->left_ == lastNode){
  326. parent->left_ = lastNode->right_;
  327. lastNode->right_->parent_ = parent;
  328. } else {
  329. parent->right_ = lastNode->right_;
  330. lastNode->right_->parent_ = parent;
  331. }
  332. } else {
  333. if (parent->left_ == lastNode){
  334. parent->left_ = nullptr;
  335. } else {
  336. parent->right_ = nullptr;
  337. }
  338. }
  339. }
  340. size_--; assert(size_ >= 0);
  341. return result;
  342. }
  343. size_t AVLCommands::size() const {
  344. return size_;
  345. }
  346. bool AVLCommands::empty() const {
  347. return size_ == 0;
  348. }
  349. bool AVLCommands::Find(int key) const {
  350. std::shared_ptr<BSTNode> currentNode = root_;
  351. while (currentNode != nullptr) {
  352. if (currentNode->key_ == key) {
  353. return true;
  354. }
  355. currentNode = (key < currentNode->key_) ?
  356. currentNode->left_ : currentNode->right_;
  357. }
  358. return false;
  359. }
  360. std::string AVLCommands::JSON() const {
  361. nlohmann::json result;
  362. std::queue< std::shared_ptr<BSTNode> > nodes;
  363. if (root_ != nullptr) {
  364. result["root"] = root_->key_;
  365. nodes.push(root_);
  366. while (!nodes.empty()) {
  367. auto v = nodes.front();
  368. nodes.pop();
  369. std::string key = std::to_string(v->key_);
  370. //std::string height = std::to_string(v->height_);
  371. //result[key]["height"] = height;
  372. result[key]["left"] = v->left_->key_;
  373. if (v->left_ != nullptr) {
  374. result[key]["left"] = v->left_->key_;
  375. nodes.push(v->left_);
  376. }
  377. if (v->right_ != nullptr) {
  378. result[key]["right"] = v->right_->key_;
  379. nodes.push(v->right_);
  380. }
  381. if (v->parent_.lock() != nullptr) {
  382. result[key]["parent"] = v->parent_.lock()->key_;
  383. } else {
  384. result[key]["root"] = true;
  385. }
  386. }
  387. }
  388. result["size"] = size_;
  389. return result.dump(2) + "\n";
  390. }