BST.h 1.1 KB

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