public static Node Create(int key) { Node node = new Node(); node.key = key; return node; }
// Linking nodes public void Link(Node child, Node parent) { child.parent = parent; child.sibling = parent.child; parent.child = child; parent.degree = parent.degree + 1; }
// Insert node public Node Insert(Node h, Node insertNode) { Node node = Init(); node = insertNode; h = Union(h, node); return h; }
// Union nodes public Node Union(Node h1, Node h2) { Node node = Init(); node = Merge(h1, h2); if (node == null) { return node; } Node h = node; Node prevH = null; Node nextH = h.sibling; while (nextH != null) { if ((h.degree != nextH.degree) || ((nextH.sibling != null) && (nextH.sibling).degree == h.degree)) { prevH = h; h = nextH; } else { if (h.key <= nextH.key) { h.sibling = nextH.sibling; Link(nextH, h); } else { if (prevH == null) { node = nextH; } else { prevH.sibling = nextH; } Link(h, nextH); h = nextH; } } nextH = h.sibling; } return node; }
public BinomialHeap() { heap = Init(); heapR = Init(); }
// Delete Nodes public void Delete(Node h, int key) { if (h == null) { Console.WriteLine("Heap is empty"); return; } DecreaseKey(h, key, int.MinValue); ExtractMinimum(h); }
// Decrease key of a node public void DecreaseKey(Node h, int curKey, int newKey) { if (newKey > curKey) { Console.WriteLine("The new key cannot exceed the current"); return; } Node foundNode = Search(h, curKey); if (foundNode == null) { Console.WriteLine("Node not found"); return; } foundNode.key = newKey; Node a = foundNode; Node b = foundNode.parent; while (b != null && a.key < b.key) { int temp = a.key; a.key = b.key; b.key = temp; a = b; b = b.parent; } }
// Search private Node Search(Node h, int key) { Node result = Init(); if (h.key == key) { result = h; return result; } if (h.child != null && result == null) { result = Search(h.child, key); } if (h.sibling != null && result == null) { result = Search(h.sibling, key); } return result; }
// Reverse list private void Reverse(Node h) { if (h.sibling != null) { Reverse(h.sibling); (h.sibling).sibling = h; } else { heapR = h; } }
// Extract minimum public Node ExtractMinimum(Node h) { if (h == null) { Console.WriteLine("Heap is empty"); return h; } heapR = Init(); Node a = Init(); Node node = h; Node temp = node; int min = node.key; while (temp.sibling != null) { if ((temp.sibling).key < min) { min = (temp.sibling).key; a = temp; node = temp.sibling; } temp = temp.sibling; } if (a == null && node.sibling == null) { h = null; } else if (a == null) { h = node.sibling; } else if (a.sibling == null) { a = null; } else { a.sibling = node.sibling; } if (node.child != null) { Reverse(node.child); (node.child).sibling = null; } heap = Union(h, heapR); return node; }
// Merge nodes public Node Merge(Node h1, Node h2) { Node node = Init(); Node tmp = Init(); if (h1 != null) { if (h2 != null) { if (h1.degree <= h2.degree) { node = h1; } else if (h1.degree > h2.degree) { node = h2; } } else { node = h1; } } else { node = h2; } while (h1 != null && h2 != null) { if (h1.degree < h2.degree) { h1 = h1.sibling; } else if (h1.degree == h2.degree) { tmp = h1.sibling; h1.sibling = h2; h1 = tmp; } else { tmp = h2.sibling; h2.sibling = h1; h2 = tmp; } } return node; }