Problem with buildHeap method in a D-ary-heap implementationWhat and where are the stack and heap?Which is faster: Stack allocation or Heap allocation“implements Runnable” vs “extends Thread” in Java'Must Override a Superclass Method' Errors after importing a project into EclipseJava: when to use static methodsCould not reserve enough space for object heapHow is the default Java heap size determined?How can building a heap be O(n) time complexity?Beginning Java Method PracticeHow to implement parcelable with my custom class containing Hashmap and SparseArray?

Motorized valve interfering with button?

How can I fix this gap between bookcases I made?

Modification to Chariots for Heavy Cavalry Analogue for 4-armed race

Can I interfere when another PC is about to be attacked?

How to make payment on the internet without leaving a money trail?

Concept of linear mappings are confusing me

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Does the radius of the Spirit Guardians spell depend on the size of the caster?

What Brexit solution does the DUP want?

Can town administrative "code" overule state laws like those forbidding trespassing?

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

What is the meaning of "of trouble" in the following sentence?

Why is "Reports" in sentence down without "The"

Why CLRS example on residual networks does not follows its formula?

Is there a minimum number of transactions in a block?

Why Is Death Allowed In the Matrix?

A Journey Through Space and Time

Closed subgroups of abelian groups

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

Why don't electron-positron collisions release infinite energy?

I see my dog run

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

New order #4: World

What is the white spray-pattern residue inside these Falcon Heavy nozzles?



Problem with buildHeap method in a D-ary-heap implementation


What and where are the stack and heap?Which is faster: Stack allocation or Heap allocation“implements Runnable” vs “extends Thread” in Java'Must Override a Superclass Method' Errors after importing a project into EclipseJava: when to use static methodsCould not reserve enough space for object heapHow is the default Java heap size determined?How can building a heap be O(n) time complexity?Beginning Java Method PracticeHow to implement parcelable with my custom class containing Hashmap and SparseArray?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is.



public class DHeap<T extends Comparable>

private int size;
private T[] array;
private int d;

public DHeap(int d)
size = 0;
this.d = d;
array = (T[]) new Comparable[10];


public DHeap(int initialSize, int d)
size = 0;
this.d = d;
array = (T[]) new Comparable[initialSize];


private void ensureCapacity(int newCapacity)
T[] newArray = Arrays.copyOf(array, newCapacity);
array = newArray;


public boolean isEmpty()
return size == 0;


public void insert(T item)
if(size == array.length - 1)
ensureCapacity(array.length * 2);

array[size++] = item;
percolateUp(size - 1);


public T delete(int index)
if(!isEmpty())
T indexElement = array[index];
array[index] = array[size - 1];
size--;
percolateDown(index);
return indexElement;
else
return null;



public T deleteMin()
T min = array[0];
delete(0);
return min;


public void buildHeap(T[] items)
size = items.length;

for(int i = 0; i<items.length; i++)
array[i] = items[i];


for(int j = size/2; j > 0; j--)
percolateDown(j);



public void printHeap()
System.out.println("n Heap = ");
for(int i = 0; i < size; i++)
System.out.print(array[i] + " ");

System.out.println();


private int parent(int i)
return (i-1)/d;


private int kThChild(int i, int k)
return d*i+k;


private int minChild(int index)
int bestChild = kThChild(index, 1);
int k = 2;
int pos = kThChild(index, k);
while((k <= d) && (pos < size))
if(array[pos].compareTo(array[bestChild]) < 0)
bestChild = pos;

pos = kThChild(index, k++);

return bestChild;


private void percolateUp(int index)
T tmp = array[index];
while(index > 0 && tmp.compareTo(array[parent(index)]) < 0)
array[index] = array[parent(index)];
index = parent(index);

array[index] = tmp;


private void percolateDown(int index)
int child;
T tmp = array[index];
while(kThChild(index, 1) < size)
child = minChild(index);
if(array[child].compareTo(tmp) < 0)
array[index] = array[child];
else
break;
index = child;

array[index] = tmp;











share|improve this question




























    0















    The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is.



    public class DHeap<T extends Comparable>

    private int size;
    private T[] array;
    private int d;

    public DHeap(int d)
    size = 0;
    this.d = d;
    array = (T[]) new Comparable[10];


    public DHeap(int initialSize, int d)
    size = 0;
    this.d = d;
    array = (T[]) new Comparable[initialSize];


    private void ensureCapacity(int newCapacity)
    T[] newArray = Arrays.copyOf(array, newCapacity);
    array = newArray;


    public boolean isEmpty()
    return size == 0;


    public void insert(T item)
    if(size == array.length - 1)
    ensureCapacity(array.length * 2);

    array[size++] = item;
    percolateUp(size - 1);


    public T delete(int index)
    if(!isEmpty())
    T indexElement = array[index];
    array[index] = array[size - 1];
    size--;
    percolateDown(index);
    return indexElement;
    else
    return null;



    public T deleteMin()
    T min = array[0];
    delete(0);
    return min;


    public void buildHeap(T[] items)
    size = items.length;

    for(int i = 0; i<items.length; i++)
    array[i] = items[i];


    for(int j = size/2; j > 0; j--)
    percolateDown(j);



    public void printHeap()
    System.out.println("n Heap = ");
    for(int i = 0; i < size; i++)
    System.out.print(array[i] + " ");

    System.out.println();


    private int parent(int i)
    return (i-1)/d;


    private int kThChild(int i, int k)
    return d*i+k;


    private int minChild(int index)
    int bestChild = kThChild(index, 1);
    int k = 2;
    int pos = kThChild(index, k);
    while((k <= d) && (pos < size))
    if(array[pos].compareTo(array[bestChild]) < 0)
    bestChild = pos;

    pos = kThChild(index, k++);

    return bestChild;


    private void percolateUp(int index)
    T tmp = array[index];
    while(index > 0 && tmp.compareTo(array[parent(index)]) < 0)
    array[index] = array[parent(index)];
    index = parent(index);

    array[index] = tmp;


    private void percolateDown(int index)
    int child;
    T tmp = array[index];
    while(kThChild(index, 1) < size)
    child = minChild(index);
    if(array[child].compareTo(tmp) < 0)
    array[index] = array[child];
    else
    break;
    index = child;

    array[index] = tmp;











    share|improve this question
























      0












      0








      0








      The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is.



      public class DHeap<T extends Comparable>

      private int size;
      private T[] array;
      private int d;

      public DHeap(int d)
      size = 0;
      this.d = d;
      array = (T[]) new Comparable[10];


      public DHeap(int initialSize, int d)
      size = 0;
      this.d = d;
      array = (T[]) new Comparable[initialSize];


      private void ensureCapacity(int newCapacity)
      T[] newArray = Arrays.copyOf(array, newCapacity);
      array = newArray;


      public boolean isEmpty()
      return size == 0;


      public void insert(T item)
      if(size == array.length - 1)
      ensureCapacity(array.length * 2);

      array[size++] = item;
      percolateUp(size - 1);


      public T delete(int index)
      if(!isEmpty())
      T indexElement = array[index];
      array[index] = array[size - 1];
      size--;
      percolateDown(index);
      return indexElement;
      else
      return null;



      public T deleteMin()
      T min = array[0];
      delete(0);
      return min;


      public void buildHeap(T[] items)
      size = items.length;

      for(int i = 0; i<items.length; i++)
      array[i] = items[i];


      for(int j = size/2; j > 0; j--)
      percolateDown(j);



      public void printHeap()
      System.out.println("n Heap = ");
      for(int i = 0; i < size; i++)
      System.out.print(array[i] + " ");

      System.out.println();


      private int parent(int i)
      return (i-1)/d;


      private int kThChild(int i, int k)
      return d*i+k;


      private int minChild(int index)
      int bestChild = kThChild(index, 1);
      int k = 2;
      int pos = kThChild(index, k);
      while((k <= d) && (pos < size))
      if(array[pos].compareTo(array[bestChild]) < 0)
      bestChild = pos;

      pos = kThChild(index, k++);

      return bestChild;


      private void percolateUp(int index)
      T tmp = array[index];
      while(index > 0 && tmp.compareTo(array[parent(index)]) < 0)
      array[index] = array[parent(index)];
      index = parent(index);

      array[index] = tmp;


      private void percolateDown(int index)
      int child;
      T tmp = array[index];
      while(kThChild(index, 1) < size)
      child = minChild(index);
      if(array[child].compareTo(tmp) < 0)
      array[index] = array[child];
      else
      break;
      index = child;

      array[index] = tmp;











      share|improve this question














      The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is. The buildHeap method doesn't work, i checked the code multiple times and i don't get it. I think that the problem lies in the methos percolateDown but i can't tell which is.



      public class DHeap<T extends Comparable>

      private int size;
      private T[] array;
      private int d;

      public DHeap(int d)
      size = 0;
      this.d = d;
      array = (T[]) new Comparable[10];


      public DHeap(int initialSize, int d)
      size = 0;
      this.d = d;
      array = (T[]) new Comparable[initialSize];


      private void ensureCapacity(int newCapacity)
      T[] newArray = Arrays.copyOf(array, newCapacity);
      array = newArray;


      public boolean isEmpty()
      return size == 0;


      public void insert(T item)
      if(size == array.length - 1)
      ensureCapacity(array.length * 2);

      array[size++] = item;
      percolateUp(size - 1);


      public T delete(int index)
      if(!isEmpty())
      T indexElement = array[index];
      array[index] = array[size - 1];
      size--;
      percolateDown(index);
      return indexElement;
      else
      return null;



      public T deleteMin()
      T min = array[0];
      delete(0);
      return min;


      public void buildHeap(T[] items)
      size = items.length;

      for(int i = 0; i<items.length; i++)
      array[i] = items[i];


      for(int j = size/2; j > 0; j--)
      percolateDown(j);



      public void printHeap()
      System.out.println("n Heap = ");
      for(int i = 0; i < size; i++)
      System.out.print(array[i] + " ");

      System.out.println();


      private int parent(int i)
      return (i-1)/d;


      private int kThChild(int i, int k)
      return d*i+k;


      private int minChild(int index)
      int bestChild = kThChild(index, 1);
      int k = 2;
      int pos = kThChild(index, k);
      while((k <= d) && (pos < size))
      if(array[pos].compareTo(array[bestChild]) < 0)
      bestChild = pos;

      pos = kThChild(index, k++);

      return bestChild;


      private void percolateUp(int index)
      T tmp = array[index];
      while(index > 0 && tmp.compareTo(array[parent(index)]) < 0)
      array[index] = array[parent(index)];
      index = parent(index);

      array[index] = tmp;


      private void percolateDown(int index)
      int child;
      T tmp = array[index];
      while(kThChild(index, 1) < size)
      child = minChild(index);
      if(array[child].compareTo(tmp) < 0)
      array[index] = array[child];
      else
      break;
      index = child;

      array[index] = tmp;








      java heap






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 9 at 3:50









      Andrés Felipe Castillo SopóAndrés Felipe Castillo Sopó

      11




      11






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073806%2fproblem-with-buildheap-method-in-a-d-ary-heap-implementation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073806%2fproblem-with-buildheap-method-in-a-d-ary-heap-implementation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme