在下小白一个 如有错误请指正
上代码
using System;
//数据结构
namespace DataStructure
{
/// <summary>
/// 左倾堆的节点
/// </summary>
/// <typeparam name="T"></typeparam>
public class LeftistNode<T> where T : IComparable
{
public LeftistNode<T> LeftNode { get; set; }
public LeftistNode<T> RightNode { get; set; }
public T Key { get; set; }
public int NPL { get; set; }
public LeftistNode(LeftistNode<T> leftNode, LeftistNode<T> rightNode, T key)
{
LeftNode = leftNode;
RightNode = rightNode;
NPL = 0;
Key = key;
}
}
/// <summary>
/// 左倾堆 适用于: 最值问题、模拟问题、贪心问题
/// </summary>
/// 包含父节点 左子节点 右子节点 键值 和 零距离
/// 零距离:从一个节点到一个最近的不满节点的长度 不满节点是指该节点的左右子节点 最少有一个位空 不满节点的零距离为0 空节点的0距离为-1
/// 左倾堆的基本性质
/// 1.节点的键值小于或者等于他的左右节点的键值
/// 2.左节点的零距离大于右节点的零距离
/// 3.节点的零距离等于他有孩子的零距离加1
public class LeftistHeap<T> where T : IComparable
{
public LeftistNode<T> mRootNode;
public LeftistHeap()
{
mRootNode = null;
}
/// <summary>
/// 外部 插入
/// </summary>
/// <param name="key"></param>
public void Inset(T key)
{
LeftistNode<T> node = new LeftistNode<T>(null, null, key);
Inset(node);
}
/// <summary>
/// 内部 插入
/// </summary>
/// <param name="insetNode"></param>
private void Inset(LeftistNode<T> insetNode)
{
if (insetNode != null)
this.mRootNode = MergeLeftistHeap(this.mRootNode, insetNode);
}
/// <summary>
/// 删除外部
/// </summary>
/// <param name="key"></param>
public void Remove(T key)
{
LeftistNode<T> node = new LeftistNode<T>(null, null, key);
Remove(node);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="removeNode"></param>
private void Remove(LeftistNode<T> removeNode)
{
}
/// <summary>
/// 合并
/// </summary>
/// <param name="heap"></param>
/// <param name="otherHeap"></param>
/// <returns></returns>
///(01) 如果一个空左倾堆与一个非空左倾堆合并,返回非空左倾堆。
///(02) 如果两个左倾堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将"较小堆的根节点的右孩子"和"较大堆"进行合并。
///(03) 如果新堆的右孩子的NPL > 左孩子的NPL,则交换左右孩子。
///(04) 设置新堆的根节点的NPL = 右子堆NPL + 1
private LeftistNode<T> MergeLeftistHeap(LeftistNode<T> heap, LeftistNode<T> otherHeap)
{
if (heap == null)
return otherHeap;
if (otherHeap == null)
return heap;
LeftistNode<T> newRootNode = heap;
LeftistNode<T> bigRootNode = otherHeap;
int cmp = heap.Key.CompareTo(otherHeap.Key);
if (cmp > 0)
{
newRootNode = otherHeap;
bigRootNode = heap;
}
else
{
newRootNode = heap;
bigRootNode = otherHeap;
}
newRootNode.RightNode = MergeLeftistHeap(newRootNode.RightNode, bigRootNode);
if (newRootNode.LeftNode == null || newRootNode.LeftNode.NPL < newRootNode.RightNode.NPL)
{
LeftistNode<T> tempNode = newRootNode.LeftNode;
newRootNode.LeftNode = newRootNode.RightNode;
newRootNode.RightNode = tempNode;
}
if (newRootNode.LeftNode == null || newRootNode.RightNode == null)
newRootNode.NPL = 0;
else
newRootNode.NPL = newRootNode.LeftNode.NPL > newRootNode.RightNode.NPL
? newRootNode.LeftNode.NPL + 1
: newRootNode.RightNode.NPL + 1;
return newRootNode;
}
/// <summary>
/// 合并
/// </summary>
/// <param name="otherHeap"></param>
public void MergeLeftistHeap(LeftistHeap<T> otherHeap)
{
this.mRootNode = MergeLeftistHeap(this.mRootNode, otherHeap.mRootNode);
}
/// <summary>
/// 交换值
/// </summary>
/// <param name="node"></param>
/// <param name="otherNode"></param>
public void ChangeKey(LeftistNode<T> node, LeftistNode<T> otherNode)
{
T temp = node.Key;
node.Key = otherNode.Key;
otherNode.Key = temp;
}
/// <summary>
/// 前序遍历
/// </summary>
/// <param name="heap"></param>
void PreorderLeftist(LeftistNode<T> root)
{
if (root == null) return;
Console.WriteLine(root.Key);
PreorderLeftist(root.LeftNode);
PreorderLeftist(root.RightNode);
}
/// <summary>
/// 前序遍历
/// </summary>
public void PreorderLeftist()
{
if (mRootNode != null)
PreorderLeftist(mRootNode);
}
/// <summary>
/// 中序遍历
/// </summary>
/// <param name="heap"></param>
void InorderLeftist(LeftistNode<T> heap)
{
if (heap == null) return;
PreorderLeftist(heap.LeftNode);
Console.WriteLine(heap.Key);
PreorderLeftist(heap.RightNode);
}
/// <summary>
/// 中序遍历
/// </summary>
public void InorderLeftist()
{
if (mRootNode != null)
InorderLeftist(mRootNode);
}
/// <summary>
/// 后序遍历
/// </summary>
/// <param name="heap"></param>
void PostorderLeftist(LeftistNode<T> heap)
{
if (heap == null) return;
PreorderLeftist(heap.LeftNode);
PreorderLeftist(heap.RightNode);
Console.WriteLine(heap.Key);
}
/// <summary>
/// 后序遍历
/// </summary>
public void PostorderLeftist()
{
if (mRootNode != null)
PostorderLeftist(mRootNode);
}
/// <summary>
/// 寻找最小值
/// 有返回1 没有返回0
/// </summary>
/// <param name="heap"></param>
/// <returns></returns>
public int LeftistMinimum(LeftistNode<T> heap, ref T minKey)
{
if (heap == null)
return 0;
else
minKey = heap.Key;
return 1;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="rootNode"></param>
public void Destroy(LeftistNode<T> rootNode)
{
if (rootNode == null) return;
if (rootNode.LeftNode != null)
Destroy(rootNode.LeftNode);
if (rootNode.RightNode != null)
Destroy(rootNode.RightNode);
rootNode = null;
}
private void Print(LeftistNode<T> node, int direction)
{
if (direction == -1)
Console.WriteLine("NPL :{0} Key: {1} Direction :{2}", node.NPL.ToString(), node.Key.ToString(),
"Root");
else if (direction == 0)
Console.WriteLine("NPL :{0} Key: {1} Direction :{2}", node.NPL.ToString(), node.Key.ToString(),
"Left");
else
Console.WriteLine("NPL :{0} Key: {1} Direction :{2}", node.NPL.ToString(), node.Key.ToString(),
"Right");
}
private void Print(LeftistNode<T> root)
{
if (root.LeftNode != null)
{
Print(root.LeftNode, 0);
Print(root.LeftNode);
}
if (root.RightNode != null)
{
Print(root.RightNode, 1);
Print(root.RightNode);
}
}
public void Print()
{
Print(mRootNode, -1);
Print(mRootNode);
}
}
}