Phoenix_x

Untitled

Apr 30th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct tree {
  7.     int key;
  8.     tree* left;
  9.     tree* right;
  10. } *root = NULL;
  11.  
  12. const int n = 10;//максимум брой елементи в граф
  13.  
  14. struct link //spisak ot grafa
  15. {
  16.     int key;
  17.     link *next;
  18. }
  19. *gr[n]; //масив от указатели
  20.  
  21.  
  22. void add_tree(int n, tree *&t);
  23. void obhozdane_tree(tree *t);
  24.  
  25. void inti_graph(link *gr[n]);
  26. int search_node_graph(link *gr[n], int c);
  27. int search_arc_graph(link *gr[n], int c1, int c2);
  28. void add_node_graph(link *gr[n], int c);
  29. void add_arc_graph(link *gr[n], int c1, int c2);
  30. void print_graph(link *gr[n]);
  31.  
  32. void main()
  33. {
  34.     inti_graph(gr);
  35.  
  36.     int broj;
  37.     cout << "Broj elementi na darvoto: ";
  38.     cin >> broj;
  39.  
  40.     while (broj < 1) { //ако потребителя въведе число по-малко от 1 за брой, накарай го да въвежда пак число
  41.         cout << "Nevaliden broj" << endl;
  42.         cout << "Broj elementi na darvoto: ";
  43.         cin >> broj;
  44.     }
  45.  
  46.     int number;
  47.     cout << "Vavedete elementi na darvoto: " << endl;
  48.  
  49.     for (int i = 0; i < broj; i++)
  50.     {
  51.         cin >> number;
  52.         add_tree(number, root);
  53.     }
  54.  
  55.     obhozdane_tree(root);//конвертра дървото в граф
  56.     cout << "Spisacite na grafa: " << endl;
  57.     print_graph(gr);
  58. }
  59.  
  60.  
  61. //obhozdane prav red - корен, lqvo poddarvo dqsno poddarvo
  62. void obhozdane_tree(tree *t)
  63. {
  64.     if (t)
  65.     {
  66.         if (t->left != NULL) {
  67.             add_arc_graph(gr, t->key, t->left->key);
  68.         }
  69.  
  70.         if (t->right != NULL) {
  71.             add_arc_graph(gr, t->key, t->right->key);
  72.         }
  73.  
  74.         obhozdane_tree(t->left);
  75.         obhozdane_tree(t->right);
  76.     }
  77. }
  78.  
  79. void add_tree(int n, tree *&t) //dobavqne na listo
  80. {
  81.     if (t == NULL)
  82.     {
  83.         t = new tree;
  84.         t->key = n;
  85.         t->left = t->right = NULL;
  86.     }
  87.     else
  88.     {
  89.         if (t->key < n)
  90.         {
  91.             add_tree(n, t->right);
  92.         }
  93.         else
  94.         {
  95.             add_tree(n, t->left);
  96.         }
  97.     }
  98. }
  99.  
  100. // Инициализация
  101. void inti_graph(link *gr[n])
  102. {
  103.     for (int i = 0; i < n; i++)
  104.         gr[i] = NULL;
  105. }
  106.  
  107. //Търсене на връх
  108. int search_node_graph(link *gr[n], int c)
  109. {
  110.     int flag = 0;
  111.  
  112.     for (int i = 0; i < n; i++)
  113.     {
  114.         if (gr[i])
  115.         {
  116.             if (gr[i]->key == c)
  117.                 flag = 1;
  118.         }
  119.     }
  120.     return flag;
  121. }
  122.  
  123. //Търсене на дъга
  124. int search_arc_graph(link *gr[n], int c1, int c2)
  125. {
  126.     int flag = 0;
  127.  
  128.     if (search_node_graph(gr, c1) && search_node_graph(gr, c2))
  129.     {
  130.         int i = 0;
  131.  
  132.         while (gr[i]->key != c1)
  133.             i++;
  134.  
  135.         link *p = gr[i];
  136.  
  137.         while (p->key != c2 && p->next != NULL)
  138.             p = p->next;
  139.  
  140.         if (p->key == c2)
  141.             flag = 1;
  142.     }
  143.  
  144.     return flag;
  145. }
  146.  
  147. //Добавяне на връх
  148. void add_node_graph(link *gr[n], int c)
  149. {
  150.     if (search_node_graph(gr, c))
  151.         cout << "Sashtestvuva vruh s tazi stojnost" << endl;
  152.     else
  153.     {
  154.         int i = 0;
  155.         while (gr[i] && i < n)
  156.             i++;
  157.  
  158.         if (gr[i] == NULL)
  159.         {
  160.             gr[i] = new link();
  161.             gr[i]->key = c;
  162.             gr[i]->next = NULL;
  163.         }
  164.         else
  165.             cout << "Prepalvane na strukturata" << endl;
  166.     }
  167. }
  168.  
  169. //добавяне на дъга
  170. void add_arc_graph(link *gr[n], int c1, int c2)
  171. {
  172.     if (search_arc_graph(gr, c1, c2))
  173.         cout << "Dagata veche sashtestvuca";
  174.     else
  175.     {
  176.         if (!search_node_graph(gr, c1))
  177.             add_node_graph(gr, c1);
  178.         if (!search_node_graph(gr, c2))
  179.             add_node_graph(gr, c2);
  180.  
  181.         int i = 0;
  182.         while (gr[i]->key != c1)
  183.             i++;
  184.  
  185.         link *p = new link;
  186.         p->key = c2;
  187.         p->next = gr[i]->next;
  188.         gr[i]->next = p;
  189.     }
  190. }
  191.  
  192. void print_graph(link *gr[n])
  193. {
  194.     for (int i = 0; i < n; i++)
  195.     {
  196.         if (gr[i])
  197.         {
  198.             link *p = gr[i];
  199.  
  200.             while (p)
  201.             {
  202.                 cout << p->key << "\t";
  203.                 p = p->next;
  204.             }
  205.  
  206.             cout << endl;
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment