Advertisement
Guest User

PRACTICA2SHHH

a guest
Mar 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. /*
  2.  * arbolbb.c
  3.  *
  4.  *  Created on: 21 mar. 2019
  5.  *      Author: alumno
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <errno.h>
  12.  
  13. #include "arbolbb.h"
  14.  
  15.  
  16.  
  17. void crear(T_Arbol* arbol){
  18.  
  19.     (*arbol)=NULL;
  20.  
  21. }
  22. // Destruye la estructura utilizada y libera la memoria.
  23. void destruir(T_Arbol* arbol){
  24.  
  25.     if((*arbol)!=NULL){
  26.         destruir(&(*arbol)->izq);
  27.         destruir(&(*arbol)->der);
  28.         free((void*)arbol);
  29.     }
  30.  
  31. }
  32. // Inserta num en el árbol. Si ya está insertado, no hace nada
  33. void insertar(T_Arbol* arbol,unsigned num){
  34.  
  35.     if((*arbol)==NULL){
  36.         (*arbol)=(T_Arbol)malloc(sizeof(struct T_Nodo));
  37.         (*arbol)->dato=num;
  38.         (*arbol)->izq=NULL;
  39.         (*arbol)->der=NULL;
  40.     }else{
  41.         if((*arbol)->dato < num ){
  42.             insertar(&(*arbol)->der,num);
  43.         }else if((*arbol)->dato > num){
  44.             insertar(&(*arbol)->izq,num);
  45.         }
  46.     }
  47.  
  48. }
  49. // Muestra el contenido del árbol en InOrden
  50. void mostrar(T_Arbol arbol){
  51.  
  52.     if(arbol != NULL){
  53.  
  54.         mostrar(arbol->izq);
  55.         printf("%d\t",arbol->dato);
  56.         mostrar(arbol ->der);
  57.  
  58.  
  59.     }
  60.  
  61. }
  62. // Guarda en disco el contenido del fichero
  63. void salvar(T_Arbol arbol, FILE* fichero){
  64.  
  65.     if(fichero != NULL){
  66.  
  67.         if(arbol != NULL){
  68.  
  69.                 salvar(arbol->izq,fichero);
  70.                 fwrite(&(arbol->dato),sizeof(int),1,fichero);
  71.                 salvar(arbol ->der,fichero);
  72.  
  73.             }
  74.  
  75.     }else
  76.         perror("Fichero recibido nulo \n");
  77.  
  78.     fclose(fichero);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement