二叉搜索树类的C#实现
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/**
* 二叉搜索树
* 一棵二叉搜索树是满足以下条件的二叉树
* 1.所有左节点的值都小于本节点的值
* 2.所有节点的值都小于右节点的值
**/
namespace Algorithm {
class BSTreeNode <T> where T:IComparable {
public BSTreeNode<T> left { get; set; }
public BSTreeNode<T> right { get; set; }
public BSTreeNode<T> parent { get; set; }
public T key { get; set; }
public BSTreeNode(T k) {
key = k;
}
}
class BSTree<T> where T : IComparable {
public BSTreeNode<T> Root;
public int Count = 0;
public BSTree(T key) {
Root = new BSTreeNode<T>(key);
Count++;
}
public BSTree(T[] keys) {
if (keys.Length == 0) {
return;
}
Root = new BSTreeNode<T>(keys[0]);
for (int i = 1; i < keys.Length; i++) {
Insert(keys[i]);
}
}
/**
* 插入
**/
public BSTreeNode<T> Insert(T key) {
BSTreeNode<T> node = Root;
BSTreeNode<T> newNode = new BSTreeNode<T>(key);
BSTreeNode<T> parent = null;
while (node != null) {
parent = node;
if (key.CompareTo(node.key) > 0) {
node = node.right;
} else {
node = node.left;
}
}
newNode.parent = parent;
if (parent == null) {
return newNode;
} else if (key.CompareTo(parent.key) > 0) {
parent.right = newNode;
} else {
parent.left = newNode;
}
Count++;
return newNode;
}
/**
* 递归遍历
* */
public void Walk(BSTreeNode<T> node, Action<BSTreeNode<T>> func, string type = "mid") {
if (node != null) {
if (type == "pre") func(node);
Walk(node.left, func, type);
if (type == "mid") func(node);
Walk(node.right, func, type);
if (type == "post") func(node);
}
}
/**
* 非递归 中序遍历
* **/
public void InOrderTreeWalk(BSTreeNode<T> root, Action<BSTreeNode<T>> func) {
if (root == null) {
return;
}
Stack<BSTreeNode<T>> stack = new Stack<BSTreeNode<T>>((int)(2 * Math.Log(Count + 1)));
BSTreeNode<T> current = root;
while (current != null) {
stack.Push(current);
current = current.left;
}
while (stack.Count != 0) {
current = stack.Pop();
func(current);
BSTreeNode<T> node = current.right;
while (node != null) {
stack.Push(node);
node = node.left;
}
}
}
/**
* 搜索
* **/
public BSTreeNode<T> Search(BSTreeNode<T> node, T key) {
while (node != null && key.CompareTo(node.key) != 0) {
if (key.CompareTo(node.key) < 0) {
node = node.left;
} else {
node = node.right;
}
}
return node;
}
/**
* 最小值
* **/
public BSTreeNode<T> Min(BSTreeNode<T> node) {
while (node != null && node.left != null) {
node = node.left;
}
return node;
}
/**
* 最大值
* **/
public BSTreeNode<T> Max(BSTreeNode<T> node) {
while (node != null && node.right != null) {
node = node.right;
}
return node;
}
/**
* 后继元素
* **/
public BSTreeNode<T> Succ(BSTreeNode<T> node) {
if (node.right != null) {
return Min(node.right);
}
BSTreeNode<T> parent = node.parent;
while (parent != null && node != parent.left) {
node = parent;
parent = node.parent;
}
return parent;
}
/**
* 前驱元素
* **/
public BSTreeNode<T> Pred(BSTreeNode<T> node) {
if (node.left != null) {
return Max(node.left);
}
BSTreeNode<T> parent = node.parent;
while (parent != null && node != parent.right) {
node = parent;
parent = node.parent;
}
return parent;
}
/**
* 删除节点
* */
public BSTreeNode<T> Delete(BSTreeNode<T> t ,BSTreeNode<T> x) {
if (x == null) return Root;
BSTreeNode<T> root = t;
BSTreeNode<T> oldX= x;
BSTreeNode<T> parent = x.parent;
if (x.left == null) {
x = x.right;
} else if (x.right == null) {
x = x.left;
} else {
BSTreeNode<T> y = Min(x.right);
x.key = y.key;
if (y.parent != x) {
y.parent.left = y.right;
} else {
x.right = y.right;
}
return root;
}
if (x != null) {
x.parent = parent;
}
if (parent == null) {
root = x;
} else {
if (parent.left == oldX) {
parent.left = x;
} else {
parent.right = x;
}
}
return Root;
}
public List<T> ToList() {
List<T> list = new List<T>();
Walk(Root, (BSTreeNode<T> node) => {
list.Add(node.key);
}, "mid");
return list;
}
}
}