Created
May 23, 2018 00:47
-
-
Save hjJunior/fde505949a1773fbc30961a2da68711c to your computer and use it in GitHub Desktop.
Getting start to learning tree in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef elemento el; | |
struct elemento { | |
int valor; | |
el *esquerda; | |
el *direito; | |
} | |
el* criaArvore(); | |
el* insere (el* raiz, el* elemento); | |
void imprimir(el* raiz); | |
el *criaArvore() { | |
return NULL; | |
} | |
el *aloca (int valor) { | |
el *novo = (el*)malloc(sizeof(el)); | |
novo->valor = valor; | |
novo->esquerda = novo->direito = NULL; | |
return novo; | |
} | |
el *insere (el* raiz, el* elemento) { | |
if (raiz == NULL) return elemento; // primeiro elemento | |
else if (elemento->valor > raiz->valor) raiz->direita = insere(raiz->direita, elemento);// inserir na direita | |
else if (elemento->valor < raiz->valor) raiz->esquerda = insere(raiz->esquerda, elemento); // inserir na esquerda | |
else printf("Nao pode inserir valor com o mesmo nome\n"); | |
return raiz; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment