Advertisement
Gilgamesh858

RBTree.cpp

Feb 9th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <math.h>
  4. #include "BSTree.cpp"
  5. using namespace std;
  6.  
  7. template <class H> class RBTree : public BSTree<H> {
  8.     private:
  9.         static const int BLACK = 0;
  10.         static const int RED = 1;
  11.         static const int BLACKBLACK = 2;
  12.  
  13.         void inorder_visit(Node<H>* n) {
  14.             if(n!=NULL) {
  15.                 inorder_visit(n->getLeft());
  16.                 cout << "(" << n->getKey() << ", " << printC(n->getColor()) << ") ";
  17.                 inorder_visit(n->getRight());
  18.             }
  19.         }
  20.  
  21.         void postorder_visit(Node<H>* n) {
  22.             if(n!=NULL) {
  23.                 postorder_visit(n->getLeft());
  24.                 postorder_visit(n->getRight());
  25.                 cout << "(" << n->getKey() << ", " << printC(n->getColor()) << ") ";
  26.             }
  27.         }
  28.  
  29.         void preorder_visit(Node<H>* n) {
  30.             if(n!=NULL) {
  31.                 cout << "(" << n->getKey() << ", " << printC(n->getColor()) << ") ";
  32.                 preorder_visit(n->getLeft());
  33.                 preorder_visit(n->getRight());
  34.             }
  35.         }
  36.        
  37.         char printC(int color) {
  38.             if(color==0) return 'B';
  39.             if(color==1) return 'R';
  40.             return 'X';
  41.         }
  42.        
  43.         void RBInsertFixup(Node<H> *z) {
  44.             if(z->getParent()!=NULL && z->getParent()->getColor()==BLACK) return;
  45.             if(z == this->getRoot()) {
  46.                 z->setColor(BLACK);
  47.                 return;
  48.             }
  49.             Node<H> *padre = z->getParent();
  50.             Node<H> *nonno = padre->getParent();
  51.             Node<H> *zio = nonno->getRight();
  52.             if(padre == nonno->getRight()) zio = nonno->getLeft();
  53.             if(zio!=NULL && zio->getColor() == RED) {
  54.                 // caso 1
  55.                 zio->setColor(BLACK);
  56.                 padre->setColor(BLACK);
  57.                 nonno->setColor(RED);
  58.                 RBInsertFixup(nonno);
  59.                 return;
  60.             }
  61.             if(padre == nonno->getLeft()) {
  62.                 if(z == padre->getRight()) {
  63.                     // caso 3
  64.                     this->leftRotate(padre);
  65.                     padre = z;
  66.                     z = padre->getLeft();
  67.                 }
  68.                 // caso 2
  69.                 this->rightRotate(nonno);
  70.                 padre->setColor(BLACK);
  71.                 nonno->setColor(RED);
  72.                 return;
  73.             }
  74.             else { // casi simmetrici ai precedenti
  75.                 if(z == padre->getLeft()) {
  76.                     // caso 3
  77.                     this->rightRotate(padre);
  78.                     padre = z;
  79.                     z = padre->getRight();
  80.                 }
  81.                 // caso 2
  82.                 padre->setColor(BLACK);
  83.                 nonno->setColor(RED);
  84.                 this->leftRotate(nonno);
  85.                 return;
  86.             }
  87.         }
  88.        
  89.         void swapColor(Node<H> *x, Node<H> *y) {
  90.             int tmp = x->getColor();
  91.             x->setColor(y->getColor());
  92.             y->setColor(tmp);
  93.         }
  94.  
  95.            
  96.     public:
  97.         RBTree<H>() : BSTree<H>() {}
  98.         RBTree<H> *insert(H key) {
  99.             BSTree<H>::insert(key);
  100.             Node<H> *n = this->search(key);
  101.             n->setColor(RED);
  102.             RBInsertFixup(n);
  103.             return this;
  104.         }
  105.        
  106.         void inorder() { inorder_visit(this->getRoot()); cout << endl; }
  107.         void postorder() { postorder_visit(this->getRoot()); cout << endl; }
  108.         void preorder() { preorder_visit(this->getRoot()); cout << endl; }
  109. };
  110.  
  111. int main() {
  112.     RBTree<int> *t = new RBTree<int>();
  113.     t->insert(9)->insert(22)->insert(25)->insert(15)->insert(23)->insert(13)->insert(2)->insert(14)->insert(24)->insert(1);
  114.     t->inorder();
  115.     t->postorder();
  116. }
  117.  
  118. ________________________________________________________________________________________________________________________________
  119. ________________________________________________________________________________________________________________________________
  120.  
  121. /*Albero*/
  122.  
  123. #include <iostream>
  124. #include <ctime>
  125. #include <math.h>
  126. using namespace std;
  127.  
  128. template <class T> class Node {
  129.     private:
  130.         T key;
  131.         Node<T> *left, *right, *parent;
  132.         int color;
  133.         int leaf;
  134.        
  135.     public:
  136.         Node<T>(T key) {
  137.             this->key = key;
  138.             left = right = parent = NULL;
  139.             leaf = 1;
  140.         }
  141.        
  142.         T getKey() {return key;}
  143.         int getColor() {return color;}
  144.         int isLeaf() {return leaf;}
  145.         Node<T>* getLeft() {return left;}
  146.         Node<T>* getRight() {return right;}
  147.         Node<T>* getParent() {return parent;}
  148.         void setKey(T key) {this->key = key;}
  149.         void setColor(int color) {this->color = color;}
  150.         void setLeaf(int leaf) {this->leaf = leaf;}
  151.         void setLeft(Node<T>* left) {this->left = left;}
  152.         void setRight(Node<T>* right) {this->right = right;}
  153.         void setParent(Node<T>* parent) {this->parent = parent;}
  154. };
  155.  
  156. template <class T> class BSTree {
  157.     private:
  158.         Node<T> *root;
  159.         int size;
  160.        
  161.         void inorder_visit(Node<T>* n) {
  162.             if(n!=NULL) {
  163.                 inorder_visit(n->getLeft());
  164.                 cout << n->getKey() << " ";
  165.                 inorder_visit(n->getRight());
  166.             }
  167.         }
  168.  
  169.         void postorder_visit(Node<T>* n) {
  170.             if(n!=NULL) {
  171.                 postorder_visit(n->getLeft());
  172.                 postorder_visit(n->getRight());
  173.                 cout << n->getKey() << " ";
  174.             }
  175.         }
  176.  
  177.         void preorder_visit(Node<T>* n) {
  178.             if(n!=NULL) {
  179.                 cout << n->getKey() << " ";
  180.                 preorder_visit(n->getLeft());
  181.                 preorder_visit(n->getRight());
  182.             }
  183.         }
  184.  
  185.    
  186.     protected:
  187.         Node<T> *search(T key) {
  188.             Node<T> *tmp = root;
  189.             while(tmp!=NULL && tmp->getKey()!=key) {
  190.                 if(key>tmp->getKey())
  191.                     tmp = tmp->getRight();
  192.                 else tmp = tmp->getLeft();
  193.             }
  194.             return tmp;
  195.         }
  196.         Node<T>* getRoot() {return root;}
  197.  
  198.         BSTree<T> *rightRotate(Node<T> *y) {
  199.             Node<T> *x = y->getLeft();
  200.             Node<T> *z = y->getParent();
  201.             y->setLeft(x->getRight());
  202.             x->setRight(y);
  203.             if(z!=NULL) {
  204.                 if(y==z->getLeft()) z->setLeft(x);
  205.                 else z->setRight(x);
  206.             }
  207.             else root = x;
  208.             x->setParent(z);
  209.             y->setParent(x);
  210.             if(y->getLeft()) y->getLeft()->setParent(y);
  211.             return this;
  212.         }
  213.  
  214.         BSTree<T> *leftRotate(Node<T> *y) {
  215.             Node<T> *x = y->getRight();
  216.             Node<T> *z = y->getParent();
  217.             y->setRight(x->getLeft());
  218.             x->setLeft(y);
  219.             if(z!=NULL) {
  220.                 if(y==z->getRight()) z->setRight(x);
  221.                 else z->setLeft(x);
  222.             }
  223.             else root = x;
  224.             x->setParent(z);
  225.             y->setParent(x);
  226.             if(y->getRight()) y->getRight()->setParent(y);
  227.             return this;
  228.         }
  229.        
  230.         Node<T>* minimum(Node<T>* n) {
  231.             if(n==NULL) return n;
  232.             Node<T>* tmp = n;
  233.             while(tmp->getLeft())
  234.                 tmp = tmp->getLeft();
  235.             return tmp;
  236.         }
  237.  
  238.         Node<T>* maximum(Node<T>* n) {
  239.             if(n==NULL) return n;
  240.             Node<T>* tmp = n;
  241.             while(tmp->getRight())
  242.                 tmp = tmp->getRight();
  243.             return tmp;
  244.         }
  245.        
  246.         Node<T>* predecessor(Node<T> *n) {
  247.             if(n==NULL) return n;
  248.             if(n->getLeft()) return maximum(n->getLeft());
  249.             Node<T>* tmp = n->getParent();
  250.             while(tmp!=NULL && n == tmp->getLeft()) {
  251.                 n = tmp;
  252.                 tmp = tmp->getParent();
  253.             }          
  254.             return tmp;
  255.         }
  256.  
  257.         Node<T>* successor(Node<T> *n) {
  258.             if(n==NULL) return n;
  259.             if(n->getRight()) return minimum(n->getRight());
  260.             Node<T>* tmp = n->getParent();
  261.             while(tmp!=NULL && n == tmp->getRight()) {
  262.                 n = tmp;
  263.                 tmp = tmp->getParent();
  264.             }          
  265.             return tmp;
  266.         }
  267.  
  268.         Node<T> *delete_node(T key) {
  269.             Node<T> *n = search(key);
  270.             if(!n) return NULL;
  271.             Node<T> *p = n->getParent();
  272.             // n non ha figlio sinistro
  273.             if(n->getLeft()==NULL) {
  274.                 if(p) {
  275.                     if(n == p->getLeft()) p->setLeft(n->getRight());
  276.                     else p->setRight(n->getRight());
  277.                 }
  278.                 else root = n->getRight();
  279.                 if( n->getRight() ) n->getRight()->setParent(p);
  280.                 return n;
  281.             }
  282.             // n ha figlio sinistro ma non figlio destro
  283.             if(n->getRight()==NULL) {
  284.                 if(p) {
  285.                     if(n == p->getLeft()) p->setLeft(n->getLeft());
  286.                     else p->setRight(n->getLeft());
  287.                 }
  288.                 else root = n->getLeft();
  289.                 n->getLeft()->setParent(p);
  290.                 return n;
  291.             }
  292.             // n ha entrambi i figli
  293.             Node<T> *s = successor(n);
  294.             del(s->getKey());
  295.             n->setKey(s->getKey());
  296.             return s;
  297.         }
  298.        
  299.     public:
  300.         BSTree<T>() {
  301.             root = NULL;
  302.             size = 0;
  303.         }
  304.         int getSize() {return size;}
  305.        
  306.         BSTree<T> *insert(T key) {
  307.             Node<T>* tmp = root;
  308.             Node<T>* parent = NULL;
  309.             while(tmp!=NULL) {
  310.                 parent = tmp;
  311.                 if(key>tmp->getKey()) {
  312.                     tmp = tmp->getRight();
  313.                 }
  314.                 else tmp = tmp->getLeft();
  315.             }
  316.             Node<T> *n = new Node<T>(key);
  317.             if(parent==NULL) root = n;
  318.             else {
  319.                 n->setParent(parent);
  320.                 if(key>parent->getKey()) {
  321.                     parent->setRight(n);
  322.                 }
  323.                 else parent->setLeft(n);
  324.                 parent->setLeaf(0);
  325.             }
  326.             size++;
  327.             return this;
  328.         }
  329.  
  330.         BSTree<T> *del(T key) {
  331.             delete_node(key);
  332.             return this;
  333.         }
  334.        
  335.         BSTree<T> *rightRotate(T key) {
  336.             Node<T> *n = search(key);
  337.             if(n!=NULL) rightRotate(n);
  338.             return this;
  339.         }
  340.  
  341.         BSTree<T> *leftRotate(T key) {
  342.             Node<T> *n = search(key);
  343.             if(n!=NULL) leftRotate(n);
  344.             return this;
  345.         }
  346.        
  347.         void inorder() { inorder_visit(root); cout << endl; }
  348.         void postorder() { postorder_visit(root); cout << endl; }
  349.         void preorder() { preorder_visit(root); cout << endl; }
  350.  
  351. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement