AVLCommands.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // include guard
  2. #ifndef AVLC_H
  3. #define AVLC_H
  4. #include <memory>
  5. #include <string>
  6. class AVLCommands;
  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. int height_;
  20. std::weak_ptr<BSTNode> parent_;
  21. std::shared_ptr<BSTNode> left_;
  22. std::shared_ptr<BSTNode> right_;
  23. //friend BST;
  24. friend AVLCommands;
  25. }; // class BSTNode
  26. class AVLCommands {
  27. public:
  28. AVLCommands();
  29. void Insert(int key);
  30. bool Delete(int key);
  31. bool Find(int key) const;
  32. std::string JSON() const;
  33. size_t size() const;
  34. bool empty() const;
  35. int DeleteMin();
  36. // add int getParent(); int getLeft(); int getRight();
  37. int getParent(int key);
  38. int getLeft(int key);
  39. int getRight(int key);
  40. private:
  41. int max(int a, int b);
  42. int height(std::shared_ptr<BSTNode> currentNode);
  43. int getBalance(std::shared_ptr<BSTNode> currentNode);
  44. std::shared_ptr<BSTNode> rightRotate(std::shared_ptr<BSTNode>);
  45. std::shared_ptr<BSTNode> leftRotate(std::shared_ptr<BSTNode>);
  46. void DeleteLeaf(std::shared_ptr<BSTNode> currentNode);
  47. int DeleteMin(std::shared_ptr<BSTNode> currentNode);
  48. std::shared_ptr<BSTNode> root_;
  49. size_t size_;
  50. }; // class BST
  51. #endif