Skip to content

Commit ef3e06b

Browse files
Fikset freemode, satt grense på maks 10 elementer og start med 5 elementer i predefined
1 parent 33fab79 commit ef3e06b

18 files changed

+784
-237
lines changed

Heap/.idea/workspace.xml

Lines changed: 124 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Heap/js/Controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var Controller = /** @class */ (function () {
2222
else {
2323
this.algorithm.setIndex();
2424
viewer.displayThisArray(this.algorithm.getArray());
25+
this.algorithm.removeNodes();
2526
manager.start();
2627
this.algorithm.connectNodes();
2728
}
@@ -89,6 +90,9 @@ var Controller = /** @class */ (function () {
8990
Controller.prototype.insertNewElem = function (child, value, parent) {
9091
viewer.insertNewElemThis(child, value, parent);
9192
};
93+
Controller.prototype.getArrayLength = function () {
94+
return this.algorithm.getArrayLength();
95+
};
9296
return Controller;
9397
}());
9498
var control = new Controller();

Heap/js/Controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Controller {
2424
} else {
2525
this.algorithm.setIndex();
2626
viewer.displayThisArray(this.algorithm.getArray());
27+
this.algorithm.removeNodes();
2728
manager.start();
2829
this.algorithm.connectNodes();
2930
}
@@ -64,7 +65,7 @@ class Controller {
6465
highlightSortElem(index: number, color: string) {
6566
viewer.highlightThisSortElem(index, color);
6667
}
67-
68+
6869
removeHighlight(node: number) {
6970
viewer.removeThisHighlight(node);
7071
}
@@ -111,6 +112,10 @@ class Controller {
111112
insertNewElem(child: number, value: number, parent: number) {
112113
viewer.insertNewElemThis(child, value, parent);
113114
}
115+
116+
getArrayLength() {
117+
return this.algorithm.getArrayLength();
118+
}
114119
}
115120

116121
var control: Controller = new Controller();

Heap/js/EventManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var EventManager = /** @class */ (function () {
1515
return;
1616
}
1717
var event = this.nextEvents.shift();
18-
console.log(this.nextEvents);
18+
//console.log(this.nextEvents);
1919
event.next();
2020
this.previousEvents.push(event);
2121
if (event.duration == 0)

Heap/js/EventManager.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
*
44
*/
55

6-
declare var $ : any;
6+
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
11-
nextEvents:FrontendEvent[] = [];
12-
previousEvents:FrontendEvent[] = [];
13-
eventThread : number;
10+
delayTime: number = 600; // Original value
11+
nextEvents: FrontendEvent[] = [];
12+
previousEvents: FrontendEvent[] = [];
13+
eventThread: number;
1414

1515
// Executing the next event in the queue, adding it to 'previous'
1616
next() {
1717
if (this.nextEvents.length == 0) {
1818
return;
1919
}
2020
var event = (<FrontendEvent>this.nextEvents.shift());
21-
console.log(this.nextEvents);
21+
//console.log(this.nextEvents);
2222
event.next();
2323
this.previousEvents.push(event);
2424
if (event.duration == 0)
@@ -29,22 +29,22 @@ class EventManager {
2929
previous() {
3030
if (this.previousEvents.length == 0)
3131
return;
32-
var event :FrontendEvent = (<FrontendEvent>this.previousEvents.pop());
32+
var event: FrontendEvent = (<FrontendEvent>this.previousEvents.pop());
3333
//this.delayTime = 0; //TODO: Should there be a delay when stepping backwards?
3434
event.previous();
3535
this.nextEvents.unshift(event);
3636
}
3737

38-
addEvent(event : FrontendEvent) {
38+
addEvent(event: FrontendEvent) {
3939
this.nextEvents.push(event);
4040
}
4141

4242
start() {
4343
clearInterval(this.eventThread);
4444
var manager = this; // Anonymous functions cannot access this...
45-
this.eventThread = setInterval(function() {
45+
this.eventThread = setInterval(function () {
4646
manager.next();
47-
}, manager.delayTime);
47+
}, manager.delayTime);
4848
}
4949

5050
pause() {
@@ -53,17 +53,18 @@ class EventManager {
5353
}
5454

5555
class FrontendEvent {
56-
next : Function;
57-
previous : Function;
58-
duration:number;
59-
constructor(n : Function, p : Function, d:number) {
56+
next: Function;
57+
previous: Function;
58+
duration: number;
59+
60+
constructor(n: Function, p: Function, d: number) {
6061
this.next = n;
6162
this.previous = p;
6263
this.duration = d;
6364
}
6465
}
6566

66-
var manager:EventManager = new EventManager();
67+
var manager: EventManager = new EventManager();
6768

6869
/*
6970
/** How to add FrontendEvents to manager

Heap/js/IAlgorithm.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ interface IAlgorithm {
3535
*/
3636
build(): void
3737

38+
/**
39+
* Get size of array
40+
*/
41+
getArrayLength(): number
42+
3843
/**
3944
* Set the current state of the algorithm to the given array
4045
* @param array
@@ -50,4 +55,9 @@ interface IAlgorithm {
5055
* Connect parent and child nodes
5156
*/
5257
connectNodes(): void
58+
59+
/**
60+
* Remove nodes in indexes > currIndex
61+
*/
62+
removeNodes(): void
5363
}

Heap/js/MaxHeap.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var MaxHeap = /** @class */ (function () {
77
function MaxHeap(size) {
88
this.name = "MaxHeap";
99
this.arraySize = size;
10-
this.currIndex = size - 1;
10+
this.currIndex = size;
1111
this.array = new Array;
12-
for (var i = 0; i < size; i++) {
12+
for (var i = 0; i < 10; i++) {
1313
this.array[i] = Math.floor((Math.random() * 10)) + 1;
1414
}
1515
this.backEndBuild();
@@ -18,7 +18,17 @@ var MaxHeap = /** @class */ (function () {
1818
}
1919
MaxHeap.prototype.setIndex = function () {
2020
for (var i = 0; i < this.array.length; i++) {
21-
setValueAtIndex(i, this.array[i]);
21+
if (i >= this.currIndex)
22+
setValueAtIndex(i, " ");
23+
else
24+
setValueAtIndex(i, this.array[i]);
25+
}
26+
};
27+
MaxHeap.prototype.removeNodes = function () {
28+
for (var i = this.currIndex; i < this.getArray().length; i++) {
29+
allNodes.pop();
30+
superNode.children.pop();
31+
$("#node" + i).remove();
2232
}
2333
};
2434
MaxHeap.prototype.connectNodes = function () {
@@ -81,10 +91,9 @@ var MaxHeap = /** @class */ (function () {
8191
var n = this.array.length;
8292
for (var k = Math.floor((n - 1) / 2); k >= 0; k--)
8393
this.sink(k, n);
84-
console.log(this.array);
8594
};
8695
MaxHeap.prototype.exch = function (number, number2) {
87-
if (this.array[number] === undefined || this.array[number2] === undefined)
96+
if (this.array[number] === undefined || this.array[number2] === undefined || number > this.currIndex || number > this.currIndex)
8897
return;
8998
control.saveState(this.array);
9099
var tmp = this.array[number];
@@ -94,27 +103,38 @@ var MaxHeap = /** @class */ (function () {
94103
control.setValueAtIndex(number2, this.array[number2]);
95104
};
96105
MaxHeap.prototype.add = function (a) {
97-
control.saveState(this.array); // Save the new state
106+
control.saveState(this.array);
98107
// Add to array and start frontendevents
99-
control.lockScreen(true);
100-
this.array.push(a);
101-
//insertNewElem(this.array.length - 1, a); // Create element in frontendarray
102-
//insertNewElemConnect(this.array.length - 1, Math.floor((this.array.length - 2) / 2));
103-
control.insertNewElem(this.array.length - 1, a, Math.floor((this.array.length - 2) / 2));
108+
if (this.currIndex > 10) {
109+
return;
110+
}
111+
else {
112+
control.lockScreen(true);
113+
this.array[this.currIndex] = a;
114+
setValueAtIndex(this.currIndex, a);
115+
insertNewNode(this.currIndex++, a);
116+
}
104117
// Swim to te correct index and start frontendevents
105-
this.swim(this.array.length - 1);
118+
if (this.currIndex == 1) {
119+
positioningNodes(1000);
120+
}
121+
else {
122+
insertNewElemConnect(this.currIndex - 1, Math.floor((this.currIndex - 2) / 2));
123+
this.swim(this.currIndex - 1);
124+
}
106125
control.lockScreen(false);
107-
control.saveState(this.array);
108126
};
109127
MaxHeap.prototype.remove = function () {
110128
control.saveState(this.array);
111129
control.lockScreen(true);
112130
// Remove root element, set last element to root and start frontendevents
113-
this.exch(0, this.array.length - 1);
114-
control.swapNode(this.array.length - 1, 0);
115-
control.removeElem(this.array.length - 1, true);
116-
this.array.pop();
117-
this.sink(0, this.array.length - 1);
131+
this.currIndex--;
132+
this.exch(0, this.currIndex);
133+
control.swapNode(this.currIndex, 0);
134+
control.removeElem(this.currIndex, false);
135+
control.setValueAtIndex(this.currIndex, " ");
136+
this.sink(0, this.currIndex - 1);
137+
control.saveState(this.array);
118138
control.lockScreen(false);
119139
};
120140
MaxHeap.prototype.sink = function (index, length) {
@@ -165,6 +185,9 @@ var MaxHeap = /** @class */ (function () {
165185
MaxHeap.prototype.getArray = function () {
166186
return this.array;
167187
};
188+
MaxHeap.prototype.getArrayLength = function () {
189+
return this.currIndex;
190+
};
168191
MaxHeap.prototype.setArray = function (array) {
169192
};
170193
return MaxHeap;

Heap/js/MaxHeap.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class MaxHeap implements IAlgorithm {
1414

1515
constructor(size: number) {
1616
this.arraySize = size;
17-
this.currIndex = size - 1;
17+
this.currIndex = size;
1818
this.array = new Array;
19-
for (let i = 0; i < size; i++) {
19+
for (let i = 0; i < 10; i++) {
2020
this.array[i] = Math.floor((Math.random() * 10)) + 1;
2121
}
2222
this.backEndBuild();
@@ -26,7 +26,18 @@ class MaxHeap implements IAlgorithm {
2626

2727
setIndex() {
2828
for (let i = 0; i < this.array.length; i++) {
29-
setValueAtIndex(i, this.array[i]);
29+
if (i >= this.currIndex)
30+
setValueAtIndex(i, " ");
31+
else
32+
setValueAtIndex(i, this.array[i]);
33+
}
34+
}
35+
36+
removeNodes() {
37+
for (let i = this.currIndex; i < this.getArray().length; i++) {
38+
allNodes.pop();
39+
superNode.children.pop();
40+
$("#node" + i).remove();
3041
}
3142
}
3243

@@ -98,12 +109,10 @@ class MaxHeap implements IAlgorithm {
98109
let n: number = this.array.length;
99110
for (let k: number = Math.floor((n - 1) / 2); k >= 0; k--)
100111
this.sink(k, n);
101-
102-
console.log(this.array);
103112
}
104113

105114
protected exch(number: number, number2: number) {
106-
if (this.array[number] === undefined || this.array[number2] === undefined)
115+
if (this.array[number] === undefined || this.array[number2] === undefined || number > this.currIndex || number > this.currIndex)
107116
return;
108117

109118
control.saveState(this.array);
@@ -116,30 +125,40 @@ class MaxHeap implements IAlgorithm {
116125
}
117126

118127
add(a: number): void {
119-
control.saveState(this.array); // Save the new state
128+
control.saveState(this.array);
129+
120130
// Add to array and start frontendevents
121-
control.lockScreen(true)
122-
this.array.push(a);
123-
//insertNewElem(this.array.length - 1, a); // Create element in frontendarray
124-
//insertNewElemConnect(this.array.length - 1, Math.floor((this.array.length - 2) / 2));
125-
control.insertNewElem(this.array.length - 1, a, Math.floor((this.array.length - 2) / 2));
131+
if (this.currIndex > 10) {
132+
return;
133+
} else {
134+
control.lockScreen(true);
135+
this.array[this.currIndex] = a;
136+
setValueAtIndex(this.currIndex, a);
137+
insertNewNode(this.currIndex++, a);
138+
}
126139

127140
// Swim to te correct index and start frontendevents
128-
this.swim(this.array.length - 1);
141+
if (this.currIndex == 1) {
142+
positioningNodes(1000);
143+
} else {
144+
insertNewElemConnect(this.currIndex - 1, Math.floor((this.currIndex - 2) / 2));
145+
this.swim(this.currIndex - 1);
146+
}
129147
control.lockScreen(false);
130-
control.saveState(this.array);
131148
}
132149

133150
remove(): void {
134151
control.saveState(this.array);
135152

136153
control.lockScreen(true);
137154
// Remove root element, set last element to root and start frontendevents
138-
this.exch(0, this.array.length - 1);
139-
control.swapNode(this.array.length - 1, 0);
140-
control.removeElem(this.array.length - 1, true);
141-
this.array.pop();
142-
this.sink(0, this.array.length - 1);
155+
this.currIndex--;
156+
this.exch(0, this.currIndex);
157+
control.swapNode(this.currIndex, 0);
158+
control.removeElem(this.currIndex, false);
159+
control.setValueAtIndex(this.currIndex, " ");
160+
this.sink(0, this.currIndex - 1);
161+
control.saveState(this.array);
143162
control.lockScreen(false);
144163
}
145164

@@ -198,6 +217,10 @@ class MaxHeap implements IAlgorithm {
198217
return this.array;
199218
}
200219

220+
getArrayLength(): number {
221+
return this.currIndex;
222+
}
223+
201224
setArray(array: number[]): void {
202225
}
203226

0 commit comments

Comments
 (0)