AVLCommands.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. std::weak_ptr<BSTNode> parent_;
  20. std::shared_ptr<BSTNode> left_;
  21. std::shared_ptr<BSTNode> right_;
  22. int height_;
  23. int balance_;
  24. //friend BST;
  25. friend AVLCommands;
  26. }; // class BSTNode
  27. class AVLCommands {
  28. public:
  29. AVLCommands();
  30. void Insert(int key);
  31. bool Delete(int key);
  32. bool Find(int key) const;
  33. std::string JSON() const;
  34. size_t size() const;
  35. bool empty() const;
  36. int DeleteMin();
  37. void printTree();
  38. private:
  39. int max(int a, int b);
  40. int height(std::shared_ptr<BSTNode> currentNode);
  41. void balance(std::shared_ptr<BSTNode> currentNode);
  42. std::shared_ptr<BSTNode> rightRotate(std::shared_ptr<BSTNode>);
  43. std::shared_ptr<BSTNode> leftRotate(std::shared_ptr<BSTNode>);
  44. void DeleteLeaf(std::shared_ptr<BSTNode> currentNode);
  45. int DeleteMin(std::shared_ptr<BSTNode> currentNode);
  46. void printTree(std::shared_ptr<BSTNode> currentNode);
  47. std::shared_ptr<BSTNode> root_;
  48. size_t size_;
  49. }; // class BST
  50. #endif