Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. My Heap implementation is not working...please have a look frnds..

My Heap implementation is not working...please have a look frnds..

Scheduled Pinned Locked Moved C / C++ / MFC
databasealgorithmsdata-structures
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pavarathyRock
    wrote on last edited by
    #1

    Hi Frnds, I am trying to do the heap implementation. I was following the algorithm explained in cormen....

    #include "MyHeap.h"
    void MyHeap::Build_Max_Heap(int * array, int length)
    {
    for (int i= (length/2); i>=0 ; i--)
    {
    Max_heapify(array, i, length);
    }
    }

    void MyHeap::Max_heapify(int * array, int index, int length)
    {
    int left = ((2 * index) + 1);
    int right = ((2 * index) + 2);
    int largest;
    if ((left <= length) && (array[left] > array[index]))
    {
    largest = left;
    }
    else
    {
    largest = index;
    }

    if ((right <= length) && (array\[right\] > array\[largest\]))
    {
    	largest = right;
    }
    
    if (largest != index)
    {
    	swap(array\[largest\], array\[index\]);
    	Max\_heapify(array, largest, length);
    }
    

    }

    void MyHeap::swap(int & a, int & b)
    {
    int c = a;
    a = b;
    b = c;
    }

    void MyHeap::print(int * array, int length)
    {
    for (int i = 0; i< length; i++)
    {
    cout<<array[i]<<"\t";
    }
    }

    int main()
    {
    int array[] = {14, 10, 1, 8, 7, 9, 3, 2, 4, 16};
    MyHeap h;
    h.print(array, 10);
    h.Build_Max_Heap(array, 10);
    h.print(array, 10);

    int x;
    cin>>x;
    return(0);
    

    }

    For some input it is working. For some it is not.. for the given input this is the output I am getting.. 16 14 9 8 10 1 3 2 4 7 Thanks in advance...

    I L D 3 Replies Last reply
    0
    • P pavarathyRock

      Hi Frnds, I am trying to do the heap implementation. I was following the algorithm explained in cormen....

      #include "MyHeap.h"
      void MyHeap::Build_Max_Heap(int * array, int length)
      {
      for (int i= (length/2); i>=0 ; i--)
      {
      Max_heapify(array, i, length);
      }
      }

      void MyHeap::Max_heapify(int * array, int index, int length)
      {
      int left = ((2 * index) + 1);
      int right = ((2 * index) + 2);
      int largest;
      if ((left <= length) && (array[left] > array[index]))
      {
      largest = left;
      }
      else
      {
      largest = index;
      }

      if ((right <= length) && (array\[right\] > array\[largest\]))
      {
      	largest = right;
      }
      
      if (largest != index)
      {
      	swap(array\[largest\], array\[index\]);
      	Max\_heapify(array, largest, length);
      }
      

      }

      void MyHeap::swap(int & a, int & b)
      {
      int c = a;
      a = b;
      b = c;
      }

      void MyHeap::print(int * array, int length)
      {
      for (int i = 0; i< length; i++)
      {
      cout<<array[i]<<"\t";
      }
      }

      int main()
      {
      int array[] = {14, 10, 1, 8, 7, 9, 3, 2, 4, 16};
      MyHeap h;
      h.print(array, 10);
      h.Build_Max_Heap(array, 10);
      h.print(array, 10);

      int x;
      cin>>x;
      return(0);
      

      }

      For some input it is working. For some it is not.. for the given input this is the output I am getting.. 16 14 9 8 10 1 3 2 4 7 Thanks in advance...

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      pavarathyRock wrote:

      I was following the algorithm explained in cormen....

      You forgot to explain what you're trying to do in the comments... All we currently know is that you're failing, not what you need to do. Iain.

      I have now moved to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[^]

      1 Reply Last reply
      0
      • P pavarathyRock

        Hi Frnds, I am trying to do the heap implementation. I was following the algorithm explained in cormen....

        #include "MyHeap.h"
        void MyHeap::Build_Max_Heap(int * array, int length)
        {
        for (int i= (length/2); i>=0 ; i--)
        {
        Max_heapify(array, i, length);
        }
        }

        void MyHeap::Max_heapify(int * array, int index, int length)
        {
        int left = ((2 * index) + 1);
        int right = ((2 * index) + 2);
        int largest;
        if ((left <= length) && (array[left] > array[index]))
        {
        largest = left;
        }
        else
        {
        largest = index;
        }

        if ((right <= length) && (array\[right\] > array\[largest\]))
        {
        	largest = right;
        }
        
        if (largest != index)
        {
        	swap(array\[largest\], array\[index\]);
        	Max\_heapify(array, largest, length);
        }
        

        }

        void MyHeap::swap(int & a, int & b)
        {
        int c = a;
        a = b;
        b = c;
        }

        void MyHeap::print(int * array, int length)
        {
        for (int i = 0; i< length; i++)
        {
        cout<<array[i]<<"\t";
        }
        }

        int main()
        {
        int array[] = {14, 10, 1, 8, 7, 9, 3, 2, 4, 16};
        MyHeap h;
        h.print(array, 10);
        h.Build_Max_Heap(array, 10);
        h.print(array, 10);

        int x;
        cin>>x;
        return(0);
        

        }

        For some input it is working. For some it is not.. for the given input this is the output I am getting.. 16 14 9 8 10 1 3 2 4 7 Thanks in advance...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        16 14 9 8 10 1 3 2 4 7 looks like a correct heap to me, now I may have made a mistake, but it looks like:

                  16
                 /   \\
               14     9
              /  \\   /  \\
             8   10  1  3
            / \\  /
           2  4  7
        

        Which is a correct heap, no?

        P 1 Reply Last reply
        0
        • P pavarathyRock

          Hi Frnds, I am trying to do the heap implementation. I was following the algorithm explained in cormen....

          #include "MyHeap.h"
          void MyHeap::Build_Max_Heap(int * array, int length)
          {
          for (int i= (length/2); i>=0 ; i--)
          {
          Max_heapify(array, i, length);
          }
          }

          void MyHeap::Max_heapify(int * array, int index, int length)
          {
          int left = ((2 * index) + 1);
          int right = ((2 * index) + 2);
          int largest;
          if ((left <= length) && (array[left] > array[index]))
          {
          largest = left;
          }
          else
          {
          largest = index;
          }

          if ((right <= length) && (array\[right\] > array\[largest\]))
          {
          	largest = right;
          }
          
          if (largest != index)
          {
          	swap(array\[largest\], array\[index\]);
          	Max\_heapify(array, largest, length);
          }
          

          }

          void MyHeap::swap(int & a, int & b)
          {
          int c = a;
          a = b;
          b = c;
          }

          void MyHeap::print(int * array, int length)
          {
          for (int i = 0; i< length; i++)
          {
          cout<<array[i]<<"\t";
          }
          }

          int main()
          {
          int array[] = {14, 10, 1, 8, 7, 9, 3, 2, 4, 16};
          MyHeap h;
          h.print(array, 10);
          h.Build_Max_Heap(array, 10);
          h.print(array, 10);

          int x;
          cin>>x;
          return(0);
          

          }

          For some input it is working. For some it is not.. for the given input this is the output I am getting.. 16 14 9 8 10 1 3 2 4 7 Thanks in advance...

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          pavarathyRock wrote:

          void MyHeap::Max_heapify(int * array, int index, int length) { int left = ((2 * index) + 1); int right = ((2 * index) + 2); ...

          The first time Max_heapify() is called, left equals 11 and right equals 12.

          pavarathyRock wrote:

          if ((left <= length) && (array[left] > array[index]))

          left is then used to access array, which is indexed 0-9.

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          1 Reply Last reply
          0
          • L Lost User

            16 14 9 8 10 1 3 2 4 7 looks like a correct heap to me, now I may have made a mistake, but it looks like:

                      16
                     /   \\
                   14     9
                  /  \\   /  \\
                 8   10  1  3
                / \\  /
               2  4  7
            

            Which is a correct heap, no?

            P Offline
            P Offline
            pavarathyRock
            wrote on last edited by
            #5

            But here 16 / \ 14 9 / \ / \ 8 10 1 3 / \ / 2 4 7 10 is coming after 9 . As per heaps definition the root should contain largest element right..

            L 1 Reply Last reply
            0
            • P pavarathyRock

              But here 16 / \ 14 9 / \ / \ 8 10 1 3 / \ / 2 4 7 10 is coming after 9 . As per heaps definition the root should contain largest element right..

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              No it doesn't, the layout got messed up here, but that 10 is below the 14 and the 1 and 3 are below the 9.

              P 1 Reply Last reply
              0
              • L Lost User

                No it doesn't, the layout got messed up here, but that 10 is below the 14 and the 1 and 3 are below the 9.

                P Offline
                P Offline
                pavarathyRock
                wrote on last edited by
                #7

                :) :) Thanks herald for the help..... I think my implementation is correct. I have tried to print the heap as follows.

                int main()
                {
                int array[] = {14, 10, 1, 8, 7, 9, 3, 2, 4, 16};
                MyHeap h;
                h.print(array, 10);
                h.Build_Max_Heap(array, 10);
                cout<<"Initial Heap\n";
                h.print(array, 10);
                cout<

                It was giving the correct output.

                The main cause for this doubt is that I AM A FOOOL. I blindly believed the output given in the book.
                I didn't even try to print the output and check.......

                Thanks frnd thank you very much.....:thumbsup::thumbsup::thumbsup:

                and thanks for all others for your help.

                L 1 Reply Last reply
                0
                • P pavarathyRock

                  :) :) Thanks herald for the help..... I think my implementation is correct. I have tried to print the heap as follows.

                  int main()
                  {
                  int array[] = {14, 10, 1, 8, 7, 9, 3, 2, 4, 16};
                  MyHeap h;
                  h.print(array, 10);
                  h.Build_Max_Heap(array, 10);
                  cout<<"Initial Heap\n";
                  h.print(array, 10);
                  cout<

                  It was giving the correct output.

                  The main cause for this doubt is that I AM A FOOOL. I blindly believed the output given in the book.
                  I didn't even try to print the output and check.......

                  Thanks frnd thank you very much.....:thumbsup::thumbsup::thumbsup:

                  and thanks for all others for your help.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Lol you're welcome, but you didn't spell my name right ;)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups