Phoenix_x

Untitled

Apr 30th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 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.     system("pause");
  59. }
  60.  
  61.  
  62. //obhozdane prav red - корен, lqvo poddarvo dqsno poddarvo
  63. void obhozdane_tree(tree *t)
  64. {
  65.     if (t)
  66.     {
  67.         if (t->left != NULL) {
  68.             add_arc_graph(gr, t->key, t->left->key);
  69.         }
  70.  
  71.         if (t->right != NULL) {
  72.             add_arc_graph(gr, t->key, t->right->key);
  73.         }
  74.  
  75.         obhozdane_tree(t->left);
  76.         obhozdane_tree(t->right);
  77.     }
  78. }
  79.  
  80. void add_tree(int n, tree *&t) //dobavqne na listo
  81. {
  82.     if (t == NULL)
  83.     {
  84.         t = new tree;
  85.         t->key = n;
  86.         t->left = t->right = NULL;
  87.     }
  88.     else
  89.     {
  90.         if (t->key < n)
  91.         {
  92.             add_tree(n, t->right);
  93.         }
  94.         else
  95.         {
  96.             add_tree(n, t->left);
  97.         }
  98.     }
  99. }
  100.  
  101. // Инициализация
  102. void inti_graph(link *gr[n])
  103. {
  104.     for (int i = 0; i < n; i++)
  105.         gr[i] = NULL;
  106. }
  107.  
  108. //Търсене на връх
  109. int search_node_graph(link *gr[n], int c)
  110. {
  111.     int flag = 0;
  112.  
  113.     for (int i = 0; i < n; i++)
  114.     {
  115.         if (gr[i])
  116.         {
  117.             if (gr[i]->key == c)
  118.                 flag = 1;
  119.         }
  120.     }
  121.     return flag;
  122. }
  123.  
  124. //Търсене на дъга
  125. int search_arc_graph(link *gr[n], int c1, int c2)
  126. {
  127.     int flag = 0;
  128.  
  129.     if (search_node_graph(gr, c1) && search_node_graph(gr, c2))
  130.     {
  131.         int i = 0;
  132.  
  133.         while (gr[i]->key != c1)
  134.             i++;
  135.  
  136.         link *p = gr[i];
  137.  
  138.         while (p->key != c2 && p->next != NULL)
  139.             p = p->next;
  140.  
  141.         if (p->key == c2)
  142.             flag = 1;
  143.     }
  144.  
  145.     return flag;
  146. }
  147.  
  148. //Добавяне на връх
  149. void add_node_graph(link *gr[n], int c)
  150. {
  151.     if (search_node_graph(gr, c))
  152.         cout << "Sashtestvuva vruh s tazi stojnost" << endl;
  153.     else
  154.     {
  155.         int i = 0;
  156.         while (gr[i] && i < n)
  157.             i++;
  158.  
  159.         if (gr[i] == NULL)
  160.         {
  161.             gr[i] = new link();
  162.             gr[i]->key = c;
  163.             gr[i]->next = NULL;
  164.         }
  165.         else
  166.             cout << "Prepalvane na strukturata" << endl;
  167.     }
  168. }
  169.  
  170. //добавяне на дъга
  171. void add_arc_graph(link *gr[n], int c1, int c2)
  172. {
  173.     if (search_arc_graph(gr, c1, c2))
  174.         cout << "Dagata veche sashtestvuca";
  175.     else
  176.     {
  177.         if (!search_node_graph(gr, c1))
  178.             add_node_graph(gr, c1);
  179.         if (!search_node_graph(gr, c2))
  180.             add_node_graph(gr, c2);
  181.  
  182.         int i = 0;
  183.         while (gr[i]->key != c1)
  184.             i++;
  185.  
  186.         link *p = new link;
  187.         p->key = c2;
  188.         p->next = gr[i]->next;
  189.         gr[i]->next = p;
  190.     }
  191. }
  192.  
  193. void print_graph(link *gr[n])
  194. {
  195.     for (int i = 0; i < n; i++)
  196.     {
  197.         if (gr[i])
  198.         {
  199.             link *p = gr[i];
  200.  
  201.             while (p)
  202.             {
  203.                 cout << p->key << "\t";
  204.                 p = p->next;
  205.             }
  206.  
  207.             cout << endl;
  208.         }
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment