Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. // Lab3.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include  <stdlib.h>
  10. #include  <stdio.h>
  11. #include  <string.h>
  12. #include <fstream>
  13. #include "time.h"
  14.  
  15. using namespace std;
  16.  
  17. class BST
  18. {
  19.     struct node
  20.     {
  21.         int data;
  22.         node* left;
  23.         node* right;
  24.         char tab[10];
  25.     };
  26.  
  27.     node* root;
  28.  
  29.     node* makeEmpty(node* t)
  30.     {
  31.         if (t == NULL)
  32.             return NULL;
  33.         {
  34.             makeEmpty(t->left);
  35.             makeEmpty(t->right);
  36.             delete t;
  37.         }
  38.         return NULL;
  39.     }
  40.  
  41.     node* insert(int x, node* t)
  42.     {
  43.         if (t == NULL)
  44.         {
  45.             t = new node;
  46.             t->data = x;
  47.             t->left = t->right = NULL;
  48.         }
  49.         else if (x < t->data)
  50.             t->left = insert(x, t->left);
  51.         else if (x > t->data)
  52.             t->right = insert(x, t->right);
  53.         return t;
  54.     }
  55.  
  56.     node* findMin(node* t)
  57.     {
  58.         if (t == NULL)
  59.             return NULL;
  60.         else if (t->left == NULL)
  61.             return t;
  62.         else
  63.             return findMin(t->left);
  64.     }
  65.  
  66.     node* findMax(node* t)
  67.     {
  68.         if (t == NULL)
  69.             return NULL;
  70.         else if (t->right == NULL)
  71.             return t;
  72.         else
  73.             return findMax(t->right);
  74.     }
  75.  
  76.     node* remove(int x, node* t)
  77.     {
  78.         node* temp;
  79.         if (t == NULL)
  80.             return NULL;
  81.         else if (x < t->data)
  82.             t->left = remove(x, t->left);
  83.         else if (x > t->data)
  84.             t->right = remove(x, t->right);
  85.         else if (t->left && t->right)
  86.         {
  87.             temp = findMin(t->right);
  88.             t->data = temp->data;
  89.             t->right = remove(t->data, t->right);
  90.         }
  91.         else
  92.         {
  93.             temp = t;
  94.             if (t->left == NULL)
  95.                 t = t->right;
  96.             else if (t->right == NULL)
  97.                 t = t->left;
  98.             delete temp;
  99.         }
  100.  
  101.         return t;
  102.     }
  103.  
  104.     void inorder(node* t)
  105.     {
  106.         if (t == NULL)
  107.             return;
  108.         inorder(t->left);
  109.         cout << t->data << " " << endl;;
  110.         inorder(t->right);
  111.        
  112.        
  113.     }
  114.  
  115.     node* find(node* t, int x)
  116.     {
  117.         if (t == NULL)
  118.             return NULL;
  119.         else if (x < t->data)
  120.             return find(t->left, x);
  121.         else if (x > t->data)
  122.             return find(t->right, x);
  123.         else
  124.             return t;
  125.     }
  126.     void Preorder(node* t)
  127.     {
  128.         if (t == NULL)
  129.             return;
  130.  
  131.    
  132.         cout << t->data << " " << endl ;
  133.         Preorder(t->left);
  134.         Preorder(t->right);
  135.     }
  136.  
  137. public:
  138.     BST()
  139.     {
  140.         root = NULL;
  141.     }
  142.  
  143.     ~BST()
  144.     {
  145.         root = makeEmpty(root);
  146.     }
  147.  
  148.     void insert(int x)
  149.     {
  150.         root = insert(x, root);
  151.     }
  152.  
  153.     void remove(int x)
  154.     {
  155.         if (root = NULL) {
  156.             cout << "Nie da się" << endl;
  157.         }
  158.         else {
  159.             root = remove(x, root);
  160.         }
  161.     }
  162.  
  163.     void displayI()
  164.     {
  165.         inorder(root);
  166.         cout << endl;
  167.     }
  168.     void displayP()
  169.     {
  170.         Preorder(root);
  171.         cout << endl;
  172.     }
  173.  
  174.     void search(int x)
  175.     {
  176.         root = find(root, x);
  177.        
  178.     }
  179. };
  180.  
  181. int main()
  182. {
  183.     clock_t begin, end;
  184.     double time_spent;
  185.     begin = clock();
  186.     int X, k1, k2, k3, k4, k5;
  187.     ifstream fin("inlab03.txt", ios::in | ios::out);
  188.     fin >> X >> k1 >> k2 >> k3 >> k4 >> k5;
  189.    
  190.    
  191.    
  192.    
  193.     BST t;
  194.     t.remove(k1);
  195.     t.insert(k1);
  196.     int i = 0;
  197.     for (int i = 0; i < X; i++) {
  198.         srand(time(NULL));
  199.         int j = 0;
  200.         while (j < X) {
  201.  
  202.             int y = rand() % 20001 - 9999;
  203.             t.insert(y);
  204.            
  205.             j++;
  206.         }
  207.     }
  208.  
  209.     cout << "-=======1========-" << endl;
  210.     t.displayI();
  211.     cout << "-=======2========-" << endl;
  212.     t.displayP();
  213.    
  214.     t.insert(k2);
  215.     cout << "-=======3========-" << endl;
  216.     t.displayI();
  217.    
  218.     t.insert(k3);
  219.  
  220.     t.insert(k4);
  221.  
  222.     t.remove(k1);
  223.     cout << "-=======4========-" << endl;
  224.     t.displayP();
  225.    
  226.     //t.search(k1);
  227.  
  228.  
  229.     t.remove(k2);
  230.     cout << "-=======5========-" << endl;
  231.     t.displayI();
  232.    
  233.     t.remove(k3);
  234.  
  235.     t.remove(k4);
  236.    
  237.     end = clock();
  238.     time_spent = (double)(end-begin) / CLOCKS_PER_SEC;
  239.     cout << time_spent << endl;
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement