功能实现:
//直接复制(可用)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class BST1<E> where E : IComparable<E>
{
private class Node
{
public E key;
public Node left;
public Node right;
public Node(E key)
{
this.key = key;
left = null;
right = null;
}
}
private Node root;
private int N;
public BST1()
{
root = null;
N = 0;
}
public int Count { get { return N; } }
public bool IsEmpty { get { return N == 0; } }
/// <summary>
/// 添加
/// </summary>
/// <param name="e"></param>
public void Add(E e)
{
root = Add(root, e);
}
//以node为根的树中添加元素e,添加后返回根节点node
private Node Add(Node node, E e)
{
if (node==null)
{
N++;
return new Node(e);
}
if (e.CompareTo(node.key) < 0)
{
node.left = Add(node.left, e);
}
else if(e.CompareTo(node.key) > 0)
{
node.right = Add(node.right, e);
}
return node;
}
/// <summary>
/// 查找
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public bool Contains(E e)
{
return Contains(root, e);
}
//看以node为根的树中是否包含元素e
private bool Contains(Node node, E e)
{
if (node==null)
{
return false;
}
if (e.CompareTo(node.key) ==0)
{
return true;
}
else if (e.CompareTo(node.key) <0)
{
return Contains(node.left, e);
}
else
{
return Contains(node.right, e);
}
}
/// <summary>
/// 前序遍历([根左右])
/// </summary>
public void PreOrder()
{
PreOrder(root);
}
//前序遍历以node为根的二叉查找树
private void PreOrder(Node node)
{
if (node==null)
{
return;
}
Debug.Log(node.key);
PreOrder(node.left);
PreOrder(node.right);
}
/// <summary>
/// 中序遍历(排序[左根右])
/// </summary>
public void InOrder()
{
InOrder(root);
}
//中序遍历以node为根的二叉查找树
private void InOrder(Node node)
{
if (node == null)
{
return;
}
InOrder(node.left);
Debug.Log(node.key);
InOrder(node.right);
}
/// <summary>
/// 后序遍历([左右根])
/// </summary>
public void PostOrder()
{
PostOrder(root);
}
//后序遍历以node为根的二叉查找树
private void PostOrder(Node node)
{
if (node == null)
{
return;
}
PostOrder(node.left);
PostOrder(node.right);
Debug.Log(node.key);
}
/// <summary>
/// 层序遍历
/// </summary>
public void LevelOrder()
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count!=0)
{
Node cur = q.Dequeue();
Debug.Log(cur.key);
if (cur.left!=null)
{
q.Enqueue(cur.left);
}
if (cur.right != null)
{
q.Enqueue(cur.right);
}
}
}
/// <summary>
/// 查找最小节点
/// </summary>
public E Min()
{
if (IsEmpty)
{
throw new ArgumentException("二叉树为空!");
}
return Min(root).key;
}
//返回以node为根的二叉查找树的最小值所在的节点
private Node Min(Node node)
{
if (node.left==null)
{
return node;
}
else
{
return Min(node.left);
}
}
/// <summary>
/// 查找最大节点
/// </summary>
public E Max()
{
if (IsEmpty)
{
throw new ArgumentException("二叉树为空!");
}
return Max(root).key;
}
//返回以node为根的二叉查找树的最小值所在的节点
private Node Max(Node node)
{
if (node.right == null)
{
return node;
}
else
{
return Min(node.right);
}
}
/// <summary>
/// 删除最小节点
/// </summary>
public E RemoveMin()
{
E ret = Min();
root = RemoveMin(root);
return ret;
}
//删除以node为根的二叉查找树中的最小节点
//返回删除节点后新的二叉查找树的根
private Node RemoveMin(Node node)
{
if (node.left==null)
{
N--;
return node.right;
}
node.left = RemoveMin(node.left);
return node;
}
/// <summary>
/// 删除最大节点
/// </summary>
public E RemoveMax()
{
E ret = Max();
root = RemoveMax(root);
return ret;
}
//删除以node为根的二叉查找树中的最大节点
//返回删除节点后新的二叉查找树的根
private Node RemoveMax(Node node)
{
if (node.right == null)
{
N--;
return node.left;
}
node.right = RemoveMax(node.right);
return node;
}
/// <summary>
/// 删除任意节点
/// </summary>
/// <param name="e"></param>
public void Remove(E e)
{
root = Remove(root, e);
}
//删除以node为根的二叉查找树中值为e的节点
//返回删除节点后新的二叉查找树的根
private Node Remove(Node node, E e)
{
if (node==null)
{
return null;
}
if (e.CompareTo(node.key)<0)
{
node.left = Remove(node.left, e);
return node;
}
else if (e.CompareTo(node.key)>0)
{
node.right = Remove(node.right, e);
return node;
}
else
{
if (node.right==null)
{
N--;
return node.left;
}
if (node.left==null)
{
N--;
return node.right;
}
//要删除的节点左右都有孩子
//找到比待删除的节点大的最小节点,即待删除节点右子树的最小节点
//用这个节点顶替待删除节点的位置
Node s = Min(node.right);
s.right = RemoveMin(node.right);
s.left = node.left;
return s;
}
}
/// <summary>
/// 查看二叉查找树的最大高度
/// </summary>
public int MaxHeight()
{
return MaxHeight(root);
}
private int MaxHeight(Node node)
{
if (node==null)
{
return 0;
}
return Math.Max(MaxHeight(node.left), MaxHeight(node.right) + 1);
}
}
调用:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BinaryTree : MonoBehaviour
{
void Start()
{
BST1CeShi();
}
private void BST1CeShi()
{
//想要存到二叉树中的数据
int[] a = { 8, 4, 12, 2, 6, 10, 14 };
BST1<int> bst = new BST1<int>();
//添加到二叉树中
for (int i = 0; i < a.Length; i++)
{
bst.Add(a[i]);
}
//直接调用二叉树中的方法即可
//方法在代码中均有注释
}
}