Skip to content

Commit e6cb4c6

Browse files
Gjort compare mer synlig i build (selv når ingen bytte kreves), kombinert build og sort i samme knapp i kombinert-modus og fikset bug
1 parent 54aebc6 commit e6cb4c6

File tree

12 files changed

+26
-30
lines changed

12 files changed

+26
-30
lines changed

Heap/index.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ <h1 id="headerText">Heap Datastructure</h1>
9797
Remove max
9898
</button>
9999
<br/>
100-
<button id="buildHeap" class="btn btn-primary" style="display: none">
101-
<!-- Click-Event handled in setUpBuildButton in methods.ts-->
102-
Convert to Heap
103-
</button>
104-
<br/>
105100
<button id="sortHeap" class="btn btn-primary" style="display: none">
106101
<!-- Click-Event handled in setUpSortButton in methods.ts-->
107102
Sort using HeapSort

Heap/js/HeapSort.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ var HeapSort = /** @class */ (function (_super) {
2525
control.setArrow(this.sortIndex);
2626
this.remove();
2727
}
28+
control.setArrow(-1);
2829
};
2930
HeapSort.prototype.remove = function () {
30-
// Remove root element, set last element to root and start frontendevents
31+
// Switch root and last element, remove root and start frontendevents
3132
var oldVal = this.array[0];
3233
this.exch(0, --this.currIndex);
3334
control.sortHighlightTwoNodes(this.currIndex, this.sortIndex, "orange");

Heap/js/HeapSort.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class HeapSort extends MaxHeap {
1818
control.setArrow(this.sortIndex);
1919
this.remove();
2020
}
21+
control.setArrow(-1);
2122
}
2223

2324
remove(): void {

Heap/js/MaxHeap.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,17 @@ var MaxHeap = /** @class */ (function () {
130130
var right = index * 2 + 2;
131131
if (left >= length)
132132
return;
133-
if (this.array[index] >= this.array[left] && this.array[index] >= this.array[right])
133+
if (this.array[index] >= this.array[left] && this.array[index] >= this.array[right]) {
134+
if (this.array[left] >= this.array[right] || right >= length) {
135+
control.highlightTwoNodes(index, left, "green");
136+
control.removeHighlightTwoNodes(index, left, "green");
137+
}
138+
else {
139+
control.highlightTwoNodes(index, right, "green");
140+
control.removeHighlightTwoNodes(index, right, "green");
141+
}
134142
return;
143+
}
135144
// Sink
136145
var other;
137146
if (right >= length)

Heap/js/MaxHeap.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,16 @@ class MaxHeap implements IAlgorithm {
151151

152152
if (left >= length)
153153
return;
154-
if (this.array[index] >= this.array[left] && this.array[index] >= this.array[right])
154+
if (this.array[index] >= this.array[left] && this.array[index] >= this.array[right]) {
155+
if (this.array[left] >= this.array[right] || right >= length) {
156+
control.highlightTwoNodes(index, left, "green")
157+
control.removeHighlightTwoNodes(index, left, "green");
158+
} else {
159+
control.highlightTwoNodes(index, right, "green");
160+
control.removeHighlightTwoNodes(index, right, "green");
161+
}
155162
return;
163+
}
156164

157165
// Sink
158166
let other;

Heap/js/MaxHeapCombined.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var MaxHeapCombined = /** @class */ (function (_super) {
3636
control.setArrow(this.sortIndex);
3737
this.remove();
3838
}
39+
control.setArrow(-1);
3940
};
4041
MaxHeapCombined.prototype.remove = function () {
4142
// Switch root and last element, remove root and start frontendevents

Heap/js/MaxHeapCombined.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class MaxHeapCombined extends MaxHeapFree {
2929
control.setArrow(this.sortIndex);
3030
this.remove();
3131
}
32+
control.setArrow(-1);
3233
}
3334

3435
remove(): void {

Heap/js/View.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ var View = /** @class */ (function () {
180180
control.removeNode();
181181
};
182182
View.prototype.sortHeap = function () {
183-
control.sortHeap();
184-
};
185-
View.prototype.buildHeap = function () {
186183
control.buildHeap();
184+
control.sortHeap();
187185
};
188186
View.prototype.swapNode = function (child, parent) {
189187
var forward = function (child, parent) {

Heap/js/View.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,10 @@ class View {
207207
}
208208

209209
sortHeap() {
210+
control.buildHeap();
210211
control.sortHeap();
211212
}
212213

213-
buildHeap() {
214-
control.buildHeap();
215-
}
216-
217214
swapNode(child: number, parent: number) {
218215
let forward = function (child, parent) {
219216
return function () {

Heap/js/drawGraph.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var GraphNode = /** @class */ (function () {
1616
this.moveBothDirections(xLayer * 70, yLayer * 50, time);
1717
return xLayer + 1;
1818
}
19-
// Recursive
2019
else {
2120
for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
2221
var child = _a[_i];

Heap/js/methods.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,6 @@ function setUpRemoveButton() {
312312
});
313313
}
314314
setUpRemoveButton();
315-
function setUpBuildButton() {
316-
$("#buildHeap").click(function () {
317-
viewer.buildHeap();
318-
});
319-
}
320-
setUpBuildButton();
321315
function setUpSortButton() {
322316
$("#sortHeap").click(function () {
323317
viewer.sortHeap();

Heap/js/methods.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,6 @@ function setUpRemoveButton() {
360360

361361
setUpRemoveButton();
362362

363-
function setUpBuildButton() {
364-
$("#buildHeap").click(function () {
365-
viewer.buildHeap();
366-
});
367-
}
368-
369-
setUpBuildButton()
370-
371363
function setUpSortButton() {
372364
$("#sortHeap").click(function () {
373365
viewer.sortHeap();

0 commit comments

Comments
 (0)