Having loop problems with Remove() using binary heaps & comparators -Java -
we have binary heap of 10 items. trying "remove" function work using comparators. running infinite loop situation , not sure why. thing help. thanks.
public e remove() { int current = 1; int lastvalue = size(); e element = get(lastvalue); set(1,element); remove(lastvalue); int left = current * 2; int right = current * 2 +1; e leftelement= get(left); e rightelement= get(right); int value1 = comp.compare(element, leftelement); int value2 = comp.compare(element, rightelement); while(value1 == 1 || value2 == 1){ int value3 = comp.compare(leftelement, rightelement); if (value3 == 1){ //left > right set(current, rightelement); set(right, element); current = right; } else{ set(current, leftelement); set(left, element); current= left; } int left2 = current * 2; int right2 = current * 2 + 1; leftelement = get(left2); rightelement = get(right2); value1 = comp.compare(element, leftelement); value2 = comp.compare(element, rightelement); } // must provide return null; }
Comments
Post a Comment