Advertisement
smatskevich

Seminar11

Dec 10th, 2021
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct AVLNode {
  4.   int Key;
  5.   int Height;
  6.   int Count;  // Количество узлов в поддереве с корнем.
  7.   AVLNode* Left;
  8.   AVLNode* Right;
  9.  
  10.   explicit AVLNode(int key) : Key(key), Height(1), Count(1), Left(nullptr), Right(nullptr) {}
  11. };
  12.  
  13. int Height(AVLNode* node) {
  14.   return node ? node->Height : 0;
  15. }
  16.  
  17. // Не забыть сделать FixCount(node) и вызывать в тех же местах, что и FixHeight.
  18. void FixHeight(AVLNode* node) {
  19.   node->Height = std::max(Height(node->Left), Height(node->Right)) + 1;
  20. }
  21.  
  22. int Balance(AVLNode* node) {
  23.   if (!node) return 0;
  24.   return Height(node->Left) - Height(node->Right);
  25. }
  26.  
  27. AVLNode* RotateLeft(AVLNode* root) {
  28.   AVLNode* right = root->Right;
  29.   root->Right = root->Right->Left;
  30.   right->Left = root;
  31.   FixHeight(root);
  32.   FixHeight(right);
  33.   return right;
  34. }
  35.  
  36. void RotateLeft2(AVLNode*& root) {
  37.   AVLNode* right = root->Right;
  38.   root->Right = root->Right->Left;
  39.   right->Left = root;
  40.   root = right;
  41.   FixHeight(root->Left);
  42.   FixHeight(root);
  43. }
  44.  
  45. void RotateRight(AVLNode*& /*root*/) {}
  46.  
  47. void FixTree(AVLNode*& node) {
  48.   if (Balance(node) == 2) {
  49.  
  50.   } else if (Balance(node) == -2) {
  51.     if (Balance(node->Right) == 1) {
  52.       RotateRight(node->Right);
  53.     }
  54.     RotateLeft2(node);
  55.   }
  56. }
  57.  
  58. void Add(int key, AVLNode*& node) {
  59.   if (!node) {
  60.     node = new AVLNode(key);
  61.     return;
  62.   }
  63.   if (node->Key < key) Add(key, node->Right);
  64.   else Add(key, node->Left);
  65.  
  66.   FixHeight(node);
  67.   FixTree(node);
  68. }
  69.  
  70. int Count(AVLNode* node) {
  71.   return node ? node->Count : 0;
  72. }
  73.  
  74. int Statistics(AVLNode* node, int k) {
  75.   if (Count(node->Left) == k) return node->Key;
  76.   if (Count(node->Left) > k) return Statistics(node->Left, k);
  77.   return Statistics(node->Right, k - Count(node->Left) - 1);
  78. }
  79.  
  80. int main() {
  81.   AVLNode* root = new AVLNode(5);
  82.   root->Right = new AVLNode(7);
  83.   FixHeight(root);
  84.  
  85.   std::cout << Height(root) << std::endl;
  86.   std::cout << Balance(root) << std::endl;
  87.  
  88.   root = RotateLeft(root);
  89.   std::cout << Height(root) << std::endl;
  90.   std::cout << Balance(root) << std::endl;
  91.  
  92.   root->Right = new AVLNode(10);
  93.   root->Count = 3;
  94.  
  95.   int k = 2;
  96.   if (root->Count <= k) std::cout << "No stat" << std::endl;
  97.   else std::cout << "Statistics = " << Statistics(root, k) << std::endl;
  98.   return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement