Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branches - Brianna #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
18 changes: 0 additions & 18 deletions README.md

This file was deleted.

25 changes: 25 additions & 0 deletions heapsort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// const MinHeap = require('./minheap.js')
const { MinHeap } = require('./minheap');
// This method uses a heap to sort an array.
// Time Complexity: O(n log n) - add is log n, adding n times + remove is log n --> O(2(n log n)) -> O(n log n)
// Space Complexity: O(n)

function heapsort(list) {
let myHeap = new MinHeap;

for (let i = 0; i < list.length; i++) {
myHeap.add(list[i]);
}

let result = [];
while (!myHeap.isEmpty()) {
let node = myHeap.remove();
result.push(node)
}

return result;
};

module.exports = heapsort;

console.log(heapsort([5, 27, 3, 16, -50]));
8 changes: 0 additions & 8 deletions lib/heapsort.js

This file was deleted.

72 changes: 0 additions & 72 deletions lib/minheap.js

This file was deleted.

149 changes: 149 additions & 0 deletions maxheap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
class HeapNode {
constructor(key, value) {
this.key = key;
this.value = value;
}
}

class MaxHeap {
constructor() {
this.store = [];
this.size = 0;
}

// This method adds a HeapNode instance to the heap
// Time Complexity: O(log n)
// Space Complexity: O(1)
add(key, value) {
let newNode = new HeapNode(key, value);
this.store.push(newNode);

let current = this.store.length - 1;
this.heapUp(current);
this.size++;
}

// This method removes and returns an element from the heap
// maintaining the heap structure
// Time Complexity: O(log n)
// Space Complexity: O(1)
remove() {
let result;

if (this.size === 0) {
return result
}

if (this.size === 1) {
result = this.store.pop();
this.size--;
return result.value
}

this.swap(0, this.store.length - 1);
result = this.store.pop();
this.heapDown(0)

this.size--;
return result.value;
}


// Used for Testing
toString() {
if (!this.store.length) {
return "[]";
}

const values = this.store.map(item => item.value);
const output = `[${values.join(', ')}]`;
return output;
}

// This method returns true if the heap is empty
// Time complexity: O(1)
// Space complexity: O(1)
isEmpty() {
return this.store.length === 0
}

// This helper method takes an index and
// moves it up the heap, if it is less than it's parent node.
// It could be **very** helpful for the add method.
// Time complexity: O(log n) - n: nodes in heap
// Space complexity: O(1) - no recursive calls
heapUp(index) {

let parentIndex = index % 2 === 0 ? (index - 2) / 2 : (index - 1) / 2;

while( index > 0 && (this.store[parentIndex].key < this.store[index].key)) {
this.swap(index, parentIndex);
index = parentIndex;
parentIndex = index % 2 === 0 ? (index - 2) / 2 : (index - 1) / 2;
}
}

// This helper method takes an index and
// moves it up the heap if it's smaller
// than it's parent node.
// Time complexity: O(log n) - n: nodes in heap
// Space complexity: O(1) - no recursive calls
heapDown(index) {
// console.log(this.store.length)

let leftChild = index * 2 + 1;
let rightChild = index * 2 + 2;

while ( this.store[leftChild] || this.store[rightChild]) {
if (this.store[leftChild] === undefined) {
if (this.store[rightChild].key > this.store[index].key) {
this.swap(index, rightChild);
}
index = rightChild;
} else if (this.store[rightChild] === undefined) {
if (this.store[leftChild].key > this.store[index].key) {
this.swap(index, leftChild);
}
index = leftChild;
} else if (this.store[leftChild].key >= this.store[rightChild].key && this.store[leftChild].key > this.store[index].key) {
this.swap(index, leftChild);
index = leftChild;
} else if (this.store[rightChild].key > this.store[leftChild].key && this.store[rightChild].key > this.store[index].key) {
this.swap(index, rightChild);
index = rightChild;
} else {
return index
}

leftChild = index * 2 + 1;
rightChild = index * 2 + 2;
}
}

// If you want a swap method... you're welcome
swap(index1, index2) {
const s = this.store;
let temp = s[index1];
s[index1] = s[index2];
s[index2] = temp
// [s[index1], s[index2]] = [s[index2], s[index1]];
}
}

module.exports = {MaxHeap};


let myHeap = new MaxHeap;
myHeap.add(6,6);
myHeap.add(8,8);
myHeap.add(2,2);
myHeap.add(7,7);
myHeap.add(13,13);
console.log(myHeap.toString())
myHeap.remove();
myHeap.remove();
myHeap.remove();
myHeap.remove();
myHeap.remove();
myHeap.remove();
console.log(myHeap.toString())
Loading