Aleksandr_Grigoryev

ЛОЛИЛОЛИ

May 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<fstream>
  4. using namespace std;
  5. ////////////////////////
  6. struct treeNode
  7. {
  8.     treeNode* left;
  9.     treeNode* right;
  10.     int   data;
  11. };
  12. ///////////////////////
  13. struct stackNode
  14. {
  15.     stackNode* next;
  16.     treeNode*  node;
  17. };
  18.  
  19.  
  20.  
  21. //////////////////////
  22. void push_tree(treeNode* &tree_top, int x)
  23. {
  24.     treeNode *p, *m;
  25.     bool b;
  26.     if (tree_top == NULL)
  27.     {
  28.         m = new treeNode;
  29.         m->data = x;
  30.         m->left = NULL;
  31.         m->right = NULL;
  32.         tree_top = m;
  33.     }
  34.     else
  35.     {
  36.         m = tree_top;
  37.         b = true;
  38.         while (b)
  39.         {
  40.             if (x >= m->data)
  41.             {
  42.                 if (m->right)
  43.                     m = m->right;
  44.                 else
  45.                 {
  46.                     p = new treeNode;
  47.                     p->data = x;
  48.                     p->left = NULL;
  49.                     p->right = NULL;
  50.                     m->right = p;
  51.                     b = false;
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 if (m->left) m = m->left;
  57.                 else
  58.                 {
  59.                     p = new treeNode;
  60.                     p->data = x;
  61.                     p->left = NULL;
  62.                     p->right = NULL;
  63.                     m->left = p;
  64.                     b = false;
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70. }
  71. /////////////////////////////////////////////////
  72. void push_stack(stackNode* &stack_top, treeNode* t)
  73. {
  74.     stackNode* p;
  75.     p = new stackNode;
  76.     p->node = t;
  77.     p->next = stack_top;
  78.     stack_top = p;
  79. }
  80. //////////////////////////////////////////////////////////////
  81. void create_tree(ifstream &f, treeNode* &tree_top)
  82. {
  83.     tree_top = NULL;
  84.     int x;
  85.     while (f >> x)
  86.     {
  87.         push_tree(tree_top, x);
  88.     }
  89. }
  90. /////////////////////////
  91. void pop_stack(stackNode* &stack_top, treeNode* &t)
  92. //get node of tree from stack
  93. {
  94.     stackNode *p;
  95.     if (stack_top)
  96.     {
  97.         t = stack_top->node;
  98.         p = stack_top->next;
  99.         delete stack_top;
  100.         stack_top = p;
  101.     }
  102. }
  103. /////////////////////////////////////////////////////////////
  104. void print_node(int x)
  105. {
  106.     cout << " " << x;
  107. }
  108. ////////////////////////////
  109. void inorder_tree(treeNode* tree_top)
  110. //simmetric obhod tree
  111. {
  112.     cout << "***************************" << endl;
  113.     stackNode* stack_top;
  114.     treeNode* tree_p;
  115.     tree_p = tree_top;
  116.     stack_top = NULL;
  117.     do
  118.     {
  119.         while (tree_p)
  120.         {
  121.             while (tree_p->left)
  122.             {
  123.                 push_stack(stack_top, tree_p);
  124.                 tree_p = tree_p->left;
  125.             }
  126.             print_node(tree_p->data);
  127.             tree_p = tree_p->right;
  128.         }
  129.  
  130.         if (stack_top)
  131.         {
  132.             pop_stack(stack_top, tree_p);
  133.             print_node(tree_p->data);
  134.             tree_p = tree_p->right;
  135.         }
  136.     } while (stack_top || tree_p);
  137.     cout << endl;
  138. }
  139.  
  140. void showTree(treeNode* tree_top)
  141. {
  142.     if (tree_top != NULL)
  143.     {
  144.         showTree(tree_top->left);
  145.         cout << tree_top->data << ' ';
  146.         showTree(tree_top->right);
  147.     }
  148. }
  149.  
  150. int task8a(treeNode * tree_top, int x)
  151. {
  152.     int count=0;
  153.     if (tree_top == NULL)
  154.         return 0;
  155.  
  156.          return (tree_top->data == x) + task8a(tree_top->left, x) + task8a(tree_top->right, x);
  157.  
  158. }
  159.  
  160. double task8v(treeNode *tree_top,double &x)
  161. {
  162.     if (tree_top != NULL)
  163.     {
  164.         x += tree_top->data;
  165.         task8v(tree_top->left, x);
  166.         task8v(tree_top->right, x);
  167.     }
  168.     return x;
  169. }
  170. int task8g(treeNode * tree_top)
  171. {
  172.     if (tree_top->right != NULL)
  173.     {
  174.         return task8g(tree_top->right);
  175.     }
  176.     else
  177.         return tree_top->data;
  178. }
  179. int task8e(treeNode * tree_top)
  180. {
  181.     int max;
  182.     if (tree_top == NULL)
  183.         return 0;
  184.  
  185.     int x, y;
  186.  
  187.     if (tree_top->left != NULL)
  188.     {
  189.         x = task8e(tree_top->left);
  190.     }
  191.     else
  192.         x = -1;
  193.  
  194.     if (tree_top->right != NULL)
  195.     {
  196.         y = task8e(tree_top->right);
  197.     }
  198.     else
  199.         y = -1;
  200.  
  201.     if (x > y)
  202.         max=x;
  203.     else max=y;
  204.  
  205.     return max + 1;
  206. }
  207.  
  208. ///////////////////////////////////////
  209. void main()
  210. {
  211.     setlocale(LC_ALL, "Russian");
  212.     ifstream f("Text.txt");
  213.     int x;
  214.     double count = 0;
  215.     bool found = false;
  216.     treeNode* tree_top, *p = NULL;
  217.     create_tree(f, tree_top);
  218.     showTree(tree_top);
  219.     f.close();
  220.     cout << endl;
  221.     cin >> x;
  222.     /*
  223.     if (tree_top != NULL)
  224.     {
  225.         if (task8a(tree_top, x) != 0)
  226.             cout << "yes" << endl;
  227.         else
  228.             cout << "no" << endl;
  229.     }
  230.     else
  231.         cout << "list is empty" << endl;
  232.     */
  233.     /*
  234.     if (tree_top != NULL)
  235.     {
  236.         cout << task8v(tree_top,count);
  237.     }
  238.     else
  239.         cout << "list is empty" << endl;
  240.     */
  241.    
  242.     if (tree_top != NULL)
  243.     {
  244.         cout << task8a(tree_top,x);
  245.     }
  246.     else
  247.         cout << "list is empty" << endl;
  248.    
  249.     /*
  250.     if (tree_top != NULL)
  251.     {
  252.          cout<<task8g(tree_top);
  253.     }
  254.     else
  255.         cout << "list is empty" << endl;
  256.         */
  257.     /*
  258.     if (tree_top != NULL)
  259.     {
  260.         showTree(tree_top);
  261.     }
  262.     else
  263.         cout << "list is empty" << endl;
  264.     */
  265.     /*
  266.     if (tree_top != NULL)
  267.     {
  268.         cout << task8e(tree_top);
  269.     }
  270.     else
  271.         cout << "list is empty" << endl;
  272.     */
  273.     system("pause");
  274. }
Advertisement
Add Comment
Please, Sign In to add comment