BST.h 1.2 KB

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