9 struct node
*l
, *r
, *p
;
12 int BI(struct node
* r
){ /// INDICE DE BALANCEO
13 if( r
== NULL
) return 0;
15 return r
->r
->h
- r
->l
->h
;
18 }else if( r
->r
) return r
->r
->h
+1;
21 void suplantar(struct node
* p
, struct node
* h
, struct node
* n
){
22 if( p
== NULL
)root
= n
;
23 else if( p
->l
== h
)p
->l
= n
;
26 int maxAltura(struct node
* r
){
28 if( r
->l
&& r
->h
< r
->l
->h
+ 1 ) r
->h
= r
->l
->h
+ 1;
29 if( r
->r
&& r
->h
< r
->r
->h
+ 1 ) r
->h
= r
->r
->h
+ 1;
31 void RIGHT(struct node
* r
){
32 struct node
* aux
= r
->l
;
37 suplantar(r
->p
, r
, aux
);
44 void LEFT(struct node
* r
){
45 struct node
* aux
= r
->r
;
50 suplantar(r
->p
, r
, aux
);
57 void balanceo(struct node
* r
){
60 int balanceIndex
= BI(r
);
61 if( abs(balanceIndex
) < 2 ) return;
62 if( balanceIndex
== 2 ){
75 struct node
*nuevo(int val
, struct node
* r
){
76 struct node
*a
= new node
;
77 a
->val
= val
, a
->l
= a
->r
= NULL
;
78 a
->p
= r
, a
->contador
= 1;
82 void insertr(int val
, struct node
*r
){
87 struct node
*a
= nuevo(val
,r
);
96 struct node
*a
= nuevo(val
,r
);
106 void insert(int val
){
109 root
->l
= root
->r
= root
->p
= NULL
;
110 root
->val
= val
, root
->contador
= 1, root
->h
= 0;
113 struct node
* aux
= root
;
116 int borrarMax(struct node
* r
){
119 if( r
->r
) res
= borrarMax(r
->r
);
122 suplantar(r
->p
, r
, r
->l
);
123 if( r
->l
) r
->l
->p
= r
->p
;
131 void deleteval(int val
, struct node
* r
){
136 suplantar(r
->p
, r
, r
->r
);
138 if( r
->r
)r
->r
->p
= r
->p
;
141 suplantar(r
->p
, r
, r
->l
);
143 if( r
->l
)r
->l
->p
= r
->p
;
146 int v
= borrarMax(r
->l
);
149 }else if( r
->val
> val
){
150 deleteval(val
, r
->l
);
151 }else deleteval(val
, r
->r
);
156 int search(int val
, struct node
* r
){
157 if( !r
) return 0; if( r
->val
== val
) return 1;
158 if( r
->val
> val
) return search(val
, r
->l
);else return search(val
, r
->r
);
160 void dfs(struct node
* r
){
161 if( r
== NULL
) return;
162 printf(" %i ", r
->val
);
174 printf("Introduzca números para el árbol:\n");
181 if( search(k
, root
) ) printf("Si esta!!!\n");
182 else printf("No esta!!!\n");
183 }else deleteval(k
,root
);