Advertisement
Mikhail-Podbolotov

Untitled

May 29th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include "library.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. Tree::Tree()
  6. {
  7.     Head = nullptr;
  8. }
  9.  
  10. Tree::~Tree()
  11. {
  12.     delete Head;
  13. }
  14.  
  15. void Tree::Add(int value)
  16. {
  17.     if (Head == nullptr) {
  18.         Element* CurElement = new Element(value);
  19.         CurElement->value = value;
  20.         Head = CurElement;
  21.     }
  22.     else {
  23.         Element* PrevEl = nullptr;
  24.         Element* Cur = Head;
  25.         while (Cur != nullptr) {
  26.             if (Cur->value <= value) {
  27.                 PrevEl = Cur;
  28.                 Cur = Cur->RightSon;
  29.             }
  30.             else {
  31.                 PrevEl = Cur;
  32.                 Cur = Cur->LeftSon;
  33.             }
  34.         }
  35.         if (PrevEl != nullptr) {
  36.             Element* CurElement = new Element(value);
  37.             CurElement->value = value;
  38.             if (PrevEl->value <= value) {
  39.                 PrevEl->RightSon = CurElement;
  40.             }
  41.             else PrevEl->LeftSon = CurElement;
  42.         }
  43.     }
  44. }
  45.  
  46. int Tree::GetMaxElementCount()
  47. {
  48.     if (Head == nullptr) {
  49.         return 0;
  50.     }
  51.     int MaxValue = -10000000;
  52.     int count = 0;
  53.     Element* current = Head;
  54.     while (current != nullptr) {
  55.         if (MaxValue < current->value) {
  56.             count = 0;
  57.             MaxValue = current->value;
  58.         }
  59.         ++count;
  60.         current = current->RightSon;
  61.     }
  62.     return count;
  63. }
  64.  
  65. int Tree::GetHeight(Element* node)
  66. {
  67.     if (node == nullptr) {
  68.         return 0;
  69.     }
  70.     int Left = GetHeight(node->LeftSon);
  71.     int Right = GetHeight(node->RightSon);
  72.     return 1 + max(Left,Right);
  73. }
  74.  
  75. bool Tree::isBalanced(Element* node)
  76. {
  77.     if (node == nullptr) return true;
  78.     int leftHeight = GetHeight(node->LeftSon);
  79.     int rightHeight = GetHeight(node->RightSon);
  80.     if (abs(leftHeight - rightHeight) > 1) return false;
  81.     return isBalanced(node->LeftSon) and isBalanced(node->RightSon);
  82. }
  83.  
  84. bool Tree::isBalanced()
  85. {
  86.     return isBalanced(Head);
  87. }
  88.  
  89. bool Tree::FindDivEL(Element* node, int& result)
  90. {
  91.     if (node == nullptr) {
  92.         return false;
  93.     }
  94.     if (node->LeftSon != nullptr and node->value%node->LeftSon->value == 0) {
  95.         result = node->value;
  96.         return true;
  97.     }
  98.     if (node->RightSon != nullptr and node->value % node->RightSon->value == 0) {
  99.         result = node->value;
  100.         return true;
  101.     }
  102.     return FindDivEL(node->LeftSon, result) or FindDivEL(node->RightSon, result);
  103. }
  104.  
  105. bool Tree::FindDivEL(int& result)
  106. {
  107.     return FindDivEL(Head,result);
  108. }
  109.  
  110. Element::Element(int x)
  111. {
  112.     value = x;
  113. }
  114.  
  115. Element::~Element()
  116. {
  117.     delete this->RightSon;
  118.     delete this->LeftSon;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement