/// <summary>
/// 节点类
/// </summary>
class Node
{
//三叉链表中,每个结点除了存储本身的数据外,还应该设置两个指针域left和right,
//它们分别指向左孩子和右孩子,当需要在二叉树中经常寻找某结点的双亲
//每个结点还可以加一个指向双亲的指针域parent
private int data; //数据域
private Node leftChild; //左孩子指针域
private Node rightChild;//右孩子指针域
private Node parent; //父亲指针域
public Node(int data)
{
this.data = data;
//leftChild = null;
//rightChild = null;
//parent = null;
}
public int Data { get => data; set => data = value; }
internal Node LeftChild { get => leftChild; set => leftChild = value; }
internal Node RightChild { get => rightChild; set => rightChild = value; }
internal Node Parent { get => parent; set => parent = value; }
}
/// <summary>
/// 二叉排序树 左子树小于根节点 右子树大于等于根节点
/// </summary>
class BinarySortingTree
{
private Node root; //定义一个为null的根节点
private int count; //元素个数
internal Node Root { get => root; set => root = value; }
public BinarySortingTree()
{
Root = null;
count = 0;
}
public int GetLength()
{
return count;
}
//添加数据
public void Add(int elem)
{
//若根节点为空则设置新节点为根节点
//否则在左右字树上添加
//添加的元素在叶节点上
Node newNode = new Node(elem);
if (Root == null)
{
Root = newNode;
count++;
return;
}
else
{
Node temp = Root;
while (true)
{
//往右子树添加
if (elem >= temp.Data)
{
if (temp.RightChild == null)
{
temp.RightChild = newNode;
newNode.Parent = temp;
count++;
break;
}
else
{
temp = temp.RightChild;
}
}
//往左子树添加
else
{
if (temp.LeftChild == null)
{
temp.LeftChild = newNode;
newNode.Parent = temp;
count++;
break;
}
else
{
temp = temp.LeftChild;
}
}
}
}
}
/// <summary>
/// 中序遍历
/// </summary>
public void InorderTraversal(Node node)
{
//递归结束条件
if (node == null)
{
return;
}
InorderTraversal(node.LeftChild);
Console.Write(node.Data + " ");
InorderTraversal(node.RightChild);
}
public void InorderTraversal()
{
InorderTraversal(Root);
}
//查找
public bool Find(Node node,int elem)
{
if (node == null)
{
return false;
}
if (node!=null&&node.Data==elem)
{
return true;
}
else if (node.Data>elem)
{
return Find(node.LeftChild, elem);
}
else
{
return Find(node.RightChild, elem);
}
}
public bool DeleteElem( int elem)
{
Node temp = root;
while (true)
{
if (temp==null)
{
return false;
}
if (temp.Data==elem)
{
Delete(temp);
return true;
}
if (temp.Data<elem)
{
temp = temp.RightChild;
}
else
{
temp = temp.LeftChild;
}
}
}
//删除
public void Delete(Node node)
{
//node为叶子节点时候
if (node.LeftChild==null&&node.RightChild==null)
{
//他自己为根节点的时候
if (node.Parent==null)
{
root= null;
}
else if (node.Parent.LeftChild==node)
{
node.Parent.LeftChild = null;
count--;
}
else if (node.Parent.RightChild==node)
{
node.Parent.RightChild = null;
count--;
}
return;
}
//被删的节点只有左子树或右子树的时候用其左子树或右子树替换它
if (node.LeftChild!=null&&node.RightChild==null)
{
//node.Data = node.LeftChild.Data;
//if (node.LeftChild.LeftChild!=null)
//{
//}
//else
//{
// node.LeftChild = null;
//}
node.Data = node.LeftChild.Data;
node.LeftChild = null;
count--;
return;
}
if (node.RightChild != null && node.LeftChild == null)
{
//Node temp = node; //临时节点
//Node parentNode = node.Parent; //父节点
if (node.Parent == null)
{
node.Data = node.RightChild.Data;
}
//if (parentNode.RightChild==node)
//{
// parentNode.RightChild = node.RightChild;
// temp.RightChild.Parent =parentNode;
//}
//else if (parentNode.LeftChild==node)
//{
// parentNode.LeftChild = node.LeftChild;
// temp.LeftChild.Parent = parentNode;
//}
node.Data = node.RightChild.Data;
node.RightChild= null;
count--;
return;
}
//左子树中最大节点 或右子树中最小节点替换之
Node temp = node.RightChild;
while (temp.LeftChild!=null)
{
temp = temp.LeftChild;
}
node.Data = temp.Data;
Delete(temp);
}
}