Guest User

c

a guest
Nov 14th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. ////////////////////////////////////////////////////
  3. class Node
  4. {
  5. public:
  6.     char  data;
  7.     Node* left,
  8.         * right;
  9.  
  10.     Node() : data(-1), left(NULL), right(NULL) {};
  11. } ;
  12. ////////////////////////////////////////////////////
  13. class Tree
  14. {
  15. private:
  16.     Node* btree;
  17.  
  18.     bool isNULL_(Node* leaf);
  19.     char rootBT_(Node* leaf);
  20.     Node* right_(Node* leaf);
  21.     Node* left_(Node* leaf);
  22.     Node* constrBT_(const char& data, Node*& left, Node*& right);
  23.     void destroy_(Node* leaf);
  24.     void prefix_(Node* leaf);
  25.     Node* enterBT_();
  26.     void displayBT_(Node* leaf, int n);
  27.     void prefix_();
  28. public:
  29.     Tree() : btree(NULL) {};
  30.     ~Tree() { destroy_(btree); }
  31.    
  32.     void enterBT();
  33.     void displayBT(int n);
  34.     void prefix();
  35. } ;
  36. ////////////////////////////////////////////////////
  37. //Tree class PRIVATE SECTION
  38. bool Tree::isNULL_(Node* leaf)
  39. {
  40.     return (leaf == NULL);
  41. }
  42. char Tree::rootBT_(Node* leaf)
  43. {
  44.     if (leaf == NULL)
  45.     {
  46.         std::cerr << "Error: RootBT(null) \n";
  47.         exit(1);
  48.     }
  49.     else
  50.         return leaf->data;
  51. }
  52. Node* Tree::left_(Node* leaf)
  53. {
  54.     if (leaf == NULL)
  55.     {
  56.         std::cerr << "Error: Left(null) \n";
  57.         exit(1);
  58.     }
  59.     else
  60.         leaf->left;
  61. }
  62. Node* Tree::right_(Node* leaf)
  63. {
  64.     if (leaf == NULL)
  65.     {
  66.         std::cerr << "Error: Left(null) \n";
  67.         exit(1);
  68.     }
  69.     else
  70.         leaf->right;
  71. }
  72. Node* Tree::constrBT_(const char& data, Node*& left, Node*& right)
  73. {
  74.     Node* temp = new Node;
  75.  
  76.     if (temp != NULL)
  77.     {
  78.         temp->data = data;
  79.  
  80.         temp->left = left;
  81.         temp->right = right;
  82.  
  83.         return temp;
  84.     }
  85.     else
  86.     {
  87.         std::cerr << "Memory not enough\n";
  88.         exit(1);
  89.     }
  90. }
  91. void Tree::destroy_(Node* leaf)
  92. {
  93.     if (isNULL_(leaf) == 0)
  94.     {
  95.         destroy_(leaf->left);
  96.         destroy_(leaf->right);
  97.  
  98.         delete leaf;
  99.         leaf = NULL;
  100.     }
  101. }
  102. Node* Tree::enterBT_()
  103. {
  104.     char data;
  105.     Node* leaf = new Node;
  106.  
  107.     std::cin >> data;
  108.  
  109.     if (data == '/')
  110.         return NULL;
  111.     else
  112.     {
  113.         leaf->left = enterBT_();
  114.         leaf->right = enterBT_();
  115.  
  116.         return constrBT_(data, leaf->left, leaf->right);
  117.     }
  118. }
  119. void Tree::displayBT_(Node* leaf, int n)
  120. {
  121.     // n - уровень узла
  122.  
  123.     if (leaf != NULL)
  124.     {
  125.         std::cout << ' ' << rootBT_(leaf);
  126.        
  127.         if (!isNULL_(right_(leaf)))
  128.         {
  129.             displayBT_(right_(leaf), n + 1);
  130.         }
  131.         else
  132.             std::cout << std::endl;                             //вниз
  133.         if(!isNULL_(left_(leaf)))
  134.         {
  135.             for (int i = 1; i <= n; ++i)
  136.                 std::cout << "  ";                              // вправо
  137.  
  138.             displayBT_(left_(leaf), n + 1);
  139.         }
  140.     }
  141.     else
  142.     { };
  143. }
  144. void Tree::prefix_(Node* leaf)
  145. {
  146.     if (!isNULL_(leaf))
  147.     {
  148.         prefix_(left_(leaf));
  149.         prefix_(right_(leaf));
  150.     }
  151. }
  152. ////////////////////////////////////////////////////
  153. //Tree class PUBLIC SECTION
  154. void Tree::enterBT()
  155. {
  156.     btree = enterBT_();
  157. }
  158. void Tree::displayBT(int n)
  159. {
  160.     displayBT_(btree, n);
  161. }
  162. void Tree::prefix()
  163. {
  164.     prefix_(btree);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment