using System;
using System.Collections.Generic;
using System.Linq;
namespace BTree
{
class Program
{
static void Main(string[] args)
{
#region B树的定义
//1.有x.n个关键字,x.leaf是否为叶子节点(是叶子节点为true),x.key1<=x.key2<=...x.keyn
//2.有x.n+1个x.ci指向子的指针
//3.父的关键字ki大于之前指针指向子树的所有关键字.小于之后的子树的所有关键字
//4.所有叶节点高度一样
//5.节点的关键字数目限定和子树数目的限定. 对于t>=2. 关键字的范围为 t-1~2t-1(根除外,可以只有一个关键字) ,子树范围为t~2t
//其实5的定义和阶数定义一样.m阶的b树.且根最少两个子树. 非叶子节点至少floor(m/2)-1个关键字,至多m个.则2t=m
BTree<int> tree=new BTree<int>(3);
//测试插入
BTreeInsertNode(tree,15);
BTreeInsertNode(tree, 16);
BTreeInsertNode(tree, 25);
BTreeInsertNode(tree, 34);
BTreeInsertNode(tree, 58);
BTreeInsertNode(tree, 19);
BTreeInsertNode(tree, 68);
BTreeInsertNode(tree, 78);
BTreeInsertNode(tree, 95);
//测试查找
Console.WriteLine(BTreeSearch(tree.Root,78).Position);
//测试删除
BTreeDeleteNode(tree,15);
Console.WriteLine(BTreeSearch(tree.Root,15)==null);
#endregion
}
#region 树节点数据结构定义
/// <summary>
/// B树节点的定义
/// </summary>
/// <typeparam name="T"></typeparam>
public class BTreeNode<T> where T: IComparable<T>
{
/// <summary>
/// Key关键字数量(n)
/// </summary>
public int KeyCount { get; set; }
/// <summary>
/// 关键字数组(n)
/// </summary>
public T[] Keys { get; set; }
/// <summary>
/// 孩子指针(n+1)
/// </summary>
public BTreeNode<T>[] Childs { get; set; }
/// <summary>
/// 是否叶子节点(是则为true)
/// </summary>
public bool IsLeaf { get; set; }
/// <summary>
/// 节点阶数,因为节点的数组大小受到是m阶b树的限制
/// </summary>
public int Order { get; }
/// <summary>
/// 初始化节点,数组为最大阶数大小
/// n==keycount必有n个关键字,必有n+1个孩子指针,为叶节点时孩子全部为null
/// </summary>
/// <param name="order">阶数</param>
public BTreeNode(int order)
{
KeyCount = 0;
Keys=new T[order];
Childs=new BTreeNode<T>[order+1];
IsLeaf = true;
Order = order;
}
/// <summary>
/// 清理大于KeyCount的数组位置为默认值
/// </summary>
public void Clear()
{
if (this.Keys != null)
{
for (int i = KeyCount; i < Order; i++)
{
this.Keys[i] = default(T);
}
}
}
}
#endregion
#region B树的定义
public class BTree<T> where T:IComparable<T>
{
/// <summary>
/// 根节点
/// </summary>
public BTreeNode<T> Root { get; set; }
/// <summary>
/// 阶数
/// </summary>
public int Order { get; }
/// <summary>
/// 构造时指定阶数对树进行限定
/// </summary>
/// <param name="order"></param>
public BTree(int order)
{
Order = order;
Root=new BTreeNode<T>(order);
}
/// <summary>
/// 树节点是否能被添加
/// </summary>
/// <returns></returns>
public bool IsCanAdded(BTreeNode<T> node)
{
return node.Order == Order;
}
}
#endregion
#region 创建B树
public static BTree<int> CreateBTree(int order)
{
if(order%2!=1) throw new Exception("传入的阶数不合法!");
BTree<int> tree=new BTree<int>(order);
return tree;
}
#endregion
#region B树查找返回值
public class BTreeSearchReturnValue<T> where T:IComparable<T>
{
public BTreeNode<T> Node { get; set; }
public int Position { get; set; }
public BTreeSearchReturnValue(BTreeNode<T> node, int position)
{
Node = node;
Position = position;
}
}
#endregion
#region 节点查找
public static BTreeSearchReturnValue<int> BTreeSearch(BTreeNode<int> x,int k)
{
int i = 0;
while (i<x.KeyCount && x.Keys[i]<k)
{
i++;
}
if(i<x.KeyCount && x.Keys[i]==k) return new BTreeSearchReturnValue<int>(x,i);
else if (x.IsLeaf) return null;
else return BTreeSearch(x.Childs[i], k);
}
#endregion
#region 节点的分裂
public static void BTreeSplitNode(BTreeNode<int> x,int i)
{
int splitLength = (x.Order + 1) / 2 - 1;
BTreeNode<int> z=new BTreeNode<int>(x.Order);
BTreeNode<int> y = x.Childs[i];
z.IsLeaf = y.IsLeaf;
z.KeyCount = splitLength;
for (int j = 0; j < splitLength; j++)
{
z.Keys[j] = y.Keys[splitLength + j+1];
}
if (!y.IsLeaf)
{
for (int j = 0; j <= splitLength; j++)
{
z.Childs[j] = y.Childs[splitLength +1+ j];
}
}
y.KeyCount = splitLength;
for (int j = x.KeyCount; j >=i+1; j--)
{
x.Childs[j + 1] = x.Childs[j];
}
x.Childs[i + 1] = z;
for (int j = x.KeyCount-1; j >=i; j--)
{
x.Keys[j + 1] = x.Keys[j];
}
x.Keys[i] = y.Keys[splitLength];
x.KeyCount++;
y.Clear();//清理大于keycount位置的元素,也可以不清理
}
#endregion
#region 节点的插入(非满)
public static void BTreeInsertNodeNotFull(BTreeNode<int> x,int k)
{
int i = x.KeyCount-1;
if (x.IsLeaf)
{
while (i>=0 && x.Keys[i]>k)
{
x.Keys[i + 1] = x.Keys[i];
i--;
}
x.Keys[i + 1] = k;
x.KeyCount++;
}
else
{
while (i >= 0 && x.Keys[i] > k)
{
i--;
}
i = i + 1;
if (x.Childs[i].KeyCount == x.Order)
{
BTreeSplitNode(x,i);
if (x.Keys[i] < k) i++;
}
BTreeInsertNodeNotFull(x.Childs[i],k);
}
}
#endregion
#region 节点插入(从根单程向下)
public static void BTreeInsertNode(BTree<int> t,int k)
{
BTreeNode<int> root = t.Root;
if (root.KeyCount == root.Order)
{
BTreeNode<int> s=new BTreeNode<int>(root.Order);
t.Root = s;
s.IsLeaf = false;
s.Childs[0] = root;
BTreeSplitNode(s,0);
BTreeInsertNodeNotFull(s,k);
}
else BTreeInsertNodeNotFull(root, k);
}
#endregion
#region 前驱(只找叶节点方向)
public static int BTreeFindPreDecessor(BTreeNode<int> x)
{
BTreeNode<int> y = x;
while (!y.IsLeaf)
{
y = y.Childs[y.KeyCount];
}
return y.Keys[y.KeyCount-1];
}
#endregion
#region 后继(只找叶节点方向)
public static int BTreeSearchSuccessor(BTreeNode<int> x)
{
BTreeNode<int> y = x;
while (!y.IsLeaf)
{
y = y.Childs[0];
}
return y.Keys[0];
}
#endregion
#region B树的节点删除
//1.如果关键字k在节点x中,并且x是叶子节点,则从x中删除k;
//2.如果关键字在结点x中,并且x是内部节点,则做以下操作
//a.如果x中前于k的子节点y至少包含t个关键字,则找出k在以y为根的子树中的前驱k',递归的删除k',并在x中用k'代替k.
//b.对称的,如果y少于t个关键字,则检查x后于k的节点z,若z至少有t个关键字.则找出z的后继k'.递归的删除k'.并在x中使用k'代替k
//c.否则:y和z都只包含t-1个关键字.则将k和z全部合并放入y(2t-1),这样x就失去了k和指向z的指针,释放z,并且在y中递归的删除k
//3.如果关键字k不在当前节点x中,则必在包含k的子树x.Ci中,如果x.Ci只有t-1个关键字,则执行3a或者3b保证节点至少含有t个节点.然后通过对x的某个合适的
//子节点进行递归而结束
//a.如果x.Ci只包含t-1个关键字,但是他的一个相邻兄弟至少包含t个关键字,则将x中的某一个关键字下降至x.Ci,将x.Ci相邻兄弟的一个关键字提升
//至x,并且将该兄弟的孩子指针移动到x.ci中,这样就使得x.ci增加了一个额外的关键字.
//b.如果x.ci以及相邻兄弟都只有t-1个关键字,则将x.ci与一个相邻兄弟合并,即将x的一个关键字移动至新合并的节点,使之成为新节点的中间关键字
/// <summary>
/// 特殊处理根节点,若根节点keycount==1且左右子树的keycount==t-1,则合并后删除
/// </summary>
/// <param name="t"></param>
/// <param name="k"></param>
public static void BTreeDeleteNode(BTree<int> t,int k)
{
BTreeNode<int> x = t.Root;
if (x.KeyCount == 1)
{
BTreeNode<int> y = x.Childs[0];
BTreeNode<int> z = x.Childs[1];
if (y.KeyCount == (x.Order + 1) / 2 && z.KeyCount == (x.Order + 1) / 2)
{
BTreeNodeMerge(x, 0, y, z);
t.Root = y;
BTreeDeleteNoNode(y, k);
}
else BTreeDeleteNoNode(x, k);
}
else BTreeDeleteNoNode(x,k);
}
public static void BTreeDeleteNoNode(BTreeNode<int> x,int k)
{
int i = 0;
int t = (x.Order + 1) / 2;
//获取删除位置,为i的key或者为i的子树上
while (i < x.KeyCount && x.Keys[i] < k)
{
i++;
}
if (x.IsLeaf)
{
//1.如果关键字k在节点x中,并且x是叶子节点,则从x中删除k;
if (x.Keys[i] == k)
{
for (int j = i; j < x.KeyCount; j++)
{
x.Keys[j] = x.Keys[j+1];
}
x.KeyCount--;
}
else throw new Exception("不存在该关键字!");
}
else
{
BTreeNode<int> y = x.Childs[i];
BTreeNode<int> z=null;
if (i < x.KeyCount)
{
z = x.Childs[i + 1];
}
if (i < x.KeyCount && x.Keys[i] == k)
{
//2.如果关键字在结点x中,并且x是内部节点
if (y.KeyCount > t - 1)
{
//2.a
int newK = BTreeFindPreDecessor(y);
BTreeDeleteNoNode(y,newK);
x.Keys[i] = newK;
}
else if (z.KeyCount > t - 1)
{
//2.b
int newK = BTreeSearchSuccessor(z);
BTreeDeleteNoNode(z,newK);
x.Keys[i] = newK;
}
else
{
//2.c
BTreeNodeMerge(x,i,y,z);
BTreeDeleteNoNode(y,k);
}
}
else
{
//3.如果关键字k不在当前节点x中,则必在包含k的子树x.Ci中,如果x.Ci只有t-1个关键字,则执行3a或者3b保证节点至少含有t个节点.然后通过对x的某个合适的
//子节点进行递归而结束
BTreeNode<int> p = null;
if (i > 0)
{
p = x.Childs[i-1];
}
if (y.KeyCount == t - 1)
{
if (i > 0 && p.KeyCount > t - 1)
{
//3.a-left
BTreeShiftToRightChild(x,i-1,p,y);
}
else if (i < x.KeyCount && z.KeyCount > t - 1)
{
//3.a-right
BTreeShifToLeftChild(x,i,y,z);
}
else if (i>0)
{
//3.b-merge left
BTreeNodeMerge(x,i-1,p,y);
y = p;
}
else BTreeNodeMerge(x,i,y,z);//3.b-merge right
}
BTreeDeleteNoNode(y,k);
}
}
}
#endregion
#region 节点的合并
/// <summary>
/// 当左右两个节点都为t-1个关键字时,将x.keyi与z合并到y
/// </summary>
/// <param name="x">父节点(必不为t-1个关键字,若为则需要执行3.a,b使之满足)</param>
/// <param name="i">要合并的子树索引</param>
/// <param name="y">x的第i子树</param>
/// <param name="z">x的第i+1子树</param>
public static void BTreeNodeMerge(BTreeNode<int> x,int i,BTreeNode<int> y,BTreeNode<int> z)
{
int t = (y.Order + 1) / 2;
y.KeyCount = 2*t-1;
for (int j = t; j < y.KeyCount; j++)
{
y.Keys[j] = z.Keys[j-t];
}
y.Keys[t - 1] = x.Keys[i];
if (!y.IsLeaf)
{
for (int j = t; j < y.KeyCount+1; j++)
{
y.Childs[j] = x.Childs[j-t];
}
}
for (int j = i; j < x.KeyCount-1; j++)
{
x.Keys[j] = x.Keys[j + 1];
}
for (int j = i+1; j < x.KeyCount; j++)
{
x.Childs[j] = x.Childs[j+1];
}
x.KeyCount--;
}
#endregion
#region 从左边借节点,右边是带删除关键字的节点
/// <summary>
/// z为带删除关键字的节点,只有t-1个关键字.从左边大于t-1个关键字的节点借一个关键字
/// </summary>
/// <param name="x"></param>
/// <param name="i"></param>
/// <param name="y"></param>
/// <param name="z"></param>
public static void BTreeShiftToRightChild(BTreeNode<int> x,int i,BTreeNode<int> y,BTreeNode<int> z)
{
z.KeyCount++;
for (int j = z.KeyCount; j >0; j++)
{
z.Keys[j] = z.Keys[j-1];
}
z.Keys[0] = x.Keys[i];
x.Keys[i] = y.Keys[y.KeyCount-1];
if (!z.IsLeaf)
{
for (int j = z.KeyCount+1; j >0; j++)
{
z.Childs[j] = z.Childs[j-1];
}
z.Childs[0] = y.Childs[y.KeyCount];
}
y.KeyCount--;
}
#endregion
#region 从右边借关键字,左边是带删除关键字的节点
public static void BTreeShifToLeftChild(BTreeNode<int> x, int i, BTreeNode<int> y, BTreeNode<int> z)
{
y.KeyCount++;
y.Keys[y.KeyCount - 1] = x.Keys[i];
x.Keys[i] = z.Keys[0];
for (int j = 0; j < z.KeyCount-1; j++)
{
z.Keys[j] = z.Keys[j+1];
}
if (!z.IsLeaf)
{
y.Childs[y.KeyCount] = z.Childs[0];
for (int j = 0; j < z.KeyCount; j++)
{
z.Childs[j] = z.Childs[j+1];
}
}
z.KeyCount--;
}
#endregion
}
}
c#实现b树
最新推荐文章于 2024-02-18 01:35:54 发布