14 struct node
* RIGHT(struct node
* n
){
15 struct node
* aux
= n
->l
;
16 n
->hijos
-= ( aux
->hijos
+ 1 );
17 aux
->hijos
+= n
->hijos
+1;
18 if( aux
->r
) n
->hijos
+= aux
->r
->hijos
+ 1; //, aux->hijos -= aux->r->hijos;
23 struct node
* LEFT(struct node
* n
){
24 struct node
* aux
= n
->r
;
25 n
->hijos
-= (aux
->hijos
+ 1 );
26 aux
->hijos
+= n
->hijos
+ 1;
27 if( aux
->l
) n
->hijos
+= aux
->l
->hijos
+ 1; //, aux->hijos -= aux->l->hijos;
32 struct node
* insert(int val
, struct node
* r
){
34 struct node
* a
= new node
;
35 a
->p
= rand(), a
->val
= val
, a
->l
= a
->r
= NULL
, a
->hijos
= 0;
40 r
->l
= insert(val
, r
->l
);
44 }else if( val
> r
->val
){
45 r
->r
= insert(val
, r
->r
);
52 struct node
* bubbleDown(struct node
* n
){
57 }else if( n
->l
&& n
->r
){
58 if( n
->l
->p
> n
->r
->p
){
60 aux
->r
= bubbleDown(aux
->r
);
63 aux
->l
= bubbleDown(aux
->l
);
67 aux
->r
= bubbleDown(aux
->r
);
70 aux
->l
= bubbleDown(aux
->l
);
75 // PRECONDICIÖN: val esta en el árbol
76 struct node
* deleteval(int val
, struct node
* n
){
78 if( n
->val
!= val
) n
->hijos
--;
79 if( n
->val
> val
) n
->l
= deleteval(val
, n
->l
);
80 else if( n
->val
< val
) n
->r
= deleteval(val
,n
->r
);
81 else n
= bubbleDown(n
);
84 bool search(int val
, struct node
* n
){
85 if( !n
) return false;
86 if( n
->val
== val
) return true;
87 if( n
->val
> val
) return search(val
, n
->l
);
88 if( n
->val
< val
) return search(val
, n
->r
);
90 void dfs(struct node
* r
){
91 if( r
== NULL
) return;
92 printf(" %i p%i h%i ", r
->val
, r
->p
, r
->hijos
);
105 printf("Introduzca números para el árbol ( 1 x insertar... 2 x buscar, 0 x borrar:\n");
110 root
= insert(k
, root
);
112 if( search(k
, root
) ) printf("Si esta!!!\n");
113 else printf("No esta!!!\n");
115 else if( search(k
, root
) ) root
= deleteval(k
,root
);