Skip to main content
added 17 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Two things:

  1. This is very important, and I wasn't able to properly declare an instance of my class without it. You have to add <T> after IComparable: class MinHeap<T> where T : IComparable<T>IComparable:

     class MinHeap<T> where T : IComparable<T>
    
  2. The suggestion of changing ListList to IListIList was an interesting thought. However, the reason you'd want to use List is because it automatically resizes its capacity as it grows in size. IListIList does not have this functionality, at least from what I've read. You would want to use IListIList if you are deriving another class from it (e.g. class MyList<T> : IList<T>) and want to modify the behavior of the List directly.

So, my final implementation came out to this:

class MinHeap<T> where T : IComparable<T>
{
    List<T> elements;
    ...
}

class Node : IComparable<Node>
{
    ...
}

void MyFunction()
{
    Node currNode;
    MinHeap<Node> nodeHeap = new MinHeap<Node>(1000);   //Capacity
    ...
}

Two things:

  1. This is very important, and I wasn't able to properly declare an instance of my class without it. You have to add <T> after IComparable: class MinHeap<T> where T : IComparable<T>

  2. The suggestion of changing List to IList was an interesting thought. However, the reason you'd want to use List is because it automatically resizes its capacity as it grows in size. IList does not have this functionality, at least from what I've read. You would want to use IList if you are deriving another class from it (e.g. class MyList<T> : IList<T>) and want to modify the behavior of the List directly.

So, my final implementation came out to this:

class MinHeap<T> where T : IComparable<T>
{
    List<T> elements;
    ...
}

class Node : IComparable<Node>
{
    ...
}

void MyFunction()
{
    Node currNode;
    MinHeap<Node> nodeHeap = new MinHeap<Node>(1000);   //Capacity
    ...
}

Two things:

  1. This is very important, and I wasn't able to properly declare an instance of my class without it. You have to add <T> after IComparable:

     class MinHeap<T> where T : IComparable<T>
    
  2. The suggestion of changing List to IList was an interesting thought. However, the reason you'd want to use List is because it automatically resizes its capacity as it grows in size. IList does not have this functionality, at least from what I've read. You would want to use IList if you are deriving another class from it (e.g. class MyList<T> : IList<T>) and want to modify the behavior of the List directly.

So, my final implementation came out to this:

class MinHeap<T> where T : IComparable<T>
{
    List<T> elements;
    ...
}

class Node : IComparable<Node>
{
    ...
}

void MyFunction()
{
    Node currNode;
    MinHeap<Node> nodeHeap = new MinHeap<Node>(1000);   //Capacity
    ...
}
Source Link

Two things:

  1. This is very important, and I wasn't able to properly declare an instance of my class without it. You have to add <T> after IComparable: class MinHeap<T> where T : IComparable<T>

  2. The suggestion of changing List to IList was an interesting thought. However, the reason you'd want to use List is because it automatically resizes its capacity as it grows in size. IList does not have this functionality, at least from what I've read. You would want to use IList if you are deriving another class from it (e.g. class MyList<T> : IList<T>) and want to modify the behavior of the List directly.

So, my final implementation came out to this:

class MinHeap<T> where T : IComparable<T>
{
    List<T> elements;
    ...
}

class Node : IComparable<Node>
{
    ...
}

void MyFunction()
{
    Node currNode;
    MinHeap<Node> nodeHeap = new MinHeap<Node>(1000);   //Capacity
    ...
}