private static void SwapChild(HeapNode <T> H) { var temp = H.LeftChild; H.LeftChild = H.RightChild; H.RightChild = temp; }
public static HeapNode <T> Marge(HeapNode <T> H1, HeapNode <T> H2) { if (H1 == null) { return(H2); } if (H2 == null) { return(H1); } if (H1.Element.CompareTo(H2.Element) > 0) { SwapNode(ref H1, ref H2); } if (H1.LeftChild == null) { H1.LeftChild = H2; } else { H1.RightChild = Marge(H1.RightChild, H2); if (H1.LeftChild.NPL < H1.RightChild.NPL) { SwapChild(H1); } H1.NPL = H1.RightChild.NPL + 1; } return(H1); }
private static void SwapNode(ref HeapNode <T> node1, ref HeapNode <T> node2) { var temp = node1; node1 = node2; node2 = temp; }
public T RemoveMin() { if (IsEmpty) { throw new Exception("heap is empty."); } var min = Root.Element; Root = HeapNode <T> .Marge(Root.LeftChild, Root.RightChild); return(min); }
public void Insert(T element) { var node = new HeapNode <T>(element); Root = HeapNode <T> .Marge(Root, node); }
public void MargeWith(LeftHeap <T> other) { Root = HeapNode <T> .Marge(Root, other.Root); }