BST.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // add int getParent(); int getLeft(); int getRight();
  32. int getParent(int key);
  33. int getLeft(int key);
  34. int getRight(int key);
  35. private:
  36. void DeleteLeaf(std::shared_ptr<BSTNode> currentNode);
  37. int DeleteMin(std::shared_ptr<BSTNode> currentNode);
  38. std::shared_ptr<BSTNode> root_;
  39. size_t size_;
  40. }; // class BST