Advertisement
Guest User

Untitled

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