AVLCommands.cpp 11 KB

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