void BTree::insert(int k)
{

    if (root == NULL)
    {

        root = new BTreeNode(t, true);
        root->keys[0] = k;
        root->n = 1;
    }
    else
    {

        if (root->n == 2*t-1)
        {

            BTreeNode *s = new BTreeNode(t, false);
            s->C[0] = root;
            s->splitChild(0, root);
            int i = 0;
            if (s->keys[0] < k)
                i++;
            s->C[i]->insertNonFull(k);
            root = s;
        }
        else
            root->insertNonFull(k);
    }
}
Esempio n. 2
0
void BTree::addToNode(BTreeNode *node, BTreeElement *child) {
	if (!node->addChild(child->getMinId(), child)) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Node is full");
		BTreeNode *newNode = new BTreeNode(this, nodeCapacity);
		int oldMinId = node->getMinId();
		node->split(newNode);

		BTreeNode *addTo;
		if (node->getMinId() == -1 || child->getMinId() > node->getMinId())
			addTo = node;
		else
			addTo = newNode;

		if (!addTo->addChild(child->getMinId(), child)) {
		//	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Node add FAILED!");
			return;
		}

		if (node->getParent() == NULL) {
		//	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "This node's parent is null");
			BTreeNode *parent = new BTreeNode(this, nodeCapacity);
			parent->addChild(node->getMinId(), node);
			root = parent;
		}
		addToNode(node->getParent(), newNode);
	}
	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Added new node to parent");
}
Esempio n. 3
0
TEST(BTreeNodeTest, test) {
  BTreeNodeBuilder<ValueT> builder;
  builder.set_is_leaf(false);

  const char *key = "key";
  ValueT expected = 3;
  builder.addEntry(key, expected);

  stringstream ss;
  builder.save(ss);

  BTreeNode<ValueT> node;
  node.load(ss);

  ValueT value;
  node.find(key, value);
  EXPECT_EQ(expected, value);

  key = "test";
  expected = 0;
  node.find(key, value);
  EXPECT_EQ(expected, value);

  key = "kay";
  expected = 3;
  node.lowerBound(key, value);
  EXPECT_EQ(expected, value);
}
Esempio n. 4
0
QString Expression::processConstant( const QString & expr, bool * isConstant )
{
	bool temp;
	if (!isConstant)
		isConstant = &temp;
	
	QString code;
	
	// Make a tree to put the expression in.
	BTreeBase *tree = new BTreeBase();
	BTreeNode *root = new BTreeNode();

	// parse the expression into the tree
	buildTree(expr,tree,root,0);
	// compile the tree into assembly code
	tree->setRoot(root);
	tree->pruneTree(tree->root());
	//code = traverseTree(tree->root());
	// Look to see if it is a number
	if( root->type() == number )
	{
		code = root->value();
		*isConstant = true;
	}
	else
	{
		code = "";
		*isConstant = false;
	}
	
	// Note deleting the tree deletes all nodes, so the root
	// doesn't need deleting separately.
	delete tree;
	return code;
}
Esempio n. 5
0
BTreeNode* InternalNode::remove(int value) {  
	BTreeNode *leaf = NULL;

  for(int i = 0; i < count; i++) {
  	
  	if(i == 0 && value <= keys[i]) leaf = children[0];
  	if(i == count - 1 && value >= keys[i]) leaf = children[i];
  	if(value >= keys[i] && value < keys[i+1]) leaf = children[i];

		if(leaf != NULL) {
			leaf = leaf->remove(value);
				//keys[i]	= children[i]->getMinimum();

			for(int i = 0; i < count; i++) {
				if(children[i]->getCount() == 0) {
					
					for(; i < count - 1; i++) {
						children[i] = children[i + 1];
						keys[i] = keys[i + 1];
					}

					--count;
					break;
				}
			}
		}

	}

	this->tryBorrowing(); 
	this->prune();

	if(parent == NULL && this->count == 1) return this->children[0] ;
	else return this;
} // InternalNode::remove(int value)
Esempio n. 6
0
void BPlusTree::Insert(int key , int value)
{
	BTreeNode * node = GetLeafNode(key);
	if(node == NULL)
	{
		root = new BTreeNode();
		root->setLeaf(true);
		root->InsertIntoLeafNode(key ,value);
		return;

	}

	if(node->searchLeaf(key))
	{
		return;
	}
	if(node->isFull())
	{
		split(node ,key ,value);
	}
	else
	{
		node->InsertIntoLeafNode(key,value);
	}

}
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;
}
Esempio n. 8
0
void Delete_Tree(BTreeNode *&tree)
{
	BTreeNode *NodeToDelete = tree;

	BTreeNode *TempNode;

	if (tree->Right == NULL)
		tree = tree->Left;
	else if (tree->Left == NULL)
		tree = tree->Right;
	else
	{
		TempNode = tree->Right;
		
		while (TempNode->Left != NULL)
			TempNode = TempNode->Left;

		TempNode->Left = tree->Left;

		tree = tree->Right;
	}
	Employee[NodeToDelete->Hash_Key()].free_flag = true;
		
	delete NodeToDelete;


}
Esempio n. 9
0
// The main function that inserts a new key in this B-Tree
void BTree::insert(int k)
{
	char varnam[64];

	// If tree is empty
	if (root == NULL)
	{
		bzero(varnam, 64);
		sprintf(varnam,"%d",count);
		strcat(varnam,"node");
		varnam[64]=0;
		// Allocate memory for root
		root =(BTreeNode *)nvalloc_(sizeof(BTreeNode), varnam, 0);
		//root = (BTreeNode *)nvalloc_(sizeof(BTreeNode));
		root->Initialize(root, t, true, count);	
		//root = new BTreeNode(t, true);
		root->keys[0] = k; // Insert key
		root->n = 1; // Update number of keys in root
	}
	else // If tree is not empty
	{
		// If root is full, then tree grows in height
		if (root->n == 2*t-1)
		{
			bzero(varnam, 64);
			sprintf(varnam,"%d",count);
			strcat(varnam,"node");
			varnam[64]=0;

			// Allocate memory for root
			BTreeNode *s =(BTreeNode *)nvalloc_(sizeof(BTreeNode), varnam, 0);
			s->Initialize(s, t, false, count);	
			// Allocate memory for new root
			//BTreeNode *s = new BTreeNode(t, false);
			BEGIN_OBJTRANS((void *)s,0);

			// Make old root as child of new root
			s->C[0] = root;

			// Split the old root and move 1 key to the new root
			s->splitChild(0, root);

			// New root has two children now. Decide which of the
			// two children is going to have new key
			int i = 0;
			if (s->keys[0] < k)
				i++;
			s->C[i]->insertNonFull(k);

			nvcommitobj((void *)s,0);

			// Change root
			root = s;
		}
		else // If root is not full, call insertNonFull for root
			root->insertNonFull(k);
	}
	count++;
}
void BTree::remove(string name) {
  //DELETE AT LEAF
  BTreeNode* leaf = findNode(root, name);
  if(leaf->find(name,EXACT)==NOTFOUND){
    cout << "TARGET NOT FOUND" << endl;
    return;
  }
  cout << "Found TARGET. Now deleting it. "<< endl;
  leaf=leaf->remove(name);
  cout << "DELETE SUCESSFULLY. Now ADJUSTING ROOT" <<endl;
  AdjustingRoot(leaf); 
}
Esempio n. 11
0
void BTree::print()
{
  BTreeNode *BTreeNodePtr;
  Queue<BTreeNode*> queue(1000);

  queue.enqueue(root);
  while(!queue.isEmpty())
  {
    BTreeNodePtr = queue.dequeue();
    BTreeNodePtr->print(queue);
  } // while
} // BTree::print()
Esempio n. 12
0
void BTree::remove(int value)
{  
  BTreeNode *ptr = root-> remove(value);
  if(ptr==root)
  {
    InternalNode *IPtr = static_cast < InternalNode * > (ptr);
    ptr = IPtr->firstChild();
    ptr->setParent(NULL);
    root = ptr;
  } 

// To be written by students
} // BTree::remove()
Esempio n. 13
0
void BTree::insert(const int value)
{
	BTreeNode* newReturn = root->insert(value);
	if(newReturn != NULL )
	{
		BTreeNode *oldRoot = root;
		InternalNode *newRoot= new InternalNode(internalSize, leafSize, NULL, NULL, NULL);
		newRoot->insert(oldRoot,newReturn);//set new parent's property
		oldRoot->setParent(newRoot);//set child point to parent
		newReturn->setParent(newRoot);
		root = newRoot;
	}
	// students must write this
} // BTree::insert()
Esempio n. 14
0
void BTree::insert(const int value)
{
  BTreeNode *split = root->insert(value);

  if (split)	// LeafNode needs split
  {
    InternalNode *parent = new InternalNode(internalSize, leafSize, NULL,
                                              NULL, NULL);
    root->setParent(parent);	// set parents to root
    split->setParent(parent);
    parent->insert(root);	// should be on the left
    parent->insert(split);	// should be on the right
    root = parent;
  }  // if split needed
} // BTree::insert()
Esempio n. 15
0
void BTree::remove(int k)
{
	if (!root)
	{
		cout << "The tree is empty\n";
		return;
	}

	// Call the remove function for root
	root->remove(k);

	// If the root node has 0 keys, make its first child as the new root
	// if it has a child, otherwise set root as NULL
	if (root->n==0)
	{
		BTreeNode *tmp = root;
		if (root->leaf)
			root = NULL;
		else
			root = root->C[0];

		// Free the old root
		delete tmp;
	}
	return;
}
Esempio n. 16
0
void BTree::add(int id, RecordInfo *record) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "== BTree add id: %d", id);
	if (root == NULL) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "new leaf as root: %d", leafCapacity);
		root = new BTreeLeaf(this, leafCapacity);
	}

	BTreeElement *e = root;

	while (e->type == BTreeElement::ELEMENT_NODE) {
		e = ((BTreeNode*)e)->getSubNodeFor(id);
		if (e == NULL)
			return;
	}

	BTreeLeaf *leaf = (BTreeLeaf*)e;

	if (!leaf->add(id, record)) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Not enough space in leaf");
		BTreeLeaf *newLeaf = new BTreeLeaf(this, leafCapacity);

		int oldMinId = leaf->getMinId();
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Splitting");
		leaf->split(newLeaf);
		BTreeLeaf *addTo;
		if (leaf->getMinId() == -1 || id > leaf->getMinId())
			addTo = leaf;
		else
			addTo = newLeaf;

		if (!addTo->add(id, record)) {
			//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Leaf add FAILED!");
			return;
		}

		if (leaf->getParent() == NULL) {
			//__android_log_print(ANDROID_LOG_INFO, "Jello",  "This leaf's parent is NULL, creating new node");
			BTreeNode *node = new BTreeNode(this, nodeCapacity);
			node->addChild(leaf->getMinId(), leaf);
			root = node;
		}

		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Adding new node to parent");
		addToNode(leaf->getParent(), newLeaf);
	}

}
Esempio n. 17
0
BTreeNode * BPlusTree::GetLeafNode(int key)
{
	BTreeNode * temp = root;
	while(temp!= NULL)
	{
		if(temp->isLeaf())
		{
			return temp;
		}
		else
		{
			bool found = false;
			for (unsigned int i = 0 ; i < temp->getkeys().size() ; i++)
			{
				if(key <= temp->getkeys()[i])
				{
					temp = temp->getChidNodes()[i];
					found = true;
					break;
				}
			}
			if(!found)
			{
				temp = temp->getChidNodes()[temp->getChidNodes().size() -1];
			}
		}
	}
	return NULL;
}
Esempio n. 18
0
void BPlusTree::splitParent(BTreeNode * node , int key , BTreeNode * leftNode , BTreeNode * rightNode)
{
		if(node == NULL)
			{
				root = new BTreeNode();
				root->getkeys().push_back(key);
				root->getChidNodes().push_back(leftNode);
				root->getChidNodes().push_back(rightNode);
				return;
			}
		node->insertIntoNode(key , leftNode , rightNode);
		int splitIndex = node->getkeys().size()/2;


		BTreeNode * left = node;
		BTreeNode * right = new BTreeNode();
		int keyToElevate = node->getkeys()[splitIndex];
		right->copyContent(left , splitIndex+1 , node->getkeys().size()-1);
		left->getkeys().resize(splitIndex);
		left->getChidNodes().resize(splitIndex);

		if(node->getparent() == NULL || node->getparent()->isFull())
			splitParent(node->getparent() ,keyToElevate, left , right );
		else
		{
			node->insertIntoNode(keyToElevate , left , right);
			left->setParent(node);
			right->setParent(node);
		}
		return;
}
Esempio n. 19
0
void BPlusTree::insert(DataType val) {
    if (!root) {
        root = new BTreeNode();
    }

    // searching a place for insert
    BTreeNode *tp = root;
    bool f = true;
    while (!tp->isLeaf) {
        f = false;
        for(ListNode* lp = tp->children.begin(); lp; lp = lp->next) {
            if (lp->index > val) {
                tp = lp->child;
                f = true;
                break;
            }
        }
        if (!f) {
            // right node (max)
            while (!tp->isLeaf) {
                tp = tp->children.end()->child;
            }
        }
    }
    
    if(!f) {
        //fixing delegates
        int tmp = tp->max();
        ListNode *d = tp->parent->children.find( tp->max() );
        tp->children.insert(val);
        while (d && d->child->parent && d->index == d->child->parent->max()) {
            d->index = val;
            d = d->child->parent->children.find( tmp );
        }
    } else {
        tp->children.insert(val);
    }
    
    
    // if D keys are in this Node then split Node
    if (tp->children.getLength() >= D) {
        split(tp);
    }
}
Esempio n. 20
0
void BTree::update(int id, RecordInfo *record) {
	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Update: %d", id);
	BTreeElement *e = root;

	if (e == NULL)
		return;

	while (e->type == BTreeElement::ELEMENT_NODE) {
		e = ((BTreeNode*)e)->getSubNodeFor(id);
		if (e == NULL)
			return;
	}

	BTreeLeaf *leaf = (BTreeLeaf*)e;

	if (!leaf->update(id, record)) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "update failed, splitting node");
		BTreeLeaf *newLeaf = new BTreeLeaf(this, leafCapacity);
		int oldMinId = leaf->getMinId();
		leaf->split(newLeaf);
		if (id < leaf->getMinId()) {
			if (!newLeaf->update(id, record)) {
				//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Leaf add FAILED!");
				return;
			}
		} else {
			if (!leaf->update(id, record)) {
				//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Leaf add FAILED!");
				return;
			}
		}

		if (leaf->getParent() == NULL) {
			BTreeNode *node = new BTreeNode(this, nodeCapacity);
			node->addChild(leaf->getMinId(), leaf);
		}

		addToNode(leaf->getParent(), newLeaf);
	} else
		mergeNode(leaf);

}
Esempio n. 21
0
void BPlusTree::split(BTreeNode* node) {
    DataType m = node->max();
    
    // new right Node
    BTreeNode *bro = new BTreeNode();
    bro->parent = node->parent;
    bro->lbro = node;
    bro->rbro = node->rbro;
    bro->isLeaf = node->isLeaf;
    node->rbro = bro;
    
    // splitting
    ListNode *lp = node->children.begin();
    int len1 = node->keysCount()/2;
    int len2 = node->keysCount() - len1;
    for(int i = 0; i < len1; i++) {
        lp = lp->next;
    }
    node->children.setLength( len1 );
    bro->children.setLength( len2 );
    bro->children.setBegin( lp );
    bro->children.setEnd( node->children.end() );
    node->children.setEnd( lp->prev );
    lp->prev->next = nullptr;
    lp->prev = nullptr;
    
    // fixing of delegates
    if (node == root) {
        BTreeNode *tp = new BTreeNode();
        tp->isLeaf = false;
        tp->children.insert( node->max(), node );
        tp->children.insert( bro->max(), bro );
        root = tp;
        node->parent = root;
        bro->parent = root;
    } else {
        ListNode *par = node->parent->children.find( m );
        par->child = bro;
        node->parent->children.insert( node->max(), node );
    }
}
Esempio n. 22
0
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()
Esempio n. 23
0
void BPlusTree::split(BTreeNode * node , int key , int value )
{
	// key should not be duplicate;
	// this method is only for leafnodes;
	int splitIndex = -1;
	node->InsertIntoLeafNode(key,value);
	splitIndex = node->getkeys().size()/2;
	BTreeNode * left = node;
	BTreeNode * right = new BTreeNode();
	right->setLeaf(true);
	int keyToElevate = node->getkeys()[splitIndex];
	right->copyContent(node , splitIndex +1 , node->getkeys().size()-1);
	node->getkeys().resize(splitIndex+1);
	if(node->getparent() == NULL )
	{
		root = new BTreeNode();
		root->getkeys().push_back(keyToElevate);
		root->getChidNodes().push_back(left);
		root->getChidNodes().push_back(right);
		left->setParent(root);
		right->setParent(root);
		return;
	}
	if(node->getparent()->isFull())
	{
		splitParent( node->getparent() , keyToElevate , left , right);
	}
	else
	{
		node->getparent()->insertIntoNode(keyToElevate , left , right);
	}
}
Esempio n. 24
0
void BTree::insert(int k)
{
    // If tree is empty
    if (root == NULL)
    {
        // Allocate memory for root
        root = new BTreeNode(t, true);
        root->keys[0] = k;  // Insert key
        root->n = 1;  // Update number of keys in root
    }
    else // If tree is not empty
    {
        // If root is full, then tree grows in height
        if (root->n == 2 * t - 1)
        {
            // Allocate memory for new root
            BTreeNode *s = new BTreeNode(t, false);

            // Make old root as child of new root
            s->C[0] = root;

            // Split the old root and move 1 key to the new root
            s->splitChild(0, root);

            // New root has two children now.  Decide which of the
            // two children is going to have new key
            int i = 0;
            if (s->keys[0] < k)
                i++;
            s->C[i]->insertNonFull(k);

            // Change root
            root = s;
        }
        else  // If root is not full, call insertNonFull for root
            root->insertNonFull(k);
    }
}
Esempio n. 25
0
void BTree::insertar(char k[40]){
	if (raiz == NULL){	//si el arbol esta vacio
		raiz = new BTreeNode(true);
		raiz->llaves[0] = k;	//agregamos la llave
		raiz->n = 1;
	}else{	//si el arbol no esta vacio
		if (raiz->n == 2*o-1){	//si la raiz esta llena, el arbol crece en altura
			BTreeNode *m = new BTreeNode(false);
			m->C[0] = raiz;
			m->dividirHijo(0, raiz);
			int i = 0;
			if (m->llaves[0] == k){
				i++;
			}
			m->C[i]->insertarNoLleno(k);
			raiz = m;
		}else{	//si la raiz no esta llena
			raiz->insertarNoLleno(k);
		}
	}


}
Esempio n. 26
0
    void insert(int item) 
    {
      if( root==NULL ) 
         root = new BLeaf();
      int newKey;
      BTreeNode *split = root->insert(item, newKey);
      if(split!=NULL) {      
	 cout << "Splitting root " << endl;
         BInternal *newRoot = new BInternal();
         newRoot->child[0] = root;
         newRoot->child[1] = split;
         newRoot->marker[1] = newKey;
         newRoot->currChildren = 2;
         root = newRoot;
      }
    }
Esempio n. 27
0
void test_save_open_node() {
    BTreeFS fs("test_btree_fs.dat", 4);
    BTreeNode node = fs.allocNode(true);
    node.setSentinel(rand());
    node.setKeysNum(4);
    node.addChild(23, rand());
    node.addChild(48, rand());
    node.addChild(236, rand());
    node.addChild(326, rand());
    fs.saveNode(node);
    BTreeNode saved_node = fs.openNode(node.ref());
    assert(node == saved_node);
}
void BTree::delet(int k)
{
    if (!root)
    {
        cout << "Books are not available in the market\n";
        return;
    }
    root->delet(k);
    if (root->n==0)
    {
        BTreeNode *tmp = root;
        if (root->leaf)
            root = NULL;
        else
            root = root->C[0];
        delete tmp;
    }
    return;
}
	BTreeNode *search(int k){
		return (root=NULL)?NULL:root->search(k);
	}
	void traverse(){
		if(root!=NULL){
			root->traverse();
		}
	}