Advertisement
nigatigga

Untitled

Oct 16th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include "LeafNode.h"
  3. #include "InternalNode.h"
  4. #include "QueueAr.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. LeafNode::LeafNode(int LSize, InternalNode *p,
  10.   BTreeNode *left, BTreeNode *right) : BTreeNode(LSize, p, left, right)
  11. {
  12.   values = new int[LSize];
  13. }  // LeafNode()
  14.  
  15.  
  16.  
  17. int LeafNode::getMinimum()const
  18. {
  19.   if(count > 0)  // should always be the case
  20.     return values[0];
  21.   else
  22.     return 0;
  23.  
  24. } // LeafNode::getMinimum()
  25.  
  26.  
  27. LeafNode* LeafNode::insert(int value)
  28. {
  29.   // students must write this
  30.  
  31.   //If no room left in the array
  32.   if (isFull())
  33.   {
  34.       //Check if theres room in the left sibling
  35.       if ((leftSibling != NULL) && (!leftSibling->isFull()))
  36.       {
  37.           leftSibling->insert(); //If there is insert into it
  38.       }
  39.       //Check if theres room in the right sibling
  40.       else if ((leftSibling != NULL) && (!leftSibling->isFull()))
  41.       {
  42.           rightSibling->insert(); //If there is insert into it
  43.       }
  44.       else
  45.       {
  46.           //Create a pointer and create a new node and put the bigger half of the elements into it
  47.       }
  48.   }
  49.   //Else there is room left in the array
  50.   else
  51.   {
  52.       //Go through the array and find the spot to put it into
  53.       insertIntoArray(value);
  54.   }
  55.  
  56.   return NULL; // to avoid warnings for now.
  57. }  // LeafNode::insert()
  58.  
  59.  
  60.  
  61. void LeafNode::insertIntoArray(int insert_value)
  62. {
  63.    
  64.     int array_holdcount;
  65.    
  66.     for(int array_count =0 ; array_count<count;array_count++)
  67.     {
  68.        
  69.        
  70.         if(insert_value < values[array_count]){
  71.    
  72.         int valueToPush = values[array_count];
  73.         values[array_count] = insert_value;
  74.         insert_value= valueToPush;
  75.        
  76.         array_holdcount = array_count;
  77.        
  78.         }
  79.     }
  80.    
  81.     for (int array_count = array_holdcount; array_count < count; array_count++)
  82.     {
  83.         int valueToPush = values[array_count];
  84.         values[array_count] = insert_value;
  85.         insert_value= valueToPush;
  86.     }
  87.    
  88.     value[array_count] = insert_value;
  89.    
  90. }
  91.  
  92. int LeafNode::takeLeast(int newVal)
  93. {
  94.     //If the new value is the least value, just return it
  95.     if (newVal <= *values[0])
  96.     {
  97.         return newVal;
  98.     }
  99.     //Else if the first element of the array is the least value
  100.     else
  101.     {
  102.         //Save the least value from the array
  103.         int leastVal = *values[0];
  104.        
  105.         for (int i = 0; i < count; i++)
  106.         {
  107.            
  108.         }
  109.     }
  110. }
  111. void LeafNode::takeGreatest(int newInt)
  112. {
  113.    
  114. }
  115.  
  116. bool LeafNode::isFull()
  117. {
  118.   if (count >= leafSize)
  119.   {
  120.     return true;
  121.   }
  122.  
  123.   return false;
  124. }
  125.  
  126. void LeafNode::print(Queue <BTreeNode*> &queue)
  127. {
  128.   cout << "Leaf: ";
  129.   for (int i = 0; i < count; i++)
  130.     cout << values[i] << ' ';
  131.   cout << endl;
  132. } // LeafNode::print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement