二叉排序树,又名二叉查找树,其定义为:
二叉排序树或者为空树,或者满足如下性质的二叉树
1,左子树非空,则左子树所有记录小于根节点
2,右子树非空,则右子树所有记录大于根接点
3,左右子树本身又是一棵二叉排序树
/// <summary>
/// 作者:GENGWEI
/// </summary>
class BSTTree
{
/// <summary>
/// 测试程序
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
BiNode dt = null;
IList<ElemType<int, int>> list = new List<ElemType<int, int>>();
list.Add(new ElemType<int, int>(5, 1));
list.Add(new ElemType<int, int>(2, 2));
list.Add(new ElemType<int, int>(6, 3));
list.Add(new ElemType<int, int>(1, 4));
list.Add(new ElemType<int, int>(4, 5));
list.Add(new ElemType<int, int>(7, 6));
list.Add(new ElemType<int, int>(3, 7));
list.Add(new ElemType<int, int>(9, 9));
for (int i = 0; i < list.Count; i++)
{
InsertBST(ref dt, list[i]);
}
PreTraverseBST(dt);
Console.WriteLine(DeleteBST(ref dt, 4));
Console.WriteLine();
PreTraverseBST(dt);
Console.ReadLine();
}
/// <summary>
/// 插入BST树节点
/// </summary>
/// <param name="t"></param>
/// <param name="e"></param>
/// <returns></returns>
static bool InsertBST(ref BiNode t, ElemType<int,int> e)
{
BiNode p, s = null;
bool flag;
SearchBST(t, e.KeyType, null,out p, out flag);
if (!flag)
{
s = new BiNode();
s.Data = e;
s.lchild = s.rchild = null;
if (p == null)
{
t = s;
}
else if (e.KeyType < p.Data.KeyType)
{
p.lchild = s;
}
else
{
p.rchild = s;
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// 搜索树节点
/// </summary>
/// <param name="t"></param>
/// <param name="key"></param>
/// <returns></returns>
static BiNode SearchBST(BiNode t, int key)
{
if (t == null || t.Data.KeyType == key)
{
return t;
}
else if (t.Data.KeyType > key)
{
return SearchBST(t.lchild, key);
}
else
{
return SearchBST(t.rchild, key);
}
}
static void SearchBST(BiNode t,int key,BiNode f,out BiNode p,out bool flag)
{
if (t == null)
{
p = f;
flag = false;
}
else if (key == t.Data.KeyType)
{
p = t;
flag = true;
}
else if (key < t.Data.KeyType)
{
SearchBST(t.lchild, key, t, out p, out flag);
}
else
{
SearchBST(t.rchild, key, t, out p, out flag);
}
}
/// <summary>
/// 删除树节点
/// </summary>
/// <param name="p"></param>
static void DeleteNode(ref BiNode p)
{
BiNode q, s;
if (p.rchild == null)
{
q = p;
p = p.lchild;
q = null;
}
else if (p.lchild == null)
{
q = p;
p = p.rchild;
q = null;
}
else
{
q = p;
s = p.lchild;
while (s.rchild != null)
{
q = s;
s = s.rchild;
}
p.Data = s.Data;
if (q != p)
{
q.rchild = s.lchild;
}
else
{
q.lchild = s.lchild;
}
s = null;
}
}
static bool DeleteBST(ref BiNode p, int e)
{
if (p == null)
{
return false;
}
else
{
if (p.Data.KeyType == e)
DeleteNode(ref p);
else if (p.Data.KeyType > e)
DeleteBST(ref p.lchild, e);
else
DeleteBST(ref p.rchild, e);
return true;
}
}
static void InTraverseBST(BiNode p)
{
if (p != null)
{
InTraverseBST(p.lchild);
Console.Write(p.Data.KeyType + ",");
InTraverseBST(p.rchild);
}
}
static void PreTraverseBST(BiNode p)
{
if (p != null)
{
Console.Write(p.Data.KeyType + ",");
PreTraverseBST(p.lchild);
PreTraverseBST(p.rchild);
}
}
static void PostTraverseBST(BiNode p)
{
if (p != null)
{
PostTraverseBST(p.lchild);
PostTraverseBST(p.rchild);
Console.Write(p.Data.KeyType + ",");
}
}
}
public class BiNode
{
public BiNode() { }
public BiNode(ElemType<int,int> e,BiNode left,BiNode right)
{
Data = e;
lchild = left;
rchild = right;
}
public ElemType<int,int> Data;
public BiNode lchild, rchild;
}
public class ElemType<U, V>
{
public ElemType(U keyType, V otherInfo)
{
KeyType = keyType;
OtherInfo = otherInfo;
}
public U KeyType;
public V OtherInfo;
}