Skip to content

Commit e5f0f0c

Browse files
lagt sammen funksjoner og fikset bugs i algoritmene - Det er sikkert død kode som kan slettes naa
1 parent 0c2139d commit e5f0f0c

12 files changed

+271
-100
lines changed

Heap/js/Controller.js

+15
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ var Controller = /** @class */ (function () {
9090
Controller.prototype.getArrayLength = function () {
9191
return this.algorithm.getArrayLength();
9292
};
93+
Controller.prototype.exchangeElemAndNodes = function (index1, value1, index2, value2) {
94+
viewer.exchangeElemAndNodes(index1, value1, index2, value2);
95+
};
96+
Controller.prototype.highlightTwoNodes = function (index1, index2, color) {
97+
viewer.highlightTwoNodes(index1, index2, color);
98+
};
99+
Controller.prototype.removeHighlightTwoNodes = function (index1, index2, color) {
100+
viewer.removeHighlightTwoNodes(index1, index2, color);
101+
};
102+
Controller.prototype.sortHighlightTwoNodes = function (arrIndex, sortIndex, color) {
103+
viewer.sortHighlightTwoNodes(0, sortIndex, "orange");
104+
};
105+
Controller.prototype.setSortValAndDeselect = function (sortIndex, val) {
106+
viewer.setSortValAndDeselect(sortIndex, val);
107+
};
93108
return Controller;
94109
}());
95110
var control = new Controller();

Heap/js/Controller.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class Controller {
1717
this.speed = 50;
1818
viewer.changeToCurrentAlgorithm();
1919
if (algo.getName() == "FreeMode") {
20-
(<MaxHeapFree> this.algorithm).clearArrayValues();
21-
(<MaxHeapFree> this.algorithm).maxHeapFreeInit();
20+
(<MaxHeapFree>this.algorithm).clearArrayValues();
21+
(<MaxHeapFree>this.algorithm).maxHeapFreeInit();
2222
manager.start();
2323
} else {
2424
this.algorithm.setIndex();
@@ -45,7 +45,7 @@ class Controller {
4545
viewer.selectThisIndex(index, select);
4646
}
4747

48-
setValueAtIndex(i: number, bValue : any, oldVal : any) {
48+
setValueAtIndex(i: number, bValue: any, oldVal: any) {
4949
viewer.setValueAtThisIndex(i, bValue, oldVal);
5050
}
5151

@@ -111,6 +111,25 @@ class Controller {
111111
getArrayLength() {
112112
return this.algorithm.getArrayLength();
113113
}
114+
115+
exchangeElemAndNodes(index1: number, value1: number, index2: number, value2: number) {
116+
viewer.exchangeElemAndNodes(index1, value1, index2, value2);
117+
}
118+
119+
highlightTwoNodes(index1: number, index2: number, color: string) {
120+
viewer.highlightTwoNodes(index1, index2, color);
121+
}
122+
123+
removeHighlightTwoNodes(index1: number, index2: number, color: string) {
124+
viewer.removeHighlightTwoNodes(index1, index2, color);
125+
}
126+
127+
sortHighlightTwoNodes(arrIndex: number, sortIndex: number, color: string) {
128+
viewer.sortHighlightTwoNodes(0, sortIndex, "orange");
129+
}
130+
setSortValAndDeselect(sortIndex: number, val: any) {
131+
viewer.setSortValAndDeselect(sortIndex, val);
132+
}
114133
}
115134

116135
var control: Controller = new Controller();

Heap/js/EventManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/** Manager for events stored in queue. Manager is also responsible for executing events automatically */
66
var EventManager = /** @class */ (function () {
77
function EventManager() {
8-
this.delayTime = 600; // Original value
8+
this.delayTime = 1250; // Original value
99
this.nextEvents = [];
1010
this.previousEvents = [];
1111
}

Heap/js/EventManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare var $: any;
77

88
/** Manager for events stored in queue. Manager is also responsible for executing events automatically */
99
class EventManager {
10-
delayTime: number = 600; // Original value
10+
delayTime: number = 1250; // Original value
1111
nextEvents: FrontendEvent[] = [];
1212
previousEvents: FrontendEvent[] = [];
1313
eventThread: number;

Heap/js/HeapSort.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,10 @@ var HeapSort = /** @class */ (function (_super) {
2828
};
2929
HeapSort.prototype.remove = function () {
3030
// Remove root element, set last element to root and start frontendevents
31-
this.currIndex--;
3231
var oldVal = this.array[0];
33-
control.setSelectedIndex(0, true);
34-
control.highlightNode(0, "orange");
35-
control.highlightSortElem(this.sortIndex, "orange");
36-
control.setValueAtSortIndex(this.sortIndex, this.array[0]);
37-
control.setSelectedIndex(0, false);
38-
this.exch(0, this.currIndex);
39-
control.swapNode(this.currIndex, 0);
40-
control.removeElem(this.currIndex, false);
41-
control.setValueAtIndex(this.currIndex, " ", oldVal);
42-
this.sink(0, this.currIndex - 1);
32+
control.sortHighlightTwoNodes(0, this.sortIndex, "orange");
33+
control.setSortValAndDeselect(this.sortIndex, this.array[0]);
34+
_super.prototype.remove.call(this);
4335
control.highlightSortElem(this.sortIndex--, "green");
4436
};
4537
HeapSort.prototype.insertElems = function (size) {

Heap/js/HeapSort.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,10 @@ class HeapSort extends MaxHeap {
2222

2323
remove(): void {
2424
// Remove root element, set last element to root and start frontendevents
25-
this.currIndex--
2625
let oldVal = this.array[0];
27-
control.setSelectedIndex(0, true);
28-
control.highlightNode(0, "orange");
29-
control.highlightSortElem(this.sortIndex, "orange");
30-
control.setValueAtSortIndex(this.sortIndex, this.array[0]);
31-
control.setSelectedIndex(0, false);
32-
this.exch(0, this.currIndex);
33-
control.swapNode(this.currIndex, 0);
34-
control.removeElem(this.currIndex, false);
35-
control.setValueAtIndex(this.currIndex, " ", oldVal);
36-
this.sink(0, this.currIndex - 1);
26+
control.sortHighlightTwoNodes(0, this.sortIndex, "orange");
27+
control.setSortValAndDeselect(this.sortIndex, this.array[0]);
28+
super.remove();
3729
control.highlightSortElem(this.sortIndex--, "green");
3830
}
3931

Heap/js/MaxHeap.js

+21-32
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,12 @@ var MaxHeap = /** @class */ (function () {
9191
this.sink(k, n);
9292
};
9393
MaxHeap.prototype.exch = function (number, number2) {
94-
if (this.array[number] === undefined || this.array[number2] === undefined || number > this.currIndex || number > this.currIndex)
94+
if (this.array[number] === undefined || this.array[number2] === undefined)
9595
return;
96-
var tmp = this.array[number];
97-
this.array[number] = this.array[number2];
98-
control.setValueAtIndex(number, this.array[number], tmp);
99-
this.array[number2] = tmp;
100-
control.setValueAtIndex(number2, this.array[number2], this.array[number]);
96+
control.highlightTwoNodes(number, number2, "orange");
97+
control.removeHighlightTwoNodes(number, number2, "orange");
98+
control.exchangeElemAndNodes(number, this.array[number], number2, this.array[number2]);
99+
this.backEndExch(number, number2);
101100
};
102101
MaxHeap.prototype.add = function (a) {
103102
// Add to array and start frontendevents
@@ -112,58 +111,48 @@ var MaxHeap = /** @class */ (function () {
112111
};
113112
MaxHeap.prototype.remove = function () {
114113
// Remove root element, set last element to root and start frontendevents
115-
this.currIndex--;
116-
var oldVal = this.array[0];
117-
this.exch(0, this.currIndex);
118-
control.swapNode(this.currIndex, 0);
114+
this.exch(0, --this.currIndex);
119115
control.removeElem(this.currIndex, false);
120-
control.setValueAtIndex(this.currIndex, " ", oldVal);
121-
this.sink(0, this.currIndex - 1);
116+
this.sink(0, this.currIndex);
122117
};
123118
MaxHeap.prototype.sink = function (index, length) {
124119
var left = index * 2 + 1;
125120
var right = index * 2 + 2;
126-
if (right > length)
121+
if (left >= length)
127122
return;
128123
if (this.array[index] >= this.array[left] && this.array[index] >= this.array[right])
129124
return;
130125
// Sink
131126
var other;
132-
if (this.array[right] > this.array[left]) {
127+
if (right >= length)
128+
other = left;
129+
else if (this.array[right] > this.array[left]) {
133130
other = right;
134131
}
135132
else {
136133
other = left;
137134
}
138-
control.highlightNode(index, "orange");
139-
control.highlightNode(other, "orange");
140-
control.swapNode(index, other);
135+
// Check once more before exchange
136+
if (this.array[index] >= this.array[other]) {
137+
control.highlightTwoNodes(index, other, "green");
138+
control.removeHighlightTwoNodes(index, other, "green");
139+
return;
140+
}
141141
this.exch(index, other);
142-
control.highlightNode(index, "green");
143-
control.highlightNode(other, "green");
144-
control.removeHighlight(index);
145-
control.removeHighlight(other);
142+
control.highlightTwoNodes(index, other, "green");
143+
control.removeHighlightTwoNodes(index, other, "green");
146144
this.sink(other, length);
147145
};
148146
MaxHeap.prototype.swim = function (index) {
149147
var other = Math.floor((index - 1) / 2);
150148
while (other >= 0 && this.array[index] > this.array[other]) {
151-
control.highlightNode(index, "orange");
152-
control.highlightNode(other, "orange");
153-
control.swapNode(index, other);
154149
this.exch(index, other);
155-
control.highlightNode(index, "green");
156-
control.highlightNode(other, "green");
157-
control.removeHighlight(index);
158-
control.removeHighlight(other);
159150
index = Math.floor((index - 1) / 2);
160151
other = Math.floor((index - 1) / 2);
161152
}
162153
if (index !== 0) {
163-
control.highlightNode(index, "green");
164-
control.highlightNode(other, "green");
165-
control.removeHighlight(index);
166-
control.removeHighlight(other);
154+
control.highlightTwoNodes(index, other, "green");
155+
control.removeHighlightTwoNodes(index, other, "green");
167156
}
168157
};
169158
MaxHeap.prototype.getName = function () {

Heap/js/MaxHeap.ts

+22-32
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ class MaxHeap implements IAlgorithm {
110110
}
111111

112112
protected exch(number: number, number2: number) {
113-
if (this.array[number] === undefined || this.array[number2] === undefined || number > this.currIndex || number > this.currIndex)
113+
if (this.array[number] === undefined || this.array[number2] === undefined)
114114
return;
115115

116-
let tmp = this.array[number];
117-
this.array[number] = this.array[number2];
118-
control.setValueAtIndex(number, this.array[number], tmp);
119-
this.array[number2] = tmp;
120-
control.setValueAtIndex(number2, this.array[number2], this.array[number]);
116+
control.highlightTwoNodes(number, number2, "orange");
117+
control.removeHighlightTwoNodes(number, number2, "orange");
118+
control.exchangeElemAndNodes(number, this.array[number], number2, this.array[number2]);
119+
this.backEndExch(number, number2);
121120
}
122121

123122
add(a: number): void {
@@ -133,63 +132,54 @@ class MaxHeap implements IAlgorithm {
133132

134133
remove(): void {
135134
// Remove root element, set last element to root and start frontendevents
136-
this.currIndex--;
137-
let oldVal = this.array[0];
138-
this.exch(0, this.currIndex);
139-
control.swapNode(this.currIndex, 0);
135+
this.exch(0, --this.currIndex);
140136
control.removeElem(this.currIndex, false);
141-
control.setValueAtIndex(this.currIndex, " ", oldVal);
142-
this.sink(0, this.currIndex - 1);
137+
this.sink(0, this.currIndex);
143138
}
144139

145140
protected sink(index: number, length: number): void {
146141
let left: number = index * 2 + 1;
147142
let right: number = index * 2 + 2;
148143

149-
if (right > length)
144+
if (left >= length)
150145
return;
151146
if (this.array[index] >= this.array[left] && this.array[index] >= this.array[right])
152147
return;
153148

154149
// Sink
155150
let other;
156-
if (this.array[right] > this.array[left]) {
151+
if (right >= length)
152+
other = left;
153+
else if (this.array[right] > this.array[left]) {
157154
other = right;
158155
} else {
159156
other = left;
160157
}
161158

162-
control.highlightNode(index, "orange");
163-
control.highlightNode(other, "orange");
164-
control.swapNode(index, other);
159+
// Check once more before exchange
160+
if (this.array[index] >= this.array[other]) {
161+
control.highlightTwoNodes(index, other, "green");
162+
control.removeHighlightTwoNodes(index, other, "green");
163+
return;
164+
}
165+
165166
this.exch(index, other);
166-
control.highlightNode(index, "green");
167-
control.highlightNode(other, "green");
168-
control.removeHighlight(index);
169-
control.removeHighlight(other);
167+
control.highlightTwoNodes(index, other, "green");
168+
control.removeHighlightTwoNodes(index, other, "green");
170169
this.sink(other, length);
171170
}
172171

173172
protected swim(index: number): void {
174173
let other: number = Math.floor((index - 1) / 2);
175174
while (other >= 0 && this.array[index] > this.array[other]) {
176-
control.highlightNode(index, "orange");
177-
control.highlightNode(other, "orange");
178-
control.swapNode(index, other);
179175
this.exch(index, other);
180-
control.highlightNode(index, "green");
181-
control.highlightNode(other, "green");
182-
control.removeHighlight(index);
183-
control.removeHighlight(other);
184176
index = Math.floor((index - 1) / 2);
185177
other = Math.floor((index - 1) / 2);
186178
}
187179

188180
if (index !== 0) {
189-
control.highlightNode(index, "green");
190-
control.highlightNode(other, "green");
191-
control.removeHighlight(index);
192-
control.removeHighlight(other);
181+
control.highlightTwoNodes(index, other, "green");
182+
control.removeHighlightTwoNodes(index, other, "green");
193183
}
194184
}
195185

0 commit comments

Comments
 (0)