HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Heap Implementation in C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationheapstackoverflow

Problem

I am learning fundamental data structures, and I want to know if this is a valid implementation of a heap. Can any C# features be used to improve or extend the implementation? In addition to heapsort, what are some applications of a heap?

```
class BinaryHeap
{

///
/// A binary heap implementation used to sort an integer array.
///
/// 1. The Max-Heapify and Min-Heapify methods maintain the heap properties.
///
/// 2. The Build-Heap methods produce the heap from an unordered array.
///
/// 3. The HeapSort methods sort the array in place and runs in O(n ln n) time.
///

private int heapSize;

public BinaryHeap()
{
heapSize = 0;
}

private int ParentIndex(int currentIndex)
{
return currentIndex / 2;
}

private int LeftIndex(int currentIndex)
{
return currentIndex * 2;
}

private int RightIndex(int currentIndex)
{
return currentIndex * 2 + 1;
}

// Building the heap
#region
public void BuildMaxHeap(int[] A)
{
heapSize = A.Length - 1;

for(int i = A.Length/2; i >= 0; i--)
{
MaxHeapify(A, i);
}

}
public void BuildMinHeap(int[] A)
{
heapSize = A.Length - 1;
for (int i = A.Length / 2; i >= 0; i--)
{
MinHeapify(A, i);
}
}
#endregion

// Maintaining heap properties
// MaxHeapify: Ensure that parents are larger than children
// MinHeapify: Ensure that children are larger than parents
#region
public void MaxHeapify(int[] A, int i)
{

int leftIndex = LeftIndex(i);
int rightIndex = RightIndex(i);

int largestIndex = 0;

// Check to see which node in the tree subset has the largest value
if (leftIndex A[i])
{
largestIndex = leftIndex;
}
else
{
largestIndex = i;
}
if (rightIndex A[l

Solution

C#/Design related comments

  • Use small-letters for passing arguments.



  • Put your comments (The ` ) about the class on top of the class definition, not the field.



  • The way of using # regions is not really done the way you do in C# usually. There should be a description of the region right after the region on same line.



-
Use C# comments styling when commenting classes, methods, their parameters.

-
There is not much sense in designing Heap class that stores just a single integer (heap size). You could simply have
public static methods (utility methods) for that purpose, because it seems none of your methods really need to store a state

  • If you design this as a Heap, depends on use-cases you might want to design Push/Pop methods and store the underlying array in the class. Then having a class would make sense.



  • Try to use C# Generics instead of strongly defined int. This way you can make your code re-usable for other types (e.g. short) and/or for any object that implements IComparable (check out MSDN for more info on these).



Where to use
Heap.

In addition to sorting you can use
Heap as PriorityQueue.
If you add
Push/Pop methods and slightly modify the implementation, then it can serve as a PriorityQueue, which allows you to add elements(any object that implements IComparable) in random order, and the higher/lower ones will get to the top/bottom of the queue. You can go even further by designing another method - Update(...)`, which can modify the priority of an element (technically you could implement it by removing the old value from queue and then adding the updated value back to the queue).

Didn't validate your code correctness, but you can/should validate it by testing on some regular and corner cases etc.

Context

StackExchange Code Review Q#131397, answer score: 6

Revisions (0)

No revisions yet.