103 lines
1.8 KiB
C++
103 lines
1.8 KiB
C++
#include <stdio.h>
|
|
#include <conio.h>
|
|
#include <iostream.h>
|
|
#include <alloc.h>
|
|
#include <stdlib.h>
|
|
|
|
struct nodo {
|
|
int dato;
|
|
struct nodo *padre;
|
|
struct nodo *hi;
|
|
struct nodo *hd;
|
|
};
|
|
|
|
int ARBOL_VACIO (struct nodo *);
|
|
void INI_ARBOL (struct nodo **);
|
|
void INS_ARBOL (struct nodo **, int);
|
|
int LISTAR_IRD (struct nodo *);
|
|
|
|
void main (void) {
|
|
|
|
int d;
|
|
|
|
struct nodo *arbol;
|
|
|
|
INI_ARBOL(&arbol);
|
|
for (int i=0; i<37; i++) {
|
|
printf("\n Introduce n£mero: ");
|
|
fflush (stdin);
|
|
scanf ("%d", &d);
|
|
INS_ARBOL(&arbol, d);
|
|
}
|
|
printf("Numero de niveles -> %d",LISTAR_IRD(arbol));
|
|
getch();
|
|
free (arbol);
|
|
}
|
|
|
|
int ARBOL_VACIO(struct nodo *p) {
|
|
if (p == NULL)
|
|
return (1);
|
|
else
|
|
return (0);
|
|
}
|
|
|
|
void INI_ARBOL(struct nodo **p) { *p = NULL; }
|
|
|
|
void INS_ARBOL(struct nodo **p, int n) {
|
|
struct nodo *aux;
|
|
struct nodo **q;
|
|
int sw;
|
|
|
|
if ((aux = (struct nodo*) malloc (sizeof (struct nodo)))== NULL)
|
|
exit(1);
|
|
aux -> dato = n;
|
|
|
|
if (ARBOL_VACIO(*p)) {
|
|
aux -> hi = NULL;
|
|
aux -> hd = NULL;
|
|
aux -> padre = aux;
|
|
*p = aux;
|
|
}
|
|
else {
|
|
|
|
sw = 1;
|
|
q = p;
|
|
while (sw) {
|
|
sw = 0;
|
|
if ((*q) -> dato <= n && (*q) -> hd != NULL) {
|
|
q = &(*q) -> hd;
|
|
sw = 1;
|
|
}
|
|
if ((*q) -> dato >= n && (*q) -> hi != NULL) {
|
|
q = &(*q) -> hi;
|
|
sw = 1;
|
|
}
|
|
}
|
|
aux -> hi = aux -> hd = NULL;
|
|
aux -> padre = (*q);
|
|
((*q) -> dato < n) ? (*q) -> hd = aux : (*q) -> hi = aux;
|
|
|
|
}
|
|
}
|
|
|
|
int LISTAR_IRD(struct nodo *p) {
|
|
static int max=0;
|
|
|
|
static int nivel=1;
|
|
if (p != NULL)
|
|
{
|
|
|
|
nivel++;LISTAR_IRD(p -> hi);
|
|
if (nivel>max)
|
|
max=nivel;
|
|
nivel--;
|
|
//printf (" %d ", p -> dato);
|
|
|
|
nivel++;LISTAR_IRD(p -> hd);
|
|
nivel--;
|
|
if (nivel>max)
|
|
max=nivel;
|
|
|
|
}
|
|
return (max-1);
|
|
} |