Advertisement
Timtsa

18.12.2018

Dec 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9. template <typename T>
  10. class BTNode
  11. {
  12.     T data;
  13.     BTNode*pLeft;
  14.     BTNode*pRigt;
  15.  
  16. public:
  17.     BTNode(const T& _data = T(), BTNode *_pLeft = nullptr, BTNode *_pRigt = nullptr) : data(_data), pLeft(_pLeft), pRigt(_pRigt) {}
  18.  
  19.  
  20.     void setData(const T&_data)
  21.     {
  22.         T data = _data;
  23.     }
  24.  
  25.  
  26.     T getData()
  27.     {
  28.         return data;
  29.     }
  30.  
  31.     void setpRight(BTNode*_pRigt)
  32.     {
  33.         pRigt = _pRigt;
  34.     }
  35.     void setpLeft(BTNode*_pLeft)
  36.     {
  37.         pLeft = _pLeft;
  38.     }
  39.  
  40.     const BTNode *getpRight()const
  41.     {
  42.         return pRigt;
  43.     }
  44.  
  45.     BTNode *getpRight()
  46.     {
  47.         return pRigt;
  48.     }
  49.  
  50.  
  51.     const BTNode *getpLeft()const
  52.     {
  53.         return pLeft;
  54.     }
  55.     BTNode *getpLeft()
  56.     {
  57.         return pLeft;
  58.     }
  59.  
  60.     bool isLeaf()const
  61.     {
  62.         if (pLeft == nullptr&&pRigt == nullptr)
  63.         {
  64.             return true;
  65.         }
  66.     }
  67. };
  68.  
  69.  
  70.  
  71. template<typename T>
  72.  void Add(const T & data, BTNode <T> *&root)
  73. {
  74.     if (root == nullptr)
  75.     {
  76.         root = new BTNode<T>(data);
  77.             return;
  78.     }
  79.     BTNode<T> *pCurrent = root;
  80.     while (true)
  81.     {
  82.         if (pCurrent->getData() == data)
  83.             return;
  84.         if (pCurrent->getData()<data)
  85.         {
  86.            
  87.            
  88.             if (pCurrent->getpRight()==nullptr)
  89.             {
  90.                 pCurrent->setpRight(new BTNode<T>(data));
  91.                 return;
  92.             }
  93.             else
  94.             {
  95.                 pCurrent = pCurrent->getpRight();
  96.             }
  97.  
  98.         }
  99.         else
  100.         {
  101.             if (pCurrent->getpLeft() == nullptr)
  102.             {
  103.                 pCurrent->setpLeft(new BTNode<T>(data));
  104.                 return;
  105.             }
  106.             else
  107.             {
  108.                 pCurrent = pCurrent->getpLeft();
  109.             }
  110.         }
  111.     }
  112.    
  113. }
  114. template<typename T>
  115. BTNode <T> find (const T& data, BTNode <T>*root)
  116. {
  117.        if (root==nullptr)
  118.           return nullptr;
  119.  auto pCurrent=root;
  120.  while(true)
  121. {
  122.     if (pCurrent->getData() == data)
  123.          return pCurren;
  124.       else if(data>pCurren->getData())
  125.            {
  126.              if (pCurrent ->getRight()!=nullptr)
  127.            { pCurrent=pCurrent->getRight();}
  128.              else
  129.               {
  130.                return nullptr;
  131.               }
  132.  
  133.     else
  134. {
  135.  if (pCurrent->getData() == data)
  136.          return pCurren;
  137.       else if(data<pCurren->getData())
  138.            {
  139.              if (pCurrent ->getLeft()!=nullptr)
  140.            { pCurrent=pCurrent->getLeft();}
  141.              else
  142.               {
  143.                return nullptr;
  144.               }
  145.  
  146.  
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. //void fillTree(BTNode<string>*&root)
  154. //{
  155. //  root = new BTNode<string>("Оно Живет в воде?");
  156. //  BTNode<string>*temp = new BTNode <string>("У него есть жабры?");
  157. //  temp->setpLeft(new BTNode<string>("Щука"));
  158. //  temp->setpRight(new BTNode<string>("Лягушка"));
  159. //  root->setpLeft(temp);
  160. //  temp = new BTNode <string>("у него 4 лапы?");
  161. //  temp->setpLeft(new BTNode<string>("Кот"));
  162. //  temp->setpRight(new BTNode<string>("Змея"));
  163. //  root->setpRight(temp);
  164. //}
  165. template <typename T>
  166. void printTree(BTNode <T>*root,int indent=0)
  167. {
  168.     if (root != nullptr)
  169.     {
  170.         printTree(root->getpRight(), indent + 8);
  171.        
  172.         cout << setw(indent) << root->getData() << endl;
  173.        
  174.        
  175.         printTree(root->getpLeft(), indent + 8);
  176.  
  177.         //cout << root->getData() << endl;
  178.        
  179.     }
  180. }
  181.  
  182.  
  183. void main()
  184. {
  185.     BTNode <int>*root = nullptr;
  186.    
  187.     //printTree(root);
  188.    
  189.     for (int i = 0; i < 100; i++)
  190.     {
  191.         Add(rand() % 100, root);
  192.  
  193.     }
  194.     printTree(root);
  195.     system("Pause");
  196.    
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement