Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- template <typename T>
- class Node
- {
- public:
- T elem;
- Node* izq;
- Node* der;
- Node(T elem, Node* iz = nullptr, Node* der = nullptr): elem(elem), izq(iz), der(der){}
- };
- template <typename T>
- class AVL
- {
- private:
- Node<T>* raiz;
- void agregarAVL(T elem, Node<T>*& n)
- {
- if (n == nullptr)
- {
- n = new Node<T>(elem);
- }else if (elem < n->elem)
- {
- agregarAVL(elem, n->izq);
- }
- else if(elem > n->elem){
- agregarAVL(elem, n->der);
- }
- }
- void borrarAVL(Node<T>* n)
- {
- if (n != nullptr)
- {
- borrarAVL(n->izq);
- borrarAVL(n->der);
- delete n;
- }
- }
- void mostrarAVL(Node<T>* n)
- {
- if (n != nullptr)
- {
- mostrarAVL(n->izq);
- std::cout << n->elem << " -> ";
- mostrarAVL(n->der);
- }
- }
- public:
- int n1, n2, cont;
- AVL() : raiz(nullptr), n1(0), n2(0) { cont = 0; }
- ~AVL() { borrarAVL(raiz); }
- void mostrarAVL_Public() { mostrarAVL(raiz); }
- void agregarAVL_Public(T elem) { agregarAVL(elem, raiz); }
- double Operaciones() { return OperacionesPrivate(raiz); }
- void agregarEspecialPrivate(T elem, T elem1, T elem2)
- {
- raiz = new Node<T>(elem);
- raiz->izq = new Node<T>(elem1);
- raiz->der = new Node<T>(elem2);
- }
- double OperacionesPrivate(Node<T>* n)
- {
- if (n != nullptr)
- {
- std::string a = n->izq->elem;
- n1 = std::atoi(a.c_str());
- std::cout << "\nA: " << a;
- std::string b = n->der->elem;
- n2 = std::atoi(b.c_str());
- std::cout << "\nB: " << b;
- std::string c = n->elem;
- if (c == "*")
- {
- return (n1 * n2);
- }
- else if (c == "+")
- {
- return (n1 + n2);
- }
- else if (c == "-")
- {
- return (n1 - n2);
- }
- else if (c == "/")
- {
- return (n1 / n2);
- }
- std::cout << "\nOperador: " << c;
- }
- }
- };
- #include <iostream>
- #include <string>
- #include <map>
- #include "Header.h"
- #include "AVL.h"
- int main()
- {
- /*int a[] = { 1,6,2,7,3,8,4,9,5,10 };
- sort(a,0, 10,10);
- for (int i = 0; i < 10; ++i)
- std::cout << a[i] << " -> ";
- */
- AVL<std::string>* avl = new AVL<std::string>();
- avl->agregarEspecialPrivate("/", "5", "2");
- avl->mostrarAVL_Public();
- std::cout << "El resultado es: " << avl->Operaciones();
- delete avl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement