C/Visual C - Ayuda con arbol

 
Vista:
sin imagen de perfil

Ayuda con arbol

Publicado por knmox (11 intervenciones) el 31/05/2018 05:57:16
Hola, que tal, escribo con la finalidad de que alguien pueda decirme por que motivo no me imprime los datos ingresados, en preorden...Es como si no almacenara los valores ingresados. Espero saldar la duda, gracias de antemano.
Aqui les dejo el código para que me digan por que no almacena los datos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
typedef struct nodo{
	int dato;
	struct nodo *derecha;
	struct nodo *izquierda;
}TTT;
 
typedef TTT *pNodo;
TTT *arbol = NULL;
 
/*prototipo*/
TTT *crear(int);
void insertar(TTT *, int);
int preOrden(TTT *);
void menu();
 
main()
{
 
	menu();
 
	getch();
	return 0;
}
 
/*FUNCION PARA CREAR UN NODO*/
TTT *crear(int n)
{
	TTT *nuevo = (pNodo)malloc(sizeof(TTT));
	printf("\n Ingresa un nodo: "); scanf("%d",&nuevo->dato);
	nuevo->dato = n;
	nuevo->derecha = NULL;
	nuevo->izquierda = NULL;
 
	return nuevo;
}
 
/* FUNCION PARA AÑADIR DATOS*/
void insertar(TTT *arbol, int n)
{
	if(arbol == NULL)
	{
		TTT *nuevo = crear(n);
		arbol = nuevo;
	}
	else
	{
		int ppp = arbol->dato;
		if(n < ppp)
		{
			insertar(arbol->izquierda, n);
		}
		else
		{
			insertar(arbol->derecha, n);
		}
	}
}
/*RECORRIDO EN PREORDEN*/
int preOrden(TTT *arbol)
{
	if(arbol == NULL)
	{
		return ;
	}
	else
	{
		printf(" %d -",arbol->dato);
		preOrden(arbol->izquierda);
		preOrden(arbol->derecha);
	}
	printf("\n\n\n");
}
 
//MENU 
void menu()
{
	int opc,x;
 
	do{
		printf("     MENU ARBOL  \n");
		printf("1. Insertar nodo\n");
		printf("2. PREORDEN\n");
		printf("3. Salir\n");
		printf("\n  Ingresa una opcion: "); scanf("%d",&opc);
 
		switch(opc)
		{
			case 1: insertar(arbol,x); break;
			case 2: printf("\n Recorrido en preorden: ");
					preOrden(arbol); printf("\n\n\n"); break;
		}
		system("pause");
		system("cls");
	}while(opc != 3);
}
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