Instead of using Union to perform an Insert into a binomial heap H, we can write code that directly does the insert. Note that we keep the list of roots of H sorted by degree.
Insert(H, item)
let x be a pointer to a new node x with Nil in its parent, child and sibling pointers
x.key = item
x.degree = 0
loop
let y be a pointer to the rst root in H's list of roots
exit when y = Nil or x.degree 6 =/ y.degree
remove the node that y points to from H's list of roots
if x.key > y.key then swap the pointers x and y
make node y the first child of node x
end loop
add node x to the beginning of H's list of roots
end Insert
Use the potential method to show that the total time required to do a sequence of m Inserts on an initially empty binomial heap is O(m).
Answer: Please find the below provided code to your requirement.
/** ************************* BinomialHeap.cpp ************************************* **/
// Binomial Heap
#include<bits/stdc++.h>
using namespace std;
// A Binomial Tree node.
struct Node
{
int data, degree;
Node *child, *sibling, *parent;
};
Node* newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->degree = 0;
temp->child = temp->parent = temp->sibling =
NULL;
return temp;
}
// This function merge two Binomial Trees.
Node* mergeBinomialTrees(Node *b1, Node *b2)
{
// Make sure b1 is smaller
if (b1->data > b2->data)
swap(b1, b2);
// We basically make larger valued tree
// a child of smaller valued tree
b2->parent = b1;
b2->sibling = b1->child;
b1->child = b2;
b1->degree++;
return b1;
}
// This function perform union operation on two
// binomial heap i.e. l1 & l2
list<Node*> BionomialHeap(list<Node*> l1,
list<Node*> l2)
{
// _new to another binomial heap which contain
// new heap after merging l1 & l2
list<Node*> _new;
list<Node*>::iterator it = l1.begin();
list<Node*>::iterator ot = l2.begin();
while (it!=l1.end() && ot!=l2.end())
{
// if D(l1) <= D(l2)
if((*it)->degree <=
(*ot)->degree)
{
_new.push_back(*it);
it++;
}
// if D(l1) > D(l2)
else
{
_new.push_back(*ot);
ot++;
}
}
// if there remains some elements in l1
// binomial heap
while (it != l1.end())
{
_new.push_back(*it);
it++;
}
// if there remains some elements in l2
// binomial heap
while (ot!=l2.end())
{
_new.push_back(*ot);
ot++;
}
return _new;
}
// adjust function rearranges the heap so that
// heap is in increasing order of degree and
// no two binomial trees have same degree in this heap
list<Node*> adjust(list<Node*> _heap)
{
if (_heap.size() <= 1)
return _heap;
list<Node*> new_heap;
list<Node*>::iterator it1,it2,it3;
it1 = it2 = it3 = _heap.begin();
if (_heap.size() == 2)
{
it2 = it1;
it2++;
it3 = _heap.end();
}
else
{
it2++;
it3=it2;
it3++;
}
while (it1 != _heap.end())
{
// if only one element remains to
be processed
if (it2 == _heap.end())
it1++;
// If D(it1) < D(it2) i.e.
merging of Binomial
// Tree pointed by it1 & it2 is
not possible
// then move next in heap
else if ((*it1)->degree <
(*it2)->degree)
{
it1++;
it2++;
if(it3!=_heap.end())
it3++;
}
// if D(it1),D(it2) & D(it3)
are same i.e.
// degree of three consecutive
Binomial Tree are same
// in heap
else if (it3!=_heap.end()
&&
(*it1)->degree == (*it2)->degree
&&
(*it1)->degree == (*it3)->degree)
{
it1++;
it2++;
it3++;
}
// if degree of two Binomial
Tree are same in heap
else if ((*it1)->degree ==
(*it2)->degree)
{
Node
*temp;
*it1 =
mergeBinomialTrees(*it1,*it2);
it2 =
_heap.erase(it2);
if(it3 !=
_heap.end())
it3++;
}
}
return _heap;
}
// inserting a Binomial Tree into binomial heap
list<Node*> insertATreeInHeap(list<Node*> _heap,
Node *tree)
{
// creating a new heap i.e temp
list<Node*> temp;
// inserting Binomial Tree into heap
temp.push_back(tree);
// perform union operation to finally insert
// Binomial Tree in original heap
temp = BionomialHeap(_heap,temp);
return adjust(temp);
}
// inserting a key into the binomial heap
list<Node*> insert(list<Node*> _head, int key)
{
Node *temp = newNode(key);
return insertATreeInHeap(_head,temp);
}
// return pointer of minimum value Node
// present in the binomial heap
Node* getMin(list<Node*> _heap)
{
list<Node*>::iterator it = _heap.begin();
Node *temp = *it;
while (it != _heap.end())
{
if ((*it)->data <
temp->data)
temp =
*it;
it++;
}
return temp;
}
// print function for Binomial Tree
void printTree(Node *h)
{
while (h)
{
cout << h->data << "
";
printTree(h->child);
h = h->sibling;
}
}
// print function for binomial heap
void printHeap(list<Node*> _heap)
{
list<Node*> ::iterator it;
it = _heap.begin();
while (it != _heap.end())
{
printTree(*it);
it++;
}
}
// Main function to perform an Insert into a binomial heap H(i.e.
_heap)
int main()
{
int ch,key;
list<Node*> _heap;
// Insert data in the heap
_heap = insert(_heap,10);
cout << "Heap elements after
insertion:\n";
printHeap(_heap);
return 0;
}
/** ************************* end of BinomialHeap.cpp
************************************* **/
Instead of using Union to perform an Insert into a binomial heap H, we can write...
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...
starter code
To write a program using the starter code which is
TestLinkedList to see if the LinkedList program has bugs. It will
produce ether a pass or fail.More information is in the first two
pictures.
LinkedList.java
/**
* @author someone
*
* Implements a double-linked list with four errors
*/
public class LinkedList<E>
{
// The first and last nodes in the list
private Node<E> head, tail;
// Number of items stored in the list
private int size;
//...
Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...
1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...
Hello I need help with this program. Should programmed in C!
Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...
Using C++ in Visual Studios
Rewrite the code to use a Stack instead of a Queue. You will
need to think about the differences in the Stack and Queue. Think
about how they operate and how the nodes may differ.
Modify the code as necessary.
You will need to push to the Stack, pop from the Stack, peek at
the Stack. You will need a member functions like in the example
code. Your program will need to show that everything...
I need this in C++. This is all
one question
Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...
I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...