当前位置:网站首页>C implementation of left leaning reactor
C implementation of left leaning reactor
2022-07-21 23:01:00 【A shuttle keyboard, Ren lifetime】
I'm Xiaobai Please correct me if there is any mistake
Code up
using System;
// data structure
namespace DataStructure
{
/// <summary>
/// Node of left leaning pile
/// </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>
/// Left leaning reactor Apply to : The most valuable question 、 Simulation problem 、 The problem of greed
/// </summary>
/// Include parent node The left child node The right child node Key value and Zero distance
/// Zero distance : The length from a node to the nearest dissatisfied node Dissatisfied nodes refer to the left and right child nodes of the node At least one bit is empty The zero distance of the dissatisfied node is 0 Empty node 0 A distance of -1
/// Basic properties of left leaning pile
/// 1. The key value of a node is less than or equal to the key value of its left and right nodes
/// 2. The zero distance of the left node is greater than that of the right node
/// 3. The zero distance of the node is equal to the zero distance with children plus 1
public class LeftistHeap<T> where T : IComparable
{
public LeftistNode<T> mRootNode;
public LeftistHeap()
{
mRootNode = null;
}
/// <summary>
/// external Insert
/// </summary>
/// <param name="key"></param>
public void Inset(T key)
{
LeftistNode<T> node = new LeftistNode<T>(null, null, key);
Inset(node);
}
/// <summary>
/// Inside Insert
/// </summary>
/// <param name="insetNode"></param>
private void Inset(LeftistNode<T> insetNode)
{
if (insetNode != null)
this.mRootNode = MergeLeftistHeap(this.mRootNode, insetNode);
}
/// <summary>
/// Delete external
/// </summary>
/// <param name="key"></param>
public void Remove(T key)
{
LeftistNode<T> node = new LeftistNode<T>(null, null, key);
Remove(node);
}
/// <summary>
/// Delete
/// </summary>
/// <param name="removeNode"></param>
private void Remove(LeftistNode<T> removeNode)
{
}
/// <summary>
/// Merge
/// </summary>
/// <param name="heap"></param>
/// <param name="otherHeap"></param>
/// <returns></returns>
///(01) If an empty left leaning heap is merged with a non empty left leaning heap , Returns a non empty left leaning heap .
///(02) If both left leaning piles are not empty , Then compare the two root nodes , Take the root node of the smaller heap as the new root node . take " The right child of the root node of the smaller heap " and " A lot " A merger .
///(03) If the right child of the new pile NPL > Left child's NPL, Then exchange left and right children .
///(04) Set the root node of the new heap NPL = Right side pile 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>
/// Merge
/// </summary>
/// <param name="otherHeap"></param>
public void MergeLeftistHeap(LeftistHeap<T> otherHeap)
{
this.mRootNode = MergeLeftistHeap(this.mRootNode, otherHeap.mRootNode);
}
/// <summary>
/// Exchange value
/// </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>
/// The former sequence traversal
/// </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>
/// The former sequence traversal
/// </summary>
public void PreorderLeftist()
{
if (mRootNode != null)
PreorderLeftist(mRootNode);
}
/// <summary>
/// In the sequence traversal
/// </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>
/// In the sequence traversal
/// </summary>
public void InorderLeftist()
{
if (mRootNode != null)
InorderLeftist(mRootNode);
}
/// <summary>
/// After the sequence traversal
/// </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>
/// After the sequence traversal
/// </summary>
public void PostorderLeftist()
{
if (mRootNode != null)
PostorderLeftist(mRootNode);
}
/// <summary>
/// Find the minimum
/// There is a return 1 No return 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>
/// Delete
/// </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);
}
}
}
边栏推荐
猜你喜欢
IM开发技术分享:浅谈IM系统中离线消息、历史消息的最佳实践
Realize 2D map 3D character 45 degree angle RPG game effect notes in cocos creator 3.2 (camera setting scheme)
windows mysql 5.7+ 修改表名大小写敏感属性
MySql the first
VolP终端与网关
Directx11--窗口初始化(win32)
The mysql57 service on the local computer stops after it is started
什么是IMS(IP多媒体子系统)
[oops framework] supporting hot update plug-ins
print的运用以及两数的交换
随机推荐
windows mysql 5.7+ 修改表名大小写敏感属性
关于对齐次裁剪空间及HLSL语义的理解
HCIP 第四天
OSPF --- 开放式最短路径优先协议
呼叫中心客服系统如何搭建?
基于Unity的Hololens2与服务器进行Json、模型以及视频流传输实战(个人Hololens2进阶开发总结)
OSPF路由回馈及策略
社交软件红包技术解密(十二):解密抖音春节红包背后的技术设计与实践
GRE,MGRE
Sharing of live video technology: understand the push-pull streaming architecture and transmission protocols of mainstream live video systems
TMUX learning records
CocosCreator 3.x 环境搭建
Window common messages
Realize 2D map 3D character 45 degree angle RPG game effect notes in cocos creator 3.2 (camera setting scheme)
OSPF的路由控制
背面剔除|无剔除模式与透明物体的绘制
Kickback record: dynamic programming 5 subsequence problem (3) palindrome - 647 palindrome subsequence, 516 longest palindrome subsequence
HCIP 第八天
OSPF及MGRE综合实验
What is IMS (IP multimedia subsystem)