Facebook
From Chunky Baboon, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 130
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstring>
  4. #include<chrono>
  5. using namespace std;
  6. class BST {
  7.  
  8.     struct node {
  9.         string data;
  10.         int ilosc;
  11.         node* left;
  12.         node* right;
  13.     };
  14.     int counter = 0;
  15.     int maxdepth=0;
  16.     string randsearch="";
  17.     node* root;
  18.  
  19.     node* makeEmpty(node* t) {
  20.         if(t == NULL)
  21.             return NULL;
  22.         {
  23.             makeEmpty(t->left);
  24.             makeEmpty(t->right);
  25.             delete t;
  26.         }
  27.         return NULL;
  28.     }
  29.  
  30.     node* insert(string x, int ile, node* t)
  31.     {
  32.         if(t == NULL)
  33.         {
  34.             t = new node;
  35.             t->data = x;
  36.             t->ilosc = ile;
  37.             t->left = t->right = NULL;
  38.         }
  39.         else if(x < t->data)
  40.             t->left = insert(x, ile, t->left);
  41.         else if(x > t->data)
  42.             t->right = insert(x, ile, t->right);
  43.         else if(x == t->data){
  44.             t->ilosc++;
  45.     //        cout<<t->ilosc<<" ";
  46.         }
  47.         return t;
  48.     }
  49.   void inorder(node* t) {
  50.         if(t == NULL)
  51.             return;
  52.         inorder(t->left);
  53.         cout << t->data << " "<<endl;
  54.         inorder(t->right);
  55.     }
  56.  
  57.     node* find(node* t, string x) {
  58.             counter++;
  59.         if(t == NULL)
  60.         {
  61.             cout<<"brak slowa"<<endl;
  62.             return NULL;
  63.         }
  64.         else if (x < t->data)
  65.             return find(t->left, x);
  66.         else if(x > t->data)
  67.             return find(t->right, x);
  68.         else if(x == t->data)
  69.         {
  70.             cout<<t->data<<" jest "<<t->ilosc<<" razy"<<endl;
  71.         }
  72.             return t;
  73.     }
  74.     node* randnod(node* root) {
  75.     srand(time(0));
  76.  
  77.     int counte = rand() % (2);
  78.     if (!root)
  79.         cout<<"no";
  80.  
  81.     else
  82.         cout<<root->data;
  83.     }
  84.  
  85. node* maxDepth(node* node, int x)
  86. {
  87.     if (node == NULL)
  88.     {
  89.         if(x>maxdepth)maxdepth=x;
  90.         return 0;
  91.     }
  92.     else
  93.     {
  94.         x++;
  95.         maxDepth(node->left, x);
  96.         maxDepth(node->right, x);
  97.     }
  98. }
  99. public:
  100.     BST() {
  101.         root = NULL;
  102.     }
  103.  
  104.     ~BST() {root = makeEmpty(root);}
  105.     void insert(string x, int ile) {root = insert(x, ile, root);}
  106.  //   void remove(int x) {root = remove(x, root);}
  107.     void display() {
  108.         inorder(root);
  109.         cout << endl;}
  110.     void search(string x) {find(root, x);}
  111.     void insertow() {cout<<"Znaleziono wykonujac "<<counter<<" Operacji"<<endl;}
  112. int maxDepth(node* node)
  113. {
  114.     if (node == NULL)
  115.         return 0;
  116.     else
  117.     {
  118.         int lDepth = maxDepth(node->left);
  119.         int rDepth = maxDepth(node->right);
  120.  
  121.         if (lDepth > rDepth)
  122.             return(lDepth + 1);
  123.         else return(rDepth + 1);
  124.     }
  125. }
  126.  
  127.     void depth(){int x=0;
  128. root = maxDepth(root, x);cout<<maxdepth<<endl;}
  129. };
  130.  
  131. int main() {
  132.     fstream tekst;
  133.     BST t;
  134.      tekst.open("joyland.txt", ios::in);
  135.     string slowo;
  136.     int ile=1;
  137.     auto t1 = std::chrono::high_resolution_clock::now();
  138.     while(tekst >> slowo){t.insert(slowo, 1);}
  139.     auto t2 = std::chrono::high_resolution_clock::now();
  140.     double durat = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  141.     cout<<"W czasie "<<durat<<" mikrosekund"<<endl;
  142.     tekst.close();
  143. //  t.display();
  144.     cout<<"Szukanie liczb: (wpisz 'stop' aby przerwac i wyswietlic wysokosc drzewa)"<<endl;
  145.     while(true)
  146.     {
  147.         string szukamy;
  148.         cout<<"Co mam znalezc?"<<endl;
  149.         cin>>szukamy;
  150.         if(szukamy=="stop") break;
  151.         t.search(szukamy);
  152.     }
  153.     t.insertow();
  154.     cout << "Najwieksza wysokosc drzewa: ";
  155.     t.depth();
  156.     return 0;
  157. }
  158.