Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. class BTreeNode
  6. {
  7.     int *keys;
  8.     int t;      
  9.     BTreeNode **C;
  10.     int n;    
  11.     bool leaf;
  12. public:
  13.     BTreeNode(int _t, bool _leaf);  
  14.  
  15.    
  16.     void insertNonFull(int k);
  17.  
  18.     void splitChild(int i, BTreeNode *y);
  19.  
  20.  
  21.     void traverse();
  22.  
  23.    
  24.     BTreeNode *search(int k);  
  25.  
  26.  
  27. friend class BTree;
  28. };
  29.  
  30.  
  31. class BTree
  32. {
  33.     BTreeNode *root; /
  34.     int t;  
  35. public:
  36.  
  37.     BTree(int _t)
  38.     {  root = NULL;  t = _t; }
  39.  
  40.  
  41.     void traverse()
  42.     {  if (root != NULL) root->traverse(); }
  43.  
  44.     BTreeNode* search(int k)
  45.     {  return (root == NULL)? NULL : root->search(k); }
  46.  
  47.  
  48.     void insert(int k);
  49. };
  50.  
  51.  
  52. BTreeNode::BTreeNode(int t1, bool leaf1)
  53. {
  54.  
  55.     t = t1;
  56.     leaf = leaf1;
  57.  
  58.  
  59.     keys = new int[2*t-1];
  60.     C = new BTreeNode *[2*t];
  61.  
  62.  
  63.     n = 0;
  64. }
  65.  
  66.  
  67. void BTreeNode::traverse()
  68. {
  69.  
  70.     int i;
  71.     for (i = 0; i < n; i++)
  72.     {
  73.         if (leaf == false)
  74.             C[i]->traverse();
  75.         cout << " " << keys[i];
  76.     }
  77.  
  78.  
  79.     if (leaf == false)
  80.         C[i]->traverse();
  81. }
  82.  
  83.  
  84. BTreeNode *BTreeNode::search(int k)
  85. {
  86.  
  87.     int i = 0;
  88.     while (i < n && k > keys[i])
  89.         i++;
  90.  
  91.  
  92.     if (keys[i] == k)
  93.         return this;
  94.  
  95.  
  96.     if (leaf == true)
  97.         return NULL;
  98.  
  99.  
  100.     return C[i]->search(k);
  101. }
  102.  
  103.  
  104. void BTree::insert(int k)
  105. {
  106.  
  107.     if (root == NULL)
  108.     {
  109.  
  110.         root = new BTreeNode(t, true);
  111.         root->keys[0] = k;  
  112.         root->n = 1;  
  113.     }
  114.     else
  115.     {
  116.  
  117.         if (root->n == 2*t-1)
  118.         {
  119.  
  120.             BTreeNode *s = new BTreeNode(t, false);
  121.  
  122.  
  123.             s->C[0] = root;
  124.  
  125.             s->splitChild(0, root);
  126.  
  127.  
  128.             int i = 0;
  129.             if (s->keys[0] < k)
  130.                 i++;
  131.             s->C[i]->insertNonFull(k);
  132.  
  133.  
  134.             root = s;
  135.         }
  136.         else  
  137.             root->insertNonFull(k);
  138.     }
  139. }
  140.  
  141.  
  142. void BTreeNode::insertNonFull(int k)
  143. {
  144.  
  145.     int i = n-1;
  146.  
  147.  
  148.     if (leaf == true)
  149.     {
  150.         while (i >= 0 && keys[i] > k)
  151.         {
  152.             keys[i+1] = keys[i];
  153.             i--;
  154.         }
  155.  
  156.         keys[i+1] = k;
  157.         n = n+1;
  158.     }
  159.     else
  160.  
  161.         while (i >= 0 && keys[i] > k)
  162.             i--;
  163.  
  164.  
  165.         if (C[i+1]->n == 2*t-1)
  166.         {
  167.  
  168.             splitChild(i+1, C[i+1]);
  169.  
  170.             if (keys[i+1] < k)
  171.                 i++;
  172.         }
  173.         C[i+1]->insertNonFull(k);
  174.     }
  175. }
  176.  
  177.  
  178. void BTreeNode::splitChild(int i, BTreeNode *y)
  179. {
  180.  
  181.     BTreeNode *z = new BTreeNode(y->t, y->leaf);
  182.     z->n = t - 1;
  183.  
  184.  
  185.     for (int j = 0; j < t-1; j++)
  186.         z->keys[j] = y->keys[j+t];
  187.  
  188.  
  189.     if (y->leaf == false)
  190.     {
  191.         for (int j = 0; j < t; j++)
  192.             z->C[j] = y->C[j+t];
  193.     }
  194.  
  195.  
  196.     y->n = t - 1;
  197.  
  198.  
  199.     for (int j = n; j >= i+1; j--)
  200.         C[j+1] = C[j];
  201.  
  202.  
  203.     C[i+1] = z;
  204.  
  205.  
  206.     for (int j = n-1; j >= i; j--)
  207.         keys[j+1] = keys[j];
  208.  
  209.  
  210.     keys[i] = y->keys[t-1];
  211.  
  212.  
  213.     n = n + 1;
  214. }
  215.  
  216. int main()
  217. {
  218.     BTree t(3);
  219.     t.insert(10);
  220.     t.insert(20);
  221.     t.insert(5);
  222.     t.insert(6);
  223.     t.insert(12);
  224.     t.insert(30);
  225.     t.insert(7);
  226.     t.insert(17);
  227.  
  228.     cout << "Traversal of the constucted tree is ";
  229.     t.traverse();
  230.  
  231.     int k = 6;
  232.     (t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
  233.  
  234.     k = 15;
  235.     (t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
  236.  
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement