Skip to content

Commit 54aebc6

Browse files
ØyvindØyvind
authored andcommitted
lagt til kombinert visualisering som Marc ønsket
1 parent f4c243a commit 54aebc6

15 files changed

+239
-34
lines changed

Heap/css/heap_style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ div.settings {
146146
text-align: center;
147147
padding: 10px 25px;
148148
vertical-align: top;
149-
height: 110px;
149+
height: 140px;
150150
border-right: 2px black solid;
151151
border-left: 2px black solid;
152152
}

Heap/index.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<script src="js/MaxHeapFree.js"></script>
1818
<script src="js/BuildHeap.js"></script>
1919
<script src="js/HeapSort.js"></script>
20+
<script src="js/MaxHeapCombined.js"></script>
2021
</head>
2122

2223

@@ -60,10 +61,12 @@ <h1 id="headerText">Heap Datastructure</h1>
6061
onclick="viewer.switchAlgorithm('MaxHeap')">Predefined<br>
6162
<input id="MaxHeapFree" type="radio" name="algorithm" value="max"
6263
onclick="viewer.switchAlgorithm('MaxHeapFree')">FreeMode<br>
63-
<input id="HeapSort" type="radio" name="algorithm" value="max"
64-
onclick="viewer.switchAlgorithm('HeapSort')">HeapSort<br>
6564
<input id="Heapify" type="radio" name="algorithm" value="max"
6665
onclick="viewer.switchAlgorithm('BuildHeap')">Construct Heap<br>
66+
<input id="HeapSort" type="radio" name="algorithm" value="max"
67+
onclick="viewer.switchAlgorithm('HeapSort')">HeapSort<br>
68+
<input id="MaxHeapCombiend" type="radio" name="algorithm" value="max"
69+
onclick="viewer.switchAlgorithm('MaxHeapCombined')">All combined<br>
6770
</form>
6871
</div>
6972
<div class="settings">
@@ -93,6 +96,16 @@ <h1 id="headerText">Heap Datastructure</h1>
9396
<!-- Click-Event handled in setUpRemoveButton in methods.ts-->
9497
Remove max
9598
</button>
99+
<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/>
105+
<button id="sortHeap" class="btn btn-primary" style="display: none">
106+
<!-- Click-Event handled in setUpSortButton in methods.ts-->
107+
Sort using HeapSort
108+
</button>
96109
</div>
97110
</div>
98111
</div>

Heap/js/Controller.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
///<reference path="View.ts"/>
88
///<reference path="methods.ts"/>
99
///<reference path="MaxHeapFree.ts"/>
10+
///<reference path="MaxHeapCombined.ts"/>
1011
var Controller = /** @class */ (function () {
1112
function Controller() {
1213
}
@@ -15,11 +16,13 @@ var Controller = /** @class */ (function () {
1516
this.algoName = this.algorithm.getName();
1617
this.speed = 50;
1718
viewer.changeToCurrentAlgorithm();
18-
if (algo.getName() == "FreeMode" || algo.getName() == "MaxHeapFree") {
19+
if (algo.getName() == "FreeMode" || algo.getName() == "MaxHeapFree" || algo.getName() == "MaxHeapCombined") {
1920
this.algorithm.clearArrayValues();
2021
this.algorithm.maxHeapFreeInit();
2122
manager.start();
2223
}
24+
else if (algo.getName() == "MaxHeapCombined") {
25+
}
2326
else {
2427
this.algorithm.setIndex();
2528
viewer.displayThisArray(this.algorithm.getArray());
@@ -31,6 +34,14 @@ var Controller = /** @class */ (function () {
3134
Controller.prototype.getAlgorithm = function () {
3235
return this.algorithm;
3336
};
37+
Controller.prototype.sortHeap = function () {
38+
if (this.algorithm.getName() === "MaxHeapCombined")
39+
this.algorithm.sort();
40+
};
41+
Controller.prototype.buildHeap = function () {
42+
if (this.algorithm.getName() === "MaxHeapCombined")
43+
this.algorithm.build();
44+
};
3445
Controller.prototype.setArrow = function (index) {
3546
viewer.setThisArrow(index);
3647
};
@@ -74,7 +85,7 @@ var Controller = /** @class */ (function () {
7485
viewer.removeHighlightTwoNodes(index1, index2, color);
7586
};
7687
Controller.prototype.sortHighlightTwoNodes = function (arrIndex, sortIndex, color) {
77-
viewer.sortHighlightTwoNodes(0, sortIndex, "orange");
88+
viewer.sortHighlightTwoNodes(arrIndex, sortIndex, "orange");
7889
};
7990
Controller.prototype.setSortValAndDeselect = function (sortIndex, val) {
8091
viewer.setSortValAndDeselect(sortIndex, val);

Heap/js/Controller.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
///<reference path="View.ts"/>
88
///<reference path="methods.ts"/>
99
///<reference path="MaxHeapFree.ts"/>
10+
///<reference path="MaxHeapCombined.ts"/>
11+
1012

1113
class Controller {
1214
private algorithm: IAlgorithm;
@@ -18,10 +20,12 @@ class Controller {
1820
this.algoName = this.algorithm.getName();
1921
this.speed = 50;
2022
viewer.changeToCurrentAlgorithm();
21-
if (algo.getName() == "FreeMode" || algo.getName() == "MaxHeapFree") {
23+
if (algo.getName() == "FreeMode" || algo.getName() == "MaxHeapFree" || algo.getName() == "MaxHeapCombined") {
2224
(<MaxHeapFree>this.algorithm).clearArrayValues();
2325
(<MaxHeapFree>this.algorithm).maxHeapFreeInit();
2426
manager.start();
27+
} else if (algo.getName() == "MaxHeapCombined") {
28+
2529
} else {
2630
this.algorithm.setIndex();
2731
viewer.displayThisArray(this.algorithm.getArray());
@@ -35,6 +39,16 @@ class Controller {
3539
return this.algorithm;
3640
}
3741

42+
sortHeap() {
43+
if(this.algorithm.getName() === "MaxHeapCombined")
44+
(<MaxHeapCombined> this.algorithm).sort();
45+
}
46+
47+
buildHeap() {
48+
if(this.algorithm.getName() === "MaxHeapCombined")
49+
(<MaxHeapCombined> this.algorithm).build();
50+
}
51+
3852
setArrow(index: number) {
3953
viewer.setThisArrow(index);
4054
}
@@ -92,7 +106,7 @@ class Controller {
92106
}
93107

94108
sortHighlightTwoNodes(arrIndex: number, sortIndex: number, color: string) {
95-
viewer.sortHighlightTwoNodes(0, sortIndex, "orange");
109+
viewer.sortHighlightTwoNodes(arrIndex, sortIndex, "orange");
96110
}
97111
setSortValAndDeselect(sortIndex: number, val: any) {
98112
viewer.setSortValAndDeselect(sortIndex, val);

Heap/js/HeapSort.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var HeapSort = /** @class */ (function (_super) {
1616
__extends(HeapSort, _super);
1717
function HeapSort(size) {
1818
var _this = _super.call(this, size) || this;
19-
_this.insertElems(size);
19+
HeapSort.insertElems(size);
2020
_this.sortIndex = _this.currIndex - 1;
2121
return _this;
2222
}
@@ -29,12 +29,14 @@ var HeapSort = /** @class */ (function (_super) {
2929
HeapSort.prototype.remove = function () {
3030
// Remove root element, set last element to root and start frontendevents
3131
var oldVal = this.array[0];
32-
control.sortHighlightTwoNodes(0, this.sortIndex, "orange");
33-
control.setSortValAndDeselect(this.sortIndex, this.array[0]);
34-
_super.prototype.remove.call(this);
32+
this.exch(0, --this.currIndex);
33+
control.sortHighlightTwoNodes(this.currIndex, this.sortIndex, "orange");
34+
control.setSortValAndDeselect(this.sortIndex, oldVal);
35+
control.removeElem(this.currIndex, false);
36+
this.sink(0, this.currIndex);
3537
control.highlightSortElem(this.sortIndex--, "green");
3638
};
37-
HeapSort.prototype.insertElems = function (size) {
39+
HeapSort.insertElems = function (size) {
3840
for (var i = 0; i < size; i++) {
3941
$("#sortArrayUL").append("<li id='sortArrayElem" + i + "'><div class='index'>" + i + "</div><div class='content' id='sortArrayContent" + i + "'>" + " " + "</div></li>");
4042
}

Heap/js/HeapSort.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class HeapSort extends MaxHeap {
99

1010
constructor(size: number) {
1111
super(size);
12-
this.insertElems(size);
12+
HeapSort.insertElems(size);
1313
this.sortIndex = this.currIndex - 1;
1414
}
1515

@@ -21,16 +21,18 @@ class HeapSort extends MaxHeap {
2121
}
2222

2323
remove(): void {
24-
// Remove root element, set last element to root and start frontendevents
24+
// Switch root and last element, remove root and start frontendevents
2525
let oldVal = this.array[0];
26-
control.sortHighlightTwoNodes(0, this.sortIndex, "orange");
27-
control.setSortValAndDeselect(this.sortIndex, this.array[0]);
28-
super.remove();
26+
this.exch(0, --this.currIndex);
27+
control.sortHighlightTwoNodes(this.currIndex, this.sortIndex, "orange");
28+
control.setSortValAndDeselect(this.sortIndex, oldVal);
29+
control.removeElem(this.currIndex, false);
30+
this.sink(0, this.currIndex);
2931
control.highlightSortElem(this.sortIndex--, "green");
3032
}
3133

3234

33-
private insertElems(size: number) {
35+
static insertElems(size: number) {
3436
for (var i = 0; i < size; i++) {
3537
$("#sortArrayUL").append("<li id='sortArrayElem" + i + "'><div class='index'>" + i + "</div><div class='content' id='sortArrayContent" + i + "'>" + " " + "</div></li>");
3638
}

Heap/js/MaxHeap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ var MaxHeap = /** @class */ (function () {
9696
* Build a max heap
9797
*/
9898
MaxHeap.prototype.build = function () {
99-
var n = this.array.length;
99+
var n = this.currIndex;
100100
for (var k = Math.floor((n - 1) / 2); k >= 0; k--)
101101
this.sink(k, n);
102102
};

Heap/js/MaxHeap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class MaxHeap implements IAlgorithm {
112112
* Build a max heap
113113
*/
114114
build() {
115-
let n: number = this.array.length;
115+
let n: number = this.currIndex;
116116
for (let k: number = Math.floor((n - 1) / 2); k >= 0; k--)
117117
this.sink(k, n);
118118
}

Heap/js/MaxHeapCombined.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Created by Øyvind Skeie Liland on 13.03.18
3+
*/
4+
var __extends = (this && this.__extends) || (function () {
5+
var extendStatics = Object.setPrototypeOf ||
6+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
8+
return function (d, b) {
9+
extendStatics(d, b);
10+
function __() { this.constructor = d; }
11+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12+
};
13+
})();
14+
///<reference path="methods.ts"/>
15+
///<reference path="drawGraph.ts"/>
16+
var MaxHeapCombined = /** @class */ (function (_super) {
17+
__extends(MaxHeapCombined, _super);
18+
function MaxHeapCombined(size) {
19+
var _this = _super.call(this, size) || this;
20+
HeapSort.insertElems(10);
21+
_this.sortIndex = _this.currIndex - 1;
22+
return _this;
23+
}
24+
MaxHeapCombined.prototype.add = function (a) {
25+
this.sortIndex++;
26+
if (this.currIndex > 10) {
27+
return;
28+
}
29+
else {
30+
this.array[this.currIndex] = a;
31+
control.insertNewElem(this.currIndex++, a, Math.floor((this.currIndex - 2) / 2));
32+
}
33+
};
34+
MaxHeapCombined.prototype.sort = function () {
35+
for (var i = this.sortIndex; i >= 0; i--) {
36+
control.setArrow(this.sortIndex);
37+
this.remove();
38+
}
39+
};
40+
MaxHeapCombined.prototype.remove = function () {
41+
// Switch root and last element, remove root and start frontendevents
42+
var oldVal = this.array[0];
43+
this.exch(0, --this.currIndex);
44+
control.sortHighlightTwoNodes(this.currIndex, this.sortIndex, "orange");
45+
control.setSortValAndDeselect(this.sortIndex, oldVal);
46+
control.removeElem(this.currIndex, false);
47+
this.sink(0, this.currIndex);
48+
control.highlightSortElem(this.sortIndex--, "green");
49+
};
50+
MaxHeapCombined.prototype.getName = function () {
51+
return "MaxHeapCombined";
52+
};
53+
return MaxHeapCombined;
54+
}(MaxHeapFree));

Heap/js/MaxHeapCombined.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Created by Øyvind Skeie Liland on 13.03.18
3+
*/
4+
5+
///<reference path="methods.ts"/>
6+
///<reference path="drawGraph.ts"/>
7+
8+
class MaxHeapCombined extends MaxHeapFree {
9+
private sortIndex;
10+
11+
constructor(size: number) {
12+
super(size);
13+
HeapSort.insertElems(10);
14+
this.sortIndex = this.currIndex - 1;
15+
}
16+
17+
add(a: number) {
18+
this.sortIndex++;
19+
if (this.currIndex > 10) {
20+
return;
21+
} else {
22+
this.array[this.currIndex] = a;
23+
control.insertNewElem(this.currIndex++, a, Math.floor((this.currIndex - 2) / 2));
24+
}
25+
}
26+
27+
sort() {
28+
for (let i = this.sortIndex; i >= 0; i--) {
29+
control.setArrow(this.sortIndex);
30+
this.remove();
31+
}
32+
}
33+
34+
remove(): void {
35+
// Switch root and last element, remove root and start frontendevents
36+
let oldVal = this.array[0];
37+
this.exch(0, --this.currIndex);
38+
control.sortHighlightTwoNodes(this.currIndex, this.sortIndex, "orange");
39+
control.setSortValAndDeselect(this.sortIndex, oldVal);
40+
control.removeElem(this.currIndex, false);
41+
this.sink(0, this.currIndex);
42+
control.highlightSortElem(this.sortIndex--, "green");
43+
}
44+
45+
getName() {
46+
return "MaxHeapCombined";
47+
}
48+
}

0 commit comments

Comments
 (0)