Skip to content

Instantly share code, notes, and snippets.

@hjJunior
Created May 23, 2018 00:47
Show Gist options
  • Save hjJunior/fde505949a1773fbc30961a2da68711c to your computer and use it in GitHub Desktop.
Save hjJunior/fde505949a1773fbc30961a2da68711c to your computer and use it in GitHub Desktop.
Getting start to learning tree in c
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