Facebook
From Abrupt Sheep, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 146
  1. #include <iostream>
  2. #include<fstream>
  3. #include<cstring>
  4. #include<chrono>
  5. using namespace std;
  6. struct Node {
  7.         string data;
  8.         int ile=1;
  9.         Node *parent;
  10.         Node *left;
  11.         Node *right;
  12. };
  13.  
  14. typedef Node *NodePtr;
  15. class SplayTree {
  16. private:
  17.     int maxdepth=0;
  18.     int counter = 0;
  19.         NodePtr root;
  20.  
  21.         void preOrderHelper(NodePtr node) {
  22.                 if (node != nullptr) {
  23.                         cout<<node->data<<" ";
  24.                         preOrderHelper(node->left);
  25.                         preOrderHelper(node->right);
  26.                 }
  27.         }
  28.  
  29.         void inOrderHelper(NodePtr node) {
  30.                 if (node != nullptr) {
  31.                         inOrderHelper(node->left);
  32.                         cout<<node->data<<" ";
  33.                         inOrderHelper(node->right);
  34.                 }
  35.         }
  36.  
  37.         void postOrderHelper(NodePtr node) {
  38.                 if (node != nullptr) {
  39.                         postOrderHelper(node->left);
  40.                         postOrderHelper(node->right);
  41.                         cout<<node->data<<" ";
  42.                 }
  43.         }
  44.  
  45.         NodePtr searchTreeHelper(NodePtr node, string key) {
  46.             counter++;
  47.                 if (node == nullptr || key == node->data) {
  48.                         return node;
  49.                 }
  50.  
  51.                 if (key < node->data) {
  52.                         return searchTreeHelper(node->left, key);
  53.                 }
  54.                 return searchTreeHelper(node->right, key);
  55.         }
  56.  
  57.         void deleteNodeHelper(NodePtr node, string key) {
  58.                 NodePtr x = nullptr;
  59.                 NodePtr t, s;
  60.                 while (node != nullptr){
  61.                         if (node->data == key) {
  62.                                 x = node;
  63.                         }
  64.  
  65.                         if (node->data <= key) {
  66.                                 node = node->right;
  67.                         } else {
  68.                                 node = node->left;
  69.                         }
  70.                 }
  71.  
  72.                 if (x == nullptr) {
  73.                         cout<<"Couldn't find key in the tree"<<endl;
  74.                         return;
  75.                 }
  76.                 split(x, s, t);
  77.                 if (s->left){
  78.                         s->left->parent = nullptr;
  79.                 }
  80.                 root = join(s->left, t);
  81.                 delete(s);
  82.                 s = nullptr;
  83.         }
  84.  
  85.         void printHelper(NodePtr root, string indent, bool last) {
  86.                 if (root != nullptr) {
  87.                    cout<<indent;
  88.  
  89.                    cout<<root->data<<endl;
  90.  
  91.                    printHelper(root->left, indent, false);
  92.                    printHelper(root->right, indent, true);
  93.                 }
  94.         }
  95.  
  96.         void leftRotate(NodePtr x) {
  97.                 NodePtr y = x->right;
  98.                 x->right = y->left;
  99.                 if (y->left != nullptr) {
  100.                         y->left->parent = x;
  101.                 }
  102.                 y->parent = x->parent;
  103.                 if (x->parent == nullptr) {
  104.                         this->root = y;
  105.                 } else if (x == x->parent->left) {
  106.                         x->parent->left = y;
  107.                 } else {
  108.                         x->parent->right = y;
  109.                 }
  110.                 y->left = x;
  111.                 x->parent = y;
  112.         }
  113.  
  114.         void rightRotate(NodePtr x) {
  115.                 NodePtr y = x->left;
  116.                 x->left = y->right;
  117.                 if (y->right != nullptr) {
  118.                         y->right->parent = x;
  119.                 }
  120.                 y->parent = x->parent;
  121.                 if (x->parent == nullptr) {
  122.                         this->root = y;
  123.                 } else if (x == x->parent->right) {
  124.                         x->parent->right = y;
  125.                 } else {
  126.                         x->parent->left = y;
  127.                 }
  128.                 y->right = x;
  129.                 x->parent = y;
  130.         }
  131.  
  132.         // splaying
  133.         void splay(NodePtr x) {
  134.                 while (x->parent) {
  135.                         if (!x->parent->parent) {
  136.                                 if (x == x->parent->left) {
  137.                                         // zig rotation
  138.                                         rightRotate(x->parent);
  139.                                 } else {
  140.                                         // zag rotation
  141.                                         leftRotate(x->parent);
  142.                                 }
  143.                         } else if (x == x->parent->left && x->parent == x->parent->parent->left) {
  144.                                 // zig-zig rotation
  145.                                 rightRotate(x->parent->parent);
  146.                                 rightRotate(x->parent);
  147.                         } else if (x == x->parent->right && x->parent == x->parent->parent->right) {
  148.                                 // zag-zag rotation
  149.                                 leftRotate(x->parent->parent);
  150.                                 leftRotate(x->parent);
  151.                         } else if (x == x->parent->right && x->parent == x->parent->parent->left) {
  152.                                 // zig-zag rotation
  153.                                 leftRotate(x->parent);
  154.                                 rightRotate(x->parent);
  155.                         } else {
  156.                                 // zag-zig rotation
  157.                                 rightRotate(x->parent);
  158.                                 leftRotate(x->parent);
  159.                         }
  160.                 }
  161.         }
  162.  
  163.         // joins two trees s and t
  164.         NodePtr join(NodePtr s, NodePtr t){
  165.                 if (!s) {
  166.                         return t;
  167.                 }
  168.  
  169.                 if (!t) {
  170.                         return s;
  171.                 }
  172.                 NodePtr x = maximum(s);
  173.                 splay(x);
  174.                 x->right = t;
  175.                 t->parent = x;
  176.                 return x;
  177.         }
  178.  
  179.         // splits the tree into s and t
  180.         void split(NodePtr &x, NodePtr &s, NodePtr &t) {
  181.                 splay(x);
  182.                 if (x->right) {
  183.                         t = x->right;
  184.                         t->parent = nullptr;
  185.                 } else {
  186.                         t = nullptr;
  187.                 }
  188.                 s = x;
  189.                 s->right = nullptr;
  190.                 x = nullptr;
  191.         }
  192. NodePtr maxDepth(NodePtr node, int g)
  193. {
  194.     if (node == NULL)
  195.     {
  196.         if(g>maxdepth)maxdepth=g;
  197.         return 0;
  198.     }
  199.     else
  200.     {
  201.         g++;
  202.         maxDepth(node->left, g);
  203.         maxDepth(node->right, g);
  204.     }
  205. }
  206. public:
  207.         SplayTree() {
  208.                 root = nullptr;
  209.         }
  210.  
  211.         void preorder() {
  212.                 preOrderHelper(this->root);
  213.         }
  214.  
  215.         void inorder() {
  216.                 inOrderHelper(this->root);
  217.         }
  218.  
  219.         void postorder() {
  220.                 postOrderHelper(this->root);
  221.         }
  222.  
  223.         NodePtr searchTree(string k) {
  224.                 NodePtr x = searchTreeHelper(this->root, k);
  225.                 if (x) {
  226.                         splay(x);
  227.                 }
  228.                 if(x==0) cout<<"brak";
  229.                 else cout<<"jest";
  230.                 return x;
  231.         }
  232.  
  233.         NodePtr minimum(NodePtr node) {
  234.                 while (node->left != nullptr) {
  235.                         node = node->left;
  236.                 }
  237.                 return node;
  238.         }
  239.  
  240.         NodePtr maximum(NodePtr node) {
  241.                 while (node->right != nullptr) {
  242.                         node = node->right;
  243.                 }
  244.                 return node;
  245.         }
  246.  
  247.         NodePtr successor(NodePtr x) {
  248.                 if (x->right != nullptr) {
  249.                         return minimum(x->right);
  250.                 }
  251.  
  252.                 NodePtr y = x->parent;
  253.                 while (y != nullptr && x == y->right) {
  254.                         x = y;
  255.                         y = y->parent;
  256.                 }
  257.                 return y;
  258.         }
  259.         NodePtr predecessor(NodePtr x) {
  260.                 if (x->left != nullptr) {
  261.                         return maximum(x->left);
  262.                 }
  263.  
  264.                 NodePtr y = x->parent;
  265.                 while (y != nullptr && x == y->left) {
  266.                         x = y;
  267.                         y = y->parent;
  268.                 }
  269.  
  270.                 return y;
  271.         }
  272.     void insertow() {cout<<"Znaleziono wykonujac "<<counter<<" Operacji"<<endl;}
  273.         void insert(string key, int ile) {
  274.                 NodePtr node = new Node;
  275.                 node->parent = nullptr;
  276.                 node->left = nullptr;
  277.                 node->right = nullptr;
  278.                 node->data = key;
  279.                 NodePtr y = nullptr;
  280.                 NodePtr x = this->root;
  281.                 while (x != nullptr) {
  282.                         y = x;
  283.                         if (node->data < x->data) {
  284.                                 x = x->left;
  285.                         }
  286.                         else
  287.                 {
  288.                                 x = x->right;
  289.                         }
  290.                 }
  291.                 node->parent = y;
  292.                 if (y == nullptr) {
  293.                         root = node;
  294.                 } else if (node->data < y->data) {
  295.                         y->left = node;
  296.                         splay(node);
  297.                 } else if(node->data > y->data){
  298.                         y->right = node;
  299.                         splay(node);
  300.                 }
  301.         else{
  302.             node->ile++;
  303.                 }
  304.         }
  305.  
  306.         NodePtr getRoot(){
  307.                 return this->root;
  308.         }
  309.         void prettyPrint() {
  310.                 printHelper(this->root, "", true);
  311.         }
  312.     void depth(){int g=0;
  313. root = maxDepth(root, g);cout<<maxdepth<<endl;}
  314. };
  315.  
  316. int main() {
  317.         SplayTree bst;
  318.     fstream tekst;
  319.      tekst.open("joyland.txt", ios::in);
  320.     string slowo;
  321.     int ile=1;
  322.     auto t1 = std::chrono::high_resolution_clock::now();
  323.     while(tekst >> slowo){bst.insert(slowo, ile);}
  324.     auto t2 = std::chrono::high_resolution_clock::now();
  325.     double durat = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  326.     cout<<"W czasie "<<durat<<" mikrosekund"<<endl;
  327.     tekst.close();
  328. //      bst.prettyPrint();
  329.     cout<<"Szukanie liczb: (wpisz 'stop' aby przerwac i wyswietlic wysokosc drzewa)"<<endl;
  330.     while(true)
  331.     {
  332.         string szukamy;
  333.         cout<<"Co mam znalezc?"<<endl;
  334.         cin>>szukamy;
  335.         if(szukamy=="stop") break;
  336.         bst.searchTree(szukamy);
  337.     }
  338.     bst.insertow();
  339.     cout << "Wysokosc drzewa: ";
  340.     bst.depth();
  341.         return 0;
  342. }
  343.