Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. /*--------- Estructura del arbol -------*/
  7. typedef struct nodo{
  8. int nro;
  9. struct nodo *izq, *der;
  10. }*ABB;
  11.  
  12.  
  13. ABB crearNodo(int x);
  14. void insertar(ABB &arbol, int x);
  15. void verArbol(ABB arbol, int n);
  16. bool verificarPertenencia(ABB arbol, int dato);
  17. void menu();
  18. ABB unirConjuntos(ABB x, ABB y);
  19. ABB hacerInterseccion(ABB x, ABB y);
  20. ABB hacerDiferencia(ABB x, ABB y);
  21.  
  22. int main(int argc, char** argv) {
  23. ABB arbol, arbol2, arbolUnion, arbolInterseccion, arbolDif;
  24. arbol = NULL;
  25. arbol2 = NULL;
  26. arbolUnion = NULL;
  27. arbolInterseccion = NULL;
  28. arbolDif = NULL;
  29. int op, op2, x;
  30. char r;
  31. bool bandera, salir;
  32. bandera= false;
  33. salir = true;
  34.  
  35. do{
  36. menu(); cin>> op;
  37. cout << endl;
  38.  
  39. switch(op){
  40. case 1:
  41. cout << "CONJUNTOS INICIALIZADOS" << endl;
  42. bandera = true;
  43. break;
  44. case 2:
  45. if(bandera == true){
  46. cout << "¿Conjunto A o Conjunto B?"<<endl;
  47. cin >> r;
  48. if((r == 'a') || (r == 'A')){
  49. cout << "Ingrese Valor" << endl;
  50. cin >> x;
  51. insertar(arbol, x);
  52. }
  53. if((r == 'b') || (r == 'B')){
  54. cout << "Ingrese Valor" << endl;
  55. cin >> x;
  56. insertar(arbol2, x);
  57. }
  58. }else
  59. cout << "Debe elegir primero la opcion 1" << endl;
  60. break;
  61. case 3:
  62. cout << "Conjunto A" << endl;
  63. verArbol(arbol, 0);
  64. cout << "Conjunto B" << endl;
  65. verArbol(arbol2, 0);
  66. break;
  67.  
  68. case 4:
  69. arbolUnion = unirConjuntos(arbol, arbol2);
  70. verArbol(arbolUnion, 0);
  71. break;
  72. case 5:
  73. arbolInterseccion = hacerInterseccion(arbol, arbol2);
  74. verArbol(arbolInterseccion, 0);
  75. break;
  76.  
  77. case 6:
  78. bool band;
  79. cout<<" Valor a buscar: "; cin>> x;
  80. band = verificarPertenencia(arbol,x);
  81. if(band==1)
  82. cout << "\n\tSi Pertenece...";
  83. else
  84. cout << "\n\tNo Pertenece...";
  85. break;
  86. case 7:
  87. arbolDif = hacerDiferencia(arbol, arbol2);
  88. verArbol(arbolDif, 0);
  89. break;
  90.  
  91. case 8:
  92. cout << " Autor: Marina Soledad, Gonzalez Torres " << endl;
  93. cout << " Fecha: 26 de Noviembre de 2014 " << endl;
  94. salir = false;
  95. break;
  96. }
  97.  
  98. cout<<"\n\n\n";
  99. }while(salir == true);
  100.  
  101.  
  102. return 0;
  103. }
  104.  
  105.  
  106. ABB crearNodo(int x){
  107. ABB nuevoNodo = new(struct nodo);
  108. nuevoNodo->nro = x;
  109. nuevoNodo->izq = NULL;
  110. nuevoNodo->der = NULL;
  111.  
  112. return nuevoNodo;
  113. }
  114.  
  115. void insertar(ABB &arbol, int x){
  116. if(arbol==NULL){
  117. arbol = crearNodo(x); //LLamo al metodo para crear un nodo nuevo en el arbol
  118. }
  119. else{
  120. if(x < arbol->nro)
  121. insertar(arbol->izq, x);
  122. else {
  123. if(x > arbol->nro)
  124. insertar(arbol->der, x);
  125. }
  126. }
  127. }
  128.  
  129. void verArbol(ABB arbol, int n){
  130. if(arbol==NULL)
  131. return;
  132. verArbol(arbol->der, n+1);
  133. for(int i=0; i<n; i++)
  134. cout<<" ";
  135. cout<< arbol->nro <<endl;
  136. verArbol(arbol->izq, n+1);
  137. }
  138.  
  139. bool verificarPertenencia(ABB arbol, int dato){
  140. int r=0; // 0 indica que lo encontre
  141. if(arbol==NULL)
  142. return r;
  143. if(dato<arbol->nro)
  144. r = verificarPertenencia(arbol->izq, dato);
  145.  
  146. else{
  147. if(dato> arbol->nro)
  148. r =verificarPertenencia(arbol->der, dato);
  149. else
  150. r = 1; // son iguales, lo encontre
  151. }
  152. return r;
  153. }
  154.  
  155. void menu(){
  156.  
  157. cout << "\n\t\t ..************ MENÚ ************.. \n\n";
  158. cout << "\t [1] Inicializar Conjuntos A y B \n";
  159. cout << "\t [2] Agregar elementos a los conjuntos A y B \n";
  160. cout << "\t [3] Mostrar los conjuntos A y B \n";
  161. cout << "\t [4] Unir los conjuntos A y B \n";
  162. cout << "\t [5] Interseccion de los conjuntos A y B \n";
  163. cout << "\t [6] Verificar pertenencia (Conjunto A)\n";
  164. cout << "\t [7] Diferencia entre A y B \n";
  165. cout << "\t [8] Salir \n";
  166. cout << "\n\t\t ..************ MENÚ ************.. \n\n";
  167.  
  168. cout << "\n\t Ingrese opcion : ";
  169. }
  170.  
  171. ABB unirConjuntos(ABB x, ABB y){
  172. ABB z;
  173. if(x == NULL)
  174. return y;
  175. if(y == NULL)
  176. return x;
  177. z = unirConjuntos(x->der, y->izq);
  178. x->der = z;
  179. y->izq = x;
  180. return y;
  181. }
  182.  
  183. ABB hacerInterseccion(ABB x, ABB y){
  184. ABB z;
  185. if((x != NULL) && (y != NULL)){
  186. if(y->nro == x->nro)
  187. return y;
  188. z = hacerInterseccion(x->der, y->izq);
  189.  
  190. }
  191. return y;
  192. }
  193.  
  194. ABB hacerDiferencia(ABB x, ABB y){
  195. ABB d;
  196. if(x != NULL){
  197. if(x->nro != y->nro){
  198. return x;
  199. hacerDiferencia(x->izq, y);
  200. hacerDiferencia(x->der, y);
  201. }else{
  202. hacerDiferencia(x->izq, y);
  203. hacerDiferencia(x->der, y);
  204. }
  205. }
  206.  
  207. return d;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement