AVLCommands.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. //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. void printTree();
  37. private:
  38. int max(int a, int b);
  39. int height(std::shared_ptr<BSTNode> currentNode);
  40. int getBalance(std::shared_ptr<BSTNode> currentNode);
  41. std::shared_ptr<BSTNode> rightRotate(std::shared_ptr<BSTNode>);
  42. std::shared_ptr<BSTNode> leftRotate(std::shared_ptr<BSTNode>);
  43. void DeleteLeaf(std::shared_ptr<BSTNode> currentNode);
  44. int DeleteMin(std::shared_ptr<BSTNode> currentNode);
  45. void printTree(std::shared_ptr<BSTNode> currentNode);
  46. std::shared_ptr<BSTNode> root_;
  47. size_t size_;
  48. }; // class BST
  49. #endif