Advertisement
amsavchenko

for Nastya

May 17th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.47 KB | None | 0 0
  1. template <typename T>
  2. struct treenode {
  3.     T data;
  4.     int kids;
  5.     treenode *leftkid;
  6.     treenode *rightkid;
  7.  
  8.    
  9. };
  10.  
  11. template <typename T>
  12. class tree {
  13.     treenode<T> *root;
  14.    
  15.     void destroytree(treenode<T> *node) {
  16.         if (node != nullptr) {
  17.             destroytree(node->leftkid);
  18.             destroytree(node->rightkid);
  19.             delete node;
  20.         }
  21.     }
  22.    
  23.     void rootRightLeft_print(treenode<T> *node) {
  24.         if (node != nullptr) {
  25.             cout << node->data << " ";
  26.             rootRightLeft_print(node->rightkid);
  27.             rootRightLeft_print(node->leftkid);
  28.         }
  29.     }
  30.    
  31.     void leftRootRight_print(treenode<T> *node) {
  32.         if (node != nullptr) {
  33.             leftRootRight_print(node->leftkid);
  34.             cout << node->data << " ";
  35.             leftRootRight_print(node->rightkid);
  36.         }
  37.     }
  38.    
  39.     int get_value_of_function (treenode<void*> *node) {
  40.         return ((int(*)(int))(node->data))(5);
  41.     }
  42.    
  43.     void print_value_of_function (treenode<void*> *node) {
  44.         if (node != nullptr) {
  45.             cout << get_value_of_function(node) << endl;
  46.             print_value_of_function(node->leftkid);
  47.             print_value_of_function(node->rightkid);
  48.         }
  49.     }
  50.    
  51.    
  52.     // 1. map
  53.     void map (treenode<T> *node, T value) {
  54.         if (node != nullptr) {
  55.             node->data = value;
  56.             map (node->leftkid, value);
  57.             map (node->rightkid, value);
  58.         }
  59.     }
  60.    
  61.     bool check (treenode<string> *node) {
  62.         if ((node->data).length() < 5) return true;
  63.         else return false;
  64.     }
  65.    
  66.     bool check (treenode<void*> *node) {
  67.         if (get_value_of_function(node) < 10) return true;
  68.         else return false;
  69.     }
  70.    
  71.     // 2. where
  72.     void where (treenode<T> *node, tree<T> *new_tree) { // УКАЗАТЕЛЬ  на новое дерево
  73.         if (node != nullptr) {
  74.             if (check(node)) {
  75.                 new_tree->insert(node->data);
  76.             }
  77.             where (node->leftkid, new_tree);
  78.             where (node->rightkid, new_tree);
  79.         }
  80.     }
  81.    
  82.    
  83.     // 3. слияние
  84.     void merge_trees (treenode<T> *node_of_1st_tree, tree<T> *second_tree) {
  85.         // к дереву second_tree прибавляем все элементы дерева для к-ого вызвана ф-ия
  86.         if (node_of_1st_tree != nullptr) {
  87.             second_tree->insert(node_of_1st_tree->data);
  88.             merge_trees(node_of_1st_tree->leftkid, second_tree);
  89.             merge_trees(node_of_1st_tree->rightkid, second_tree);
  90.         }
  91.     }
  92.    
  93.     // 4. извлечение поддерева по заданному элементу
  94.     void subtree_extraction (treenode<T> *node, T value, tree<T> *res_tree, int *flag) {
  95.         if (node != nullptr && *flag == 0) {
  96.             if(node->data == value) {
  97.                 res_tree->root = node;
  98.                 *flag = 1;
  99.             }
  100.             subtree_extraction(node->leftkid, value, res_tree, flag);
  101.             subtree_extraction(node->rightkid, value, res_tree, flag);
  102.         }
  103.     }
  104.    
  105.    
  106.     // 5. поиск элемента на вхождение
  107.     void if_element_contain (treenode<T> *node, T value, bool* check_ptr) {
  108.         if (node != nullptr && *check_ptr == false) {
  109.             if (node->data == value) {
  110.                 *check_ptr = true;
  111.             }
  112.             if_element_contain(node->leftkid, value, check_ptr);
  113.             if_element_contain(node->rightkid, value, check_ptr);
  114.         }
  115.     }
  116.    
  117.    
  118.     void print_mentioned_level (treenode<T> *node, int level) {
  119.         if (node == nullptr) return;
  120.         else {
  121.             print_mentioned_level(node->leftkid, level - 1);
  122.             if (level == 0) cout << node->data << " ";
  123.             print_mentioned_level (node->rightkid, level - 1);
  124.         }
  125.     }
  126.    
  127.     void compare_trees(treenode<T> *first, treenode<T> *second, bool *if_same) {
  128.         if (second != nullptr) {
  129.             if ((first == nullptr) || (first->data != second->data)) *if_same = 0;
  130.             if ((*if_same != 0)&&(first != nullptr)) {
  131.                 compare_trees(first->leftkid, second->leftkid, if_same);
  132.                 compare_trees(first->rightkid, second->rightkid, if_same);
  133.             }
  134.         }
  135.     }
  136.    
  137.     void if_tree_contain(treenode<T> *big_node, treenode<T> *small_node, bool *check_ptr, bool *if_same) {
  138.         if (big_node != nullptr) {
  139.             if ((big_node->data == small_node->data) && (!(*check_ptr))) {
  140.                 compare_trees(big_node, small_node, if_same);
  141.                 if (*if_same) {
  142.                     *check_ptr = 1;
  143.                 }
  144.             }
  145.             if (!(*check_ptr)) {
  146.                 if_tree_contain(big_node->leftkid, small_node, check_ptr, if_same);
  147.                 if_tree_contain(big_node->rightkid, small_node, check_ptr, if_same);
  148.             }
  149.         }
  150.     }
  151.    
  152.    
  153.    
  154.    
  155.    
  156.    
  157.  
  158. ////////////////////////////////////////////////////////////
  159.    
  160. public:
  161.     tree() {
  162.         root = nullptr;
  163.     }
  164.    
  165.     ~tree() {
  166.         cout << "destructor" << endl;
  167.         destroytree(root);
  168.     }
  169.  
  170.     void insert(T data);
  171.    
  172.     void rootRightLeft_print() { // КПЛ обход
  173.         cout << "КПЛ обход ->" << endl;
  174.         rootRightLeft_print(root);
  175.         cout << endl;
  176.         cout << endl;
  177.     }
  178.     void leftRootRight_print() {    // ЛКП обход
  179.         cout << "ЛКП обход ->" << endl;
  180.         leftRootRight_print(root);
  181.         cout << endl;
  182.         cout << endl;
  183.     }
  184.     void map (T value) {
  185.         map (root, value);
  186.     }
  187.     void print_value_of_function () {
  188.         cout << "Значения функций от x=5 " << endl;
  189.         print_value_of_function (root);
  190.     }
  191.     void where (tree<T> *new_tree) {
  192.         where (root, new_tree);
  193.     }
  194.     void merge_trees (tree<T> *second_tree) { //
  195.         merge_trees(root, second_tree);
  196.     }
  197.     void subtree_extraction (T value, tree<T> *res_tree) {
  198.         int *flag = new int; *flag = 0;
  199.         subtree_extraction(root, value, res_tree, flag);
  200.         delete flag;
  201.     }
  202.     bool if_element_contain (T value) {
  203.         bool* check_ptr = new bool; *check_ptr = false;
  204.         if_element_contain(root, value, check_ptr);
  205.         bool check = *check_ptr;
  206.         delete check_ptr;
  207.         return check;
  208.     }
  209.    
  210.     void print_by_levels () {
  211.         cout << "Печать по строкам -> " << endl;
  212.  
  213.         int max_level = 0;
  214.         while (pow(2, max_level) - 2 <= root->kids)  max_level ++;
  215.         for (int i = 0; i < max_level; i ++) {
  216.             print_mentioned_level(root, i);
  217.             cout << endl;
  218.         }
  219.     }
  220.    
  221.     bool if_tree_contain(tree<T> *smallTree) {
  222.         bool *check_ptr = new bool; *check_ptr = 0;
  223.         bool *if_same = new bool; *if_same = 1;
  224.         if_tree_contain(root, smallTree->root,check_ptr, if_same);
  225.         delete if_same;
  226.         bool to_return = *check_ptr;
  227.         delete check_ptr;
  228.         return to_return;
  229.     }
  230.    
  231.    
  232.    
  233.     //////////////////
  234.     void set_tree () {
  235.         cout << "Введите количество элементов в дереве: ";
  236.         int amount_of_elements;
  237.         do {cin >> amount_of_elements; } while (amount_of_elements < 1);
  238.         cout << "Введите " << amount_of_elements << " строки: " << endl;
  239.         for (int i = 0; i < amount_of_elements; i ++) {
  240.             cout << "  " << i + 1 << ": " ;
  241.             string str; cin >> str;
  242.             this->insert(str);
  243.         }
  244.     }
  245.    
  246.     void set_function_tree (void **ptr) {
  247.         cout << "Список функций: " << endl;
  248.         cout << " 1: x * 2\n 2: x^3\n 3: x + 1\n 4: x^2\n 5: x + 2\n";
  249.         cout << "Введите количество элементов в дереве: ";
  250.         int amount_of_elements;
  251.         do {cin >> amount_of_elements; } while (amount_of_elements < 1);
  252.         cout << "Выберите " << amount_of_elements << " функций из списка выше " << endl;
  253.         for (int i = 0; i < amount_of_elements; i ++) {
  254.             int func_number;
  255.             do {
  256.                 cout << "  " << i + 1 << ": " ;
  257.                 cin >> func_number;
  258.             } while (func_number != 1 && func_number != 2 && func_number != 3 && func_number != 4 && func_number != 5);
  259.             this->insert(ptr[func_number-1]);
  260.         }
  261.     }
  262.    
  263.  
  264. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement