Question

//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * *...

//implement the binomial heap please
import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 * Binomial Heap Implementation
 *
 * @author First Last
 * @since ${date}
 */
public class BinomialHeap<T extends Comparable<? super T>>  implements binomialHeapInterface<T> {


    private static final int DEFAULT = 5; // default size for the binomial heap
    public Node<T>[] forest;
    private int n;
    private boolean isMaxHeap;

    /**
     * Node Class for nodes in Binomial Heap
     */
    protected class Node<T> {
        private T value;
        private int degree;
        private LinkedList<Node<T>> children;

        /**
         * Node Constructor
         * @param x -> key value of the node
         */
        public Node(T x) {
            /* TODO */
        }

        /**
         * method to add children to node
         * @param node the new child of this node
         */
        public void addChild(Node node) {
            /* TODO */
        }

        /**
         * Getter method for degree
         *
         * @return degree of the current node
         */
        public int getDegree() {
            /* TODO */
            return 0;
        }

        /**
         * Getter method for the value of the node
         * @return value of the node
         */
        public T getValue() {
            /* TODO */
            return null;
        }

        /**
         * getter method for the children
         * @return the children of a node
         */
        public LinkedList<Node<T>> getChildren() {
            /* TODO */
            return null;
        }
    }

    /**
     * Initializes a binomial max heap with capacity = 5
     */
    public BinomialHeap() {
        /* TODO */
    }

    /**
     * Initializes a binomial max heap with a given initial capacity.
     *
     * @param heapSize The initial capacity of the heap.
     */
    public BinomialHeap(int heapSize) {
        /* TODO */
    }

    /**
     * Initializes a binomial heap (with a given value for d), with a given
     * initial capacity.
     *
     * @param heapSize The initial capacity of the heap.
     * @param isMaxHeap indicates whether the heap should be max or min
     */
    public BinomialHeap(int heapSize, boolean isMaxHeap) {
        /* TODO */
    }

    /**
     * size of the heap
     * @return number of elements in heap
     */
    public int size() {
        /* TODO */
        return 0;
    }

    /**
     * clears the Binomial Heap
     */
    public void clear() {
        /* TODO */
    }

    /**
     * Add element to the binomial Heap
     * @param o The element to add.
     * @throws NullPointerException if o is null
     */
    public void add(T o) {
        /* TODO */
    }

    /**
     * Helper Method for inserting elements into the Binomial Heap
     * @param node node to insert into forest
     */
    private void insertHelper(Node<T> node) {
        /* TODO */
        // hint 1: the 'node' of degree k is going to be placed at forest[k]
        // hint 2: consider how this helper can be used in the add and remove method
        // attempt to place 'node' in forest degree of the Node (for the rest, assume degree of 'node' is k)
            // if forest[k] is empty, place 'node' at forest[k]
            // if forest[k] contains a Node:
                // compare 'node' and forest[k] -> one of these becomes the child of the other 
                // (consider using the addChild method of Node)
                // 'node' <-- the non-child of the either 'node' and forest[k]
                // return back to the first step of trying to place 'node' into forest
                    // note: because I have added a child, the degree of 'node' has increased by 1
        // if my forest wasn't big enough for the 'node' (consider how I can tell if this is true):
            // resize the forest & place the 'node' in the appropriate spot. 
    }

    /**
     * compareTo wrapper to take care of Max Heap & Min Heap Implementation
     *
     * @param node1 first node
     * @param node2 second node
     * @return compareTo values between node1 and node2 relative to the Heap type
     */
    private Boolean compareToWrapper(Node<T> node1, Node<T> node2) {
        /* TODO */
        return false;
    }

    /**
     * remove method for binomial heap
     * @return removes the min/max from the heap
     * @throws NoSuchElementException if heap is empty
     */
    public T remove() throws NoSuchElementException {
        /* TODO */
        return null;
    }

    /**
     * Find the index of extrema
     * @return index of extrema
     */
    private int findExtrema() {
        /* TODO */
        return 0;
    }

    /**
     * resizes the forest
     */
    private void resize() {
        /* TODO */
    }

    /**
     * looks at top element
     * @return returns min/max relative to heap type
     * @throws NoSuchElementException if heap is empty
     */
    public T element() throws NoSuchElementException {
        /* TODO */
        return null;
    }
import java.util.NoSuchElementException;

public interface binomialHeapInterface<T extends java.lang.Comparable<? super T>> {

    /**
     * Returns the number of elements stored in the heap.
     *
     * @return The number of elements stored in the heap.
     */
    int size();

    /**
     * Removes and returns the element at the root. If the
     * heap is empty, then this method throws a NoSuchElementException.
     *
     * @return The element at the root stored in the heap.
     * @throws java.util.NoSuchElementException if the heap is empty
     */

    T remove() throws NoSuchElementException;

    /**
     * Adds the specified element to the heap; o cannot be null.
     * Resizes the storage if full.
     *
     * @param o The element to add.
     * @throws NullPointerException if o is null.
     */
    void add( T o ) throws NullPointerException;

    /**
     * Clears all the items in the heap
     * Heap will be empty after this call returns
     */
    public void clear();

    /**
     * Retrieves, but does not remove, the element at the root.
     * @return item at the root of the heap
     * @throws NoSuchElementException - if this heap is empty
     */
    public T element() throws NoSuchElementException;

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
please give thumbs up if the solution is useful to you.
import java.util.Scanner;

class BinomialHeapNode {
        int key, degree;
        BinomialHeapNode parent;
        BinomialHeapNode sibling;
        BinomialHeapNode child;

        public BinomialHeapNode(int k) {
                key = k;
                degree = 0;
                parent = null;
                sibling = null;
                child = null;
        }

        public BinomialHeapNode reverse(BinomialHeapNode sibl) {
                BinomialHeapNode ret;
                if (sibling != null)
                        ret = sibling.reverse(this);
                else
                        ret = this;
                sibling = sibl;
                return ret;
        }

        public BinomialHeapNode findMinNode() {
                BinomialHeapNode x = this, y = this;
                int min = x.key;

                while (x != null) {
                        if (x.key < min) {
                                y = x;
                                min = x.key;
                        }
                        x = x.sibling;
                }

                return y;
        }

        public BinomialHeapNode findANodeWithKey(int value) {
                BinomialHeapNode temp = this, node = null;

                while (temp != null) {
                        if (temp.key == value) {
                                node = temp;
                                break;
                        }
                        if (temp.child == null)
                                temp = temp.sibling;
                        else {
                                node = temp.child.findANodeWithKey(value);
                                if (node == null)
                                        temp = temp.sibling;
                                else
                                        break;
                        }
                }

                return node;
        }
public int getSize() {
                return (1 + ((child == null) ? 0 : child.getSize()) + ((sibling == null) ? 0 : sibling.getSize()));
        }
}

class BinomialHeap {
        private BinomialHeapNode Nodes;
        private int size;

        public BinomialHeap() {
                Nodes = null;
                size = 0;
        }

        public boolean isEmpty() {
                return Nodes == null;
        }

        public int getSize() {
                return size;
        }

        public void makeEmpty() {
                Nodes = null;
                size = 0;
        }

        public void insert(int value) {
                if (value > 0) {
                        BinomialHeapNode temp = new BinomialHeapNode(value);
                        if (Nodes == null) {
                                Nodes = temp;
                                size = 1;
                        } else {
                                unionNodes(temp);
                                size++;
                        }
                }
        }

        private void merge(BinomialHeapNode binHeap) {
                BinomialHeapNode temp1 = Nodes, temp2 = binHeap;

                while ((temp1 != null) && (temp2 != null)) {
                        if (temp1.degree == temp2.degree) {
                                BinomialHeapNode tmp = temp2;
                                temp2 = temp2.sibling;
                                tmp.sibling = temp1.sibling;
                                temp1.sibling = tmp;
                                temp1 = tmp.sibling;
                        } else {
                                if (temp1.degree < temp2.degree) {
                                        if ((temp1.sibling == null) || (temp1.sibling.degree > temp2.degree)) {
                                                BinomialHeapNode tmp = temp2;
                                                temp2 = temp2.sibling;
                                                tmp.sibling = temp1.sibling;
                                                temp1.sibling = tmp;
                                                temp1 = tmp.sibling;
                                        } else {
                                                temp1 = temp1.sibling;
                                        }
                                } else {
                                        BinomialHeapNode tmp = temp1;
                                        temp1 = temp2;
                                        temp2 = temp2.sibling;
                                        temp1.sibling = tmp;
                                        if (tmp == Nodes) {
                                                Nodes = temp1;
                                        } else {

                                        }
                                }
                        }
                }
                if (temp1 == null) {
                        temp1 = Nodes;
                        while (temp1.sibling != null) {
                                temp1 = temp1.sibling;
                        }
                        temp1.sibling = temp2;
                } else {

                }
        }
private void unionNodes(BinomialHeapNode binHeap) {
                merge(binHeap);

                BinomialHeapNode prevTemp = null, temp = Nodes, nextTemp = Nodes.sibling;

                while (nextTemp != null) {
                        if ((temp.degree != nextTemp.degree)
                                        || ((nextTemp.sibling != null) && (nextTemp.sibling.degree == temp.degree))) {
                                prevTemp = temp;
                                temp = nextTemp;
                        } else {
                                if (temp.key <= nextTemp.key) {
                                        temp.sibling = nextTemp.sibling;
                                        nextTemp.parent = temp;
                                        nextTemp.sibling = temp.child;
                                        temp.child = nextTemp;
                                        temp.degree++;
                                } else {
                                        if (prevTemp == null) {
                                                Nodes = nextTemp;
                                        } else {
                                                prevTemp.sibling = nextTemp;
                                        }
                                        temp.parent = nextTemp;
                                        temp.sibling = nextTemp.child;
                                        nextTemp.child = temp;
                                        nextTemp.degree++;
                                        temp = nextTemp;
                                }
                        }
                        nextTemp = temp.sibling;
                }
        }

        public int findMinimum() {
                return Nodes.findMinNode().key;
        }

        public void delete(int value) {
                if ((Nodes != null) && (Nodes.findANodeWithKey(value) != null)) {
                        decreaseKeyValue(value, findMinimum() - 1);
                        extractMin();
                }
        }

        public void decreaseKeyValue(int old_value, int new_value) {
                BinomialHeapNode temp = Nodes.findANodeWithKey(old_value);
                if (temp == null)
                        return;
                temp.key = new_value;
                BinomialHeapNode tempParent = temp.parent;

                while ((tempParent != null) && (temp.key < tempParent.key)) {
                        int z = temp.key;
                        temp.key = tempParent.key;
                        tempParent.key = z;

                        temp = tempParent;
                        tempParent = tempParent.parent;
                }
        }

        public int extractMin() {
                if (Nodes == null)
                        return -1;

                BinomialHeapNode temp = Nodes, prevTemp = null;
                BinomialHeapNode minNode = Nodes.findMinNode();

                while (temp.key != minNode.key) {
                        prevTemp = temp;
                        temp = temp.sibling;
                }

                if (prevTemp == null) {
                        Nodes = temp.sibling;
                } else {
                        prevTemp.sibling = temp.sibling;
                }

                temp = temp.child;
                BinomialHeapNode fakeNode = temp;

                while (temp != null) {
                        temp.parent = null;
                        temp = temp.sibling;
                }

                if ((Nodes == null) && (fakeNode == null)) {
                        size = 0;
                } else {
                        if ((Nodes == null) && (fakeNode != null)) {
                                Nodes = fakeNode.reverse(null);
                                size = Nodes.getSize();
                        } else {
                                if ((Nodes != null) && (fakeNode == null)) {
                                        size = Nodes.getSize();
                                } else {
                                        unionNodes(fakeNode.reverse(null));
                                        size = Nodes.getSize();
                                }
                        }
                }

                return minNode.key;
        }

        public void displayHeap() {
                System.out.print("\nHeap : ");
                displayHeap(Nodes);
                System.out.println("\n");
        }

        private void displayHeap(BinomialHeapNode r) {
                if (r != null) {
                        displayHeap(r.child);
                        System.out.print(r.key + " ");
                        displayHeap(r.sibling);
                }
        }
}
public class JavaBinomialHeapExample {

        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                System.out.println("Binomial Heap Test\n\n");

                BinomialHeap bh = new BinomialHeap();

                char ch;

                do {
                        System.out.println("\nBinomialHeap Operations\n");
                        System.out.println("1. insert ");
                        System.out.println("2. delete ");
                        System.out.println("3. size");
                        System.out.println("4. check empty");
                        System.out.println("5. clear");

                        int choice = scan.nextInt();
                        switch (choice) {
                        case 1:
                                System.out.println("Enter integer element to insert");
                                bh.insert(scan.nextInt());
                                break;
                        case 2:
                                System.out.println("Enter element to delete ");
                                bh.delete(scan.nextInt());
                                break;
                        case 3:
                                System.out.println("Size = " + bh.getSize());
                                break;
                        case 4:
                                System.out.println("Empty status = " + bh.isEmpty());
                                break;
                        case 5:
                                bh.makeEmpty();
                                System.out.println("Heap Cleared\n");
                                break;
                        default:
                                System.out.println("Wrong Entry \n ");
                                break;
                        }

                        bh.displayHeap();

                        System.out.println("\nDo you want to continue (Type y or n) \n");
                        ch = scan.next().charAt(0);
                } while (ch == 'Y' || ch == 'y');
        }
}
Add a comment
Know the answer?
Add Answer to:
//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Since we do not want to have to rewrite this code when the element type changes,...

    Since we do not want to have to rewrite this code when the element type changes, this classes uses a Comparator to assist in ordering the elements. You will need to complete the siftUpComparator() and siftDownComparator() methods in the LinkedHeap Class. siftUp §Added element may violate heap-order property §siftUp() restores order starting at added node §Processing will continue working up the tree until: úFinds properly ordered node & parent úReaches the root (reaches node without parent) siftDown §Restores heap’s order...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT