need help with priority queue/heapSort
I have been programming in c++ for awhile and now I am learning c# and I am still learning how it works. I have to make a priority queue with heap sort code. I know the code in c++ but I am finding it write to convert it to c#
heres some of the code I am having trouble with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace priority
{
public class HeapSort
{
private int numItems;
private int maxItems;
private ArrayList items;
public HeapSort()
{
int index;
for (index = numItems / 2 - 1; index >= 0; index--)
ReheapDown(items, index, numItems - 1);
}
public bool IsEmpty()
{
return numItems == 0;
}
public bool IsFull()
{
return numItems == maxItems;
}
public void Enqueue(Patient newPatient)
{
items.Add(newPatient);
numItems++;
ReheapUp();
}
public void Dequeue(Patient newPatient)
{
items.Remove(newPatient);
numItems--;
ReheapUp();
}
public void ReheapUp(int root, int bottom)
{
int parent;
if (bottom > root)
{
parent = (bottom - 1) / 2;
if (items[parent] < items[bottom])
{
Swap(items[parent], items[bottom]);
ReheapUp(root, parent);
}
}
}
public void ReheapDown(ArrayList items, int root, int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild = root * 2 + 1;
rightChild = root * 2 + 2;
if (leftChild <= bottom)
{
maxChild = leftChild;
}
else
{
if(items[leftChild] <= items[rightChild])
{
maxChild = rightChild;
}
else
{
maxChild = leftChild;
}
if(items[root] < items[maxChild])
{
Swap(items[root], items[maxChild]);
ReheapDown(items, maxChild, bottom);
}
}
}
public void Swap()
{
}
}
}
I have red underline on the if statements. Could someone please help?
Re: need help with priority queue/heapSort
Please enclose your code in [ code ] [/ code ] brackets- it improves readability. It's too hard to try to read large code sections without it.
Re: need help with priority queue/heapSort
Ok here it is in tags sorry about that
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace priority
{
public class HeapSort
{
private int numItems;
private int maxItems;
private ArrayList items;
public HeapSort()
{
int index;
for (index = numItems / 2 - 1; index >= 0; index--)
ReheapDown(items, index, numItems - 1);
}
public bool IsEmpty()
{
return numItems == 0;
}
public bool IsFull()
{
return numItems == maxItems;
}
public void Enqueue(Patient newPatient)
{
items.Add(newPatient);
numItems++;
ReheapUp();
}
public void Dequeue(Patient newPatient)
{
items.Remove(newPatient);
numItems--;
ReheapUp();
}
public void ReheapUp(int root, int bottom)
{
int parent;
if (bottom > root)
{
parent = (bottom - 1) / 2;
if (items[parent] < items[bottom])
{
Swap(items[parent], items[bottom]);
ReheapUp(root, parent);
}
}
}
public void ReheapDown(ArrayList items, int root, int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild = root * 2 + 1;
rightChild = root * 2 + 2;
if (leftChild <= bottom)
{
maxChild = leftChild;
}
else
{
if(items[leftChild] <= items[rightChild])
{
maxChild = rightChild;
}
else
{
maxChild = leftChild;
}
if(items[root] < items[maxChild])
{
Swap(items[root], items[maxChild]);
ReheapDown(items, maxChild, bottom);
}
}
}
public void Swap()
{
}
}
}
Re: need help with priority queue/heapSort
Re: need help with priority queue/heapSort
It's hard to read code that is not indented. Also, you've not actually told us what your problems is.
People here will help you, but you need to be more specific. You can't just say "My program doesn't work". You need to give an example, like... I entered this data and... this was the output.... but I expected this.... etc.