BST.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // written by Rob Gysel
  2. #include "BST.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. BST::BST() : root_(nullptr), size_(0) {} // add height here?
  53. // searches through the node keys to get the parent of specified key
  54. int BST::getParent(int key) {
  55. int parent = 0;
  56. if (root_ == nullptr) {
  57. //root_ = std::make_shared<BSTNode>(key);
  58. //size_++;
  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 BST::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 BST::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. void BST::Insert(int key) {
  95. if (root_ == nullptr) {
  96. root_ = std::make_shared<BSTNode>(key);
  97. size_++;
  98. return;
  99. }
  100. std::shared_ptr<BSTNode> currentNode = root_, lastNode = nullptr;
  101. while (currentNode != nullptr) {
  102. lastNode = currentNode;
  103. currentNode = (key < currentNode->key_) ?
  104. currentNode->left_ : currentNode->right_;
  105. }
  106. if (key < lastNode->key_) {
  107. lastNode->left_ = std::make_shared<BSTNode>(key, lastNode);
  108. } else {
  109. lastNode->right_ = std::make_shared<BSTNode>(key, lastNode);
  110. }
  111. size_++;
  112. }
  113. bool BST::Delete(int key) {
  114. std::shared_ptr<BSTNode> currentNode = root_;
  115. while (currentNode != nullptr) {
  116. if (currentNode->key_ == key) {
  117. if (currentNode->IsLeaf()) {
  118. DeleteLeaf(currentNode);
  119. } else if (currentNode->left_ == nullptr) {
  120. assert(currentNode->right_ != nullptr);
  121. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  122. parent->ReplaceChild(currentNode, currentNode->right_);
  123. size_--; assert(size_ >= 0);
  124. } else if (currentNode->right_ == nullptr) {
  125. assert(currentNode->left_ != nullptr);
  126. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  127. parent->ReplaceChild(currentNode, currentNode->left_);
  128. size_--; assert(size_ >= 0);
  129. } else {
  130. currentNode->key_ = DeleteMin(currentNode);
  131. }
  132. }
  133. currentNode = (key < currentNode->key_) ?
  134. currentNode->left_ : currentNode->right_;
  135. }
  136. return false;
  137. }
  138. int BST::DeleteMin() {
  139. return DeleteMin(root_);
  140. }
  141. void BST::DeleteLeaf(std::shared_ptr<BSTNode> currentNode) {
  142. std::shared_ptr<BSTNode> parent = currentNode->parent_.lock();
  143. if (parent == nullptr) {
  144. // Delete root
  145. root_ = nullptr;
  146. size_--; assert(size_ == 0);
  147. } else {
  148. if (parent->right_ == currentNode) {
  149. parent->right_ = nullptr;
  150. } else if (parent->left_ == currentNode) {
  151. parent->left_ = nullptr;
  152. } else {
  153. std::cerr << "BST::DeleteLeaf Error: inconsistent state\n";
  154. }
  155. size_--; assert(size_ >= 0);
  156. }
  157. }
  158. int BST::DeleteMin(std::shared_ptr<BSTNode> currentNode) {
  159. std::shared_ptr<BSTNode> lastNode = nullptr;
  160. while (currentNode != nullptr) {
  161. lastNode = currentNode;
  162. currentNode = currentNode->left_;
  163. }
  164. int result = lastNode->key_;
  165. std::shared_ptr<BSTNode> parent = lastNode->parent_.lock();
  166. if (parent == nullptr) {
  167. // lastNode is root
  168. if (lastNode->right_ != nullptr) {
  169. root_ = lastNode->right_;
  170. lastNode->right_->parent_.reset();
  171. } else {
  172. root_ = nullptr;
  173. }
  174. } else {
  175. // lastNode under the root
  176. if (lastNode->right_ != nullptr) {
  177. parent->left_ = lastNode->right_;
  178. lastNode->right_->parent_ = parent;
  179. } else {
  180. parent->left_ = nullptr;
  181. }
  182. }
  183. size_--; assert(size_ >= 0);
  184. return result;
  185. }
  186. size_t BST::size() const {
  187. return size_;
  188. }
  189. bool BST::empty() const {
  190. return size_ == 0;
  191. }
  192. bool BST::Find(int key) const {
  193. std::shared_ptr<BSTNode> currentNode = root_;
  194. while (currentNode != nullptr) {
  195. if (currentNode->key_ == key) {
  196. return true;
  197. }
  198. currentNode = (key < currentNode->key_) ?
  199. currentNode->left_ : currentNode->right_;
  200. }
  201. return false;
  202. }
  203. std::string BST::JSON() const {
  204. nlohmann::json result;
  205. std::queue< std::shared_ptr<BSTNode> > nodes;
  206. if (root_ != nullptr) {
  207. result["root"] = root_->key_;
  208. nodes.push(root_);
  209. while (!nodes.empty()) {
  210. auto v = nodes.front();
  211. nodes.pop();
  212. std::string key = std::to_string(v->key_);
  213. if (v->left_ != nullptr) {
  214. result[key]["left"] = v->left_->key_;
  215. nodes.push(v->left_);
  216. }
  217. if (v->right_ != nullptr) {
  218. result[key]["right"] = v->right_->key_;
  219. nodes.push(v->right_);
  220. }
  221. if (v->parent_.lock() != nullptr) {
  222. result[key]["parent"] = v->parent_.lock()->key_;
  223. } else {
  224. result[key]["root"] = true;
  225. }
  226. }
  227. }
  228. result["size"] = size_;
  229. return result.dump(2) + "\n";
  230. }