my code stock.com

A simple routine for insertion in a Binary search tree

Snippet options

Download: Download snippet as binary-tree-insertion.c.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

//coded by htp://twitter.com/gccgomes

typedef struct Folha{
  char word[TMAX]; 
  int cnt;
  struct Folha  *l, *r; //left and right leaf
}*tipoArvore;

tipoArvore insere( char *X, tipoArvore T ){
  if( T == NULL ){   // create a sub tree
    T = malloc( sizeof( struct Folha ) );
    if( T == NULL )
      printf( "sem memoria\n");
    else{
      strcpy(T->word, X);
      T->e = T->d = NULL;
      T->cnt=1;
    }
  }
  else 
    if( strcmp(X,T->word)<0) // insert on left tree
      T->e = insere( X, T->l );
    if( strcmp(X, T->word)>0) // insert on rigth tree
      T->d = insere( X, T->r ); 
	if( strcmp(X, T->word)==0)
       T->cnt++;// count the word
  return T;  //return the tree
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.