Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ee3cb1

Browse files
refactoring
1 parent 1f91a98 commit 7ee3cb1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

‎src/main/java/ir/sk/adt/datastructure/tree/binarytree/heaptree/MaxBinaryHeap.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,23 @@ public void insert(int element) {
138138
return;
139139

140140
array[++size] = element;
141-
heapifyUp();
141+
heapifyUpIterative();
142+
}
143+
144+
private void heapifyUp(int index) {
145+
int current = size;
146+
int parentIndex = getParentIndex(index);
147+
if (array[index] > array[parentIndex]) {
148+
swapWithIndex(index, parentIndex);
149+
heapifyUp(parentIndex);
150+
}
142151
}
143152

144153
/**
145154
*
146155
*/
147156
@TimeComplexity("O(Log n)")
148-
private void heapifyUp() {
157+
private void heapifyUpIterative() {
149158
int current = size;
150159

151160
while (array[current] > array[getParentIndex(current)]) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /