Implement a min-heap, Data Structure & Algorithms

Assignment Help:

Description

A heap is an efficient tree-based data structure that can be used as a priority queue. Recall that the abstract data type of a priority queue has the following operations

  • size, isEmpty, min
  • insert
  • removeMin

We can describe the priority queue ADT using the following Java Entry class and interface:

1 import java.lang.Comparable;

2

3 /**

4 * When items are added to the heap, you should create an Entry object

5 * to hold the key and value and store this in the appropriate node

6 */

7 public class Entryextends Comparable,V> {

8 protected K key;

9 protected V value;

10 public MyEntry(K k, V v) { key = k; value = v; }

11 public K getKey() { return key; }

12 public V getValue() { return value; }

13 public String toString() { return "(" + key + "," + value + ")"; }

14 }

15

16 public interface PriorityQueueextends Comparable,V> {

17 /** Returns the number of items in the priority queue. */

18 public int size();

19 /** Returns whether the priority queue is empty. */

20 public boolean isEmpty();

21 /** Returns but does not remove an entry with minimum key. */

22 public Entry min();

23 /** Inserts a key-value pair and return the entry created. */

24 public Entry insert(K key, V value);

25 /** Removes and returns an entry with minimum key. */

26 public Entry removeMin();

27 }

 

The main operations (insert, removeMin) can be done in O(log n) with a heap, while the other operations of the priority queue ADT (isEmpty, size, or look up the min value) are constant time. In lectures we have seen how to implement a heap using an array-based implementation.

58_Implement a min-heap.png

Figure 1: 3-way heap example

For this assignment you must implement a min-heap using a using a tree-based implementation (similar to the binary tree class we have used in tutorials). This tree should be 3-way tree, where each node needs to have (at most) three children

Note that the definition of a 3-way heap is identical to that of a binary heap, except for allowing at most three children (see Figure ). As with a binary tree, every node must have all of its children, except for the nodes at the last levels of the tree. In more detail, your task is to

1. Design a 3-way tree structure that you will use for building your heap. You can use code provided in the book. You can use any helper data structures that you need (linked lists, arrays etc.), but you must implement the tree structure yourself.

2. Implement your design for a generic 3-way heap in a class called ThreewayHeap. You will need to implement all operations (insert, removeMin, isEmpty, etc.) in the supplied interface and skeleton for the 3-way heap. In most cases the extension is straightforward from binary heaps, with certain extra cases that you need to check.

3. Include a method in your heap to print out a visual representation in DOT format (helpful for testing/debugging purposes).

4. Design test cases for your new heap structure, used as a priority queue

5. Use the provided test code on your implementation


Related Discussions:- Implement a min-heap

Exact analysis of insertion sort, Exact analysis of insertion sort: Let...

Exact analysis of insertion sort: Let us assume the following pseudocode to analyse the exact runtime complexity of insertion sort. T j   is the time taken to execute the s

Data structure queue, In this unit, we described about the data structure Q...

In this unit, we described about the data structure Queue. It had two ends. One is front from where the elements can be removed and the other is rear where the elements can be inse

Question, A binary search tree is used to locate the number 43. Which of th...

A binary search tree is used to locate the number 43. Which of the following probe sequences are possible and which are not? Explain. (a) 61 52 14 17 40 43 (b) 2 3 50 40 60 43 (c)

Number of leaf nodes in a complete binary tree, The number of leaf nodes in...

The number of leaf nodes in a complete binary tree of depth d is    2 d

State the range of operation of abstract data type, State the range of oper...

State the range of operation of ADT Operations of the Range of T ADT includes following, where a, b ∈ T and r and s are values of Range of T: a...b-returns a range value (an

Simulation of queues, Simulation of queues: Simulation is the process of f...

Simulation of queues: Simulation is the process of forming an abstract model of a real world situation in order to understand the effect of modifications and the effect of introdu

Different ways for representing s graph, W h at are the different ways by...

W h at are the different ways by which we can represent graph?  Represent the graph drawn below using those ways.     T he d iff e r e nt w a y s by

Define order of growth, Define order of growth The  efficiency  analysi...

Define order of growth The  efficiency  analysis  framework  concentrates   on  the  order  of  growth  of  an  algorithm's   basic operation count as the principal indicator o

ERM, Hi, can you give me a quote for an E-R diagram

Hi, can you give me a quote for an E-R diagram

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd