BST.h 931 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <memory>
  2. #include <string>
  3. class BST;
  4. class BSTNode {
  5. public:
  6. BSTNode(int key);
  7. BSTNode(int key, std::weak_ptr<BSTNode> parent);
  8. bool IsLeaf() const;
  9. bool IsMissingChild() const;
  10. bool HasLeftChild() const;
  11. bool HasRightChild() const;
  12. void DeleteChild(std::shared_ptr<BSTNode> v);
  13. void ReplaceChild(std::shared_ptr<BSTNode> v, std::shared_ptr<BSTNode> u);
  14. private:
  15. int key_;
  16. std::weak_ptr<BSTNode> parent_;
  17. std::shared_ptr<BSTNode> left_;
  18. std::shared_ptr<BSTNode> right_;
  19. friend BST;
  20. }; // class BSTNode
  21. class BST {
  22. public:
  23. BST();
  24. void Insert(int key);
  25. bool Delete(int key);
  26. bool Find(int key) const;
  27. std::string JSON() const;
  28. size_t size() const;
  29. bool empty() const;
  30. int DeleteMin();
  31. private:
  32. void DeleteLeaf(std::shared_ptr<BSTNode> currentNode);
  33. int DeleteMin(std::shared_ptr<BSTNode> currentNode);
  34. std::shared_ptr<BSTNode> root_;
  35. size_t size_;
  36. }; // class BST