当前位置: 首页>>代码示例>>C++>>正文


C++ LeafNode::insert方法代码示例

本文整理汇总了C++中LeafNode::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ LeafNode::insert方法的具体用法?C++ LeafNode::insert怎么用?C++ LeafNode::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在LeafNode的用法示例。


在下文中一共展示了LeafNode::insert方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: split

LeafNode* LeafNode::split(int value)
{
  int last, i;
  if (value > values[count - 1])
    last = value;
  else
  {
    last = values[count - 1];
    for (i = count - 2; i >= 0 && values[i] > value; i--)
      values[i + 1] = values[i];
    values[i + 1] = value;
  }

  LeafNode *newNode = new LeafNode(leafSize, parent, this, rightSibling);

  if(rightSibling)
    rightSibling->setLeftSibling(newNode);
  rightSibling = newNode;
  for (i = (leafSize + 1) / 2; i < leafSize; i++)
    newNode->insert(values[i]);
  count = (leafSize + 1 ) / 2;
  newNode->insert(last);
  return newNode;
} // split()
开发者ID:adamsx97,项目名称:Practice-and-Work,代码行数:24,代码来源:LeafNode.cpp

示例2: insertValue

LeafNode* LeafNode::insert(int value) {		//Returns null unless action is required on the part of the parent, in which case it returns the new node which needs a home
#ifdef DEBUG
	cout << "Inserting " << value << " into leaf @ " << this << ": ";
#endif
	//Insert into this node
	if (count < leafSize) {
#ifdef DEBUG
		cout << "Have found room in this node, inserting into values[]:\n";
#endif
		insertValue(value);
		if (parent) {
			parent->updateMinimums();
		}
		return NULL;
	}

	//Insert into sibling node
	else if ( ((rightSibling) && (rightSibling->getCount() < leafSize)) ||
			((leftSibling) && (leftSibling->getCount() < leafSize)) ) { //Check right sibling then left sibling


		//so the end result here is that value now contains the value to be sent to the new node.

		if ((leftSibling) && (leftSibling->getCount() < leafSize)) {
#ifdef DEBUG
			cout << " Going left ";
#endif
			//decide which value is getting kicked out
			value = arrangeLarger(value, false);
			leftSibling->insert(value);
		}

		//kick it out
		else { // ((rightSibling) && (rightSibling->getCount() < leafSize)) {
#ifdef DEBUG
			cout << " Going right ";
#endif
			//decide which value is getting kicked out
			value = arrangeLarger(value, true);
			rightSibling->insert(value);
		}

		if (parent) {
			parent->updateMinimums();
		}
		return NULL;
	}

	//Insert into
	else { //We cannot find space and must split
#ifdef DEBUG
		cout << "Need to make a new leaf node: ";
#endif
		//Decide which to kick out and rearrange data array
		value = arrangeLarger(value, true);

		//Create new node
		LeafNode* newnode = new LeafNode(leafSize, parent, this, rightSibling);	//Make new node, considerations at ¹.  Has only changed parameters of newnode.
#ifdef FINALDEBUG
		cout << "New child LeafNode:  " << newnode << endl;
#endif
		if (rightSibling) {		//If we have a right sibling
			rightSibling->setLeftSibling(newnode);		//Set the left sibling of our soon-to-be-former right sibling to the new node
		}

		this->rightSibling = newnode;

		//Copy stuff to new node
		newnode->insert(value);			//rightSibling.insert(value) should be an equivalent call
		for (int pos=leafSize-1; pos>((leafSize/2)-1+(leafSize%2)); --pos) {
#ifdef DEBUG
			cout << "Inserting " << values[pos] << endl;
#endif
			newnode->insert(values[pos]);
			values[pos] = 0;						//²
			--count;
		}

		if (parent) {
			parent->updateMinimums();
		}

		return newnode;
	}
}  // LeafNode::insert()
开发者ID:robinwu9000,项目名称:btree,代码行数:85,代码来源:LeafNode.cpp

示例3: 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]);
//.........这里部分代码省略.........
开发者ID:michaelromero,项目名称:Algorithms_DataStructures_CPP,代码行数:101,代码来源:LeafNode.cpp


注:本文中的LeafNode::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。