程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> B樹的實現與源代碼二(刪除源代碼)

B樹的實現與源代碼二(刪除源代碼)

編輯:C++入門知識

int BTreeMaximum( BNode *x )
{
	if ( x->leaf )
	{
		return x->key[x->size - 1];
	}
	else
	{
		return BTreeMaximum( x->child[x->size] );
	}
}

int BTreeMinimum( BNode *x )
{
	if ( x->leaf )
	{
		return x->key[0];
	}
	else
	{
		return BTreeMinimum( x->child[0] );
	}
}

void BTreeDelete( BNode *&x, int k )
{
	int i = 0;
	while ( i < x->size && k > x->key[i] )
	{
		i++;
	}
	// case 1
	if ( i < x->size && k == x->key[i] && x->leaf )
	{
		for ( int j = i; j < x->size - 1; ++j )
		{
			x->key[j] = x->key[j + 1];
		}
		x->size--;
	}
	// case 2
	else if ( i < x->size && k == x->key[i] && !x->leaf )
	{
		BNode *y = x->child[i];
		BNode *z = x->child[i + 1];
		// 2a
		if ( y->size >= t )  
		{
			int k_ = BTreeMaximum( y );
			x->key[i] = k_;
			BTreeDelete( y, k_ );
		}
		// 2b
		else if ( z->size >= t )
		{
			int k_ = BTreeMinimum( z );
			x->key[i] = k_;
			BTreeDelete( z, k_ );
		}
		// 2c
		else
		{
			// update the node y
			y->key[t - 1] = k;
			for ( int j = t; j < 2 * t - 1; ++j )
			{
				y->key[j] = z->key[j - t];
				y->child[j] = z->child[j - t];
			}
			y->child[2 * t - 1] = z->child[t - 1];
			y->size = y->size + z->size + 1;

			// update the node x
			for ( int j = i; j < x->size - 1; ++j )
			{
				x->key[j] = x->key[j + 1];
				x->child[j + 1] = x->child[j + 2];
			}
			x->size--;

			// delete z
			delete z;
			BTreeDelete( y, k );
		} // end else 2c
	} // end if case 2
	// case 3
	else if ( i <= x->size )
	{
		if ( x->child[i]->size == t - 1 )
		{
			if ( i > 0 && x->child[i - 1]->size >= t )
			{
				// update x->child[i]
				for ( int j = t - 2; j >= 0; --j )
				{
					x->child[i]->key[j + 1] = x->child[i]->key[j];
					if ( !x->child[i]->leaf )
					{
						x->child[i]->child[j + 2] = x->child[i]->child[j + 1];
					}
				}
				x->child[i]->child[1] = x->child[i]->child[0];
				x->child[i]->key[0] = x->key[i - 1];
				x->child[i]->child[0] = x->child[i - 1]->child[x->child[i - 1]->size];
				x->child[i]->size++;
				// update x
				x->key[i - 1] = x->child[i - 1]->key[x->child[i - 1]->size - 1];
				// update x->child[i - 1]
				x->child[i - 1]->size--;
				BTreeDelete( x->child[i], k );
			}
			else if ( i < x->size && x->child[i + 1]->size >= t )
			{
				// update x->child[i]
				x->child[i]->key[t - 1] = x->key[i];
				x->child[i]->child[t] = x->child[i - 1]->child[0];
				x->child[i]->size++;
				//update x
				x->key[i] = x->child[i - 1]->key[0];
				//update x->child[i - 1]
				for ( int j = 0; j < x->child[i - 1]->size - 1; ++j )
				{
					x->child[i - 1]->key[j] = x->child[i - 1]->key[j + 1];
					x->child[i - 1]->child[j] = x->child[i - 1]->child[j + 1];
				}
				x->child[i - 1]->child[x->child[i - 1]->size - 1] = x->child[i - 1]->child[x->child[i - 1]->size];
				x->child[i - 1]->size--;
				BTreeDelete( x->child[i], k );
			}
			// case 3b
			// merge with the left node x->child[i - 1]
			else if ( i > 0 )
			{
				// update x->child[i - 1]
				x->child[i - 1]->key[t - 1] = x->key[i - 1];
				for ( int j = t; j < 2 * t - 1; ++j )
				{
					x->child[i - 1]->key[j] = x->child[i]->key[j - t];
					x->child[i - 1]->child[j] = x->child[i]->child[j - t];
				}
				x->child[i - 1]->child[2 * t - 1] = x->child[i]->child[t - 1];
				x->child[i - 1]->size = 2 * t - 1;
				// delete x->child[i]
				delete x->child[i];
				// update x
				for ( int j = i; j < x->size; ++j )
				{
					x->key[i - 1] = x->key[i];
					x->child[i] = x->child[i + 1];
				}
				x->size--;
				if ( x->size == 0 )
				{
					x = x->child[0];
					BTreeDelete( x, k );
				}
				else
				{
					BTreeDelete( x->child[i - 1], k );
				}
			}
			// merge with the right node x->child[i + 1]
			else if ( i < x->size )
			{
				// update x->child[i]
				x->child[i]->key[t - 1] = x->key[i];
				for ( int j = t; j < 2 * t - 1; ++j )
				{
					x->child[i]->key[j] = x->child[i + 1]->key[j - t];
					x->child[i]->child[j] = x->child[i + 1]->child[j - t];
				}
				x->child[i]->child[2 * t - 1] = x->child[i + 1]->child[t - 1];
				x->child[i]->size = 2 * t - 1;
				// delete x->child[i + 1]
				delete x->child[i + 1];
				// update x
				for ( int j = i; j < x->size - 1; ++j )
				{
					x->key[j] = x->key[j + 1];
					x->child[j + 1] = x->child[j + 2];
				}
				x->size--;
				if ( x->size == 0 )
				{
					x = x->child[0];
					BTreeDelete( x, k );
				}
				else
				{
					BTreeDelete( x->child[i - 1], k );
				}
			}
		} // end if case 3
		else
		{
			BTreeDelete( x->child[i], k );
        }
	//	
	}
	
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved