-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArbol.h
110 lines (92 loc) · 2.35 KB
/
Arbol.h
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
100
101
102
103
104
105
106
107
108
109
#ifndef ARBOL_H
#define ARBOL_H
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define TEST_NUMBER 20
typedef struct nodo{
struct nodo * izquierda, * derecha;
int valor;
char caracter;
} nodo;
// [ izquierda, visit, derecha ]
void PrintIn(nodo * n){
if(n->izquierda) PrintIn(n->izquierda);
printf("%d ", n->valor);
if(n->derecha) PrintIn(n->derecha);
}
// [ visit, izquierda, derecha ]
void PrintPre(nodo * n){
printf("%d ", n->valor);
if(n->izquierda) PrintPre(n->izquierda);
if(n->derecha) PrintPre(n->derecha);
}
// [ izquierda, derecha, visit ]
void PrintPost(nodo * n){
if(n->izquierda) PrintPost(n->izquierda);
if(n->derecha) PrintPost(n->derecha);
printf("%d ", n->valor);
}
// create a new nodo & set default nodos
nodo * NuevoNodo(int valor){
nodo * n = malloc(sizeof(nodo));
n->valor = valor;
n->izquierda = n->derecha = NULL;
return n;
}
// recursive insertion from the tree raiz
void InsertarNodo(nodo ** raiz, nodo * hijo){
if(!*raiz) *raiz = hijo; // tree raiz not exists
else InsertarNodo( hijo->valor <= (*raiz)->valor ? &(*raiz)->izquierda : &(*raiz)->derecha , hijo ); // recursive call
}
// recursive insertion from the tree raiz
void InsertarNodoGreedy(nodo ** raiz, nodo * hijo){
if(!*raiz) *raiz = hijo; // tree raiz not exists
else InsertarNodo( hijo->valor <= (*raiz)->valor ? &(*raiz)->izquierda : &(*raiz)->derecha , hijo ); // recursive call
}
// recursive Buscar of a nodo
nodo * Buscar(nodo * raiz, int valor){
return !raiz ? NULL : raiz->valor == valor ? raiz : Buscar( valor > raiz->valor ? raiz->derecha : raiz->izquierda , valor );
}
void BuscarPorPuntero(nodo * raiz, int valor, nodo ** save){
*save = Buscar(raiz,valor);
}
//int main(){
//
// int test, c, success;
// test = c = success = 0;
//
// srand(time(NULL));
//
// nodo * raiz = NULL;
//
// // INSERTION TEST
//
// while(c++ < TEST_NUMBER)
// insert(&raiz, new(rand() % 150));
//
// // PRINT TEST
//
// printf("\n > IN ORDER -> ");
// in(raiz);
//
// printf("\n\n > PRE ORDER -> ");
// pre(raiz);
//
// printf("\n\n > POST ORDER -> ");
// post(raiz);
//
// // Buscar TEST
//
// puts("\n\n > TEST Buscar:");
//
// while(test++ < TEST_NUMBER)
// if(Buscar(raiz, test) > 0){
// printf(" - %d\n", test);
// success++;
// }
//
// printf("\n <SUCCESS> = %d <FAILED> = %d\n", success, TEST_NUMBER - success);
//
//}
#endif /* ARBOL_H */