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;
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
add a comment |
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
add a comment |
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
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
java heap
asked Mar 9 at 3:50
Andrés Felipe Castillo SopóAndrés Felipe Castillo Sopó
11
11
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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