Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. No noMinimo (No no) {
  2.         No aux = no;
  3.         while (aux.esq != null) {
  4.             aux = aux.esq;
  5.         }
  6.         return aux;
  7.     }
  8.    
  9.     void transplantar (No a, No b) {
  10.         if(a.mae == null) {
  11.             this.raiz = b;
  12.         } else if (a == a.mae.esq) {
  13.             a.mae.esq = b;
  14.         } else {
  15.             a.mae.dir = b;
  16.         }
  17.         if (b != null) {
  18.             b.mae = a.mae;
  19.         }
  20.     }
  21.    
  22.     void remover2 (No no, int num) {
  23.         No a = buscar(no, num);
  24.         if (a == null) {
  25.             return;
  26.         } else {
  27.             if (a.esq == null) {
  28.                 transplantar (a, a.dir);
  29.             } else if (a.dir == null) {
  30.                 transplantar (a, a.esq);
  31.             } else {
  32.                 No b = noMinimo(a.dir);
  33.                 if (b.mae != a){
  34.                     transplantar (b, b.dir);
  35.                     b.dir = a.dir;
  36.                     b.dir.mae = b;
  37.                 }
  38.                 transplantar (a, b);
  39.                 b.esq = a.esq;
  40.                 b.esq.mae = b;
  41.             }
  42.         }
  43.     }
  44.    
  45.     void inserir2 (int num) {
  46.         No c = new No(num);
  47.         No b = raiz;
  48.         No a = b;
  49.         while(b != null) {
  50.             a = b;
  51.             if (c.num < b.num) {
  52.                 b = b.esq;
  53.             } else if (c.num > b.num) {
  54.                 b = b.dir;
  55.             } else {
  56.                 return;
  57.             }
  58.         }
  59.         c.mae = a;
  60.         if(a == null) {
  61.             raiz = c;
  62.         } else if (c.num < a.num){
  63.             a.esq = c;
  64.         } else if (c.num > a.num){
  65.             a.dir = c;
  66.         } else {
  67.             return;
  68.         }
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement