Advertisement
Kulagina_JuliA

17.8

May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.33 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 treeNodeChar
  14. {
  15.     treeNode* left;
  16.     treeNode* right;
  17.     char  data;
  18. };
  19. /////////////////////////
  20. struct stackNode
  21. {
  22.     stackNode* next;
  23.     treeNode*  node;
  24. };
  25. //////////////////////
  26. void push_tree(treeNode* &tree_top, int x)
  27. {
  28.     treeNode *p, *m;
  29.     bool b;
  30.     if (tree_top == NULL)
  31.     {
  32.         m = new treeNode;
  33.         m->data = x;
  34.         m->left = NULL;
  35.         m->right = NULL;
  36.         tree_top = m;
  37.     }
  38.     else
  39.     {
  40.         m = tree_top;
  41.         b = true;
  42.         while (b)
  43.         {
  44.             if (x >= m->data)
  45.             {
  46.                 if (m->right)
  47.                     m = m->right;
  48.                 else
  49.                 {
  50.                     p = new treeNode;
  51.                     p->data = x;
  52.                     p->left = NULL;
  53.                     p->right = NULL;
  54.                     m->right = p;
  55.                     b = false;
  56.                 }
  57.             }
  58.             else
  59.             {
  60.                 if (m->left) m = m->left;
  61.                 else
  62.                 {
  63.                     p = new treeNode;
  64.                     p->data = x;
  65.                     p->left = NULL;
  66.                     p->right = NULL;
  67.                     m->left = p;
  68.                     b = false;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74. }
  75. /////////////////////////////////////////////////
  76. void push_treeChar(treeNode* &tree_top, char x)
  77. {
  78.     treeNode *p, *m;
  79.     bool b;
  80.     if (tree_top == NULL)
  81.     {
  82.         m = new treeNode;
  83.         m->data = x;
  84.         m->left = NULL;
  85.         m->right = NULL;
  86.         tree_top = m;
  87.     }
  88.     else
  89.     {
  90.         m = tree_top;
  91.         b = true;
  92.         while (b)
  93.         {
  94.             if (x >= m->data)
  95.             {
  96.                 if (m->right)
  97.                     m = m->right;
  98.                 else
  99.                 {
  100.                     p = new treeNode;
  101.                     p->data = x;
  102.                     p->left = NULL;
  103.                     p->right = NULL;
  104.                     m->right = p;
  105.                     b = false;
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 if (m->left) m = m->left;
  111.                 else
  112.                 {
  113.                     p = new treeNode;
  114.                     p->data = x;
  115.                     p->left = NULL;
  116.                     p->right = NULL;
  117.                     m->left = p;
  118.                     b = false;
  119.                 }
  120.             }
  121.         }
  122.     }
  123.  
  124. }
  125. ////////////////////////////////////////////////////
  126. void push_stack(stackNode* &stack_top, treeNode* t)
  127. {
  128.     stackNode* p;
  129.     p = new stackNode;
  130.     p->node = t;
  131.     p->next = stack_top;
  132.     stack_top = p;
  133. }
  134. //////////////////////////////////////////////////////////////
  135. void create_tree(ifstream &f, treeNode* &tree_top)
  136. {
  137.     tree_top = NULL;
  138.     int x;
  139.     while (f >> x)
  140.     {
  141.         push_tree(tree_top, x);
  142.     }
  143. }
  144. /////////////////////////
  145. void create_treeChar(ifstream &f, treeNode* &tree_top)
  146. {
  147.     tree_top = NULL;
  148.     char x;
  149.     while (f >> x)
  150.     {
  151.         push_tree(tree_top, x);
  152.     }
  153. }
  154. /////////////////////////
  155. void pop_stack(stackNode* &stack_top, treeNode* &t)
  156. //get node of tree from stack
  157. {
  158.     stackNode *p;
  159.     if (stack_top)
  160.     {
  161.         t = stack_top->node;
  162.         p = stack_top->next;
  163.         delete stack_top;
  164.         stack_top = p;
  165.     }
  166. }
  167. /////////////////////////////////////////////////////////////
  168. void print_node(int x)
  169. {
  170.     cout << " " << x;
  171. }
  172. ////////////////////////////
  173. void inorder_tree(treeNode* tree_top)
  174. //simmetric obhod tree
  175. {
  176.     stackNode* stack_top;
  177.     treeNode* tree_p;
  178.     tree_p = tree_top;
  179.     stack_top = NULL;
  180.     do
  181.     {
  182.         while (tree_p)
  183.         {
  184.             while (tree_p->left)
  185.             {
  186.                 push_stack(stack_top, tree_p);
  187.                 tree_p = tree_p->left;
  188.             }
  189.             print_node(tree_p->data);
  190.             tree_p = tree_p->right;
  191.         }
  192.  
  193.         if (stack_top)
  194.         {
  195.             pop_stack(stack_top, tree_p);
  196.             print_node(tree_p->data);
  197.             tree_p = tree_p->right;
  198.         }
  199.     } while (stack_top || tree_p);
  200.     cout << endl;
  201. }
  202. //////////////////////////////////////
  203. void print_nodeChar(char x)
  204. {
  205.     cout << " " << x;
  206. }
  207. ////////////////////////////
  208. void inorder_treeChar(treeNode* tree_top)
  209. {
  210.     stackNode* stack_top;
  211.     treeNode* tree_p;
  212.     tree_p = tree_top;
  213.     stack_top = NULL;
  214.     do
  215.     {
  216.         while (tree_p)
  217.         {
  218.             while (tree_p->left)
  219.             {
  220.                 push_stack(stack_top, tree_p);
  221.                 tree_p = tree_p->left;
  222.             }
  223.             print_nodeChar(tree_p->data);
  224.             tree_p = tree_p->right;
  225.         }
  226.  
  227.         if (stack_top)
  228.         {
  229.             pop_stack(stack_top, tree_p);
  230.             print_nodeChar(tree_p->data);
  231.             tree_p = tree_p->right;
  232.         }
  233.     } while (stack_top || tree_p);
  234.     cout << endl;
  235. }
  236. /////////////////////////////////////
  237. void show(treeNode*r)
  238. {
  239.     if (r == NULL) return;
  240.     cout << r->data << " ";
  241.     show(r->left);
  242.     show(r->right);
  243. }
  244. ///////////////////////////////
  245. bool taskA(treeNode*r, int x)
  246. {
  247.     bool A;
  248.     if (r == NULL)  return NULL;
  249.     if (r->data == x) {
  250.         A = true; return A;
  251.     }
  252.     if (x <= r->data)
  253.     {
  254.         // left
  255.         if (r->left != NULL)
  256.             return taskA(r->left, x); // рекурсивный поиск влево
  257.         else
  258.         {
  259.             return NULL; // не найден
  260.         }
  261.     }
  262.     else
  263.     {
  264.         //right
  265.         if (r->right)
  266.             return taskA(r->right, x);// рекурсивный поиск вправо
  267.         else
  268.         {
  269.             return NULL; // не найден
  270.         }
  271.     }
  272. }
  273. ///////////////////////////////
  274. void taskB(treeNode* tree_top, int x, int &counter)
  275. {
  276.     if (tree_top != NULL)
  277.     {
  278.         taskB(tree_top->left, x, counter);
  279.         if (tree_top->data == x)
  280.             counter = counter + 1;
  281.         taskB(tree_top->right, x, counter);
  282.     }
  283. }
  284. ///////////////////////////////
  285. void taskV(treeNode*r, double &sum)
  286. {
  287.     if (r != NULL)
  288.     {
  289.         sum = sum + r->data;
  290.         taskV(r->left, sum);
  291.         taskV(r->right, sum);
  292.     }
  293. }
  294. ///////////////////////////////
  295. void taskG(treeNode*r, double &max)
  296. {
  297.     if (r != NULL)
  298.     {
  299.         if (r->data > max) max = r->data;
  300.         taskG(r->left, max);
  301.         taskG(r->right, max);
  302.     }
  303. }
  304. ///////////////////////////////
  305. void taskD(treeNode*r)
  306. {
  307.     if (r != NULL)
  308.     {
  309.         taskD(r->left);
  310.         if (r->left == NULL && r->right == NULL)
  311.         {
  312.             cout << r->data << " ";
  313.         }
  314.         taskD(r->right);
  315.     }
  316. }
  317. ///////////////////////////////
  318. void taskE(treeNode*r, int&x)
  319. {
  320.     if (r != NULL)
  321.     {
  322.         x++;
  323.         if ((r->left != NULL&&r->right != NULL)&&(r->right->right==NULL||r->right->left==NULL)) x--;
  324.         taskE(r->left, x);
  325.         taskE(r->right, x);
  326.     }
  327. }
  328. int max(int x, int y)
  329. {
  330.     if (x >= y) return x;
  331.     else return y;
  332. }
  333. ///////////////////////////////
  334. int taskJ(treeNode*r,  int n)
  335. {
  336.     if (r == NULL) return 0;
  337.     if (n == 0) return 1;
  338.     return(taskJ(r->left, n - 1) + taskJ(r->right, n - 1));
  339. }
  340.  
  341.  
  342. void main()
  343. {
  344.     ifstream f("Text.txt");
  345.     treeNode* tree_top;
  346.     create_tree(f, tree_top);
  347.     show(tree_top);
  348.     f.close();
  349.    
  350.     /*////////////////////
  351.     if (tree_top != NULL)
  352.     {
  353.         int x;
  354.         cout <<endl<< "Vvedite element = ";
  355.         cin >> x;
  356.         bool t = taskA(tree_top, x);
  357.         if (t) cout << "Element est v dereve" << endl;
  358.         else cout << "Elementa net v dereve" << endl;
  359.     }
  360.     /////////////////////
  361.     if (tree_top != NULL)
  362.     {
  363.         int x;
  364.         cout << endl << "Vvedite element = ";
  365.         cin >> x;
  366.         int k = 0;
  367.         taskB(tree_top, x, k);
  368.         cout << "Element vstrechaetsa v tekste "<<k<< " ras" << endl;
  369.     }
  370.     /*///////////////////
  371.     if (tree_top != NULL)
  372.     {
  373.         double sum = 0.0;
  374.         taskV(tree_top, sum);
  375.         cout <<endl<< "Summa vseh elementov dereva = " << sum << endl;
  376.     }
  377.     /*////////////////////
  378.     if (tree_top != NULL)
  379.     {
  380.         double max = tree_top->data;
  381.         taskG(tree_top, max);
  382.         cout <<endl<< "Maksimalni element = " << max << endl;
  383.     }
  384.     //////////////////////
  385.     if (tree_top != NULL)
  386.     {
  387.         cout << "Vse listi dereva: ";
  388.         taskD(tree_top);
  389.     }
  390.     /////////////////////
  391.     if (tree_top != NULL)
  392.     {
  393.         int x=0, y=0, ma;
  394.         taskE(tree_top->left, x);
  395.         taskE(tree_top->right, y);
  396.         ma=max(x, y);
  397.         cout <<endl<< "Max dlina vetvi = "<<ma<<endl;
  398.         cout << endl << x << " " << y;
  399.     }
  400.     /////////////////////
  401.     if (tree_top != NULL)
  402.     {
  403.         int x, n;
  404.         cout << endl<<"Vvedite nomer yrovna= ";
  405.         cin >> n;
  406.         x = taskJ(tree_top, n);
  407.         cout << "Kol-vo vershin na " << n << " yrovne = " << x  << endl;
  408.  
  409.     }
  410.     /*//////////
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement