1. 定义:

public class BinarySortTree<T> where T : IComparable
{
public T Data { set; get; }
public BinarySortTree<T> LeftChild { set; get; }
public BinarySortTree<T> RightChild { set; get; }
private BinarySortTree()
{
}
}
{
public T Data { set; get; }
public BinarySortTree<T> LeftChild { set; get; }
public BinarySortTree<T> RightChild { set; get; }
private BinarySortTree()
{
}
}
2. 创建:

#region Create a new binary sort tree
public static BinarySortTree<T> Create(T[] dataList)
{
if (dataList == null || dataList.Count() < 1)
{
return null;
}
BinarySortTree<T> tree = new BinarySortTree<T>
{
Data = dataList[0]
};
for (int i = 1; i < dataList.Length; i++)
{
InsertNode(tree, dataList[i]);
}
return tree;
}
private static void InsertNode(BinarySortTree<T> tree, T data)
{
if (tree == null)
{
tree = new BinarySortTree<T>
{
Data = data
};
return;
}
BinarySortTree<T> node = tree;
do
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
{
node.LeftChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.LeftChild;
}
}
else
{
if (node.RightChild == null)
{
node.RightChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.RightChild;
}
}
} while (node != null);
}
#endregion
public static BinarySortTree<T> Create(T[] dataList)
{
if (dataList == null || dataList.Count() < 1)
{
return null;
}
BinarySortTree<T> tree = new BinarySortTree<T>
{
Data = dataList[0]
};
for (int i = 1; i < dataList.Length; i++)
{
InsertNode(tree, dataList[i]);
}
return tree;
}
private static void InsertNode(BinarySortTree<T> tree, T data)
{
if (tree == null)
{
tree = new BinarySortTree<T>
{
Data = data
};
return;
}
BinarySortTree<T> node = tree;
do
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
{
node.LeftChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.LeftChild;
}
}
else
{
if (node.RightChild == null)
{
node.RightChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.RightChild;
}
}
} while (node != null);
}
#endregion
3. 插入新节点:

/// <summary>
/// insert a node
/// </summary>
/// <param name="data"></param>
public void InsertNode(T data)
{
BinarySortTree<T> node = this;
do
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
{
node.LeftChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.LeftChild;
}
}
else
{
if (node.RightChild == null)
{
node.RightChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.RightChild;
}
}
} while (node != null);
}
/// insert a node
/// </summary>
/// <param name="data"></param>
public void InsertNode(T data)
{
BinarySortTree<T> node = this;
do
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
{
node.LeftChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.LeftChild;
}
}
else
{
if (node.RightChild == null)
{
node.RightChild = new BinarySortTree<T>
{
Data = data
};
return;
}
else
{
node = node.RightChild;
}
}
} while (node != null);
}
4. search:

/// <summary>
/// Search the node of whose Data equal to data
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BinarySortTree<T> Search(T data)
{
BinarySortTree<T> node = this;
do
{
if (data.CompareTo(node.Data) == 0)
{
return node;
}
else
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
{
break;
}
else
{
node = node.LeftChild;
}
}
else
{
if (node.RightChild == null)
{
break;
}
else
{
node = node.RightChild;
}
}
}
} while (node != null);
return null;
}
/// Search the node of whose Data equal to data
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BinarySortTree<T> Search(T data)
{
BinarySortTree<T> node = this;
do
{
if (data.CompareTo(node.Data) == 0)
{
return node;
}
else
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
{
break;
}
else
{
node = node.LeftChild;
}
}
else
{
if (node.RightChild == null)
{
break;
}
else
{
node = node.RightChild;
}
}
}
} while (node != null);
return null;
}
5. 删除节点:

/// <summary>
/// Remove the node of whose data equal to data from binary sort tree
/// </summary>
/// <param name="data"></param>
public static void Remove(BinarySortTree<T> tree, T data)
{
if (tree == null)
return;
// if equal to root node
BinarySortTree<T> node = tree;
if (data.CompareTo(node.Data) == 0)
{
if (node.LeftChild == null)
{
tree = node.RightChild;
}
else
{
if (node.RightChild == null)
{
tree = node.LeftChild;
}
else
{
BinarySortTree<T> minParentNode = node;
BinarySortTree<T> minNode = node.RightChild;
while (minNode.RightChild != null)
{
minParentNode = minNode;
minNode = minNode.RightChild;
}
minNode.LeftChild = node.LeftChild;
minNode.RightChild = minNode.Equals(node.RightChild) ? null : node.RightChild;
minParentNode.LeftChild = null;
tree = minNode;
}
}
return;
}
// don't equal to root node
BinarySortTree<T> parentNode = tree;
bool isLeft = true;
do
{
// equal to current node
if (data.CompareTo(node.Data) == 0)
{
if (node.LeftChild == null)
{
node = node.RightChild;
}
else
{
if (node.RightChild == null)
{
node = node.LeftChild;
}
else
{
BinarySortTree<T> minParentNode = node;
BinarySortTree<T> minNode = node.RightChild;
while (minNode.LeftChild != null)
{
minParentNode = minNode;
minNode = minNode.LeftChild;
}
minNode.LeftChild = node.LeftChild;
minNode.RightChild = node.RightChild;
minParentNode.LeftChild = null;
node = minNode;
}
}
if (isLeft)
parentNode.LeftChild = node;
else
parentNode.RightChild = node;
return;
}
else
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
return;
else
{
parentNode = node;
node = node.LeftChild;
isLeft = true;
}
}
else
{
if (node.RightChild == null)
return;
else
{
parentNode = node;
node = node.RightChild;
isLeft = false;
}
}
}
} while (node != null);
}
/// Remove the node of whose data equal to data from binary sort tree
/// </summary>
/// <param name="data"></param>
public static void Remove(BinarySortTree<T> tree, T data)
{
if (tree == null)
return;
// if equal to root node
BinarySortTree<T> node = tree;
if (data.CompareTo(node.Data) == 0)
{
if (node.LeftChild == null)
{
tree = node.RightChild;
}
else
{
if (node.RightChild == null)
{
tree = node.LeftChild;
}
else
{
BinarySortTree<T> minParentNode = node;
BinarySortTree<T> minNode = node.RightChild;
while (minNode.RightChild != null)
{
minParentNode = minNode;
minNode = minNode.RightChild;
}
minNode.LeftChild = node.LeftChild;
minNode.RightChild = minNode.Equals(node.RightChild) ? null : node.RightChild;
minParentNode.LeftChild = null;
tree = minNode;
}
}
return;
}
// don't equal to root node
BinarySortTree<T> parentNode = tree;
bool isLeft = true;
do
{
// equal to current node
if (data.CompareTo(node.Data) == 0)
{
if (node.LeftChild == null)
{
node = node.RightChild;
}
else
{
if (node.RightChild == null)
{
node = node.LeftChild;
}
else
{
BinarySortTree<T> minParentNode = node;
BinarySortTree<T> minNode = node.RightChild;
while (minNode.LeftChild != null)
{
minParentNode = minNode;
minNode = minNode.LeftChild;
}
minNode.LeftChild = node.LeftChild;
minNode.RightChild = node.RightChild;
minParentNode.LeftChild = null;
node = minNode;
}
}
if (isLeft)
parentNode.LeftChild = node;
else
parentNode.RightChild = node;
return;
}
else
{
if (data.CompareTo(node.Data) < 0)
{
if (node.LeftChild == null)
return;
else
{
parentNode = node;
node = node.LeftChild;
isLeft = true;
}
}
else
{
if (node.RightChild == null)
return;
else
{
parentNode = node;
node = node.RightChild;
isLeft = false;
}
}
}
} while (node != null);
}
6. 测试:

BinarySortTree<int> tree = BinarySortTree<int>.Create(new int[]{5, 10, 5, 20, 17, 12, 19, 2});
tree.InsertNode(1);
tree.InsertNode(0);
BinarySortTree<int> node = tree.Search(1);
TreeAction<int>.Remove(tree, 10);
tree.InsertNode(1);
tree.InsertNode(0);
BinarySortTree<int> node = tree.Search(1);
TreeAction<int>.Remove(tree, 10);