Advertisement
Guest User

RBtree

a guest
May 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. // RBTree.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5.  
  6.  
  7. // Drzewo RB
  8.  
  9. #include "stdafx.h"
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16.  
  17.  
  18. struct node {
  19.     node *up;
  20.     node *left;
  21.     node *right;
  22.     int key;
  23.     char color;
  24. };
  25.  
  26. void loadFile(node *& root, node *&sentinel);
  27.  
  28.  
  29. void printBT(string sp, string sn, node * root, node *sentinel);
  30. node * minBST(node * root);
  31. node * maxBST(node * root);
  32. node * findBST(node * root, int value);
  33. void inorder(node * root);
  34. void saveFile(node *root);
  35. void RBinsert(node *&root, int value, node *sentinel);
  36. void RBfixup(node *&root, node *x);
  37. node *createRB(node *sentinel,int value );
  38. void deleteNode(node **root, int value);
  39. void removeBST(node * & root, int value);
  40. void Rrot(node *&root, node *&X);
  41. void Lrot(node *&root, node *&X);
  42.  
  43. int _tmain(int argc, _TCHAR* argv[])
  44. {
  45.  
  46.     node *root = new node;
  47.     root = NULL;
  48.     node *sentinel=new node;
  49.     node *temp = new node;
  50.     sentinel->color='B';
  51.     sentinel->key=80085;
  52.    
  53.    
  54.    
  55.  
  56.     int option, value;
  57.     do {
  58.         cout << "1. Add NOde" << endl;
  59.         cout << "2. Load File" << endl;
  60.         cout << "3. Store File" << endl;
  61.         cout << "4. Minimum" << endl;
  62.         cout << "5. Maximum" << endl;
  63.         cout << "6. Find value" << endl;
  64.         cout << "7. InOrder " << endl;
  65.         cout << "10. Print Tree" << endl;
  66.         cout << "\nPodaj opcje: ";
  67.         cin >> option;
  68.  
  69.  
  70.         switch (option) {
  71.         case 1:
  72.             cout << "Podaj liczbe: " << endl;
  73.             cin >> value;
  74.             RBinsert(root,  value,  sentinel);
  75.             cout << "root " << root << " wartosc " << root->key;
  76.             break;
  77.         case 2:
  78.             loadFile(root,sentinel);
  79.             break;
  80.         case 3:
  81.             saveFile(root);
  82.             break;
  83.         case 4:
  84.             cout << "Minimalny element to" << minBST(root)->key << endl;
  85.             break;
  86.         case 5:
  87.             cout << "Maksymalny element to" << maxBST(root)->key << endl;
  88.             break;
  89.         case 6:
  90.             cout << "Podaj liczbe: " << endl;
  91.             cin >> value;
  92.             temp = findBST(root, value);
  93.             if (temp != NULL) {
  94.                 cout << "Znaleziony element  " << temp->key << " pod adresem " << temp << endl;
  95.             }
  96.             else cout << "Nie ma takiego elementu";
  97.             break;
  98.         case 7:
  99.             inorder(root);
  100.             break;
  101.         case 8:
  102.             cout << "Podaj liczbe: " << endl;
  103.             cin >> value;
  104.             removeBST(root, value);
  105.             break;
  106.         case 10:
  107.              printBT(" "," " , root,sentinel);
  108.              break;
  109.  
  110.         }
  111.  
  112.         cout << "\n\n";
  113.  
  114.     } while (option != 0);
  115.  
  116.  
  117.     return 0;
  118. }
  119.  
  120. void RBinsert(node *&root, int value, node *sentinel)
  121. {
  122.         if(root==NULL)
  123.     {
  124.         root=new node;
  125.         root->key=value;
  126.         root->up=sentinel;
  127.         root->left=sentinel;
  128.         root->right=sentinel;
  129.     }
  130.     else
  131.     {
  132.         node *temp=root;
  133.         node *par =new node;
  134.         while(temp!=root->up)
  135.         {
  136.             par=temp;
  137.             if(value<temp->key)
  138.                 temp=temp->left;
  139.             else
  140.                 temp=temp->right;
  141.         }
  142.  
  143.         node *newPtr=new node;
  144.  
  145.         if(par->key<value)
  146.         {
  147.             par->right=newPtr;
  148.         }
  149.         else
  150.         {
  151.             par->left=newPtr;
  152.  
  153.         }
  154.         newPtr->key=value;
  155.         newPtr->left=root->up;
  156.         newPtr->right=root->up;
  157.         newPtr->up=par;
  158.         newPtr->color='R';
  159.  
  160.        // RBfixup(root,newPtr);
  161.     }
  162. }
  163.  
  164.  
  165. void RBfixup(node *&root, node *z)
  166. {
  167.     node *Y;
  168.     while(z->up->color=='R'){
  169.         if (z->up==z->up->up->left){
  170.             Y=z->up->up->left;
  171.             if(Y->color=='R'){
  172.                 z->up->color='B';
  173.                 Y->color='B';
  174.                 z->up->up->color='R';
  175.                 z=z->up->up;
  176.             }
  177.             else if(z==z->up->right ){
  178.                 z=z->up;
  179.                 Lrot(root,z);
  180.                 }
  181.             z->up->color='B';
  182.             z->up->up->color='R';
  183.             Rrot(root,z->up->up);
  184.         }else if(z->up==z->up->up->right){
  185.             Y=z->up->up->right;
  186.             if(Y->color=='R'){
  187.                 z->up->color='B';
  188.                 Y->color='B';
  189.                 z->up->up->color='R';
  190.                 z=z->up->up;
  191.             }
  192.             else if(z==z->up->left ){
  193.                 z=z->up;
  194.                 Rrot(root,z);
  195.                 }
  196.             z->up->color='B';
  197.             z->up->up->color='R';
  198.             Lrot(root,z->up->up);
  199.         }
  200.     }
  201.    
  202. }
  203. void loadFile(node *&root,node *&sentinel) {
  204.     int idx;
  205.     fstream plik;
  206.     plik.open("RBdata.txt", ios::in);
  207.     if (plik) {
  208.         while (!plik.eof()) {
  209.             plik >> idx;
  210.             RBinsert(root,idx, sentinel);
  211.         }
  212.         cout << "Plik wczytany" << endl;
  213.     }
  214.     plik.close();
  215. }
  216.  
  217. // rekurencyjny zapis do pliku IN ORDER
  218. void saveFile(node *root) {
  219.     ofstream plik;
  220.     plik.open("result.txt", ios::out | ios::app);
  221.  
  222.     if (root->left)
  223.         saveFile(root->left);
  224.  
  225.     plik << root->key << " ";
  226.  
  227.     if (root->right)
  228.         saveFile(root->right);
  229.  
  230.     plik.close();
  231. }
  232.  
  233. node * minBST(node * root) {
  234.     while (root->left) {
  235.         root = root->left;
  236.     }
  237.     return root;
  238. }
  239.  
  240. node * maxBST(node * root) {
  241.     while (root->right) {
  242.         root = root->right;
  243.     }
  244.     return root;
  245. }
  246.  
  247. node * findBST(node * root, int value)
  248. {
  249.     while (root) {
  250.         if (value == root->key)
  251.             return root;
  252.         else if (value < root->key)
  253.             root = root->left;
  254.         else if (value > root->key)
  255.             root = root->right;
  256.     }
  257.     return NULL;
  258. }
  259.  node *createRB(node *sentinel,int value ) {
  260.      node *newNode = new node;
  261.      newNode->up = sentinel;
  262.      newNode->left = sentinel;
  263.      newNode->right = sentinel;
  264.      newNode->key = value;
  265.      newNode->color='R';
  266.      return newNode;
  267. }
  268.  
  269. void printBT(string sp, string sn, node * root, node *sentinel)
  270. {
  271.     string s;
  272.     string cr, cl, cp;
  273.     cr = cl = cp = "  ";
  274.     cr[0] = 218; cr[1] = 196;
  275.     cl[0] = 192; cl[1] = 196;
  276.     cp[0] = 179;
  277.  
  278.     if (root!=sentinel)
  279.     {
  280.         s = sp;
  281.         if (sn == cr) s[s.length() - 2] = ' ';
  282.         printBT(s + cp, cr, root->right,sentinel);
  283.  
  284.         s = s.substr(0, sp.length() - 2);
  285.         cout << s << sn << root->key<<":"<< root->color<< endl;
  286.  
  287.         s = sp;
  288.         if (sn == cl) s[s.length() - 2] = ' ';
  289.         printBT(s + cp, cl, root->left,sentinel);
  290.     }
  291. }
  292.  
  293. void inorder(node * root) {
  294.     if (root->left)
  295.         inorder(root->left);
  296.     cout << root->key << " ";
  297.     if (root->right)
  298.         inorder(root->right);
  299.  
  300. }
  301.  
  302. void deleteNode(node **root, int value) {
  303.     node *A = findBST(*root,value);
  304.     node *B, *C;
  305.     if (A->right == NULL && A->left == NULL) {
  306.         if (A== A->up->left) A->up->left = NULL;
  307.         else A->up->right = NULL;
  308.         delete A;
  309.     }
  310.     else if(!A->left != !A->right){
  311.  
  312.         if (A->left) B = A->left;
  313.         else B = A->right;
  314.  
  315.         B->up = A->up;
  316.         if (!A->up) *root = B;
  317.  
  318.         if (A == A->up->left) A->up->left = B;
  319.         else A->up->right = B;
  320.         delete A;
  321.     }
  322.     else {
  323.        
  324.         B = minBST(A->right);
  325.  
  326.         if (B->left) C = B->left;
  327.         else C = B->right;
  328.  
  329.         B->up = C->up;
  330.         if (!B->up) *root = C;
  331.  
  332.         if (B == B->up->left) B->up->left = C;
  333.         else B->up->right = C;
  334.        
  335.         if (B != A) A->key = B->key;
  336.  
  337.         delete B;
  338.     }
  339. }
  340.  
  341. void removeBST(node * & root, int value)
  342. {
  343.     node *A = findBST(root, value);
  344.     node * B, *C;
  345.  
  346.     if (A)
  347.     {
  348.        
  349.  
  350.         //Jesli A ma obu potomków to B= nastepnik A ( tu zawsze min od A->right)
  351.         //jak ma jednego lub mniej to B=A
  352.        
  353.         if (A->left && A->right) B = minBST(A->right);
  354.         else B = A;
  355.        
  356.         // C wskazuje syna B lub NULL
  357.         if (B->left) C = B->left;
  358.         else C = B->right;
  359.  
  360.         // Jeśli syn B istnieje, to zastąpi C w drzewie dowiąznie up
  361.         if (C) C->up = B->up;
  362.  
  363.         // B zostaje zastąpione przez C w drzewie
  364.         if (!B->up) root = C;
  365.         else if (B == B->up->left) B->up->left = C;
  366.             else  B->up->right = C;
  367.  
  368.         // Jeśli B jest następnikiem A, to kopiujemy dane
  369.         if (B != A) A->key = B->key;
  370.  
  371.         delete B;
  372.  
  373.     }
  374. }
  375.  
  376. void Lrot(node *&root, node *&X){
  377.     node *Y = X->right;
  378.     X->right = Y->left;
  379.  
  380.     if(Y->left) Y->left->up=X;
  381.     Y->up = X->up;
  382.     if(!X->up)  root = Y;
  383.     else if (X==X->up->left) X->up->left= Y;
  384.         else X->up->right=Y;
  385.     Y->left = X;
  386.     X->up = Y;
  387. }
  388.  
  389. void Rrot(node *&root, node *&X){
  390.     node *Y = X->left;
  391.     X->left = Y->right;
  392.  
  393.     if(Y->right) Y->right->up=X;
  394.     Y->up = X->up;
  395.     if(!X->up)  root = Y;
  396.     else if (X==X->up->right) X->up->right= Y;
  397.         else X->up->left=Y;
  398.     Y->right = X;
  399.     X->up = Y;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement