Ejemplo n.º 1
0
int main() {
	CompleteTree testTree = {NULL, 12};
	/*
	        0
	   1         2
	 3   4     5    6
	7 8 9 10 11
	*/
	CompleteTree *tree = &testTree;
	for (unsigned int i = 0; i < 12; i++) {
		if (i < 6) assert(!isLeaf(tree, i));
		else assert(isLeaf(tree, i));
	}
	assert(getLeftChild(tree, 0) == 1);
	assert(getLeftChild(tree, 2) == 5);
	assert(getLeftChild(tree, 4) == 9);
	assert(getLeftChild(tree, 5) == 11);
	for (unsigned int i = 4; i < 7; i++) assert(getLeftSibling(tree, i) == (i - 1));
	for (unsigned int i = 3; i < 6; i++) assert(getRightSibling(tree, i) == (i + 1));
	for (unsigned int i = 1; i < 3; i++) assert(getParent(tree, i) == 0);
	for (unsigned int i = 3; i < 5; i++) assert(getParent(tree, i) == 1);
	for (unsigned int i = 5; i < 7; i++) assert(getParent(tree, i) == 2);
	for (unsigned int i = 7; i < 9; i++) assert(getParent(tree, i) == 3);
	for (unsigned int i = 9; i < 11; i++) assert(getParent(tree, i) == 4);
	assert(getParent(tree, 11) == 5);
	assert(!getDepth(tree, 0));
	for (unsigned int i = 1; i < 3; i++) assert(getDepth(tree, i) == 1);
	for (unsigned int i = 3; i < 7; i++) assert(getDepth(tree, i) == 2);
	for (unsigned int i = 7; i < testTree.length; i++) assert(getDepth(tree, i) == 3);
	assert(getHeight(tree, 0) == 4);
	for (unsigned int i = 1; i < 3; i++) assert(getHeight(tree, i) == 3);
	for (unsigned int i = 3; i < 6; i++) assert(getHeight(tree, i) == 2);
	for (unsigned int i = 6; i < testTree.length; i++) assert(getHeight(tree, i) == 1);
}
Ejemplo n.º 2
0
void LeafNode::splitNode(FatherNode* parentNode, int childIndex) {
	LeafNode* newNode = new LeafNode();
	setKeyNum(MIN_LEAF);
	newNode->setKeyNum(MIN_LEAF + 1);
	newNode->setRightSibling(getRightSibling());
	setRightSibling(newNode);
	newNode->setLeftSibling(this);
	int i;
	for (i = 0; i < MIN_LEAF + 1; ++i) {
		newNode->setKeyValue(i, m_KeyValues[i + MIN_LEAF]);
	}
	for (i = 0; i < MIN_LEAF + 1; ++i) {
		newNode->setData(i, m_Datas[i + MIN_LEAF]);
	}
	((InternalNode*)parentNode)->insert(childIndex, childIndex + 1, m_KeyValues[MIN_LEAF], newNode);
}
Ejemplo n.º 3
0
void BTreeBuilder::truncateChildren(BTreeNode const &node)
{
    assert(node.height);
    assert(node.nEntries);
    PageId pageId = getChild(node, 0);
    BTreePageLock pageLock;
    pageLock.accessSegment(treeDescriptor.segmentAccessor);
    if (node.height > 1) {
        pageLock.lockExclusive(pageId);
        truncateChildren(pageLock.getNodeForRead());
        pageLock.unlock();
    }
    while (pageId != NULL_PAGE_ID) {
        PageId nextPageId = getRightSibling(pageId);
        pageLock.deallocateUnlockedPage(pageId);
        pageId = nextPageId;
    }
}
Ejemplo n.º 4
0
void CLeafNode::split(CNode* parentNode, int childIndex)
{
    CLeafNode* newNode = new CLeafNode();//分裂后的右节点
    setKeyNum(MINNUM_LEAF);
    newNode->setKeyNum(MINNUM_LEAF+1);
    newNode->setRightSibling(getRightSibling());
    setRightSibling(newNode);
    newNode->setLeftSibling(this);
    int i;
    for (i=0; i<MINNUM_LEAF+1; ++i)// 拷贝关键字的值
    {
        newNode->setKeyValue(i, m_KeyValues[i+MINNUM_LEAF]);
    }
    for (i=0; i<MINNUM_LEAF+1; ++i)// 拷贝数据
    {
        newNode->setData(i, m_Datas[i+MINNUM_LEAF]);
    }
    ((CInternalNode*)parentNode)->insert(childIndex, childIndex+1, m_KeyValues[MINNUM_LEAF], newNode);
}