Advertisement
Timtsa

Tree

Dec 14th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. template <typename T>
  9. class BTNode
  10. {
  11.     T data;
  12.     BTNode*pLeft;
  13.     BTNode*pRigt;
  14.  
  15. public:
  16.     BTNode(const T& _data = T(), BTNode *_pLeft = nullptr, BTNode *_pRigt = nullptr) : data(_data), pLeft(_pLeft), pRigt(_pRigt) {}
  17.  
  18.    
  19.     void setData(const T&_data)
  20.     {
  21.         T data = _data;
  22.     }
  23.  
  24.  
  25.     T getData()
  26.     {
  27.         return data;
  28.     }
  29.  
  30.     void setpRight(BTNode*_pRigt)
  31.     {
  32.         pRigt = _pRigt;
  33.     }
  34.     void setpLeft(BTNode*_pLeft)
  35.     {
  36.         pLeft = _pLeft;
  37.     }
  38.  
  39.     const BTNode *getpRight()const
  40.     {
  41.         return pRigt;
  42.     }
  43.    
  44.     BTNode *getpRight()
  45.     {
  46.         return pRigt;
  47.     }
  48.    
  49.        
  50.     const BTNode *getpLeft()const
  51.     {
  52.         return pLeft;
  53.     }
  54.      BTNode *getpLeft()
  55.     {
  56.         return pLeft;
  57.     }
  58.        
  59.     bool isLeaf()const
  60.     {
  61.         if (pLeft == nullptr&&pRigt == nullptr)
  62.         {
  63.             return true;
  64.         }
  65.     }
  66. };
  67.  
  68. void fillTree(BTNode<string>*&root)
  69. {
  70.     root = new BTNode<string>("Оно Живет в воде?");
  71.     BTNode<string>*temp = new BTNode <string>("У него есть жабры?");
  72.     temp->setpLeft(new BTNode<string>("Щука"));
  73.     temp->setpRight(new BTNode<string>("Лягушка"));
  74.     root->setpLeft(temp);
  75.     temp = new BTNode <string>("у него 4 лапы?");
  76.     temp->setpLeft(new BTNode<string>("Кот"));
  77.     temp->setpRight(new BTNode<string>("Змея"));
  78.     root->setpRight(temp);
  79. }
  80. template <typename T>
  81. void printTree(BTNode <T>*root)
  82. {
  83.     if (root != nullptr)
  84.     {
  85.         printTree(root->getpLeft());
  86.         //cout << root->getData() << endl;
  87.         printTree(root->getpRight());
  88.  
  89.         cout << root->getData() << endl;
  90.         //cout << root->getData() << endl;
  91.     }
  92. }
  93.  
  94.  
  95. void main()
  96. {
  97.     setlocale(LC_ALL, "Russian");
  98.     BTNode <string>*root = nullptr;
  99.     fillTree(root);
  100.     printTree(root);
  101.        
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement