本文整理汇总了C++中BTreeNode::setLeftSibling方法的典型用法代码示例。如果您正苦于以下问题:C++ BTreeNode::setLeftSibling方法的具体用法?C++ BTreeNode::setLeftSibling怎么用?C++ BTreeNode::setLeftSibling使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BTreeNode
的用法示例。
在下文中一共展示了BTreeNode::setLeftSibling方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: split
InternalNode* InternalNode::split(int value){
InternalNode* newNode = new InternalNode(internalSize, leafSize, parent, this, NULL);
sort();
BTreeNode* oldRightNode = getRightSibling();
if(oldRightNode){
oldRightNode->setLeftSibling(newNode);
newNode->setRightSibling(oldRightNode);
newNode->setLeftSibling(this);
}
//cout << keys[0] << " " << keys[1] << " " << keys[2] << endl;
//cout << children[0]->getMinimum() << " " << children[1]->getMinimum() << endl;
for(int i = ((internalSize+1) / 2); i < internalSize; i++){
//cout << "Internal: Inserting " << keys[i] << " into new Node" << endl;
newNode->insert(keys[i]);
}
count = count - newNode->getCount();
if(keys[0] > value){
newNode->insert(keys[0]);
keys[0] = value;
}
else{
newNode->insert(value);
}
setRightSibling(newNode);
return newNode;
}
示例2: split
LeafNode* LeafNode::split(int value)
{
if (rightSibling) //if there is a right sibling
{
BTreeNode* ptr = rightSibling;
rightSibling = new LeafNode(leafSize, parent, this, ptr);
ptr->setLeftSibling(rightSibling);
}
else //no right sibling
rightSibling = new LeafNode(leafSize, parent, this, NULL);
//cout << "leafnode split" << endl;
int max = values[leafSize - 1];
if (max > value)
{
values[leafSize - 1] = value;
for (int pos = (leafSize - 1); pos > 0; pos--)
{
if ((values[pos - 1]) > value)
{
values[pos] = values[pos - 1];
values[pos - 1] = value;
}
}
}
else max = value;
((LeafNode*)rightSibling)->insert(max);
int numpass = leafSize / 2;//how many number to give to rightSiblihng other than max
for (int pos = (leafSize - 1); pos > (leafSize - 1 - numpass); pos --)
{
((LeafNode*)rightSibling)->insert(values[pos]);
count --;
}
return (LeafNode*)rightSibling;
return NULL; //to avoid warnings
} //LeafNode::split()
示例3: remove
BTreeNode* InternalNode::remove(int value)
{ // to be written by students
BTreeNode* ptr;
// int pos = Position(value);
int pos;
for(pos = count -1; pos > 0 && keys[pos] > value; pos--);
BTreeNode* random = children[pos]->remove(value);
if(random != NULL)
{
BTreeNode* hey = random;
if (hey->getCount() == internalSize)
return hey;
delete random;
int temp_zero_location;
for (int i = 0; i < count; i++)
{
if (keys[i] == 0)
{
temp_zero_location = i;
}
}
for (int j = temp_zero_location; j < count; j++)
{
keys[j] = keys[j+1];
children[j] = children[j+1];
}
count--;
if(parent)
{
parent->resetMinimum(this);
}
if (count < (internalSize+1)/2)// && parent != NULL)
{
//borrow from left sibling
// ((InternalNode*)leftSibling);
if(((InternalNode*)leftSibling))
{ //If Left sibling is there
if(((InternalNode*)leftSibling)->getCount()-1 < (internalSize+1)/2)
{
for (int i = (count -1); i >= 0; i--)
{
((InternalNode*)leftSibling)->insert(children[i]);
}
((InternalNode*)leftSibling)->setRightSibling(((InternalNode*)rightSibling));
return ((InternalNode*)leftSibling);
}
}
// else{
// }
// ((InternalNode*)rightSibling);
else if(((InternalNode*)rightSibling))
{
if((((InternalNode*)rightSibling)->getCount()-1) < (internalSize+1)/2)
{
for (int i = (count - 1); i >= 0; i--)
{
((InternalNode*)rightSibling)->insert(children[i]);
}
BTreeNode* temp = this->getRightSibling();
temp->setLeftSibling(((InternalNode*)leftSibling));
return temp;
// else{
}
}
}
}
if (count == 1)
{
children[0]->setParent(NULL);
return children[0];
}
return NULL;
}
示例4: insert
InternalNode* InternalNode::insert(int value)
{
// students must write this
updateMinimums();
sort();
//updateMinimums();
//cout << "Starting InternalNode insert: " << value << endl;
//search keys for position to insert, count - 1 because elements start at 0
int searchKey = count-1;
for(int i = count-1; keys[searchKey] > value && i > 0; i--){
//cout << keys[searchKey] << " > " << value << endl;
searchKey--;
}
BTreeNode* leafSplit = children[searchKey]->insert(value);
updateMinimums();
sort();
//updateMinimums();
//for(int i = 6; i >= 0; i--)
// cout << "Keys[" << i << "]: " << keys[i] << endl;
if(!leafSplit){
//cout << "no leaf split" << endl;
return NULL;
}
if(leafSplit && count < internalSize){
//add here
//cout << "InternalNode Insert Here" << endl;
//cout << "InternalNode leafSplit has min: " << leafSplit->getMinimum() << endl;
keys[count] = leafSplit->getMinimum();
children[count] = leafSplit;
count++;
updateMinimums();
sort();
//updateMinimums();
return NULL;
}
else if(leftSibling && leftSibling->getCount() < internalSize){
//addToLeft(internalSplit)
//cout << "NEED TO SHARE TO LEFT" << endl;
// cout << "Adding to left internal" << endl;
((InternalNode*)leftSibling)->insert(children[0]);
((InternalNode*)leftSibling)->updateMinimums();
((InternalNode*)leftSibling)->sort();
//((InternalNode*)leftSibling)->updateMinimums();
count--;
for(int i = 0; i < count; i++)
{
children[i] = children[i + 1];
keys[i] = keys[i + 1];
}
children[count] = leafSplit;
keys[count] = leafSplit->getMinimum();
count++;
updateMinimums();
sort();
//updateMinimums();
//cout << "leafSplit min: " << leafSplit->getMinimum() << endl;
leafSplit->setParent(this);
if(parent)
parent->findMin(this);
return NULL;
}
else if(rightSibling && rightSibling->getCount() < internalSize){
//addtoRight
if(keys[count-1] > leafSplit->getMinimum()){
//cout << "Internal Passing to right: " << keys[count-1] << endl;
((InternalNode*) rightSibling)->insert(children[count-1]);
((InternalNode*) rightSibling)->updateMinimums();
((InternalNode*) rightSibling)->sort();
children[count-1] = leafSplit;
updateMinimums();
sort();
if(((InternalNode*) rightSibling)->parent){
(((InternalNode*) rightSibling)->parent)->updateMinimums();
(((InternalNode*) rightSibling)->parent)->sort();
}
}
return NULL;
}
else if(leafSplit){
//cout << "Internal Split" << endl;
InternalNode* newRight = new InternalNode(internalSize, leafSize, parent, this, rightSibling);
BTreeNode* oldRight = getRightSibling();
if(rightSibling){
oldRight->setLeftSibling(newRight);
newRight->setRightSibling(oldRight);
newRight->setLeftSibling(this);
}
rightSibling = newRight;
int rightCount;
for(int i = (internalSize+1) / 2; i < internalSize; i++){
//cout << "i: " << i << endl;
//.........这里部分代码省略.........
示例5: 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]);
//.........这里部分代码省略.........