elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Árbol
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Árbol  (Leído 1,666 veces)
CGB

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Árbol
« en: 14 Diciembre 2015, 23:45 pm »

Hola, quisiera saber como se podría agregar una cadena, que acepte cadena y entero, los dos; por ejemplo:
-Ingrese nombre:
pedro
-Ingrese edad:
10
y que se visualice como: pedro-10

Les dejo el código por si tienen alguna idea o la solución :P
Gracias :D

Código
  1. #include <iostream>
  2. #include<cctype>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int element;
  11. node *left;
  12. node *right;
  13. int altura;
  14. };
  15. typedef struct node *nodeptr;
  16. class bsarbol
  17. {
  18. public:
  19. void insert(int,nodeptr &);
  20. void del(int, nodeptr &);
  21. int deletemin(nodeptr &);
  22. void find(int,nodeptr &);
  23. nodeptr findmin(nodeptr);
  24. nodeptr findmax(nodeptr);
  25. void makeempty(nodeptr &);
  26. void copy(nodeptr &,nodeptr &);
  27. nodeptr nodecopy(nodeptr &);
  28. void preorder(nodeptr);
  29. void inorder(nodeptr);
  30. void postorder(nodeptr);
  31. int bsheight(nodeptr);
  32. nodeptr srl(nodeptr &);
  33. nodeptr drl(nodeptr &);
  34. nodeptr srr(nodeptr &);
  35. nodeptr drr(nodeptr &);
  36. int max(int,int);
  37. int nonodes(nodeptr);
  38. };
  39. // Inserting a node
  40. void bsarbol::insert(int x,nodeptr &p)
  41. {
  42. if (p == NULL)
  43. {
  44. p = new node;
  45. p->element = x;
  46. p->left=NULL;
  47. p->right = NULL;
  48. p->altura=0;
  49. if (p==NULL)
  50. {
  51. cout<<"Fuera de rango\n"<<endl;
  52. }
  53. }
  54. else
  55. {
  56. if (x<p->element)
  57. {
  58. insert(x,p->left);
  59. if ((bsheight(p->left) - bsheight(p->right))==2)
  60. {
  61. if (x < p->left->element)
  62. {
  63. p=srl(p);
  64. }
  65. else
  66. {
  67. p = drl(p);
  68. }
  69. }
  70. }
  71. else if (x>p->element)
  72. {
  73. insert(x,p->right);
  74. if ((bsheight(p->right) - bsheight(p->left))==2)
  75. {
  76. if (x > p->right->element)
  77. {
  78. p=srr(p);
  79. }
  80. else
  81. {
  82. p = drr(p);
  83. }
  84. }
  85. }
  86. else
  87. {
  88. cout<<"El elemento existe\n"<<endl;
  89. }
  90. }
  91. int m,n,d;
  92. m=bsheight(p->left);
  93. n=bsheight(p->right);
  94. d=max(m,n);
  95. p->altura = d + 1;
  96. }
  97. // Finding the Smallest
  98. nodeptr bsarbol::findmin(nodeptr p)
  99. {
  100. if (p==NULL)
  101. {
  102. cout<<"El arbol esta vacio\n"<<endl;
  103. return p;
  104. }
  105. else
  106. {
  107. while(p->left !=NULL)
  108. {
  109. p=p->left;
  110. //return p;
  111. }
  112. return p;
  113. }
  114. }
  115. // Finding the Largest node
  116. nodeptr bsarbol::findmax(nodeptr p)
  117. {
  118. if (p==NULL)
  119. {
  120. cout<<"El arbol esta vacio\n"<<endl;
  121. return p;
  122. }
  123. else
  124. {
  125. while(p->right !=NULL)
  126. {
  127. p=p->right;
  128. //return p;
  129. }
  130. return p;
  131. }
  132. }
  133. // Finding an element
  134. void bsarbol::find(int x,nodeptr &p)
  135. {
  136. if (p==NULL)
  137. {
  138. cout<<"Elemento no encontrado \n"<<endl;
  139. }
  140. else
  141. {
  142. if (x < p->element)
  143. {
  144. find(x,p->left);
  145. }
  146. else
  147. {
  148. if (x>p->element)
  149. {
  150. find(x,p->right);
  151. }
  152. else
  153. {
  154. cout<<"Elemento encontrado\n"<<endl;
  155. }
  156. }
  157. }
  158. }
  159. // Copy a tree
  160. void bsarbol::copy(nodeptr &p,nodeptr &p1)
  161. {
  162. makeempty(p1);
  163. p1 = nodecopy(p);
  164. }
  165. // Make a tree empty
  166. void bsarbol::makeempty(nodeptr &p)
  167. {
  168. nodeptr d;
  169. if (p != NULL)
  170. {
  171. makeempty(p->left);
  172. makeempty(p->right);
  173. d=p;
  174. free(d);
  175. p=NULL;
  176. }
  177. }
  178. // Copy the nodes
  179. nodeptr bsarbol::nodecopy(nodeptr &p)
  180. {
  181. nodeptr temp;
  182. if (p==NULL)
  183. {
  184. return p;
  185. }
  186. else
  187. {
  188. temp = new node;
  189. temp->element = p->element;
  190. temp->left = nodecopy(p->left);
  191. temp->right = nodecopy(p->right);
  192. return temp;
  193. }
  194. }
  195.  
  196. // Deleting a node
  197. void bsarbol::del(int x,nodeptr &p)
  198. {
  199. nodeptr d;
  200. if (p==NULL)
  201. {
  202. cout<<"Elemnto no encontrado\n"<<endl;
  203. }
  204. else if ( x < p->element)
  205. {
  206. del(x,p->left);
  207. }
  208. else if (x > p->element)
  209. {
  210. del(x,p->right);
  211. }
  212. else if ((p->left == NULL) && (p->right == NULL))
  213. {
  214. d=p;
  215. free(d);
  216. p=NULL;
  217. cout<<"Elemento borrado\n"<<endl;
  218. }
  219. else if (p->left == NULL)
  220. {
  221. d=p;
  222. free(d);
  223. p=p->right;
  224. cout<<"Elemento borrado\n"<<endl;
  225. }
  226. else if (p->right == NULL)
  227. {
  228. d=p;
  229. p=p->left;
  230. free(d);
  231. cout<<"Elemento borrado\n"<<endl;
  232. }
  233. else
  234. {
  235. p->element = deletemin(p->right);
  236. }
  237. }
  238.  
  239. int bsarbol::deletemin(nodeptr &p)
  240. {
  241. int c;
  242. cout<<"inside deltemin\n"<<endl;
  243. if (p->left == NULL)
  244. {
  245. c=p->element;
  246. p=p->right;
  247. return c;
  248. }
  249. else
  250. {
  251. c=deletemin(p->left);
  252. return c;
  253. }
  254. }
  255. void bsarbol::preorder(nodeptr p)
  256. {
  257. if (p!=NULL)
  258. {
  259. cout<<p->element<<"\t";
  260. preorder(p->left);
  261. preorder(p->right);
  262. }
  263. }
  264.  
  265. // Inorder Printing
  266. void bsarbol::inorder(nodeptr p)
  267. {
  268. if (p!=NULL)
  269. {
  270. inorder(p->left);
  271. cout<<p->element<<"\t";
  272. inorder(p->right);
  273. }
  274. }
  275.  
  276. // PostOrder Printing
  277. void bsarbol::postorder(nodeptr p)
  278. {
  279. if (p!=NULL)
  280. {
  281. postorder(p->left);
  282. postorder(p->right);
  283. cout<<p->element<<"\t";
  284. }
  285. }
  286.  
  287. int bsarbol::max(int value1, int value2)
  288. {
  289. return ((value1 > value2) ? value1 : value2);
  290. }
  291. int bsarbol::bsheight(nodeptr p)
  292. {
  293. int t;
  294. if (p == NULL)
  295. {
  296. return -1;
  297. }
  298. else
  299. {
  300. t = p->altura;
  301. return t;
  302. }
  303. }
  304.  
  305. nodeptr bsarbol:: srl(nodeptr &p1)
  306. {
  307. nodeptr p2;
  308. p2 = p1->left;
  309. p1->left = p2->right;
  310. p2->right = p1;
  311. p1->altura = max(bsheight(p1->left),bsheight(p1->right)) + 1;
  312. p2->altura = max(bsheight(p2->left),p1->altura) + 1;
  313. return p2;
  314. }
  315. nodeptr bsarbol:: srr(nodeptr &p1)
  316. {
  317. nodeptr p2;
  318. p2 = p1->right;
  319. p1->right = p2->left;
  320. p2->left = p1;
  321. p1->altura = max(bsheight(p1->left),bsheight(p1->right)) + 1;
  322. p2->altura = max(p1->altura,bsheight(p2->right)) + 1;
  323. return p2;
  324. }
  325. nodeptr bsarbol:: drl(nodeptr &p1)
  326. {
  327. p1->left=srr(p1->left);
  328. return srl(p1);
  329. }
  330. nodeptr bsarbol::drr(nodeptr &p1)
  331. {
  332. p1->right = srl(p1->right);
  333. return srr(p1);
  334. }
  335.  
  336. int bsarbol::nonodes(nodeptr p)
  337. {
  338. int count=0;
  339. if (p!=NULL)
  340. {
  341. nonodes(p->left);
  342. nonodes(p->right);
  343. count++;
  344. }
  345. return count;
  346. }
  347.  
  348. int main()
  349. {
  350. //clrscr();
  351. nodeptr root,root1,min,max;//,flag;
  352. int a,op,findele,delele;
  353. char ch='y';
  354. bsarbol bst;
  355.  
  356. //system("clear");
  357. root = NULL;
  358. root1=NULL;
  359. cout<<"\n\t\t\t\tBIENVENIDO A AVL "<<endl;
  360. cout<<"\t\t\t\t:::::::::::::::::::\n"<<endl;
  361.  
  362. do
  363. {
  364. cout<<"\t\t::::::::::::::::::::::::::::::::::::::::::::::::"<<endl;
  365. cout<<"\t\t::::Ingrese 1 para nuevo nodo::::::::::::::::"<<endl;
  366. cout<<"\t\t::::Ingrese 2 para encontrar el minimo valor:::::::::::"<<endl;
  367. cout<<"\t\t::::Ingrese 3 para encontrar maximo valor:::::::::::::::"<<endl;
  368. cout<<"\t\t::::Ingrese 4 para buscar un valor:::::::::::::::::::"<<endl;
  369. cout<<"\t\t::::Ingrese 5 para borrar un valor:::::::::::::::::::"<<endl;
  370. cout<<"\t\t::::Ingrese 6 para mostrar Preorden:::::::::::::::::"<<endl;
  371. cout<<"\t\t::::Ingrese 7 para mostrar Inorden::::::::::::::::::"<<endl;
  372. cout<<"\t\t::::Ingrese 8 para mostara Postorden::::::::::::::::"<<endl;
  373. cout<<"\t\t::::Ingrese 9 para mostrar la altura del arbol:::"<<endl;
  374. cout<<"\t\t::::Ingrese 0 para salir:::::::::::::::::::::::::::::"<<endl;
  375. cout<<"\t\t::::::::::::::::::::::::::::::::::::::::::::::::\n"<<endl;
  376.  
  377. cout<<"\nIngrese una opcion: ";
  378. cin>>op;
  379.  
  380. switch(op)
  381. {
  382. case 1:
  383. cout<<"\n\t\tAGREGANDO NUEVO NODO"<<endl;
  384. cout<<"\t\t:::::::::::::\n"<<endl;
  385. cout<<"Ingrese un valor: ";
  386. cin>>a;
  387. bst.insert(a,root);
  388. cout<<"\nEl nuevo valor se ha agregado\n"<<endl;
  389. break;
  390. case 2:
  391. if (root !=NULL)
  392. {
  393. min=bst.findmin(root);
  394. cout<<"\nEl minimo elemento del arbol es: "<<min->element<<endl;
  395. }
  396. break;
  397. case 3:
  398. if (root !=NULL)
  399. {
  400. max=bst.findmax(root);
  401. cout<<"\nEl maximo elemento del arbol es: "<<max->element<<endl;
  402. }
  403. break;
  404. case 4:
  405. cout<<"\nNodo a buscar: ";
  406. cin>>findele;
  407. if (root != NULL)
  408. {
  409. bst.find(findele,root);
  410. }
  411. break;
  412. case 5:
  413. cout<<"\nNodo a borrar: ";
  414. cin>>delele;
  415. bst.del(delele,root);
  416. bst.inorder(root);
  417. cout<<endl;
  418. break;
  419. case 6:
  420. cout<<"\n\t\tPRE-ORDER "<<endl;
  421. bst.preorder(root);
  422. cout<<endl;
  423. break;
  424. case 7:
  425. cout<<"\n\t\tIN-ORDER "<<endl;
  426. bst.inorder(root);
  427. cout<<endl;
  428. break;
  429. case 8:
  430. cout<<"\n\t\tPOST ORDER "<<endl;
  431. bst.postorder(root);
  432. cout<<endl;
  433. break;
  434. case 9:
  435. cout<<"\n\t\ALTURA\n"<<endl;
  436. cout<<"La altura del arbol es: "<<bst.bsheight(root)<<endl;
  437.  
  438. break;
  439. case 0:
  440. cout<<"\n\tAdios\n"<<endl;
  441. break;
  442. default:
  443. cout<<"Intente otra vez\n"<<endl;
  444. break;
  445. }
  446. system("pause");
  447. system("cls");
  448. }while(op != 0);
  449. return 0;
  450. }
  451.  


Mod: Los códigos deben ir en etiquetas GeSHi


« Última modificación: 14 Diciembre 2015, 23:51 pm por engel lex » En línea

DarK_FirefoX


Desconectado Desconectado

Mensajes: 1.263


Be the change you wanna see in te world


Ver Perfil
Re: Árbol
« Respuesta #1 en: 15 Diciembre 2015, 15:05 pm »

Y entonces? Pedazo de código y ya? Por favor esmerate más en redactar tu pregunta o tus dudas o nadie podrá ayudarte. ¿Que tiene que ver lo que dices arriba con la clase árbol que expones? (supongo que esa clase este bien diseñada)

Salu2s


En línea

0xFer


Desconectado Desconectado

Mensajes: 400



Ver Perfil
Re: Árbol
« Respuesta #2 en: 15 Diciembre 2015, 20:16 pm »

Convierte el número a string;

Código
  1. int Numero = 10;
  2. string NumeroString;
  3.  
  4. stringstream convert;
  5. convert << Numero;
  6. NumeroString = convert.str();

y luego nadamás concatena la cadena "pedro" con el 10
Código
  1. string nom = "pedro";
  2. string cadena = nom + NumeroString;

No era necesario crear un tema, pudiste haber resuelto tu duda googleando

Aunque sospecho que esa no era tu duda, en ese caso explicate mejor  :P
« Última modificación: 15 Diciembre 2015, 20:25 pm por 0xFer » En línea

Código
  1. int getRandomNumber(){
  2.    return 4; //chosen by fair dice roll
  3.              //guaranteed to be random
  4. }
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Mi arbol
Diseño Gráfico
Azielito 1 2,371 Último mensaje 4 Marzo 2004, 02:28 am
por StraTovario
Un arbol con columnas
.NET (C#, VB.NET, ASP)
elmaro 6 4,883 Último mensaje 4 Febrero 2008, 21:39 pm
por MANULOMM
Arbol AVL
Java
arkaos 6 18,939 Último mensaje 31 Mayo 2009, 05:33 am
por arkaos
arbol avl c++
Programación C/C++
jovanny12 2 4,391 Último mensaje 13 Abril 2014, 15:54 pm
por d91
Árbol de preguntas con javascript
Desarrollo Web
pachem 1 1,597 Último mensaje 20 Abril 2016, 00:15 am
por _Enko
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines