Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <climits>
  5. #include <random>
  6. #include <chrono>
  7. #include <string>
  8. #include <iomanip>
  9. #include <math.h>
  10. #include <deque>
  11. #include <algorithm>
  12. #include "structures.h"
  13. #include <stdlib.h>
  14. #include <crtdbg.h>
  15. #include <sstream>
  16.  
  17. const int MAX_SIZE = 4;
  18.  
  19. ///@todo pliki naglowowe
  20. //#include "functions.h"
  21.  
  22. void deleteChildrenList(el_child*& child);
  23. void deleteKeyList(el_key*& key);
  24. int sizeOfKeyList(el_key*& pH);
  25. //TODO change path to relative
  26. const std::string file_name = "C:/Users/Święty/Documents/B-tree project/note.txt";
  27. node* root = nullptr;
  28.  
  29.  
  30. void printKey(el_key* pK, std::ofstream*& myFile)
  31. {
  32.     if (myFile != nullptr)
  33.         std::cout.rdbuf(myFile->rdbuf());
  34.     while (pK)
  35.     {
  36.         std::cout << pK->key << " ";
  37.         pK = pK->pNext;
  38.     }
  39. }
  40.  
  41. void printKey(el_key* pK, int number, std::ofstream*& myFile)   // print key from exact place
  42. {
  43.     if (pK)
  44.     {
  45.         if (myFile != nullptr)
  46.             std::cout.rdbuf(myFile->rdbuf());
  47.         el_key* p = pK;
  48.         for (int i = 0; i < number; i++)
  49.         {
  50.             p = p->pNext;
  51.         }
  52.         std::cout << p->key << " ";
  53.     }
  54. }
  55.  
  56. void printKeyWithLevel(el_key* pK, int number, int level, std::ofstream*& myFile)
  57. {
  58.     if (myFile != nullptr)
  59.         std::cout.rdbuf(myFile->rdbuf());
  60.     std::cout << std::setw(level * 4);
  61.     printKey(pK, number, myFile);
  62.     std::cout << std::endl;
  63.     std::cout << std::setw(0);
  64. }
  65.  
  66. void printNode(node*& nodeP, std::ofstream*& myFile)
  67. {
  68.    
  69.     if (nodeP)
  70.     {
  71.         int counter = 0;
  72.         if (nodeP->pListChildren != nullptr) {
  73.             el_child* tmpChild = nodeP->pListChildren;
  74.             printNode(tmpChild->pChild, myFile);
  75.  
  76.             for (int i = 0; i < sizeOfKeyList(nodeP->pListKeys); i++)
  77.             {
  78.                 printKey(nodeP->pListKeys, counter, myFile);
  79.                 tmpChild = tmpChild->pNext;
  80.                 printNode(tmpChild->pChild, myFile);
  81.                 counter++;
  82.             }
  83.         }
  84.         else {
  85.             printKey(nodeP->pListKeys, myFile);
  86.         }
  87.     }
  88. }
  89.  
  90. void printStructure(node*& nodeP, int level, std::ofstream*& myFile)
  91. {
  92.     if (nodeP)
  93.     {
  94.         if (nodeP->pListChildren != nullptr) {
  95.             int counter = 0;
  96.             el_child* tmpChild = nodeP->pListChildren;
  97.             printStructure(tmpChild->pChild, level + 1, myFile);
  98.             for (int i = 0; i < sizeOfKeyList(nodeP->pListKeys); i++)
  99.             {
  100.                 printKeyWithLevel(nodeP->pListKeys, counter, level, myFile);
  101.                 tmpChild = tmpChild->pNext;
  102.                 printStructure(tmpChild->pChild, level + 1, myFile);
  103.                 counter++;
  104.             }
  105.         }
  106.         else {
  107.             int counter = 0;
  108.             for (int i = 0; i < sizeOfKeyList(nodeP->pListKeys); i++)
  109.             {
  110.                 printKeyWithLevel(nodeP->pListKeys, counter, level, myFile);
  111.                 counter++;
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. int addKey(el_key*& pK, const double& v)
  118. {
  119.     auto p = pK;
  120.     if (not pK)     // if the listOfKeys is empty
  121.     {
  122.         pK = new el_key{ v, nullptr };
  123.         return 0;
  124.     }
  125.     else if (pK->key > v)   // add key at the beggining
  126.     {
  127.         pK = new el_key{ v, pK };
  128.         return 0;
  129.     }
  130.     else  // add value sorted way
  131.     {
  132.         int counter = 2;
  133.         while (p->pNext && v > p->pNext->key)
  134.         {
  135.             p = p->pNext;
  136.             counter++;
  137.         }
  138.         p->pNext = new el_key{ v, p->pNext };
  139.         return counter;
  140.     }
  141. }
  142.  
  143. void removeKey(el_key*& pK, int value)
  144. {
  145.     auto p = pK;
  146.  
  147.     if (not pK) // if there is nothing to remove
  148.     {
  149.         return;
  150.     }
  151.     else if (pK->key == value)  // remove first element
  152.     {
  153.         pK = pK->pNext;
  154.         delete p;
  155.         return;
  156.     }
  157.     while (p->pNext) // remove element with value
  158.     {
  159.         if (p->pNext->key == value)
  160.         {
  161.             el_key* kToRemove = p->pNext;
  162.             p->pNext = p->pNext->pNext;
  163.             delete kToRemove;
  164.             return;
  165.         }
  166.         p = p->pNext;
  167.     }
  168. }
  169.  
  170. int sizeOfKeyList(el_key*& pH)
  171. {
  172.     auto p = pH;
  173.     int counter = 0;
  174.     while (p)
  175.     {
  176.         counter++;
  177.         p = p->pNext;
  178.     }
  179.     return counter;
  180. }
  181.  
  182. void addChild(el_child*& pK, node* newNode, int number)
  183. {
  184.     auto p = pK;
  185.     if (not pK)
  186.     {
  187.         pK = new el_child{ newNode, nullptr };
  188.     }
  189.     else if (number == 0)
  190.     {
  191.         pK = new el_child{ newNode, pK };
  192.     }
  193.     else
  194.     {
  195.         for (int i = 1; i < number; i++)
  196.         {
  197.             p = p->pNext;
  198.         }
  199.         p->pNext = new el_child{ newNode, p->pNext };
  200.     }
  201. }
  202.  
  203. void balanceRoot(node*& pointerToNode) {
  204.     el_key* tmpP = pointerToNode->pListKeys;
  205.     el_key* splitKey = nullptr;
  206.     el_child* tmpChild = pointerToNode->pListChildren;
  207.     el_child* splitChild = nullptr;
  208.     int size = sizeOfKeyList(pointerToNode->pListKeys);
  209.  
  210.     for (int i = 1; i < size; i++) {
  211.         if (tmpChild != nullptr)
  212.             tmpChild = tmpChild->pNext;
  213.         if (i == floor(size / 2)) {
  214.             splitKey = tmpP->pNext; //find split key iterating over keylist
  215.             if (tmpChild != nullptr) {
  216.                 splitChild = tmpChild->pNext;  //find split child iterating over childList
  217.                 tmpChild->pNext = nullptr;
  218.             }
  219.             tmpP->pNext = nullptr;
  220.             break;
  221.         }
  222.         tmpP = tmpP->pNext;
  223.        
  224.     }
  225.  
  226.     node* newChild = new node{ splitKey->pNext , splitChild }; //new node splitted by new root value
  227.     splitKey->pNext = nullptr;
  228.  
  229.     el_child* child2 = new el_child{ newChild, nullptr };   //create new child for fresh new rootNode using created node above
  230.     el_child* child1 = new el_child{ pointerToNode, child2 }; //create new child for fresh new rootNode using old root node
  231.     pointerToNode = new node{ splitKey, child1 }; //create node using splitted key and created childList above
  232. }
  233.  
  234. bool addValueToTree(node*& pointerToNode, const double& value, bool isRoot) {
  235.     node* p = pointerToNode;
  236.  
  237.     if (pointerToNode == nullptr)   // if node is empty
  238.     {
  239.         el_key* key = new el_key{ value, nullptr };
  240.         pointerToNode = new node{ key, nullptr };                           //create node
  241.     }
  242.     else if (pointerToNode->pListChildren == nullptr) //add key to root node
  243.     {
  244.         addKey(pointerToNode->pListKeys, value);
  245.         if (sizeOfKeyList(pointerToNode->pListKeys) >= MAX_SIZE)
  246.         {
  247.             if (isRoot)
  248.                 balanceRoot(pointerToNode);
  249.             else
  250.                 return true;
  251.         }
  252.     }
  253.     else { //if node have children
  254.         el_key* tmpP = pointerToNode->pListKeys;
  255.         el_child* tmpP2 = pointerToNode->pListChildren;
  256.         while (tmpP && value > tmpP->key) //find key higher than value
  257.         {
  258.             tmpP = tmpP->pNext;
  259.             tmpP2 = tmpP2->pNext;
  260.         }
  261.         bool needBalance = addValueToTree(tmpP2->pChild, value, false);
  262.         if (needBalance) { //if unbalanced
  263.             el_key* tmpP = tmpP2->pChild->pListKeys;
  264.             el_key* splitKey = nullptr;
  265.             int size = sizeOfKeyList(tmpP);
  266.             for (int i = 1; i < size; i++) {
  267.                 if (i == floor(size / 2)) {//find split key iterating over keylist
  268.                     splitKey = tmpP->pNext;
  269.                     tmpP->pNext = nullptr;
  270.                     break;
  271.                 }
  272.                 tmpP = tmpP->pNext;
  273.             }
  274.            
  275.             int keyNumber = addKey(pointerToNode->pListKeys, splitKey->key);//add key to keyList
  276.  
  277.             tmpP2 = pointerToNode->pListChildren;
  278.             for (int i = 1; i < keyNumber; i++)         //find proper child to add the value
  279.             {
  280.                 tmpP2 = tmpP2->pNext;
  281.             }
  282.        
  283.             node* newNode = new node{ splitKey->pNext , nullptr };
  284.             el_child* newChild = new el_child{ newNode , tmpP2->pNext };
  285.             tmpP2->pNext = newChild;
  286.             splitKey->pNext = nullptr;
  287.  
  288.             if (sizeOfKeyList(pointerToNode->pListKeys) >= MAX_SIZE)
  289.             {
  290.                 if (isRoot)
  291.                     balanceRoot(pointerToNode);
  292.                 else
  293.                     return true;
  294.             }
  295.         }
  296.     }
  297.     return false;
  298. }
  299.  
  300. el_key* getKeyFromRightChildrens(el_child*& childP) {
  301.     if (sizeOfKeyList(childP->pChild->pListKeys) > 1) {
  302.         el_key* tmpP = childP->pChild->pListKeys;
  303.         childP->pChild->pListKeys = tmpP->pNext;
  304.         return tmpP;
  305.     }
  306. }
  307.  
  308.  
  309. el_key* getKeyFromChildrens(el_child*& childP) {
  310.     if (sizeOfKeyList(childP->pChild->pListKeys) > 1) { //get from left child
  311.         el_key* tmpP = childP->pChild->pListKeys;
  312.         el_key* prevKey = nullptr;
  313.         while (tmpP->pNext != nullptr) //get last key from key list
  314.         {
  315.             prevKey = tmpP;
  316.             tmpP = tmpP->pNext;
  317.         }
  318.         if (prevKey == nullptr)
  319.             childP->pChild->pListKeys = tmpP->pNext;
  320.         else
  321.             prevKey->pNext = tmpP->pNext;
  322.         return tmpP;
  323.     }
  324.     else if (childP->pNext != nullptr) {
  325.         return getKeyFromRightChildrens(childP->pNext); //get from the right child
  326.     }
  327.     else return nullptr;
  328. }
  329.  
  330. bool compareDoubles(double a, double b)
  331. {
  332.     return abs(a - b) < 0.000001;
  333. }
  334.  
  335. void removeKey(node*& pointerToNode, double value) {
  336.     el_key* tmpP = pointerToNode->pListKeys;
  337.     el_key* prevKey = nullptr;
  338.     el_child* prevChild = nullptr;
  339.     el_child* tmpChild = nullptr;
  340.     if (pointerToNode->pListChildren != nullptr) {
  341.         tmpChild = pointerToNode->pListChildren;
  342.     }
  343.     while (tmpP->pNext != nullptr && value > tmpP->key)
  344.     {
  345.         if (pointerToNode->pListChildren != nullptr) {
  346.             prevChild = tmpChild;
  347.             tmpChild = tmpChild->pNext;
  348.         }
  349.         prevKey = tmpP;
  350.         tmpP = tmpP->pNext;
  351.     }
  352.  
  353.     if (compareDoubles(tmpP->key, value)) {
  354.         if (pointerToNode->pListKeys == tmpP) {
  355.             pointerToNode->pListKeys = pointerToNode->pListKeys->pNext;
  356.             delete tmpP;
  357.         }
  358.         else {
  359.             prevKey->pNext = tmpP->pNext;
  360.             delete tmpP;
  361.         }
  362.         if (pointerToNode->pListChildren != nullptr) { // check if can get key from childrens
  363.             tmpP = getKeyFromChildrens(pointerToNode->pListChildren);
  364.             if (tmpP != nullptr) {
  365.                 if (prevKey != nullptr) {
  366.                     tmpP->pNext = prevKey->pNext;
  367.                     prevKey->pNext = tmpP;
  368.                 }
  369.                 else {
  370.                     tmpP->pNext = pointerToNode->pListKeys;
  371.                     pointerToNode->pListKeys = tmpP;
  372.                 }
  373.                
  374.             }
  375.         }
  376.  
  377.     }
  378.     else if (pointerToNode->pListChildren != nullptr){
  379.         if (value > tmpP->key) {
  380.             prevChild = tmpChild;
  381.             tmpChild = tmpChild->pNext;
  382.         }
  383.         removeKey(tmpChild->pChild, value);
  384.         if (sizeOfKeyList(tmpChild->pChild->pListKeys) == 0) { //check if there is a need to give key to child
  385.             if (prevChild != nullptr && sizeOfKeyList(prevChild->pChild->pListKeys) > 1) { //check if can borrow key from prev child
  386.                 tmpChild->pChild->pListKeys = getKeyFromChildrens(prevChild);
  387.             }
  388.             else if(tmpChild->pNext != nullptr && sizeOfKeyList(tmpChild->pNext->pChild->pListKeys) > 1) {//check if can borrow key from next child
  389.                 tmpChild->pChild->pListKeys = getKeyFromRightChildrens(tmpChild->pNext);
  390.             }
  391.             else if(tmpChild->pChild->pListChildren == nullptr) {
  392.                 if (prevChild != nullptr) { //has left sibling
  393.                     prevChild->pNext = tmpChild->pNext;
  394.                     delete tmpChild;
  395.                     //add root case
  396.                     prevKey->pNext = tmpP->pNext;
  397.                     el_key* lastKeyOfChild = prevChild->pChild->pListKeys;
  398.                     while (lastKeyOfChild->pNext != nullptr)
  399.                     {
  400.                         lastKeyOfChild = lastKeyOfChild->pNext;
  401.                     }
  402.                     lastKeyOfChild->pNext = tmpP;
  403.                     tmpP->pNext = nullptr;
  404.                 }
  405.                 else { //has right sibling and has no left sibling. move key to children on right
  406.                     pointerToNode->pListChildren = tmpChild->pNext;
  407.                     delete tmpChild;
  408.                     pointerToNode->pListKeys = tmpP->pNext;
  409.                     tmpP->pNext = pointerToNode->pListChildren->pChild->pListKeys;
  410.                     pointerToNode->pListChildren->pChild->pListKeys = tmpP;
  411.                 }
  412.                
  413.             }
  414.            
  415.         }
  416.     }
  417.     else {
  418.         std::cout << "There is no such key as: " << value << std::endl;
  419.     }
  420.     std::cout << "Removed value:  " << value << std::endl;
  421.  
  422. }
  423.  
  424. void deleteNode(node*& pR)
  425. {
  426.     if (pR) {
  427.         if (pR->pListKeys)
  428.         {
  429.             deleteKeyList(pR->pListKeys);
  430.         }
  431.         if (pR->pListChildren) {
  432.             deleteChildrenList(pR->pListChildren);
  433.         }
  434.         delete pR;
  435.     }
  436. }
  437.  
  438. void deleteKeyList(el_key*& key) {
  439.     if (key) {
  440.         if (key->pNext) {
  441.             deleteKeyList(key->pNext);
  442.         }
  443.         delete key;
  444.     }
  445.    
  446. }
  447.  
  448. void deleteChildrenList(el_child*& child) {
  449.     if (child) {
  450.         if (child->pNext) {
  451.             deleteChildrenList(child->pNext);
  452.         }
  453.         deleteNode(child->pChild);
  454.         delete child;
  455.     }
  456. }
  457.  
  458. int read_data() {
  459.  
  460.     std::ifstream file(file_name);
  461.     std::string line;
  462.  
  463.     if (file)
  464.     {
  465.         while (std::getline(file, line))
  466.         {
  467.             std::stringstream linestream(line);
  468.             std::string  word;
  469.             double value;
  470.  
  471.             linestream >> word;
  472.             if (word == "add") {
  473.                 while (linestream >> word)
  474.                 {
  475.                     if (word == "%") {
  476.                         break;
  477.                     }
  478.                     std::istringstream(word) >> value;
  479.                     addValueToTree(root, value, true);
  480.                 }
  481.             }
  482.             else  if (word == "graph") {
  483.                 linestream >> word;
  484.                 std::ofstream* streamPointer = nullptr;
  485.                 std::ofstream inputStream;
  486.                 if (word == "+") {
  487.                     linestream >> word;
  488.                     inputStream.open(word, std::fstream::app);
  489.                     if (inputStream)
  490.                         streamPointer = &inputStream;
  491.                 }
  492.                 else if (word != "graph") {
  493.                     linestream >> word;
  494.                     inputStream.open(word, std::fstream::trunc);
  495.                     if (inputStream)
  496.                         streamPointer = &inputStream;
  497.                 }
  498.                 printStructure(root, 0, streamPointer);
  499.  
  500.             }
  501.             else  if (word == "print") {
  502.                 linestream >> word;
  503.                 std::ofstream* streamPointer = nullptr;
  504.                 std::ofstream inputStream;
  505.                 if (word == "+") {
  506.                     linestream >> word;
  507.                     inputStream.open(word, std::fstream::app);
  508.                     if (inputStream)
  509.                         streamPointer = &inputStream;
  510.                 }
  511.                 else if (word != "print" && word != "%") {
  512.                     linestream >> word;
  513.                     inputStream.open(word, std::fstream::trunc);
  514.                     if (inputStream)
  515.                         streamPointer = &inputStream;
  516.                 }
  517.                 printNode(root, streamPointer);
  518.                
  519.             }
  520.             else {
  521.                 std::cout << "Wrong command" << std::endl;
  522.             }
  523.  
  524.             std::cout << std::endl;
  525.         }
  526.     }
  527.     else {
  528.         std::cout << "Can't open file";
  529.         return 0;
  530.     }
  531. }
  532.  
  533. int main()
  534. {
  535.     std::ofstream* streamPointer = nullptr;
  536.    
  537.     addValueToTree(root, 2, true);
  538.     addValueToTree(root, 8, true);
  539.     addValueToTree(root, 2, true);
  540.     addValueToTree(root, 18, true);
  541.     addValueToTree(root, 4, true);
  542.     addValueToTree(root, 64, true);
  543.     addValueToTree(root, 6, true);
  544.     addValueToTree(root, 34, true);
  545.     addValueToTree(root, 12, true);
  546.     addValueToTree(root, 52, true);
  547.     addValueToTree(root, 5, true);
  548.     addValueToTree(root, 32, true);
  549.    
  550.     printStructure(root, 0, streamPointer);
  551.     printNode(root, streamPointer);
  552.  
  553.  
  554.     //printStructure(root, 0, streamPointer);
  555.     removeKey(root, 2);
  556.  
  557.     removeKey(root, 8);
  558.     printStructure(root, 0, streamPointer);
  559.     removeKey(root, 2);
  560.     printStructure(root, 0, streamPointer);
  561.     removeKey(root, 18);
  562.     printStructure(root, 0, streamPointer);
  563.     removeKey(root, 4);
  564.     printStructure(root, 0, streamPointer);
  565.     removeKey(root, 64);
  566.     printStructure(root, 0, streamPointer);
  567.     removeKey(root, 6);
  568.     printStructure(root, 0, streamPointer);
  569.     removeKey(root, 34);
  570.     printStructure(root, 0, streamPointer);
  571.     removeKey(root, 12);
  572.     printStructure(root, 0, streamPointer);
  573.     removeKey(root, 52);
  574.     printStructure(root, 0, streamPointer);
  575.     removeKey(root, 5);
  576.     printStructure(root, 0, streamPointer);
  577.     removeKey(root, 32);
  578.     printStructure(root, 0, streamPointer);
  579.     removeKey(root, 4);
  580.     printStructure(root, 0, streamPointer);
  581.     printNode(root, streamPointer);
  582.     //read_data();
  583.    
  584.     deleteNode(root);                   // delete whole tree
  585. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement