daily pastebin goal
7%
SHARE
TWEET

AVL Tree, old notes

luisoncpp Mar 26th, 2019 (edited) 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Copyright (c) <''2010''> <''Luis E Vargas''>
  2.  
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6.  
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10.  
  11. 1. The origin of this software must not be misrepresented; you must not
  12.    claim that you wrote the original software. If you use this software
  13.    in a product, an acknowledgment in the product documentation would be
  14.    appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16.    misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. template<typename T>
  20. struct arbolAVL{
  21.     struct nodo{
  22.         T clave;
  23.         int altura;
  24.         nodo *izq, *der;
  25.     };
  26.     nodo Dato[2000000];
  27.     int nodos;
  28.     arbolAVL(){
  29.         nodos=0;
  30.     }
  31.     nodo* nuevo_nodo(T &clave){
  32.         Dato[nodos].clave=clave;
  33.         Dato[nodos].izq=NULL;
  34.         Dato[nodos].der=NULL;
  35.         Dato[nodos].altura=1;
  36.         nodos++;
  37.         return &Dato[nodos-1];
  38.     }
  39.     int actualiza_altura(nodo *a){
  40.         int hizq=0, hder=0;
  41.         if(a->izq!=NULL)
  42.             hizq=a->izq->altura;
  43.         if(a->der!=NULL)
  44.             hder=a->der->altura;
  45.         a->altura=max(hizq, hder)+1;
  46.     }
  47.     void rota_izq(nodo *x, nodo** padre=NULL){
  48.         nodo *y, *a, *b, *c;
  49.         int h=x->altura;
  50.         assert(x->der!=NULL);
  51.         assert(padre==NULL || *padre==x);
  52.         y=x->der;
  53.         a=x->izq;
  54.         b=y->izq;
  55.         c=y->der;
  56.         y->izq=x;
  57.         y->der=c;
  58.         x->izq=a;
  59.         x->der=b;
  60.         if(padre!=NULL)
  61.             *padre=y;
  62.         actualiza_altura(x);
  63.         actualiza_altura(y);
  64.     }
  65.     void rota_der(nodo *y, nodo** padre=NULL){
  66.         nodo *x, *a, *b, *c;
  67.         assert(y->izq!=NULL);
  68.         assert(padre==NULL || *padre==y);
  69.         x=y->izq;
  70.         a=x->izq;
  71.         b=x->der;
  72.         c=y->der;
  73.         x->izq=a;
  74.         x->der=y;
  75.         y->izq=b;
  76.         y->der=c;
  77.         if(padre!=NULL)
  78.             *padre=x;
  79.         actualiza_altura(y);
  80.         actualiza_altura(x);
  81.     }
  82.     void inserta(nodo *a, T &clave, nodo** padre=NULL){
  83.         if(clave<a->clave){
  84.             if(a->izq==NULL){
  85.                 a->izq=nuevo_nodo(clave);
  86.             }else{
  87.                 inserta(a->izq, clave, &a->izq);
  88.             }
  89.         }else{
  90.             if(a->der==NULL){
  91.                 a->der=nuevo_nodo(clave);
  92.             }else{
  93.                 inserta(a->der, clave, &a->der);
  94.             }
  95.         }
  96.         int hizq=0, hder=0;
  97.         if(a->izq!=NULL)
  98.             hizq=a->izq->altura;
  99.         if(a->der!=NULL)
  100.             hder=a->der->altura;
  101.         if(hizq-hder>1){
  102.             rota_der(a, padre);
  103.         }else if(hder-hizq>1){
  104.             rota_izq(a, padre);
  105.         }else{
  106.             actualiza_altura(a);
  107.         }
  108.     }
  109.     void orden(nodo *a, vector<T> &sal){
  110.         if(a->izq!=NULL){
  111.             orden(a->izq, sal);
  112.         }
  113.         sal.push_back(a->clave);
  114.         if(a->der!=NULL){
  115.             orden(a->der, sal);
  116.         }
  117.     }
  118. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top