C/Visual C - arboles binariode busquedaa

 
Vista:

arboles binariode busquedaa

Publicado por luisrt (9 intervenciones) el 18/10/2004 19:47:36
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
# define NULLO

typedef struct _nodo {
int dato;
struct _nodo *derecho;
struct _nodo *izquierdo;
} tipoNodo;

typedef tipoNodo *pNodo;
typedef tipoNodo *Arbol;
void Insertar(Arbol *a, int dat) {
pNodo padre = NULL; /* (1) */
pNodo actual = *a; /* (2) */

while(!Vacio(actual) && dat != actual->dato) { /* (3) */
padre = actual;
if(dat < actual->dato) actual = actual->izquierdo; /* (3-a) */
else if(dat > actual->dato) actual = actual->derecho; /* (3-b) */
}

if(!Vacio(actual)) return; /* (4) */
if(Vacio(padre)) { /* (5) */
*a = (Arbol)malloc(sizeof(tipoNodo));
(*a)->dato = dat;
(*a)->izquierdo = (*a)->derecho = NULL;
}
else if(dat < padre->dato) { /* (6) */
actual = (Arbol)malloc(sizeof(tipoNodo));
padre->izquierdo = actual;
actual->dato = dat;
actual->izquierdo = actual->derecho = NULL;
}
else if(dat > padre->dato) { /* (7) */
actual = (Arbol)malloc(sizeof(tipoNodo));
padre->derecho = actual;
actual->dato = dat;
actual->izquierdo = actual->derecho = NULL;
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder