Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct tree{
  5.     int n;
  6.     struct tree *dir, *esq;
  7. }Ttree;
  8.  
  9.  
  10. void inserirOrdenado (Ttree **raiz, int n){//ordenado com repetição n recursivo
  11.     Ttree *novo, *aux;
  12.     if(*raiz == NULL){
  13.         novo = (Ttree*)malloc(sizeof(Ttree));
  14.         novo->n = n;
  15.         novo->dir = NULL;
  16.         novo->esq = NULL;
  17.         *raiz = novo;
  18.     }
  19.     else{
  20.         aux = *raiz;
  21.         while(1){
  22.             if(aux->n <= n){
  23.                 if(aux->dir == NULL){
  24.                     novo = (Ttree*)malloc(sizeof(Ttree));
  25.                     novo->n = n;
  26.                     novo->dir = NULL;
  27.                     novo->esq = NULL;
  28.                     aux->dir = novo;
  29.                     return;
  30.                 }
  31.                 aux = aux->dir;
  32.             }
  33.             else if(aux->n > n){
  34.                 if(aux->esq == NULL){
  35.                     novo = (Ttree*)malloc(sizeof(Ttree));
  36.                     novo->n = n;
  37.                     novo->dir = NULL;
  38.                     novo->esq = NULL;
  39.                     aux->esq = novo;
  40.                     return;
  41.                 }
  42.                 aux = aux->esq;
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement