eduardovp97

ex7.c

Nov 14th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "tree.h"
  4.  
  5. void duplicarArbol(TNode *node1, TTree *arbol2);
  6. int insertarNodo2(TNode *node,TNode *nuevo);
  7. int insertar2(TTree *tree, TData data);
  8.  
  9. int main(){
  10.  
  11.     TTree arbol;
  12.     TTree arbol2;
  13.     crearArbol(&arbol);
  14.     crearArbol(&arbol2);
  15.     insertar(&arbol,2);
  16.     insertar(&arbol,1);
  17.     insertar(&arbol,3);
  18.  
  19.     duplicarArbol(arbol.root,&arbol2);
  20.     preorden(arbol2.root);
  21.  
  22.     return 0;
  23. }
  24.  
  25. void duplicarArbol(TNode *node1, TTree *arbol2){
  26.     if(node1 == NULL)
  27.         return ;
  28.     insertar2(arbol2,node1->info);
  29.     insertar2(arbol2,node1->info);
  30.     duplicarArbol(node1->izq,arbol2);
  31.     duplicarArbol(node1->der,arbol2);
  32. }
  33.  
  34. int insertarNodo2(TNode *node,TNode *nuevo){
  35.     if(node->info <= nuevo->info){
  36.         if(node->der == NULL){
  37.             node->der = nuevo;
  38.             return 1;
  39.         }else
  40.             return insertarNodo2(node->der,nuevo);
  41.     }
  42.     if(node->info > nuevo->info){
  43.         if(node->izq == NULL){
  44.             node->izq = nuevo;
  45.             return 1;
  46.         }else
  47.             return insertarNodo2(node->izq,nuevo);
  48.     }
  49.    
  50. }
  51.  
  52. int insertar2(TTree *tree, TData data){
  53.     TNode *nuevo = malloc(sizeof(TNode));
  54.     nuevo->info = data;
  55.     nuevo->izq = NULL;
  56.     nuevo->der = NULL;
  57.     if(arbolVacio(tree)){
  58.         tree->root = nuevo;
  59.     }else{
  60.         insertarNodo2(tree->root,nuevo);
  61.     }
  62. }
Add Comment
Please, Sign In to add comment