public void Merge(BinomialHeap <TKey, TValue> other) { List <BinomialTree <TKey, TValue> > oldRoots = rootList.Concat(other.rootList).OrderBy(tree => tree.Order).ToList(); rootList = new List <BinomialTree <TKey, TValue> >(); BinomialTree <TKey, TValue> currentTree = null; foreach (BinomialTree <TKey, TValue> tree in oldRoots) { if (currentTree == null) { currentTree = tree; } else { if (currentTree.Order < tree.Order) { rootList.Add(currentTree); currentTree = tree; } else { currentTree = MergeTrees(currentTree, tree); } } } if (currentTree != null) { rootList.Add(currentTree); } }
public KeyValuePair <TKey, TValue> DeleteMinimum() { BinomialTree <TKey, TValue> minimumRootTree = MinimumRootTree(); rootList.Remove(minimumRootTree); Merge(new BinomialHeap <TKey, TValue>(minimumRootTree.SubTrees)); return(minimumRootTree.Root); }
private BinomialTree <TKey, TValue> MergeTrees(BinomialTree <TKey, TValue> tree1, BinomialTree <TKey, TValue> tree2) { if (tree1.RootKey.CompareTo(tree2.RootKey) <= 0) { tree1.AddSubTree(tree2); return(tree1); } else { tree2.AddSubTree(tree1); return(tree2); } }
public void AddSubTree(BinomialTree <TKey, TValue> subTree) { subTrees.Add(subTree); }