Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.70 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <stack>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. class Node{
  10. public:
  11.     Node(int);
  12.     int key;
  13.     Node* leftChild;
  14.     Node* rightChild;
  15.     char charArray[10];
  16. };
  17.  
  18. Node::Node(int k){
  19.     this->key = k;
  20.     this->rightChild = nullptr;
  21.     this->leftChild = nullptr;
  22.     string keyString = to_string(k);
  23.     int keyLength = keyString.length();
  24.     for (int i=0;i<keyLength;i++){
  25.         this->charArray[i] = keyString[i];
  26.     }
  27. }
  28.  
  29. class BinarySearchTree{
  30.     stack<Node*> nodesStack;
  31.  
  32. public:
  33.     Node* root;
  34.     BinarySearchTree();
  35.     bool insertNode(int);
  36.     void insertNodesBulk(int);
  37.     Node* findNode(int);
  38.     void rotateRight(Node*, Node*, Node*);
  39.     void rotateLeft(Node*, Node*, Node*);
  40.     void display(Node*,int);
  41.     int makeBackbone(Node*);
  42.     void makePerfectTree(int);
  43.     int calculateHeight(Node*);
  44.     bool removeNode(int);
  45. };
  46. int BinarySearchTree::calculateHeight(Node* current) {
  47.     if (current == nullptr)
  48.         return 0;
  49.     else
  50.     {
  51.         int lDepth = calculateHeight(current->leftChild);
  52.         int rDepth = calculateHeight(current->rightChild);
  53.  
  54.         if (lDepth > rDepth)
  55.             return(lDepth + 1);
  56.         else return(rDepth + 1);
  57.     }
  58. }
  59.  
  60. void BinarySearchTree::makePerfectTree(int N){
  61.     Node* grandfather = nullptr;
  62.     Node* tmp = root;
  63.     int m = 1;
  64.     while ( m <= N ){
  65.         m = 2 * m + 1;
  66.     }
  67.     m = m / 2;
  68.     for (int i = 0; i < (N-m); i++)
  69.     {
  70.         Node* tmp2 = tmp->rightChild;
  71.         if ( tmp2 != nullptr ){
  72.             rotateLeft(grandfather, tmp, tmp->rightChild);
  73.             grandfather = tmp2;
  74.             tmp = tmp2->rightChild;
  75.         }
  76.     }
  77.     while (m>1){
  78.         m = m/2;
  79.     grandfather = nullptr;
  80.     tmp = root;
  81.         for (int i = 0; i < m; i++)
  82.         {
  83.             Node* tmp2 = tmp->rightChild;
  84.             rotateLeft(grandfather, tmp, tmp->rightChild);
  85.             grandfather = tmp2;
  86.             tmp = tmp2->rightChild;
  87.         }
  88.     }
  89. }
  90. int BinarySearchTree::makeBackbone(Node* root) {
  91.         Node* grandfather = nullptr;
  92.         Node* tmp = root;
  93.         int c =0;
  94.         while (tmp != nullptr)
  95.         {
  96.             c++;
  97.             if ((tmp->leftChild) != nullptr)//UWAGA: zmiana „root’a” obsłużona w rotacji!
  98.             {
  99.                 Node* tmp2 = tmp->leftChild;
  100.                 rotateRight(grandfather, tmp, tmp->leftChild);
  101.                 tmp = tmp2;
  102.                 c--;
  103.             }
  104.             else {
  105.                 grandfather = tmp;
  106.                 tmp = tmp->rightChild;
  107.             }
  108.  
  109.         }// Złożoność I fazy algorytmu DSW O(N)
  110.         return c;
  111.     }//
  112.  
  113.  
  114.  
  115. void BinarySearchTree::rotateLeft(Node* grandfather, Node* parent, Node* child){
  116.     if(grandfather!=nullptr){
  117.         if(grandfather->rightChild == parent){
  118.             grandfather->rightChild = child;
  119.         }else{
  120.             grandfather->leftChild = child;
  121.         }
  122.     }else {
  123.         root = child;
  124.     }
  125.     Node* tmp = child->leftChild;
  126.     child->leftChild = parent;
  127.     parent->rightChild = tmp;
  128.     return;
  129. }
  130.  
  131. void BinarySearchTree::rotateRight(Node* grandfather, Node* parent, Node* child){
  132.     if(grandfather!=nullptr){
  133.         if(grandfather->rightChild == parent){
  134.             grandfather->rightChild = child;
  135.         }else{
  136.             grandfather->leftChild = child;
  137.         }
  138.     }else {
  139.         root = child;
  140.     }
  141.     Node* tmp = child->rightChild;
  142.     child->rightChild = parent;
  143.     parent->leftChild = tmp;
  144.     return;
  145.     }
  146.  
  147.  
  148. bool BinarySearchTree::removeNode(int k){
  149.     Node* current = this->root;
  150.     if(current == nullptr){
  151.         return false;
  152.     }
  153.     nodesStack.push(current);
  154.     while((current!=nullptr && current->key!=k)){
  155.         if(current->key < k){
  156.                 current = current->rightChild;
  157.                 }
  158.             else{
  159.                 current = current->leftChild;
  160.         }
  161.         nodesStack.push(current);
  162.     }
  163.  
  164.     if(current == nullptr){
  165.         return false;
  166.     }
  167.     current = nodesStack.top();
  168.  
  169.     //wezel jest lisciem
  170.     if(current->leftChild == nullptr && current->rightChild == nullptr){
  171.         nodesStack.pop();
  172.         Node* parent = nodesStack.top();
  173.         if(parent->leftChild == current){
  174.             parent->leftChild = nullptr;
  175.         }else{
  176.             parent->rightChild = nullptr;
  177.         }
  178.         delete current;
  179.     }
  180.     //wezel ma dwoch potomkow
  181.     else if(current->leftChild != nullptr && current->rightChild != nullptr){
  182.         Node* nodeToRemove = current;
  183.         current = current->rightChild;
  184.         nodesStack.push(current);
  185.         while(current->leftChild != nullptr){
  186.             current = current->leftChild;
  187.             nodesStack.push(current);
  188.         }
  189.         Node* successor = nodesStack.top();
  190.         nodesStack.pop();
  191.         if(successor->rightChild != nullptr){
  192.             nodesStack.top()->leftChild = successor->rightChild;
  193.         }else{
  194.             nodesStack.top()->leftChild == nullptr;
  195.         }
  196.         successor->leftChild = nodeToRemove->leftChild;
  197.         successor->rightChild = nodeToRemove->rightChild;
  198.         if(nodeToRemove == root){
  199.             root = successor;
  200.         }
  201.         else{
  202.             while(nodesStack.top() != nodeToRemove && !nodesStack.empty()){
  203.                 nodesStack.pop();
  204.             }
  205.             nodesStack.pop();
  206.             if(nodesStack.top()->leftChild == nodeToRemove){
  207.                 nodesStack.top()->leftChild = successor;
  208.             }else{
  209.                 nodesStack.top()->rightChild = successor;
  210.             }
  211.         }
  212.         delete nodeToRemove;
  213.     }
  214.     //wezel ma lewego potomka
  215.     else if(current->leftChild != nullptr && current->rightChild == nullptr){
  216.         nodesStack.pop();
  217.         if(nodesStack.top()->leftChild == current) {
  218.             nodesStack.top()->leftChild = current->leftChild;
  219.         }else{
  220.             nodesStack.top()->rightChild=current->leftChild;
  221.         }
  222.  
  223.     }
  224.     else if(current->rightChild!=nullptr && current->leftChild == nullptr){
  225.         nodesStack.pop();
  226.         if(nodesStack.top()->leftChild == current){
  227.             nodesStack.top()->leftChild = current->rightChild;
  228.         }else{
  229.             nodesStack.top()->rightChild = current->rightChild;
  230.         }
  231.     }
  232.     while(!nodesStack.empty()){
  233.         nodesStack.pop();
  234.     }
  235.     return true;
  236. }
  237. //zwraca wskaznik na wezel o podanym kluczu. Zwraca wskaznik na nullptr jesli nie znaleziono klucza.
  238. Node* BinarySearchTree::findNode(int k){
  239.     Node* current = this->root;
  240.     nodesStack.push(current);
  241.     while((current!=nullptr && current->key!=k)){
  242.         if(current->key < k)
  243.                 current = current->rightChild;
  244.             else
  245.                 current = current->leftChild;
  246.         nodesStack.push(current);
  247.     }
  248.  
  249.     return current;
  250. }
  251.  
  252. BinarySearchTree::BinarySearchTree(){
  253.     this->root = nullptr;
  254. }
  255. //zwraca true jesli udalo sie wprowadzic wezel do BST
  256. bool BinarySearchTree::insertNode(int k){
  257.  
  258.     if (this->root == nullptr){
  259.         this->root = new Node(k);
  260.     }
  261.     Node* current = this->root;
  262.     Node* parent = nullptr;
  263.     while(current!=nullptr){
  264.         if(current->key == k)
  265.             return false;
  266.         parent = current;
  267.         if(current->key < k){
  268.             current = current->rightChild;
  269.         }
  270.         else{
  271.             current = current->leftChild;}
  272.     }
  273.     if(parent->key >k){
  274.             parent->leftChild = new Node(k);
  275.     }
  276.     else{
  277.     parent->rightChild = new Node(k);
  278.     }
  279.     return true;
  280. }
  281.  
  282.  
  283. void BinarySearchTree::insertNodesBulk(int n){
  284.     int currentInserted = 0;
  285.     while(currentInserted <n-1){
  286.         if(insertNode(((rand() % 20001) - 10000)) == true){
  287.             currentInserted++;
  288.         }
  289.     }
  290. }
  291.  
  292. int main()
  293. {
  294.     srand(time(NULL));
  295.     clock_t begin, end;
  296.     double time_spent;
  297.     FILE* fp = fopen("inlab04.txt", "r");
  298.     int X1;
  299.     int X2;
  300.     if (fp == NULL)
  301.         return -1;
  302.     fscanf (fp, "%d %d", &X1 ,&X2);
  303.     fclose(fp);
  304.     begin = clock();
  305.  
  306.     BinarySearchTree* bst = new BinarySearchTree();
  307.     bst->insertNodesBulk(X1);
  308.     cout << bst->calculateHeight(bst->root) << endl;
  309.  
  310.     int height = bst->makeBackbone(bst->root);
  311.     bst->makePerfectTree(height);
  312.  
  313.     cout << bst->calculateHeight(bst->root) << endl;
  314.     bst->root = nullptr;
  315.     bst->insertNodesBulk(X2);
  316.     cout << bst->calculateHeight(bst->root) << endl;
  317.  
  318.     height = bst->makeBackbone(bst->root);
  319.     bst->makePerfectTree(height);
  320.  
  321.     cout << bst->calculateHeight(bst->root) << endl;
  322.     end = clock();
  323.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  324.  
  325.     cout << endl << to_string(time_spent);
  326.     return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement