Advertisement
Guest User

Tamales...

a guest
Oct 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. template <typename T>
  6. class Node
  7. {
  8. public:
  9. T elem;
  10. Node* izq;
  11. Node* der;
  12. Node(T elem, Node* iz = nullptr, Node* der = nullptr): elem(elem), izq(iz), der(der){}
  13. };
  14.  
  15. template <typename T>
  16. class AVL
  17. {
  18. private:
  19. Node<T>* raiz;
  20. void agregarAVL(T elem, Node<T>*& n)
  21. {
  22. if (n == nullptr)
  23. {
  24. n = new Node<T>(elem);
  25. }else if (elem < n->elem)
  26. {
  27. agregarAVL(elem, n->izq);
  28. }
  29. else if(elem > n->elem){
  30. agregarAVL(elem, n->der);
  31. }
  32. }
  33.  
  34. void borrarAVL(Node<T>* n)
  35. {
  36. if (n != nullptr)
  37. {
  38. borrarAVL(n->izq);
  39. borrarAVL(n->der);
  40. delete n;
  41. }
  42. }
  43.  
  44. void mostrarAVL(Node<T>* n)
  45. {
  46. if (n != nullptr)
  47. {
  48. mostrarAVL(n->izq);
  49. std::cout << n->elem << " -> ";
  50. mostrarAVL(n->der);
  51. }
  52. }
  53. public:
  54. int n1, n2, cont;
  55. AVL() : raiz(nullptr), n1(0), n2(0) { cont = 0; }
  56. ~AVL() { borrarAVL(raiz); }
  57. void mostrarAVL_Public() { mostrarAVL(raiz); }
  58. void agregarAVL_Public(T elem) { agregarAVL(elem, raiz); }
  59. double Operaciones() { return OperacionesPrivate(raiz); }
  60.  
  61. void agregarEspecialPrivate(T elem, T elem1, T elem2)
  62. {
  63. raiz = new Node<T>(elem);
  64. raiz->izq = new Node<T>(elem1);
  65. raiz->der = new Node<T>(elem2);
  66. }
  67. double OperacionesPrivate(Node<T>* n)
  68. {
  69. if (n != nullptr)
  70. {
  71.  
  72. std::string a = n->izq->elem;
  73. n1 = std::atoi(a.c_str());
  74. std::cout << "\nA: " << a;
  75.  
  76.  
  77. std::string b = n->der->elem;
  78. n2 = std::atoi(b.c_str());
  79. std::cout << "\nB: " << b;
  80.  
  81. std::string c = n->elem;
  82. if (c == "*")
  83. {
  84. return (n1 * n2);
  85. }
  86. else if (c == "+")
  87. {
  88. return (n1 + n2);
  89. }
  90. else if (c == "-")
  91. {
  92. return (n1 - n2);
  93. }
  94. else if (c == "/")
  95. {
  96. return (n1 / n2);
  97. }
  98. std::cout << "\nOperador: " << c;
  99.  
  100. }
  101. }
  102. };
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. #include <iostream>
  111. #include <string>
  112. #include <map>
  113. #include "Header.h"
  114. #include "AVL.h"
  115. int main()
  116. {
  117. /*int a[] = { 1,6,2,7,3,8,4,9,5,10 };
  118. sort(a,0, 10,10);
  119. for (int i = 0; i < 10; ++i)
  120. std::cout << a[i] << " -> ";
  121. */
  122. AVL<std::string>* avl = new AVL<std::string>();
  123. avl->agregarEspecialPrivate("/", "5", "2");
  124. avl->mostrarAVL_Public();
  125. std::cout << "El resultado es: " << avl->Operaciones();
  126. delete avl;
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement