CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    99

    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?

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    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.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Mar 2012
    Posts
    99

    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()
    {
    
    }
    }
    }
    Last edited by beginner91; March 16th, 2012 at 02:27 PM.

  4. #4
    Join Date
    Mar 2012
    Posts
    99

    Re: need help with priority queue/heapSort

    can anyone help?

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured