Advertisement
Mikhail-Podbolotov

Untitled

May 22nd, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include "library.h"
  2.  
  3. Tree::Tree()
  4. {
  5.     Head = nullptr;
  6. }
  7.  
  8. void Tree::Add(double value)
  9. {
  10.     if (Head == nullptr) {
  11.         Element* CurElement = new Element(value);
  12.         CurElement->value = value;
  13.         Head = CurElement;
  14.     }
  15.     else {
  16.         Element* PrevEl = nullptr;
  17.         Element* Cur = Head;
  18.         while (Cur != nullptr) {
  19.             if (Cur->value <= value) {
  20.                 PrevEl = Cur;
  21.                 Cur = Cur->RightSon;
  22.             }
  23.             else {
  24.                 PrevEl = Cur;
  25.                 Cur = Cur->LeftSon;
  26.             }
  27.         }
  28.         if (PrevEl != nullptr) {
  29.             Element* CurElement = new Element(value);
  30.             CurElement->value = value;
  31.             if (PrevEl->value <= value) {
  32.                 PrevEl->RightSon = CurElement;
  33.             }
  34.             else PrevEl->LeftSon = CurElement;
  35.         }
  36.     }
  37. }
  38.  
  39. int Tree::GetMaxElementCount()
  40. {
  41.     if (Head == nullptr) {
  42.         return 0;
  43.     }
  44.     double MaxValue = -10000000;
  45.     int count = 0;
  46.     Element* current = Head;
  47.     while (current != nullptr) {
  48.         if (MaxValue < current->value) {
  49.             count = 0;
  50.             MaxValue = current->value;
  51.         }
  52.         ++count;
  53.         current = current->RightSon;
  54.     }
  55.     return count;
  56. }
  57.  
  58. Element::Element(double x)
  59. {
  60.     value = x;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement