Advertisement
Guest User

<3 2ch

a guest
Dec 29th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. template<typename T>
  2. class Btree
  3. {
  4.     public:
  5.         Btree()
  6.         {
  7.         }
  8.  
  9.         Btree(T data)
  10.         {
  11.             this->data = data;
  12.         }
  13.  
  14.        ~Btree()
  15.         {
  16.             delete this->left;
  17.             delete this->rigth;
  18.         }
  19.  
  20.         void add(T value)
  21.         {
  22.             if(value >= this->data)
  23.             {
  24.                 this->m_add(value, this->rigth);
  25.             }
  26.             else
  27.             {
  28.                 this->m_add(value, this->left);
  29.             }
  30.         }
  31.  
  32.     private:
  33.         T data;
  34.         Btree* left;
  35.         Btree* rigth;
  36.  
  37.         bool is_exist(Btree* branch)
  38.         {
  39.             return branch ? 1 : 0;
  40.         }
  41.  
  42.         static void m_add(T value, Btree*& branch)
  43.         {
  44.             if(!branch)
  45.             {
  46.                 branch = new Btree<T>;
  47.                 branch->data = value;
  48.                 return;
  49.             }
  50.             else
  51.             {
  52.                 if(value >= branch->data)
  53.                 {
  54.                     m_add(value, branch->rigth);
  55.                 }
  56.                 else
  57.                 {
  58.                     m_add(value, branch->left);
  59.                 }
  60.             }
  61.         }
  62. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement