/// <summary>
/// 二叉树节点的定义
/// </summary>
public class Node
{
//本身的数据
public int data;
//左右节点
public Node left;
public Node right;
public void DisplayData()
{
Console.Write(data + " ");
}
}
public class BinarySearchTree
{
Node rootNode = null;
public void Insert(int data)
{
Node parent;
//包装数据进节点
Node newNode = new Node();
newNode.data = data;
//根节点
if (rootNode == null)
{
rootNode = newNode;
}
//找到合适的子节点位置插入
else
{
Node currentNode = rootNode;
while (true)
{
parent = currentNode;
if (newNode.data < currentNode.data)
{
currentNode = currentNode.left;
if (currentNode == null)
{
parent.left = newNode;
break;
}
}
else
{
currentNode = currentNode.right;
if (currentNode == null)
{
parent.right = newNode;
break;
}
}
}
}
}
/// <summary>
/// 中序遍历
/// </summary>
/// <param name="theRoot"></param>
public void InOrder(Node theRoot)
{
if (theRoot != null)
{
InOrder(theRoot.left);
theRoot.DisplayData();
InOrder(theRoot.right);
}
}
//先序遍历
public void PreOrder(Node theRoot)
{
if (theRoot != null)
{
theRoot.DisplayData();
PreOrder(theRoot.left);
PreOrder(theRoot.right);
}
}
//后序遍历
public void PostOrder(Node theRoot)
{
if (theRoot != null)
{
PostOrder(theRoot.left);
PostOrder(theRoot.right);
theRoot.DisplayData();
}
}
public void FindMax()
{
Node current = rootNode;
//找到最右边的即可
while (current.right != null)
{
current = current.right;
}
Console.WriteLine("max:" + current.data);
}
public void FindMin()
{
Node current = rootNode;
//找到最左边的节点即可
while (current.left != null)
{
current = current.left;
}
Console.WriteLine("min:" + current.data);
}
public Node Search(int i)
{
Node current = rootNode;
while (true)
{
if (i < current.data)
{
if (current.left == null) break;
current = current.left;
}
else if (i > current.data)
{
if (current == null) break;
current = current.right;
}
else
{
return current;
}
}
if (current.data != i)
{
return null;
}
return current;
}
public Node Delete(int key)
{
Node parent = rootNode;
Node current = rootNode;
//首先找到需要被删除的节点和其父节点
while (true)
{
if (key < current.data)
{
if (current.left == null) break;
parent = current;
current = current.left;
}
else if (key > current.data)
{
if (current == null) break;
parent = current;
current = current.right;
}
else
{
break;
}
}
//找到被删除的节点后,分四种情况
//1.当是子节点的时候,直接删除
if (current.left == null && current.right == null)
{
if (current == rootNode && rootNode.left == null && rootNode.right == null)
{
rootNode = null;
}
else if (current.data < parent.data)
{
parent.left = null;
}
else
{
parent.right = null;
}
}
//所删除的节点只有左节点的时
else if (current.left != null && current.right == null)
{
if (current.data < parent.data)
parent.left = current.left;
else
parent.right = current.left;
}
//所删除的节点只有右节点时
else if (current.left == null && current.right != null)
{
if (current.data < parent.data)
parent.left = current.right;
else
parent.right = current.right;
}
//当删除的节点有左右节点的时候
else
{
//current是被删除的节点,temp是被删左子树最右边的节点
Node temp;
//先判断是父节点的左节点还是右节点
if (current.data < parent.data)
{
parent.left = current.left;
temp = current.left;
//寻找被删除节点最深的右孩子
while (temp.right != null)
{
temp = temp.right;
}
temp.right = current.right;
}
//右节点
else if (current.data > parent.data)
{
parent.right = current.left;
temp = current.left;
//寻找被删除节点最深的左孩子
while (temp.left != null)
{
temp = temp.left;
}
temp.right = current.right;点击打开链接
}
//当被删节点是根节点,并且有两个孩子时
else
{
temp = current.left;
while (temp.right != null)
{
temp = temp.right;
}
temp.right = rootNode.right;
rootNode = current.left;
}
}
return current;
}
}
转载自 : 点击打开链接