2 using System.Collections.Generic;
7 public class PriorityQueue<T>
8 where T : IComparable<T>
14 public readonly LinkedList<Node> Childs = new LinkedList<Node>();
22 private Node rootNode;
24 public int Count { get; private set; }
26 public T First { get { return this.rootNode.Value; } }
34 public void Push(T item)
36 this.rootNode = this.Merge(this.rootNode, new Node(item));
47 public void RemoveFirst()
49 this.rootNode = this.Unify(this.rootNode.Childs);
53 private Node Merge(Node l, Node r)
55 if (l == null) return r;
56 if (r == null) return l;
58 if (l.Value.CompareTo(r.Value) > 0)
70 private Node Unify(LinkedList<Node> nodes)
72 if (nodes == null || nodes.Count == 0) return null;
74 Node x = nodes.First.Value;
76 if (nodes.Count == 0) return x;
78 Node y = nodes.First.Value;
81 return this.Merge(this.Merge(x, y), this.Unify(nodes));