-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace the stack used for sibling search with a heap
- Loading branch information
Showing
8 changed files
with
207 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
export class Heap { | ||
constructor(array, comparator) { | ||
this.heap = array; | ||
this.compare = comparator; | ||
this.heapify(); | ||
} | ||
push(value) { | ||
this.heap.push(value); | ||
let index = this.heap.length - 1; | ||
while (index > 0) { | ||
let parent = Math.trunc((index - 1) / 2); | ||
if (this.compare(this.heap[parent], this.heap[index])) { | ||
break; | ||
} | ||
let tmp = this.heap[parent]; | ||
this.heap[parent] = this.heap[index]; | ||
this.heap[index] = tmp; | ||
index = parent; | ||
} | ||
} | ||
pop() { | ||
let value = this.heap[0]; | ||
this.heap[0] = this.heap[this.heap.length - 1]; | ||
this.heap.pop(); | ||
this.downHeap(0); | ||
return value; | ||
} | ||
downHeap(index) { | ||
let size = this.length; | ||
let largest = index; | ||
let left = 2 * index + 1; | ||
let right = 2 * index + 2; | ||
if (left < size && this.compare(this.heap[left], this.heap[largest])) { | ||
largest = left; | ||
} | ||
if (right < size && this.compare(this.heap[right], this.heap[largest])) { | ||
largest = right; | ||
} | ||
if (largest != index) { | ||
// swap | ||
let tmp = this.heap[index]; | ||
this.heap[index] = this.heap[largest]; | ||
this.heap[largest] = tmp; | ||
// recurse down | ||
this.downHeap(largest); | ||
} | ||
} | ||
heapify() { | ||
// start from middle | ||
for (let i = Math.trunc((this.length / 2) - 1); i >= 0; --i) { | ||
this.downHeap(i); | ||
} | ||
} | ||
get length() { | ||
return this.heap.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
export class Heap<T> | ||
{ | ||
public heap: T[]; | ||
private compare; | ||
|
||
constructor(array: T[], comparator: (a: T, b: T) => boolean) | ||
{ | ||
this.heap = array; | ||
this.compare = comparator; | ||
|
||
this.heapify(); | ||
} | ||
|
||
public push(value: T) | ||
{ | ||
this.heap.push(value); | ||
|
||
let index = this.heap.length - 1; | ||
while (index > 0) | ||
{ | ||
let parent = Math.trunc((index - 1) / 2); | ||
|
||
if (this.compare(this.heap[parent], this.heap[index])) | ||
{ | ||
break; | ||
} | ||
|
||
let tmp = this.heap[parent]; | ||
this.heap[parent] = this.heap[index]; | ||
this.heap[index] = tmp; | ||
|
||
index = parent; | ||
} | ||
} | ||
|
||
public pop(): T | undefined | ||
{ | ||
let value = this.heap[0]; | ||
|
||
this.heap[0] = this.heap[this.heap.length - 1]; | ||
this.heap.pop(); | ||
|
||
this.downHeap(0); | ||
|
||
return value; | ||
} | ||
|
||
private downHeap(index: number) | ||
{ | ||
let size = this.length; | ||
|
||
let largest = index; | ||
let left = 2 * index + 1; | ||
let right = 2 * index + 2; | ||
|
||
if (left < size && this.compare(this.heap[left], this.heap[largest])) | ||
{ | ||
largest = left; | ||
} | ||
|
||
if (right < size && this.compare(this.heap[right], this.heap[largest])) | ||
{ | ||
largest = right; | ||
} | ||
|
||
if (largest != index) | ||
{ | ||
// swap | ||
let tmp = this.heap[index]; | ||
this.heap[index] = this.heap[largest]; | ||
this.heap[largest] = tmp; | ||
|
||
// recurse down | ||
this.downHeap(largest); | ||
} | ||
} | ||
|
||
heapify() | ||
{ | ||
// start from middle | ||
for (let i = Math.trunc((this.length / 2) - 1); i >= 0; --i) | ||
{ | ||
this.downHeap(i); | ||
} | ||
} | ||
|
||
get length() | ||
{ | ||
return this.heap.length; | ||
} | ||
} |