参考:http://www.cnblogs.com/skywang12345/p/3638384.html
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Threading; using System.IO; using System.Collections; using System.Threading.Tasks; namespace ConsoleApplication2 { public class Program { public static void Main() { LeftListHeapTree<int> leftListTreeA = new LeftListHeapTree<int>(); leftListTreeA.Insert(10); leftListTreeA.Insert(40); leftListTreeA.Insert(24); leftListTreeA.Insert(30); leftListTreeA.Insert(36); leftListTreeA.Insert(20); leftListTreeA.Insert(12); leftListTreeA.Insert(16); LeftListHeapTree<int> leftListTreeB = new LeftListHeapTree<int>(); leftListTreeB.Insert(17); leftListTreeB.Insert(13); leftListTreeB.Insert(11); leftListTreeB.Insert(15); leftListTreeB.Insert(19); leftListTreeB.Insert(21); leftListTreeB.Insert(23); leftListTreeA.Merge(leftListTreeB); Console.Read(); } } public class LeftListHeapNode<T> where T : IComparable { public T Key { get; set; } public int Npl { get; set; } public LeftListHeapNode<T> LeftNode { get; set; } public LeftListHeapNode<T> RightNode { get; set; } public LeftListHeapNode(T _key, LeftListHeapNode<T> _leftNode, LeftListHeapNode<T> _rightNode) { Key = _key; LeftNode = _leftNode; RightNode = _rightNode; Npl = 0; } public override string ToString() { return Key + ""; } } public class LeftListHeapTree<T> where T : IComparable { public LeftListHeapNode<T> RootNode { get; set; } private LeftListHeapNode<T> Merge(LeftListHeapNode<T> leftTreeNode, LeftListHeapNode<T> rightTreeNode) { if (leftTreeNode == null) { return rightTreeNode; } if (rightTreeNode == null) { return leftTreeNode; } if (leftTreeNode.Key.CompareTo(rightTreeNode.Key) > 0) { LeftListHeapNode<T> temp = leftTreeNode; leftTreeNode = rightTreeNode; rightTreeNode = temp; } leftTreeNode.RightNode = Merge(leftTreeNode.RightNode,rightTreeNode); if (leftTreeNode.LeftNode == null || leftTreeNode.LeftNode.Npl < leftTreeNode.RightNode.Npl) { LeftListHeapNode<T> temp = leftTreeNode.LeftNode; leftTreeNode.LeftNode = leftTreeNode.RightNode; leftTreeNode.RightNode = temp; } if (leftTreeNode.RightNode == null || leftTreeNode.LeftNode == null) { leftTreeNode.Npl = 0; } else { leftTreeNode.Npl = (leftTreeNode.LeftNode.Npl > leftTreeNode.RightNode.Npl) ? (leftTreeNode.RightNode.Npl + 1) : (leftTreeNode.LeftNode.Npl + 1); } return leftTreeNode; } public void Merge(LeftListHeapTree<T> other) { this.RootNode = Merge(this.RootNode,other.RootNode); } public void Insert(T key) { LeftListHeapNode<T> node = new LeftListHeapNode<T>(key,null,null); this.RootNode = Merge(this.RootNode,node); } } }