Advertisement
vkichukova

Untitled

Feb 3rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. #include <sstream>
  7. #include <queue>
  8. #include <time.h>
  9.  
  10. using namespace std;
  11.  
  12. struct Participant
  13. {
  14.     string name;
  15.     int coefficient;
  16. };
  17.  
  18. template<class T>
  19. struct Node
  20. {
  21.     int a;
  22.     Node<T> *left, *right;
  23.     int win;
  24.     Participant p;
  25.     Node(Participant a)
  26.     {
  27.         p.coefficient = a.coefficient;
  28.         p.name = a.name;
  29.         left = NULL;
  30.         right = NULL;
  31.     }
  32.     Node()
  33.     {
  34.         left = NULL;
  35.         right = NULL;
  36.     }
  37.     Node(const Node<T>& n)
  38.     {
  39.         this->p.coefficient = n.p.coefficient;
  40.         this->p.name = n.p.name;
  41.         left = n->left;
  42.         right = n->right;
  43.     }
  44.     Node& operator=(const Node<T>& n)
  45.     {
  46.         if (this != &n)
  47.         {
  48.             delete left;
  49.             delete right;
  50.             left = new Node<T>;
  51.             right = new Node<T>;
  52.             this->p.coefficient = n.p.coefficient;
  53.                 this->p.name = n.p.name;
  54.             left = n->left;
  55.             right = n->right;
  56.         }
  57.         return *this;
  58.     }
  59. };
  60.  
  61. template<class T>
  62. class Tree
  63. {
  64.     Node<T>* root;
  65.     queue<Node<T>*> q;
  66.     void printHelpBFS(Node<T>* _root);
  67.     void printHelpDFS(Node<T>* _root);
  68.  
  69. public:
  70.     Tree();
  71.     void addNode(Node<T> *);
  72.     void createTree();
  73.     void delTree(Node<T> *_root);
  74.     void printBFS();
  75.     void printDFS();
  76.     Node<T>* getRoot() const;
  77.     // task 5
  78.     // task 6
  79.     // task 7
  80.     int tournamentHelper(Node<T>* root); //finds the winner and returns his "winning number"
  81.     string tournament();
  82.     string name(int n, Node<T>* _root); //finds the winners' name by his "winning number"
  83.                                         // task 8
  84. };
  85.  
  86. template <class T>
  87. Tree<T>::Tree()
  88. {
  89.     root = NULL;
  90. }
  91.  
  92. template <class T>
  93. Node<T>* Tree<T>::getRoot() const
  94. {
  95.     return root;
  96. }
  97.  
  98. template<class T>
  99. void Tree<T>::printHelpBFS(Node<T>* _root)
  100. {
  101.     if (_root)
  102.     {
  103.         queue<Node<T>*> q;
  104.         q.push(_root);
  105.         while (!q.empty())
  106.         {
  107.             Node<T> *helper = q.front();
  108.             q.pop();
  109.             cout << helper->p.name << " " << helper->p.coefficient << endl;
  110.             if (helper->left)
  111.                 q.push(helper->left);
  112.             if (helper->right)
  113.                 q.push(helper->right);
  114.         }
  115.     }
  116. }
  117.  
  118. template<class T>
  119. void Tree<T>::printHelpDFS(Node<T>* _root)
  120. {
  121.     if (_root)
  122.     {
  123.         cout << _root->p.name << " " << _root->p.coefficient;
  124.         printHelpDFS(_root->left);
  125.         printHelpDFS(_root->right);
  126.     }
  127. }
  128.  
  129. template<class T>
  130. void Tree<T>::addNode(Node<T>* newNode)
  131. {
  132.     if (!root)
  133.     {
  134.         root = newNode;
  135.         q.push(root);
  136.         return;
  137.     }
  138.  
  139.     Node<T> *n = q.front();
  140.     if (n->left && n->right)
  141.     {
  142.         q.pop();
  143.         n = q.front();
  144.     }
  145.     if (!n->left)
  146.         n->left = newNode;
  147.     else if (!n->right)
  148.         n->right = newNode;
  149.  
  150.     q.push(newNode);
  151. }
  152.  
  153.  
  154. template<class T>
  155. void Tree<T>::createTree()
  156. {
  157.     char c;
  158.     do
  159.     {
  160.         int x;
  161.         cout << "Element: "; cin >> x;
  162.         addNode(new Node<T>(x));
  163.  
  164.         cout << "New element y/n? "; cin >> c;
  165.     } while (c == 'y');
  166. }
  167.  
  168. template<class T>
  169. void Tree<T>::delTree(Node<T>* _root)
  170. {
  171.     if (_root)
  172.     {
  173.         delTree(_root->left);
  174.         delTree(_root->right);
  175.         delete _root;
  176.         _root = nullptr;
  177.     }
  178. }
  179.  
  180. template<class T>
  181. void Tree<T>::printBFS()
  182. {
  183.     printHelpBFS(this->root);
  184. }
  185.  
  186. template<class T>
  187. void Tree<T>::printDFS()
  188. {
  189.     printHelpDFS(this->root);
  190. }
  191. //task 5 --------------------
  192.  
  193. bool inVector(vector<int> arr, const int element)
  194. {
  195.     vector<int>::iterator it = find(arr.begin(), arr.end(), element);
  196.     if (it != arr.end())
  197.         return true;
  198.  
  199.     return false;
  200. }
  201.  
  202. template<class T>
  203. void fillTree(vector<T> leaves, Tree<T>& tree)
  204. {
  205.     int size = leaves.size();
  206.     vector<int> usedPositions;
  207.     for (int i = 0; i < size; i++)
  208.         usedPositions.push_back(-1);
  209.  
  210.     int i = 0;
  211.     srand(time(NULL));
  212.     while (i < leaves.size())
  213.     {
  214.         int position = rand() % leaves.size();
  215.         if (!inVector(usedPositions, position))
  216.         {
  217.             usedPositions[i] = position;
  218.             i++;
  219.             Node<T>* nd = new Node<T>(leaves[position]);
  220.             tree.addNode(nd);
  221.         }
  222.     }
  223. }
  224. //------------- Task 6 ----------
  225.  
  226. vector<Participant> readParticipants(ifstream& file)
  227. {
  228.     file.open("p.txt");
  229.  
  230.     if (!file)
  231.     {
  232.         cerr << "File couldn't be opened!" << endl;
  233.         exit(1);
  234.     }
  235.  
  236.     vector<Participant> allParticipants;
  237.     while (!file.eof())
  238.     {
  239.         Participant a;
  240.  
  241.         while (file.peek() != ' ' && file.peek() != '\t')
  242.         {
  243.             char c;
  244.             file >> c;
  245.             a.name += c;
  246.         }
  247.  
  248.         while (file.peek() == ' ' || file.peek() == '\t')
  249.         {
  250.             file.get();
  251.         }
  252.  
  253.         string sCoefficient;
  254.         getline(file, sCoefficient); // !!! now p.coefficient is string
  255.  
  256.  
  257.  
  258.         stringstream ss(sCoefficient);
  259.         ss >> a.coefficient;
  260.         allParticipants.push_back(a);
  261.     }
  262.  
  263.     file.close();
  264.  
  265.     // get the size of the vector
  266.     int n = allParticipants.size();
  267.     // get random number
  268.     srand(time(NULL));
  269.     int random = rand() % n;
  270.     // get random participants and add them to the result vector
  271.     vector<Participant> randomParticipants;
  272.     int index;
  273.     for (int i = 0; i < random; i++)
  274.     {
  275.         index = rand() % n;
  276.         randomParticipants.push_back(allParticipants[i]);
  277.     }
  278.  
  279.     return randomParticipants;
  280.  
  281. }
  282. //--------- Task 7
  283.  
  284. template<class T>
  285. int Tree<T>::tournamentHelper(Node<T>* root)
  286. {
  287.     if (root == NULL)
  288.     {
  289.         return 0;
  290.     }
  291.     if (root->left == NULL && root->right == NULL) {
  292.         int k = rand() % (root->p.coefficient * 2 + 1) + root->p.coefficient*(-1);
  293.         root->win = k;
  294.         return k;
  295.     }
  296.  
  297.     int firstNumber = tournamentHelper(root->left);
  298.  
  299.     int secondNumber = tournamentHelper(root->right);
  300.  
  301.     if (firstNumber>secondNumber)
  302.         return firstNumber;
  303.     return secondNumber;
  304. }
  305.  
  306. template <class T>
  307. string Tree<T>::name(int n, Node<T>* _root)
  308. {
  309.     if (_root)
  310.     {
  311.         queue<Node<T>*> q;
  312.         q.push(_root);
  313.         while (!q.empty())
  314.         {
  315.             Node<T> *helper = q.front();
  316.             q.pop();
  317.             if (helper->win == n)
  318.             {
  319.                 return helper->p.name;
  320.             }
  321.             if (helper->left)
  322.                 q.push(helper->left);
  323.             if (helper->right)
  324.                 q.push(helper->right);
  325.         }
  326.     }
  327. }
  328.  
  329. template<class T>
  330. string Tree<T>::tournament()
  331. {
  332.     int n = tournamentHelper(root);
  333.     return name(n, root);
  334. }
  335. //--------------- Task 8
  336. template <class T>
  337. void findpath(string& _name, Node<T>* root)
  338. {
  339.     if (root == NULL)
  340.     {
  341.         return;
  342.     }
  343.     if (_name.empty())
  344.     {
  345.         return;
  346.     }
  347.  
  348.     if (root->left == NULL && root->right == NULL)
  349.         return;
  350.  
  351.     if (root->left->p.name == _name || root->right->p.name == _name)
  352.     {
  353.         cout << root->left->p.name << " VS " << root->right->p.name << endl;
  354.         if (root->left->p.name == _name)
  355.         {
  356.             findpath(_name, root->left);
  357.         }
  358.         else
  359.         {
  360.             findpath(_name, root->right);
  361.         }
  362.     }
  363.  
  364.     else
  365.     {
  366.         findpath(_name, root->left);
  367.         findpath(_name, root->right);
  368.     }
  369.  
  370. }
  371. int main()
  372. {
  373.     ifstream file;
  374.     vector<Participant> a;
  375.     a = readParticipants(file);
  376.  
  377.     Tree<Participant> t;
  378.  
  379.     fillTree(a, t);
  380.     cout << t.tournament() << endl;
  381.  
  382.     system("pause");
  383.     return 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement