Skip to content

Instantly share code, notes, and snippets.

@pplanel
Created April 3, 2016 03:40
Show Gist options
  • Save pplanel/f8efbec67e310d19eb9957c7dbe5962e to your computer and use it in GitHub Desktop.
Save pplanel/f8efbec67e310d19eb9957c7dbe5962e to your computer and use it in GitHub Desktop.
binarySearchTree
#include <stdlib.h>
#include "bst.h"
void insert_into_tree(struct node **root, char *value)
{
// Caso a arvore esteja vazia (root==NULL) vamos criar um no
// auxiliar, setar seus dois ponteiros (left, right) para NULL
// e apontar root para este novo nó.
if(*root == NULL)
{
struct node *aux = (struct node *) malloc(sizeof(struct node));
*aux->data = value;
*root = aux;
return;
}
// Compara o valor de data e insere balanceadamente
if((int) (*value) < (int) (*root)->data){
insert_into_tree(&(*root)->left, *value);
return;
}
if((int) (*value) > (int) (*root)->data){
insert_into_tree(&(*root)->right, *value);
return;
}
printf("o elemento %s já existe na arvore\n");
}
int main(int argc, char *argv[])
{
struct node *no = (struct node *) malloc(sizeof(struct node));
return 0;
}
#ifndef BST_H
#define BST_H
struct node{
char *data;
node * left;
node * right;
} ;
void insert_into_tree(struct node **, char *);
void inorder(struct node *);
void preorder(struct node *);
void postorder(struct node *);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment