Advertisement
Guest User

BinaryTree

a guest
May 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #ifndef __BINARYTREE_H__
  2. #define __BINARYTREE_H__
  3.  
  4. #include <queue>
  5. #include <iostream>
  6. #include <math.h>
  7.  
  8. template <typename T>
  9. class BinaryTreeNode {
  10. private:
  11.     T data;
  12.  
  13.     BinaryTreeNode<T> *leftChild;
  14.     BinaryTreeNode<T> *rightChild;
  15.  
  16. public:
  17.     int lvl;
  18.     BinaryTreeNode<T> *tati;
  19.     BinaryTreeNode<T> *T2;
  20.     BinaryTreeNode(T data) {
  21.         // TODO 1
  22.         lvl = 0;
  23.         this->data = data;
  24.         tati = NULL;
  25.         T2 = NULL;
  26.         leftChild = NULL;
  27.         rightChild = NULL;
  28.     }
  29.  
  30.     ~BinaryTreeNode() {
  31.         // TODO 2
  32.     }
  33.  
  34.     void setData(T data) {
  35.         // TODO 3
  36.         this->data = data;
  37.     }
  38.  
  39.     T getData() {
  40.         // TODO 4
  41.         return data;
  42.     }
  43.  
  44.     void setLeftChild(T data) {
  45.         // TODO 5
  46.         leftChild = new BinaryTreeNode<T>(data);
  47.         leftChild->data = data;
  48.     }
  49.  
  50.     BinaryTreeNode<T>* getLeftChild() {
  51.         // TODO 6
  52.         return leftChild;
  53.     }
  54.  
  55.     void setRightChild(T data) {
  56.         // TODO 7
  57.         rightChild = new BinaryTreeNode<T>(data);
  58.     }
  59.  
  60.     BinaryTreeNode<T>* getRightChild() {
  61.         // TODO 8
  62.         return rightChild;
  63.     }
  64. };
  65.  
  66. template <typename T>
  67. class BinaryTree {
  68. private:
  69.     int size, H;
  70.     BinaryTreeNode<T> *root;
  71.  
  72. public:
  73.     BinaryTree() {
  74.         // TODO 9
  75.         size = 0;
  76.         root = NULL;
  77.     }
  78.  
  79.     ~BinaryTree() {
  80.         // TODO 10
  81.         BinaryTreeNode<T>* tmp = root;
  82.         std::queue<BinaryTreeNode<T>*> Q;
  83.         Q.push(root);
  84.         while(!Q.empty()) {
  85.             tmp = Q.front();
  86.             Q.pop();
  87.             if (tmp->getLeftChild() != NULL) {
  88.                 Q.push(tmp->getLeftChild());
  89.             }
  90.             if (tmp->getRightChild() != NULL) {
  91.                 Q.push(tmp->getRightChild());
  92.             }
  93.             delete tmp;
  94.         }
  95.  
  96.     }
  97.  
  98.     BinaryTreeNode<T>* getRoot() {
  99.         // TODO: Si asta trebuie facut
  100.         return root;
  101.     }
  102.  
  103.     int getSize() {
  104.         // Faceti-l
  105.         return size;
  106.     }
  107.  
  108.     void insert(T data) {
  109.         // TODO 11: Insert node at the first empty position
  110.         if (size == 0) {
  111.             root = new BinaryTreeNode<T>(data);
  112.             root->setData(data);
  113.             ++size;
  114.             return;
  115.         }
  116.         BinaryTreeNode<T> *tmp = root;
  117.         std::queue<BinaryTreeNode<T>*> Q;
  118.         Q.push(root);
  119.         ++size;
  120.         while(!Q.empty()) {
  121.             tmp = Q.front();
  122.             Q.pop();
  123.             if (tmp->getLeftChild() == NULL) {
  124.                 tmp->setLeftChild(data);
  125.                 tmp->getLeftChild()->tati = tmp;
  126.                 return;
  127.             }
  128.             if (tmp->getRightChild() == NULL) {
  129.                 tmp->setRightChild(data);
  130.                 tmp->getRightChild()->tati = tmp;
  131.                 return;
  132.             }
  133.             Q.push(tmp->getLeftChild());
  134.             Q.push(tmp->getRightChild());
  135.         }
  136.     }
  137.  
  138.     std::vector<T> printTraversal(BinaryTreeNode<T>* node) {
  139.         // TODO 12
  140.         // static startNode = node;
  141.         // static std::vector<T> ans;
  142.         // if (node->getLeftChild() != NULL) {
  143.         //     printTraversal(node->getLeftChild());
  144.         // }
  145.         // ans.push_back(node);
  146.         // if (node->getRightChild() != NULL) {
  147.         //     printTraversal(node->getRightChild());
  148.         // }
  149.         // if (node == startNode) {
  150.         //     return ans;
  151.         // }
  152.         BinaryTreeNode<T>* tmp = root;
  153.         std::queue<BinaryTreeNode<T>*> Q;
  154.         Q.push(root);
  155.         while(!Q.empty()) {
  156.             tmp = Q.front();
  157.             std::cout << tmp->getData() << std::endl;
  158.             Q.pop();
  159.             if (tmp->getLeftChild() != NULL) {
  160.                 Q.push(tmp->getLeftChild());
  161.             }
  162.             if (tmp->getRightChild() != NULL) {
  163.                 Q.push(tmp->getRightChild());
  164.             }
  165.         }
  166.     }
  167.     int height() {
  168.         int height = 1;
  169.         while ((1 << height) - 1 <= size) {
  170.             ++height;
  171.         }
  172.         --height;
  173.         return height;
  174.     }
  175.     void getBuckets(BinaryTreeNode<T>* node, BinaryTreeNode<T>* tata, int level) {
  176.         node->T2 = tata;
  177.         node->lvl = level;
  178.         if (level % H == 0) {
  179.             tata = node;
  180.         }
  181.         if (node->getLeftChild() != NULL) {
  182.             getBuckets(node->getLeftChild(), tata, level + 1);
  183.         }
  184.         if (node->getRightChild() != NULL) {
  185.             getBuckets(node->getRightChild(), tata, level + 1);
  186.         }
  187.        
  188.     }
  189.     void lca(BinaryTreeNode<T>* x, BinaryTreeNode<T>* y) {
  190.         H = height();
  191.         H = sqrt(H);
  192.         getBuckets(root, NULL, 0);
  193.         while (x->T2 != y->T2) {
  194.             if (x->lvl < y->lvl) {
  195.                 y = y->T2;
  196.             } else {
  197.                 x = x->T2;
  198.             }
  199.         }
  200.         while (x != y) {
  201.             if (x->lvl < y->lvl) {
  202.                 y = y->tati;
  203.             } else {
  204.                 x = x->tati;
  205.             }
  206.         }
  207.         std::cout << x->getData();
  208.     }  
  209. };
  210.  
  211. #endif // __BINARYTREE_H__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement