Advertisement
ambigus9

ABB.Final.1

Oct 16th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. // Arboles_Binarios.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include "iostream"
  5. #include "conio.h"
  6. using namespace System;
  7. using namespace std;
  8.  
  9. class nodoarbol
  10. {
  11. public:
  12. int info;
  13. nodoarbol *der,*izq;
  14. nodoarbol()
  15. {
  16. izq=NULL;
  17. der=NULL;
  18. }
  19. nodoarbol(int dato)
  20. {
  21. info=dato;
  22. izq=NULL;
  23. der=NULL;
  24. }
  25. };
  26. struct nodo
  27. {
  28. nodoarbol *actual;
  29. nodo *sig;
  30. };
  31. class Fila
  32. {
  33. private:
  34. nodo *frente;
  35. nodo *final;
  36. int lon;
  37. public:
  38. Fila();
  39. void meter(nodoarbol *nodo);
  40. char sacar(nodoarbol *&valor);
  41. ~Fila();
  42. void limpiarFila();//Quita todos los elementos de la fila
  43. int frenteFila();//Obtiene el elemento frente de la fila
  44. int longitudFila();//Obtiene la cantidad de elementos de la fila
  45. void desplegarFila();//Muestra los elementos de la fila
  46. };
  47. char Fila::sacar(nodoarbol *&valor)//Quitar (Pop) elementos de una fila
  48. {
  49. nodo *apunt;
  50. apunt=frente;
  51. if(apunt==NULL)return 0;
  52. else
  53. {
  54. lon--;
  55. valor=apunt->actual;
  56. if(frente->sig==NULL)
  57. {
  58. frente=NULL;
  59. final=NULL;
  60. }
  61. else frente=apunt->sig;
  62. delete apunt;
  63. return 1;
  64. }
  65. }
  66. Fila::Fila()
  67. {
  68. frente=NULL;
  69. final=NULL;
  70. lon=0;
  71. }
  72. Fila::~Fila()
  73. {
  74. nodo *apunt;
  75. apunt=frente;
  76. while(frente!=NULL)
  77. {
  78. cout<<"borro"<<endl;
  79. frente=apunt->sig;
  80. delete apunt;
  81. apunt=frente;
  82. }
  83. }
  84. void Fila::meter(nodoarbol *nuevo)//Insertar un nodo
  85. {
  86. nodo *el_nodo=new nodo();
  87. el_nodo->actual=nuevo;
  88. if(final==NULL)frente=el_nodo;
  89. else final->sig=el_nodo;
  90. el_nodo->sig=NULL;
  91. final=el_nodo;
  92. lon++;
  93. }
  94. int Fila::longitudFila()
  95. {
  96. return lon;
  97. }
  98. class ABB
  99. {
  100. private:
  101. nodoarbol *raiz;
  102. public:
  103. ABB()
  104. {
  105. raiz=NULL;
  106. }
  107. ~ABB()
  108. {
  109. cout<<"Entro al destructor"<<endl;
  110. borrar_nodos(raiz);
  111. cout<<"Salgo del destructor"<<endl;
  112. }
  113. void borrar_nodos(nodoarbol *raiz)
  114. {
  115. if(raiz!=NULL)
  116. {
  117. borrar_nodos(raiz->izq);
  118. borrar_nodos(raiz->der);
  119. cout<<"Destructor de "<<raiz->info<<endl;
  120. delete raiz;
  121. }
  122. }
  123. void insertar()
  124. {
  125. nodoarbol *aux1,*aux2,*aux3;
  126. aux1=new nodoarbol;
  127. aux2=raiz;
  128. aux3=NULL;
  129. cout<<"Entre el valor del nodo a insertar:";
  130. cin>>aux1->info;
  131. while(aux2!=NULL)
  132. {
  133. aux3=aux2;
  134. if(aux1->info>aux2->info) aux2=aux2->der;
  135. else
  136. {
  137. if(aux1->info<aux2->info) aux2=aux2->izq;
  138. else
  139. {
  140. cout<<"El nodo a agregar ya existe"<<endl;
  141. break;
  142. }
  143. }
  144. }
  145. if(aux3==NULL)raiz=aux1;
  146. else
  147. if(aux1->info<aux3->info)aux3->izq=aux1;
  148. else
  149. {
  150. if(aux1->info>aux3->info)aux3->der=aux1;
  151. else delete aux1;
  152. }
  153. }
  154. nodoarbol *devolver_raiz()
  155. {
  156. return raiz;
  157. }
  158. void despliega(nodoarbol *raiz)
  159. {
  160. if(raiz!=NULL)
  161. {
  162. despliega(raiz->izq);
  163. cout<<raiz->info<<endl;
  164. despliega(raiz->der);
  165. }
  166. }
  167. void despliega2(nodoarbol *raiz)
  168. {
  169. if(raiz!=NULL)
  170. {
  171. cout<<raiz->info<<endl;
  172. despliega2(raiz->izq);
  173. despliega2(raiz->der);
  174. }
  175. }
  176. void despliega3(nodoarbol *raiz)
  177. {
  178. if(raiz!=NULL)
  179. {
  180. despliega3(raiz->izq);
  181. despliega3(raiz->der);
  182. cout<<raiz->info<<endl;
  183. }
  184. }
  185. void despliega4(nodoarbol *raiz)
  186. {
  187. if(raiz!=NULL)
  188. {
  189. despliega(raiz->der);
  190. cout<<raiz->info<<endl;
  191. despliega(raiz->izq);
  192. }
  193. }
  194. void despliega5(nodoarbol *raiz)
  195. {
  196. if(raiz!=NULL)
  197. {
  198. cout<<raiz->info<<endl;
  199. despliega2(raiz->der);
  200. despliega2(raiz->izq);
  201. }
  202. }
  203. void despliega6(nodoarbol *raiz)
  204. {
  205. if(raiz!=NULL)
  206. {
  207. despliega3(raiz->der);
  208. despliega3(raiz->izq);
  209. cout<<raiz->info<<endl;
  210. }
  211. }
  212.  
  213. void despliega7(nodoarbol *raiz)
  214. {
  215. Fila f;
  216. nodoarbol *apunt;
  217. if(raiz!=NULL)
  218. {
  219. f.meter(raiz);
  220. while(f.longitudFila()>0)
  221. {
  222. f.sacar(apunt);
  223. cout<<apunt->info<<endl;
  224. if(apunt->izq!=NULL)f.meter(apunt->izq);
  225. if(apunt->der!=NULL)f.meter(apunt->der);
  226. }
  227. }
  228. }
  229. void eliminar_nodo(int valor)
  230. {
  231. int hijo;
  232. nodoarbol *aux1, *aux2, *temp;
  233. bool b;
  234. //inicio de la busqueda del nodo a eliminar para esta parte
  235.  
  236. if(raiz != NULL)
  237. {
  238. aux1= raiz;
  239. aux2=NULL;
  240.  
  241. while(aux1->info != valor)
  242. {
  243. aux2=aux1;
  244. if(valor<aux1->info) aux1=aux1->izq;
  245. else aux1=aux1->der;
  246. if(aux1==NULL)
  247. {
  248. cout<<"el nodo a eliminar no existe"<<endl;
  249. break;
  250. }
  251. }
  252. }
  253. else
  254. {
  255. cout<<"el arbol no contiene nodos"<<endl;
  256. aux1=NULL;
  257. }
  258. if(aux1 != NULL)
  259. {
  260. temp = aux1;
  261. if((aux1->izq==NULL)&&(aux1->der==NULL))
  262. {
  263. if(aux2 != NULL)
  264. {
  265. if(aux1->info<aux2->info) aux2->der=NULL;
  266. else aux2->izq=NULL;
  267. }
  268. else
  269. {
  270. raiz = NULL;
  271. }
  272. }
  273. else
  274. {
  275. if((aux1->izq!=NULL)&&(aux1->der!=NULL))
  276. {//tiene dos hijos
  277. cout<<"el nodo padre a eliminiar tiene dos hijos"<<endl;
  278. cout<<"si desea reemplazarlo por parte del hijo menor dijite ( 1 ) "<<endl;
  279. cout<<"si desea reemplazarlo por parte del hijo meyor dijite ( 2 ) "<<endl;
  280. cin>>hijo;
  281. if(hijo == 1)
  282. {
  283. aux2=aux1;
  284. temp=aux1->izq;
  285. b=true;
  286. while(temp->der!=NULL)
  287. {
  288. aux2=temp;
  289. temp=temp->der;
  290. b=false;
  291. }
  292. aux1->info=temp->info;
  293. if(b==true)aux1->izq=temp->izq;
  294. else
  295. {
  296. if(temp->izq!=NULL) aux2->der=temp->izq;
  297. else aux2->der=NULL;
  298. }
  299. }
  300. else
  301. {
  302. aux2=aux1;
  303. temp=aux1->der;
  304. b=true;
  305. while(temp->izq!=NULL)
  306. {
  307. aux2=temp;
  308. temp=temp->izq;
  309. b=false;
  310. }
  311. aux1->info=temp->info;
  312. if(b==true)aux1->der=temp->der;
  313. else
  314. {
  315. if(temp->der!=NULL) aux2->izq=temp->der;
  316. else aux2->izq=NULL;
  317. }
  318. }
  319. }
  320. else
  321. {
  322. if(aux1->izq==NULL)
  323. if(aux2 != NULL)
  324. {
  325. if(aux1->info<aux2->info)
  326. aux2->izq=aux1->der;
  327. else aux2->der=aux1->der;
  328. }
  329. else raiz=aux1->der;
  330. else
  331. if(aux2!=NULL)
  332. {
  333. if(aux1->info<aux2->info)
  334. aux2->izq=aux1->izq;
  335. else aux2->der=aux1->izq;
  336. }
  337. else raiz=aux1->izq;
  338. }
  339. }
  340. delete temp;
  341. }
  342. }
  343. };
  344.  
  345. void main()
  346. {//Console::Title::set(" PROGRAMADOR: JULIAN VILLAMIZAR ");
  347. //Console::ForegroundColor::set(ConsoleColor::Red);
  348. cout<<"\n\t\t\t !BIENVENIDO USUARIO!\n\n";
  349. //Console::ForegroundColor::set(ConsoleColor::White);
  350.  
  351. nodoarbol *raiz;
  352. char resp, resp3;
  353. int resp1, resp2, valor;
  354. ABB n;
  355. do
  356. {
  357. n.insertar();
  358. cout<<"Desea crear otro nodo (s/n): ";
  359. cin>>resp;
  360. cout<<endl;
  361. resp=tolower(resp);
  362. }while(resp!='n');
  363. //char resp;
  364. do{
  365. cout<<endl;
  366.  
  367. cout<<"Por favor digite, como desea desplegar el arbol ?"<<endl<<endl;
  368. cout<<"1. Desplegar el arbol en Preorden"<<endl;
  369. cout<<"2. Desplegar el arbol en Inorden"<<endl;
  370. cout<<"3. Desplegar el arbol en Postorden"<<endl;
  371. cout<<"4. Desplegar el arbol en Preorden converso"<<endl;
  372. cout<<"5. Desplegar el arbol en Inorden converso"<<endl;
  373. cout<<"6. Desplegar el arbol en Postorden converso"<<endl;
  374. cout<<"7. Desplegar el arbol en Nivel por nivel"<<endl;
  375. cout<<"0. Salir"<<endl;
  376. cout<<endl;
  377. cin>>resp1;
  378. switch(resp1)
  379. {
  380. case 1:
  381. cout<<"Preorden"<<endl;
  382. raiz=n.devolver_raiz();
  383. n.despliega2(raiz);
  384. break;
  385. case 2:
  386. cout<<"Inorden"<<endl;
  387. raiz=n.devolver_raiz();
  388. n.despliega(raiz);
  389. break;
  390. case 3:
  391. cout<<"Postorden"<<endl;
  392. raiz=n.devolver_raiz();
  393. n.despliega3(raiz);
  394. break;
  395. case 4:
  396. cout<<"Preorden converso"<<endl;
  397. raiz=n.devolver_raiz();
  398. n.despliega5(raiz);
  399. break;
  400. case 5:
  401. cout<<"Inorden converso"<<endl;
  402. raiz=n.devolver_raiz();
  403. n.despliega4(raiz);
  404. break;
  405. case 6:
  406. cout<<"Postorden converso"<<endl;
  407. raiz=n.devolver_raiz();
  408. n.despliega6(raiz);
  409. break;
  410. case 7:
  411. cout<<"Nivel por nivel"<<endl;
  412. raiz=n.devolver_raiz();
  413. n.despliega7(raiz);
  414. break;
  415. case 0:
  416. cout<<"Continue"<<endl<<endl;
  417. break;
  418. default:
  419. cout<<"Opcion no valida"<<endl;
  420. }
  421. }while(resp1!=0);
  422.  
  423. do{
  424. cout<<"Entre el valor del nodo a eliminar"<<endl;
  425. cin>>valor;
  426. n.eliminar_nodo(valor);
  427. cout<<endl;
  428. //char resp;
  429. do{
  430. raiz=n.devolver_raiz();
  431. cout<<endl;
  432. cout<<"Por favor digite, como desea que se despliegue el arbol ?"<<endl<<endl;
  433. cout<<"1. Desplegar el arbol en Preorden"<<endl;
  434. cout<<"2. Desplegar el arbol en Inorden"<<endl;
  435. cout<<"3. Desplegar el arbol en Postorden"<<endl;
  436. cout<<"4. Desplegar el arbol en Preorden converso"<<endl;
  437. cout<<"5. Desplegar el arbol en Inorden converso"<<endl;
  438. cout<<"6. Desplegar el arbol en Postorden converso"<<endl;
  439. cout<<"7. Desplegar el arbol en Nivel por nivel"<<endl;
  440. cout<<"0. Salir"<<endl;
  441. cout<<endl;
  442. cin>>resp2;
  443. switch(resp2)
  444. {
  445. case 1:
  446. cout<<"Preorden"<<endl;
  447. raiz=n.devolver_raiz();
  448. n.despliega2(raiz);
  449. break;
  450. case 2:
  451. cout<<"Inorden"<<endl;
  452. raiz=n.devolver_raiz();
  453. n.despliega(raiz);
  454. break;
  455. case 3:
  456. cout<<"Postorden"<<endl;
  457. raiz=n.devolver_raiz();
  458. n.despliega3(raiz);
  459. break;
  460. case 4:
  461. cout<<"Preorden converso"<<endl;
  462. raiz=n.devolver_raiz();
  463. n.despliega5(raiz);
  464. break;
  465. case 5:
  466. cout<<"Inorden converso"<<endl;
  467. raiz=n.devolver_raiz();
  468. n.despliega4(raiz);
  469. break;
  470. case 6:
  471. cout<<"Postorden converso"<<endl;
  472. raiz=n.devolver_raiz();
  473. n.despliega6(raiz);
  474. break;
  475. case 7:
  476. cout<<"Nivel por nivel"<<endl;
  477. raiz=n.devolver_raiz();
  478. n.despliega7(raiz);
  479. break;
  480. case 0:
  481. cout<<"Continue"<<endl<<endl;
  482. break;
  483. default:
  484. cout<<"Opcion Invalida. Intentelo de nuevo"<<endl;
  485. }
  486. }while(resp2!=0);
  487.  
  488. cout<<"Desea eliminar otro nodo (s/n):"<<endl;
  489. cin>>resp3;
  490. cout<<endl;
  491. resp=tolower(resp);
  492. }while(resp3!='n');
  493. getch();
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement