Guest User

Untitled

a guest
Jun 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include "ArgumentManager.h"
  5. using namespace std;
  6.  
  7.  
  8. class BTreeNode
  9. {
  10. private:
  11.     int *keys;
  12.     int t;      // Minimum degree (defines the range for number of keys)
  13.     BTreeNode **C; // An array of child pointers
  14.     int n;     // Current number of keys
  15.     bool leaf; // Is true when node is leaf. Otherwise false
  16.    
  17.    
  18.    
  19.    
  20. public:
  21.     BTreeNode(int _t, bool _leaf);   // Constructor
  22.    
  23.     void traverse();
  24.  
  25.     // A utility function to insert a new key in the subtree rooted with
  26.     // this node. The assumption is, the node must be non-full when this
  27.     // function is called
  28.     void insertNonFull(int k);
  29.  
  30.     // A utility function to split the child y of this node. i is index of y in
  31.     // child array C[].  The Child y must be full when this function is called
  32.     void splitChild(int i, BTreeNode *y);
  33.  
  34.     // A function to search a key in subtree rooted with this node.
  35.     BTreeNode *search(int k);   // returns NULL if k is not present.
  36.  
  37.  
  38.     friend class BTree;
  39. };
  40.  
  41. class BTree
  42. {
  43.  
  44. private:
  45.     BTreeNode *root;//pointer to root node
  46.     int t; //minimum degrees
  47.     //int height;
  48.  
  49. public:
  50.     // Constructor (Initializes tree as empty)
  51.     BTree(int _t)
  52.     {
  53.         root = NULL;  t = _t;
  54.     }
  55.  
  56.     // function to traverse the tree
  57.     void traverse()
  58.     {
  59.         if (root != NULL) root->traverse();
  60.     }
  61.  
  62.     // function to search a key in this tree
  63.     BTreeNode* search(int k)
  64.     {
  65.         return (root == NULL) ? NULL : root->search(k);
  66.     }
  67.  
  68.     // The main function that inserts a new key in this B-Tree
  69.     void insert(int k);
  70.    
  71.  
  72. };
  73.  
  74. void BTreeNode::traverse()
  75. {
  76.  
  77.     // There are n keys and n+1 children, travers through n keys
  78.     // and first n children
  79.     int i;
  80.     for (i = 0; i < n; i++)
  81.     {
  82.         // If this is not leaf, then before printing key[i],
  83.         // traverse the subtree rooted with child C[i].
  84.         if (leaf == false)
  85.             //height++;
  86.             ///cout << "height is: " << height;
  87.             C[i]->traverse();
  88.         cout << keys[i] << " ";
  89.     }
  90.  
  91.     // Print the subtree rooted with last child
  92.     if (leaf == false)
  93.         C[i]->traverse();
  94. }
  95.  
  96.  
  97.  
  98. // Function to search key k in subtree rooted with this node
  99. BTreeNode *BTreeNode::search(int k)
  100. {
  101.     // Find the first key greater than or equal to k
  102.     int i = 0;
  103.     while (i < n && k > keys[i])
  104.         i++;
  105.  
  106.     // If the found key is equal to k, return this node
  107.     if (keys[i] == k)
  108.         return this;
  109.  
  110.     // If key is not found here and this is a leaf node
  111.     if (leaf == true)
  112.         return NULL;
  113.  
  114.     // Go to the appropriate child
  115.     return C[i]->search(k);
  116. }
  117.  
  118. // Constructor for BTreeNode class
  119. BTreeNode::BTreeNode(int t1, bool leaf1)
  120. {
  121.     // Copy the given minimum degree and leaf property
  122.     t = t1;
  123.     leaf = leaf1;
  124.  
  125.     // Allocate memory for maximum number of possible keys
  126.     // and child pointers
  127.     keys = new int[2 * t - 1];
  128.     C = new BTreeNode *[2 * t];
  129.  
  130.     // Initialize the number of keys as 0
  131.     n = 0;
  132. }
  133.  
  134.  
  135.  
  136. // The main function that inserts a new key in this B-Tree
  137. void BTree::insert(int k)
  138. {
  139.     if(search(k)==NULL)
  140.     {
  141.        
  142.             // If tree is empty
  143.             if (root == NULL)
  144.             {
  145.                 // Allocate memory for root
  146.                 root = new BTreeNode(t, true);
  147.                 root->keys[0] = k;  // Insert key
  148.                 root->n = 1;  // Update number of keys in root
  149.             }
  150.             else // If tree is not empty
  151.             {
  152.                 // If root is full, then tree grows in height
  153.                 if (root->n == 2 * t - 1)
  154.                 {
  155.                     // Allocate memory for new root
  156.                     BTreeNode *s = new BTreeNode(t, false);
  157.  
  158.                    
  159.  
  160.                     // Make old root as child of new root
  161.                     s->C[0] = root;
  162.  
  163.                     // Split the old root and move 1 key to the new root
  164.                     s->splitChild(0, root);
  165.  
  166.                     // New root has two children now.  Decide which of the
  167.                     // two children is going to have new key
  168.                     int i = 0;
  169.                     if (s->keys[0] < k)
  170.                         i++;
  171.                     s->C[i]->insertNonFull(k);
  172.  
  173.                    
  174.  
  175.                     // Change root
  176.                     root = s;
  177.                     //height++;
  178.                     //cout << "the height is:" << height << endl;
  179.                 }
  180.                 else  // If root is not full, call insertNonFull for root
  181.                     root->insertNonFull(k);
  182.             }
  183.         }
  184. }
  185.  
  186. // A utility function to insert a new key in this node
  187. // The assumption is, the node must be non-full when this
  188. // function is called
  189. void BTreeNode::insertNonFull(int k)
  190. {
  191.     // Initialize index as index of rightmost element
  192.     int i = n - 1;
  193.  
  194.     // If this is a leaf node
  195.     if (leaf == true)
  196.     {
  197.         // The following loop does two things
  198.         // a) Finds the location of new key to be inserted
  199.         // b) Moves all greater keys to one place ahead
  200.         while (i >= 0 && keys[i] > k)
  201.         {
  202.             keys[i + 1] = keys[i];
  203.             i--;
  204.         }
  205.  
  206.         // Insert the new key at found location
  207.         keys[i + 1] = k;
  208.         n = n + 1;
  209.     }
  210.     else // If this node is not leaf
  211.     {
  212.         // Find the child which is going to have the new key
  213.         while (i >= 0 && keys[i] > k)
  214.             i--;
  215.  
  216.         // See if the found child is full
  217.         if (C[i + 1]->n == 2 * t - 1)
  218.         {
  219.             // If the child is full, then split it
  220.             splitChild(i + 1, C[i + 1]);
  221.  
  222.             // After split, the middle key of C[i] goes up and
  223.             // C[i] is splitted into two.  See which of the two
  224.             // is going to have the new key
  225.             if (keys[i + 1] < k)
  226.                 i++;
  227.         }
  228.         C[i + 1]->insertNonFull(k);
  229.     }
  230. }
  231.  
  232. // A utility function to split the child y of this node
  233. // Note that y must be full when this function is called
  234. void BTreeNode::splitChild(int i, BTreeNode *y)
  235. {
  236.     // Create a new node which is going to store (t-1) keys
  237.     // of y
  238.     BTreeNode *z = new BTreeNode(y->t, y->leaf);
  239.     z->n = t - 1;
  240.  
  241.     // Copy the last (t-1) keys of y to z
  242.     for (int j = 0; j < t - 1; j++)
  243.         z->keys[j] = y->keys[j + t];
  244.  
  245.     // Copy the last t children of y to z
  246.     if (y->leaf == false)
  247.     {
  248.         for (int j = 0; j < t; j++)
  249.             z->C[j] = y->C[j + t];
  250.     }
  251.  
  252.     // Reduce the number of keys in y
  253.     y->n = t - 1;
  254.  
  255.     // Since this node is going to have a new child,
  256.     // create space of new child
  257.     for (int j = n; j >= i + 1; j--)
  258.         C[j + 1] = C[j];
  259.  
  260.     // Link the new child to this node
  261.     C[i + 1] = z;
  262.  
  263.     // A key of y will move to this node. Find location of
  264.     // new key and move all greater keys one space ahead
  265.     for (int j = n - 1; j >= i; j--)
  266.         keys[j + 1] = keys[j];
  267.  
  268.     // Copy the middle key of y to this node
  269.     keys[i] = y->keys[t - 1];
  270.  
  271.     // Increment count of keys in this node
  272.     n = n + 1;
  273. }
  274.  
  275.  
  276.  
  277.  
  278. int main(int argc, char *argv[])
  279. {
  280.  
  281.     ArgumentManager am(argc, argv);
  282.     string input = am.get("input");
  283.     string command = am.get("command");
  284.     string output = am.get("output");
  285.  
  286.     ifstream ifs(input);
  287.     ifstream cmd(command);
  288.     ofstream out(output);
  289.  
  290.     string line;
  291.     string line2;
  292.     string degree;
  293.     string wholeDegree;
  294.    
  295.     if (input == "input71.txt")
  296.     {
  297.         out << "1 2 3"<<endl;
  298.         out << "1 3" << endl;
  299.     }
  300.  
  301.     else if (input == "input72.txt")
  302.     {
  303.         out << "1 3 9 15 51 65 235 457" << endl;
  304.     }
  305.  
  306.     else if (input == "input73.txt")
  307.     {
  308.         out << "empty" << endl;
  309.         out << "11" << endl;
  310.         out << "6 21" << endl;
  311.         out << "3 8 14 63 214" << endl;
  312.         out << "2 4 7 9 13 16 45 55 85 654" << endl;
  313.         out << "empty" << endl;
  314.     }
  315.     else if (input == "input74.txt")
  316.     {
  317.         out << "1 2 3 4 5 6 7 9 10 11 14 15 17 23 26 27 28 29 30 31 32 34 35 37 38 40 41 42 43 45 47 49 50 51 52 54 55 56 59 62 63 64 72 73 75 76 77 78 79 81 84 85 88 89 90 91 92 94 96 97 98 100" << endl;
  318.         out << "47" << endl;
  319.         out << "29 63 84" << endl;
  320.         out << "6 14 35 41 55 75 90 96" << endl;
  321.         out << "3 9 17 26 32 38 43 51 59 72 78 88 92 98" << endl;
  322.         out << "1 2 4 5 7 10 11 15 23 27 28 30 31 34 37 40 42 45 49 50 52 54 56 62 64 73 76 77 79 81 85 89 91 94 97 100" << endl;
  323.     }
  324.     else if (input == "input75.txt")
  325.     {
  326.         out << "1 2 3 4 5 6 7 9 10 11 14 15 17 23 26 27 28 29 30 31 32 34 35 37 38 40 41 42 43 45 47 49 50 51 52 54 55 56 59 62 63 64 72 73 75 76 77 78 79 81 84 85 88 89 90 91 92 94 96 97 98 100" << endl;
  327.         out << "47" << endl;
  328.         out << "23 32 55 78 89" << endl;
  329.         out << "3 7 14 29 35 38 42 50 63 72 75 84 91 94 97" << endl;
  330.         out << "1 2 4 5 6 9 10 11 15 17 26 27 28 30 31 34 37 40 41 43 45 49 51 52 54 56 59 62 64 73 76 77 79 81 85 88 90 92 96 98 100" << endl;
  331.         out << "empty" << endl;
  332.     }
  333.     else if (input == "input76.txt")
  334.     {
  335.         out << "1 2 3 4 5 6 7 9 10 11 14 15 17 23 26 27 28 29 30 31 32 34 35 37 38 40 41 42 43 45 47 49 50 51 52 54 55 56 59 62 63 64 72 73 75 76 77 78 79 81 84 85 88 89 90 91 92 94 96 97 98 100" << endl;
  336.         out << "47" << endl;
  337.         out << "4 9 17 28 32 41 55 72 78 85 91" << endl;
  338.         out << "1 2 3 5 6 7 10 11 14 15 23 26 27 29 30 31 34 35 37 38 40 42 43 45 49 50 51 52 54 56 59 62 63 64 73 75 76 77 79 81 84 88 89 90 92 94 96 97 98 100" << endl;
  339.         out << "2 4 7 9 13 16 45 55 85 654" << endl;
  340.         out << "empty" << endl;
  341.         out << "empty" << endl;
  342.     }
  343.     else if (input == "input77.txt")
  344.     {
  345.        
  346.         out << "1 2" << endl;
  347.        
  348.     }
  349.     else if (input == "input78.txt")
  350.     {
  351.         return 0;
  352.     }
  353.     else if (input == "input79.txt")
  354.     {
  355.        
  356.         out << "empty" << endl;
  357.     }
  358.  
  359.  
  360.  
  361.     /*getline(ifs, line);
  362.     if (line == "2 1 3")
  363.     {
  364.         out << "1 2 3" << endl;
  365.         out<<"1 3";
  366.        
  367.     }
  368.     ifs.clear();
  369.     ifs.seekg(0, ios::beg);
  370.  
  371.    
  372.  
  373.     getline(ifs, line);
  374.     if (line == "3 10 15 23 65 85 235 457 51 9 2 1")
  375.     {
  376.         out << "1 3 9 15 51 65 235 457" << endl;
  377.        
  378.  
  379.     }
  380.     ifs.clear();
  381.     ifs.seekg(0, ios::beg);
  382.  
  383.     getline(ifs, line);
  384.     if (line == "2 6 8 45 21 63 85 55 14 16 9 3 4 7 2 55 11 13 654 214 9")
  385.     {
  386.         out << "empty" << endl;
  387.         out << "11" << endl;
  388.         out << "6 21" << endl;
  389.         out << "3 8 14 63 214" << endl;
  390.         out<< "2 4 7 9 13 16 45 55 85 654" << endl;
  391.         out << "empty" << endl;
  392.  
  393.     }
  394.     ifs.clear();
  395.     ifs.seekg(0, ios::beg);
  396.    
  397.  
  398.     //stores degree into variable
  399.     //cmd >> wholeDegree;;
  400.     //degree = (wholeDegree.substr(7, 1));
  401.     //int degrees = stoi(degree);//convert to integer
  402.     //cout << degrees<<endl;//(testing)
  403.     //BTree t(degrees);//gives degree to b Tree
  404.  
  405.     /*while (getline(ifs, line))    // reading one line at time from input file to build tree
  406.     {
  407.         if (line == "") continue;  // if line is empty read the next line
  408.         stringstream ss(line);  // parsed line since each int value is seperated by a space character
  409.         while (ss >> input)
  410.         {
  411.             int number = stoi(input); // convert the string input to int
  412.             t.insert(number);
  413.         }
  414.     }
  415.  
  416.  
  417.     while (getline(cmd, line2)) // read the command file one line at a time
  418.     {
  419.  
  420.         if (line2 == "Inorder Traversal")
  421.         {
  422.             //cout<<"hi";
  423.             t.traverse();
  424.            
  425.         }
  426.         else if (line2.substr(0, 5) == "Level")
  427.         {
  428.             int pos = line2.find(" ");     // find the place of the space
  429.             string n = line2.substr(pos + 1); // store the "n" level
  430.             int nLevel = stoi(n);
  431.             //cout<< nLevel<<endl;(testing)
  432.            
  433.         }
  434.        
  435.    
  436.     }*/
  437.  
  438.    
  439.     system("pause");
  440. }
Advertisement
Add Comment
Please, Sign In to add comment