首先是节点类
using System;
using System.Collections.Generic;
using System.Text;
namespace DataTructProject
{
class Node
{
public int name_Data;
public Node left;
public Node right;
public void DisplayNode()
{
Console.WriteLine(name_Data);
}
}
}
其次是二叉树查找类
using System;
using System.Collections.Generic;
using System.Text;
namespace DataTructProject
{
class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
/// <summary>
/// 插入一个新的节点和它的关键数值
/// </summary>
/// <param name="_data"></param>
public void Insert(int i)
{
Node tempNode = new Node() {name_Data=i };
if(root==null)
{
root = tempNode;
}
else
{
Node current = root;
Node parent;
while(true)
{
parent = current;
if(i<current.name_Data)
{
current = current.left;
if(current==null)
{
parent.left = tempNode;
break;
}
}
else
{
current = current.right;
if (current == null)
{
parent.right = tempNode;
break;
}
}
}
}
}
/// <summary>
/// 中序遍历 左根右顺序
/// </summary>
/// <param name="theRoot"></param>
public void Middle_Order(Node theRoot)
{
if (!(theRoot == null))
{
Middle_Order(theRoot.left);
theRoot.DisplayNode();
Middle_Order(theRoot.right);
}
}
/// <summary>
/// 先序遍历 根左右顺序
/// </summary>
/// <param name="theRoot"></param>
public void Pre_Order(Node theRoot)
{
if (!(theRoot == null))
{
theRoot.DisplayNode();
Pre_Order(theRoot.left);
Pre_Order(theRoot.right);
}
}
/// <summary>
/// 后序遍历 左右根顺序
/// </summary>
/// <param name="theRoot"></param>
public void Post_Order(Node theRoot)
{
if(!(theRoot==null))
{
Post_Order(theRoot.left);
Post_Order(theRoot.right);
theRoot.DisplayNode();
}
}
/// <summary>
/// 查找最小数 ,最小数就是整个树最靠左的支节点
/// </summary>
/// <returns></returns>
public Node FindMin()
{
Node min = new Node();
Node current = root;
while(true)
{
if(current.left==null)
{
min = current;
break;
}
current = current.left;
}
return min;
}
/// <summary>
/// 查找最大数 ,最大数就是整个树最靠右的支节点
/// </summary>
/// <returns></returns>
public Node FindMax()
{
Node max = new Node();
Node current = root;
while (true)
{
if (current.right == null)
{
max = current;
break;
}
current = current.right;
}
return max;
}
/// <summary>
/// 删除叶子(叶子,即此节点无子节点,然后可以直接删除它)
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool DeleteLeaf(int key)
{
Node current = root;
Node parent = root;
bool isLeftChild = true;
while (current.name_Data != key)
{
parent = current;
if (key < current.name_Data)
{
isLeftChild = true;
current = current.left;
}
else
{
isLeftChild = false;
current = current.right;
}
if (current == null)
{
return false;
}
}
if ((current.left == null) & (current.right == null))
{
if (current == root)
{
root = null;
}
if (isLeftChild)
parent.left = null;
else
parent.right = null;
}
return true;
}
/// <summary>
/// 删除带有一个子节点的节点
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Delete_OneChild(int key)
{
Node current = root;
Node parent = root;
bool isLeftChild = true;
while (current.name_Data != key)
{
parent = current;
if (key < current.name_Data)
{
isLeftChild = true;
current = current.left;
}
else
{
isLeftChild = false;
current = current.right;
}
if (current == null)
{
return false;
}
}
if ((current.left == null) & (current.right == null))
{
if (current == root)
{
root = null;
}
if (isLeftChild)
parent.left = null;
else
parent.right = null;
}
else if (current.right==null)
{
if(current==root)
{
current = root.left;
parent = null;
parent = current;
}
else if(isLeftChild)
{
parent.left = current.left;
}
else
{
parent.right = current.left;
}
}
else if(current.left == null)
{
if (current == root)
{
current = root.right;
parent = null;
parent = current;
}
else if (isLeftChild)
{
parent.left = current.right;
}
else
{
parent.right = current.right;
}
}
return true;
}
/// <summary>
/// 删除带有两个子节点的节点
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Delete_TwoChild(int key)
{
Node current = root;
Node parent = root;
bool isLeftChild = true;
while (current.name_Data != key)
{
parent = current;
if (key < current.name_Data)
{
isLeftChild = true;
current = current.left;
}
else
{
isLeftChild = false;
current = current.right;
}
if (current == null)
{
return false;
}
}
if ((current.left == null) & (current.right == null))
{
if (current == root)
{
root = null;
}
if (isLeftChild)
parent.left = null;
else
parent.right = null;
}
else if (current.right == null)
{
if (current == root)
{
current = root.left;
parent = null;
parent = current;
}
else if (isLeftChild)
{
parent.left = current.left;
}
else
{
parent.right = current.left;
}
}
else if (current.left == null)
{
if (current == root)
{
current = root.right;
parent = null;
parent = current;
}
else if (isLeftChild)
{
parent.left = current.right;
}
else
{
parent.right = current.right;
}
}
else
{
Node successor = GetSuccessorNode(current);
if (current == root)
{
root = successor;
root.left = parent.left;
}
else if (isLeftChild)
{
parent.left = successor;
}
else
{
parent.right = successor;
}
successor.left = current.left;
}
return true;
}
/// <summary>
/// 获取被删除节点的后继节点(即比被删除节点大的节点之中,最小的节点)
/// </summary>
/// <param name="_deleteNode"></param>
/// <returns></returns>
public Node GetSuccessorNode(Node _deleteNode)
{
Node successorParent = _deleteNode;
Node successor = _deleteNode;
Node current = _deleteNode.right;
while (true)
{
successor = current;
current = current.left;
if (current == null)
{
break;
}
successorParent = successor;
}
if (!(successor == _deleteNode.right))
{
successorParent.left = successor.right;
successor.right = _deleteNode.right;
}
return successor;
}
}
}
最后是入口类做的一些测试
using System;
namespace DataTructProject
{
class Program
{
static void Main(string[] args)
{
//BinarySearchTree类的撰写和测试
//BinarySearchTree nums = new BinarySearchTree();
//nums.Insert(23);
//nums.Insert(45);
//nums.Insert(16);
//nums.Insert(37);
//nums.Insert(3);
//nums.Insert(99);
//nums.Insert(22);
//nums.Insert(1);
//nums.Insert(35);
//nums.Insert(38);
//nums.Insert(0);
//nums.Insert(2);
//nums.Insert(200);
//nums.Insert(96);
//nums.Insert(150);
//nums.Insert(800);
// Console.WriteLine("Inorder traversal: ");
// nums.Middle_Order(nums.root);
// Console.WriteLine("Perorder traversal: ");
// nums.Pre_Order(nums.root);
// Console.WriteLine("Postorder traversal: ");
// nums.Post_Order(nums.root);
// Console.WriteLine("查找最小数: ");
// nums.FindMin().DisplayNode();
// Console.WriteLine("查找最大数: ");
// nums.FindMax().DisplayNode();
// Console.WriteLine("删除一个叶子节点: ");
// Console.WriteLine(nums.DeleteLeaf(22));
// bool empty = (nums.root.left.right == null);
//if(empty)
// Console.WriteLine("22的对应节点已被删除");
//删除只有一个子节点的节点
//Console.WriteLine("删除只有一个子节点的节点");
//nums.Delete_OneChild(3);
//nums.root.left.left.right.DisplayNode();
//nums.Delete_OneChild(99);
//nums.root.right.right.DisplayNode();
//删除带有两个子节点的节点
//Console.WriteLine("删除带有两个子节点的节点");
//nums.Delete_TwoChild(99);
//nums.root.right.right.DisplayNode();
//nums.root.right.right.left.DisplayNode();
//nums.root.right.right.right.DisplayNode();
//nums.root.right.right.right.right.DisplayNode();
//nums.root.right.right.right.left.DisplayNode();
Console.ReadKey();
}
}
}
如有错误,敬请指正。