/** * * id1:207032764 * name1:Nitsan Honen * username1:nitsanhonen * id2:206539496 * name2:Nitzan Zacharia * username2:zacharia1 * */ /** * FibonacciHeap * * An implementation of Fibonacci heap over positive integers. * */ public class FibonacciHeap { public int numTrees; public HeapNode first; public HeapNode min; public int totalLinks; public int totalCuts; public int size; /** * * Constructor to initialize an empty heap. * */ public FibonacciHeap() { numTrees = 0; first = null; min = null; totalLinks = 0; totalCuts = 0; size = 0; } /** * * pre: key > 0 * * Insert (key,info) into the heap and return the newly generated HeapNode. * * written by: Nitzan Z. * */ public HeapNode insert(int key, String info) { //acts as an envelope function for InsertHelper HeapNode Node = new HeapNode(key, info); return insertHelper(Node, true); } /** * * pre: isNew == true iff Node was created by the insert() method, false otherwise. * * Inserts the HeapNode Node into the heap and returns the newly inserted HeapNode. * * written by: Nitzan Z. * */ private HeapNode insertHelper(HeapNode Node, boolean isNew){ int key = Node.key; //if the heap is empty, insert Node as the first & minimal root if(this.first==null) { this.first=Node; Node.next=Node; Node.prev = Node; this.min = Node; } //else, insert it as the first node of the root list, //update the necessary prev/next pointers else { Node.next = this.first; Node.prev = this.first.prev; this.first.prev.next = Node; this.first.prev = Node; this.first = Node; if(key log(n) HeapNode[] arr = new HeapNode[2+(int)(Math.log(size) / 0.48121)]; while(this.first!=null) { HeapNode node = this.first; if(this.first.next == this.first) //if we reached the last root in the heap { this.first=null; } else { this.first=node.next; //remove the root from the heap HeapNode last = node.prev; last.next=this.first; this.first.prev=last; } node.next = null; node.prev=null;//detach the node from pointing to the heap recMove(arr, node); //insert node to the array, link accordingly if needed } //update num of trees to zero before re-inserting the updated linked trees this.numTrees=0; this.min = null; //delete the pointer to the current min int count = 0; HeapNode minPointer = null; //re-insert the trees to the heap, updating the number of trees and the minimum accordingly for(int i=(arr.length-1);i>=0;i--) { if(arr[i]!=null) { count++; int key_root = arr[i].key; HeapNode root_node = insertHelper(arr[i], false); if (minPointer==null) { minPointer = root_node; } else if(key_root0) consolidating(); } /** * * given a pointer to a root, deletes it from the heap. * * written by: Nitzan Z. * */ private void deleteRoot(HeapNode root) { //if the root is not a singleton, add its children to the root list, update relevant pointers if(root.rank>0) { HeapNode child = root.child; HeapNode node = child; root.child = null; child.prev.next = root.next; root.next.prev = child.prev; child.prev = root.prev; root.prev.next = child; while(node != null) { node.parent = null; if(node.mark) { node.mark = false;} if(node.next == child) { break; } else node= node.next; } child.parent = null; if(this.first==root) this.first=child; this.numTrees += root.rank-1; this.totalCuts += root.rank; } //if the heap will not be empty after deleting root, remove it and update relevant pointers else if(this.numTrees>1) { root.next.prev = root.prev; root.prev.next= root.next; if(this.first==root) this.first=root.next; this.numTrees--; } //if root is the only node in the heap, update the pointers so the heap is empty else { this.first=null; this.numTrees = 0; } root.prev = null; root.next = null; if(size>0) {size --;} //update size if necessary if(this.size==0) {this.min=null;} //update min if necessary } /** * * pre: 0