Home » News » Binary Tree in C Language

Binary Tree in C Language

In Computer Science, a binary search tree is a data structure based on nodes in which all nodes in the left subtree (left child) have a numeric value lower the root node value, and all nodes of the right (right child) have a higher value than the root node value. The aim of this tree is to structure the data in a flexible way, allowing binary search.

void inserir(tArv *t, int dado){ int ok; if (*t == NULL) { *t = (tnoarv *) malloc(sizeof(tnoarv)); if (*t == NULL) return; (*t)->esq = NULL; (*t)->dir = NULL; (*t)->info = dado; } if (dado < (*t)->info) { inserir(&((*t)->esq), dado); } else{ if (dado > (*t)->info) { inserir(&((*t)->dir), dado); } } }

Function to remove a value from a binary tree:

void remover(tArv *raiz, int valor){   
    tArv aux;   
    if(!arvore_vazia(*raiz)){   
       if(valor < (*raiz)->info){    
           remover(&((*raiz)->esq), valor);   
       }else{   
            if(valor > (*raiz)->info){    
                 remover(&((*raiz)->dir), valor);  
             }else{     
                if( !arvore_vazia((*raiz)->esq) && !arvore_vazia((*raiz)->dir) ){      
                     aux = minimo((*raiz)->dir);    
                     (*raiz)->info = (aux->info);   
                     remover(&(*raiz)->dir, (*raiz)->info);   
                }else{    
                       aux = *raiz;       
                       if(arvore_vazia((*raiz)->esq)){    
                           *raiz = (*raiz)->dir;   
                       }    
                       else {     
                            *raiz = (*raiz)->esq;                          
                       }   
                free(aux);   
                }   
            }        
       }    
    }     
}

Minimum function returns the node with minimum value:

tArv minimo(tArv T){  
    if(arvore_vazia(T)){   
       return NULL;   
    }else{   
          if( arvore_vazia(T->esq) ){   
              return T;   
          }else{   
              return minimo(T->esq);   
          }   
    }   
}

Maximum function returns the node with the maximum value:

tArv maximo(tArv T){  
     if( !arvore_vazia(T) ){   
       while( !arvore_vazia(T->dir) ){   
          T = T->dir;   
       }   
     }   
       return T;   
}

Function to search for a value in the binary tree:

tArv busca_elemento(tArv t, int dado){   
  
     tArv achou;   
     if (arvore_vazia(t)) return NULL;   
     if (t->info == dado) return t;   
         achou = busca_elemento(t->esq, dado);   
     if (arvore_vazia(achou))   
         achou = busca_elemento(t->dir, dado);   
     return achou;   
}

 

If you want to *download this project (binary tree in C) -> [download id=”168″].

*Note that to download you must register/login in this website.

 

Thanks for reading.
Like and Share if you found it may be useful to someone you know!

Would you like to check the Portuguese version?
Click on the Brazilian flag ->

Deixe um comentário