本文整理汇总了C++中LeafNode::getCount方法的典型用法代码示例。如果您正苦于以下问题:C++ LeafNode::getCount方法的具体用法?C++ LeafNode::getCount怎么用?C++ LeafNode::getCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LeafNode
的用法示例。
在下文中一共展示了LeafNode::getCount方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: split
LeafNode* LeafNode::split(int value){
//print old node
//cout << "PRINTING OLD NODE" << endl;
//cout << "COUNT: " << count << endl;
//for(int i = 0; i < count; i++){
// cout << values[i] << " ";
//}
//cout << endl;
//cout << "LeafNode:split" << endl;
//creat a new leafNode to the right, will automatically attach as sibling
LeafNode* newNode = new LeafNode(leafSize, parent, this, NULL);
//need to insert this new node between this node and potential rightSibling
BTreeNode* oldRightNode = getRightSibling();
if(oldRightNode){ // if there is a right sibling prior to split
//cout << "Leafnode:split: there is a right sibling prior to split" << endl;
//cout << "oldRight has min: " << oldRightNode->getMinimum() << endl;
//cout << "newNodes left has min: " << getMinimum() << endl;
oldRightNode->setLeftSibling(newNode); //set the right sibling to this new node in middle
newNode->setRightSibling(oldRightNode); //set new nodes right to the right sibling of this
newNode->setLeftSibling(this); // maybe not needed since constructor has this
}
//cout << "leafSize % 2 = " << leafSize % 2 << endl;
//cout << "Value: " << value << " values[" << leafSize/2 << "]: " << values[leafSize/2] << endl;
//simpler insert, if breaks, use doomsday comment below to fix
for(int i = ((leafSize+1) / 2); i < leafSize; i++){
//cout << "Leaf Split: inserting (top half)" << values[i] << " into newNode" << endl;
newNode->insert(values[i]);
}
count = count - newNode->getCount();
if(values[0] > value){
//cout << "first if" << endl;
newNode->insert(values[0]);
values[0] = value;
}
else if(values[(((leafSize+1)/2)-1)] > value){
//cout << " else if " << endl;
newNode->insert(values[(((leafSize+1)/2)-1)]);
values[(((leafSize+1)/2)-1)] = value;
sort();
parent->updateMinimums();
parent->sort();
}
else{
//cout << "Leaf Split: inserting (else)" << value << " into newNode" << endl;
if(newNode->parent){
newNode->parent->updateMinimums();
newNode->parent->sort();
//cout << "Has parent with min: " << newNode->parent->getMinimum() << endl;
}
newNode->insert(value);
}
setRightSibling(newNode);
//newNode->values[newNode->count++] = last;
//if(value == values[0] && parent)
// parent->updateMinimums();
return newNode;
// FOLLOWING COMMENT MIGHT BREAK EVERYTHING
/*
//if leaf is odd, and new value will be inserted into old, and mid to last will go into new
if((leafSize % 2 == 1) && value < values[(leafSize-1)]){
//cout << "odd: New value inserted into old" << endl;
for(int i = count/2; i < leafSize; i++){
newNode->insert(values[i]);
}
count = count - newNode->getCount();
insert(value);
//rightSibling = newNode;
setRightSibling(newNode);
}
//if leaf is odd, and new value will be inserted into new
if((leafSize % 2 == 1) && value > values[leafSize-1]){
//cout << "odd: New value inserted into new" << endl;
for(int i = (count/2)+1; i < leafSize; i++){
//cout << "Inserting " << values[i] << " into new" << endl;
newNode->insert(values[i]);
}
count = count - newNode->getCount();
newNode->insert(value);
//rightSibling = newNode;
setRightSibling(newNode);
}
//if leaf is even, and new value will be insterted into old, and mid to last will go into new
if((leafSize%2 == 0) && value < values[leafSize-1]){
cout << "even: New value " << value << " inserted into old" << endl;
//cout << value << " < " << values[leafSize/2] << endl;
//cout << "Count/2: " << count/2 << endl;
for(int i = count/2; i < leafSize; i++){
//cout << "Insering " << values[i] << " into new" << endl;
newNode->insert(values[i]);
//.........这里部分代码省略.........
示例2: remove
LeafNode* LeafNode::remove(int value)
{
int pos = 0;
// int q;
for(pos = 0; pos < count; pos++)
{
if(values[pos] == value)
{
for(int i = pos; i < count; i++)
{
values[i] = values[i + 1];
}
count--;
break;
}
if(values[pos] > value)
break;
}
if(count < ((leafSize+1)/2))
{
int transfer;
// BTreeNode *ptr2;
LeafNode *ptr;
int check = 0;
int siblingCount, i;
//int count1;
//Checks left sibling
// if(count == 0)
// return this;
if(leftSibling != NULL)
{
//cout << "left pass \n";
if((siblingCount = leftSibling->getCount()) > ((leafSize+1)/ 2))
{ //borrow from left
//cout << "problem left 1\n";
ptr = static_cast<LeafNode*>(leftSibling);
transfer = ptr->borrowLeft();
this->insert(transfer);
} else {//merge with left
//cout << "problem left 2 \n";
ptr = static_cast<LeafNode*>(leftSibling);
//cout << "ptr pass \n";
if(ptr->getLeftSibling() != NULL)
{
this->setLeftSibling(ptr->getLeftSibling());//Set new Sibling
leftSibling->setRightSibling(this);
} else {
this->setLeftSibling(NULL);
}
ptr->setRightSibling(NULL);
ptr->setLeftSibling(NULL);
for(i = 0; i < count; i++)
{
//cout << "seg1\n";
values[i + ptr->getCount()] = values[i];
}
for(i = ptr->getCount() - 1; i > -1; i--)
{
//cout << "seg2\n";
values[i] = ptr->values[i];
count++;
}
return ptr;
}
check = 1;
}//Left Sibling
if((check == 0) && (rightSibling != NULL))
{//borrow from right
if(rightSibling->getCount() > ((leafSize+1)/ 2))
{
ptr = static_cast<LeafNode*>(rightSibling);//borrow
transfer = ptr->borrowRight();
values[count] = transfer;
count++;
} else {
ptr = static_cast<LeafNode*>(rightSibling);//merge with right
if(ptr->getRightSibling() != NULL)
{
this->setRightSibling(ptr->getRightSibling());
rightSibling->setLeftSibling(this);
} else {
this->setRightSibling(NULL);
}
ptr->setLeftSibling(NULL);
ptr->setRightSibling(NULL);
for(i = 0; i < ptr->getCount(); i++)
//.........这里部分代码省略.........