Skip to content

Instantly share code, notes, and snippets.

@vicmac
Created May 17, 2015 03:27
Show Gist options
  • Save vicmac/a88d1982692a2b93cb2e to your computer and use it in GitHub Desktop.
Save vicmac/a88d1982692a2b93cb2e to your computer and use it in GitHub Desktop.
void insertar(int valor){
// YOLOcreando un nuevo nodo
Arbol *n = new Arbol();
n->Inicializar();
n->izq = NULL;
n->der = NULL;
n->numero = valor;
// Si el arbol está vacío, lo insertas en la raiz
if (raiz == null){
raiz = n;
} else {
Arbol r * = raiz;
// Si no, buscamos donde está su lugar en los siguientes nodos
while(true){
if (r->numero < valor){
if (r->izq == NULL){ // Asumiendo que los menores van a la izquierda, si el nodo a la izquierda es null, ponemos el nuevo nodo ahi
r->izq = n;
break;
} else { // Si no, nos movemos a ese nodo y en el siguiente ciclo checamos donde va
r = r->izq;
}
} else {
// La misma historia pero ahora para el caso de que sea un valor mayor al del nodo que estamos checando.
if (r->der == NULL){
r->der = n;
break;
} else {
r = r->der;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment