Skip to content
Closed
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
77 changes: 77 additions & 0 deletions Data Structures/BasicDataStructures/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Hash Table */

var hash = (string, max) => {
var hash = 0;
for (var i = 0; i < string.length; i++) {
hash += string.charCodeAt(i);
}
return hash % max;
};

let HashTable = function() {

let storage = [];
const storageLimit = 4;

this.print = function() {
console.log(storage)
}

this.add = function(key, value) {
var index = hash(key, storageLimit);
if(storage[index] === undefined) {
storage[index] = [
[key, value]
];
} else {
var inserted = false;
for(var i = 0; i < storage[index].length; i++) {
if(storage[index][i][0] === key) {
sotrage[index][i][1] = value;
inserted = true;
}
}
if(inserted === false) {
storage[index].push([key, value]);
}
}
};

this.remove = function(key) {
var index = hash(key, storageLimit);
if(storage[index].length === 1 && storage[index][0][0] === key) {
delete storage[index];
} else {
for(var i = 0; i < storage[index]; i++) {
if(storage[index][i][0] === key) {
delete storage[index][i];
}
}
}
};

this.lookup = function(key) {
var index = hash(key, storageLimit);
if(storage[index] === undefined) {
return undefined;
} else {
for(var i = 0; i < storage[index].length; i++) {
if(storage[index][i][0] == key) {
return storage[index][i][1];
}
}
}
};

};


console.log(hash('quincy', 10));

let ht = new HashTable();
ht.add('beau', 'person');
ht.add('fido', 'dog');
ht.add('rex', 'dinosour');
ht.add('tux', 'penguin');
console.log(ht.lookup('tux'))
ht.print();
67 changes: 67 additions & 0 deletions Data Structures/BasicDataStructures/heap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Heaps */

let MinHeap = function() {

let heap = [null];

this.insert = function(num) {
heap.push(num);
if(heap.length > 2) {
let idx = heap.length - 1;
while(heap[idx] < heap[Math.floor(idx/2)]) {
if(idx >= 1) {
[heap[Math.floor(idx/2)], heap[idx]] = [heap[idx], heap[Math.floor(idx/2)]];
if(Math.floor(idx/2) > 1) {
idx = Math.floor(idx/2);
} else {
break;
};
};
};
};
};

this.remove = function() {
let smallest = heap[1];
if (heap.length > 2) {
heap[1] = heap[heap.length - 1];
heap.splice(heap.length -1);
if(heap.length == 3) {
if(heap[1] > heap[2]) {
[heap[1], heap[2]] = [heap[2], heap[1]];
};
return smallest;
};
let i = 1;
let left = 2*i;
let right = 2*i+1;
while(heap[i] >= heap[left] || heap[i] >= heap[right]) {
if(heap[left] < heap[right]) {
[heap[i], heap[left]] = [heap[left], heap[i]];
i = 2*i;
} else {
[heap[i], heap[right]] = [heap[right], heap[i]];
i = 2*i+1;
};
left = 2*i;
right = 2*i+1;
if(heap[left] == undefined || heap[right] == undefined) {
break;
};
};
} else if(heap.length == 2) {
heap.splice(1, 1);
} else {
return null;
};
return smallest;
};

this.sort = function() {
let result = new Array();
while(heap.length > 1) {
result.push(this.remove());
};
return result;
};
};
139 changes: 139 additions & 0 deletions Data Structures/BasicDataStructures/linkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* LinkedList */

function LinkedList() {
var length = 0;
var head = null;

var Node = function(element) {
this.element = element;
this.next = null;
};

this.size = function(){
return length;
};

this.head = function(){
return head;
};

this.add = function(element) {
var node = new Node(element);
if(head === null) {
head = node;
} else {
currentNode = head;

while(currentNode.next) {
currentNode = currentNode.next;
}

currentNode.next = node;
}

length++;
};

this.remove = function(element) {
var currentNode = head;
var previousNode;
if(currentNode.element === element) {
head = currentNode.next;
} else {
while(currentNode.element !== element) {
previousNode = currentNode;
currentNode = currentNode.next;
}

previousNode.next = currentNode.next;
}

length--;
};

this.isEmpty = function() {
return length === 0;
};

this.indexOf = function(element) {
var currentNode = head;
var index = -1;

while(currentNode){
index++;
if(currentNode.element === element){
return index;
}
currentNode = currentNode.next;
}

return -1;
};

this.elementAt = function(index) {
var currentNode = head;
var count = 0;
while(count < index) {
count++;
currentNode = currentNode.next;
}
return currentNode.element;
};

this.addAt = function(index, element) {
var node = new Node(element);

var currentNode = head;
var previousNode;
var currentIndex = 0;

if(index > length) {
return false;
}

if(index === 0) {
node.next = currentNode;
head = node;
} else {
while(currentIndex < index) {
currentIndex++;
previousNode = currentNode;
currentNode = currentNode.next;
}
node.next = currentNode;
previousNode.next = node;
}
length++;
}

this.removeAt = function(index) {
var currentNode = head;
var previousNode;
var currentIndex = 0;
if(index < 0 || index >= length) {
return null
}
if(index === 0) {
head = currentNode.next;
} else {
while(currentIndex < index) {
currentIndex++;
previousNode = currentNode;
currentNode = currentNode.next;
}
previousNode.next = currentNode.next;
}
length--;
return currentNode.element;
}
}

var conga = new LinkedList();
conga.add('Kitten');
conga.add('Puppy');
conga.add('Dog');
conga.add('Cat');
conga.add('Fish');
console.log(conga.size());
console.log(conga.removeAt(3));
console.log(conga.size());
45 changes: 45 additions & 0 deletions Data Structures/BasicDataStructures/priorityQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function PriorityQueue() {
var collection = [];
this.printCollection = function() {
(console.log(collection));
};
this.enqueue = function(element) {
if(this.isEmpty()) {
collection.push(element);
} else {
var added = false;
for (var i = 0; i < collection.length; i++) {
if(element[i] < collection[i][i]) { // checking priorities
collection.splice(i, 0, element);
added = true;
break;
}
}
if(!added) {
collection.push(element);
}
}
};
this.dequeue = function() {
var value = collection.shift();
return value[0];
};
this.front = function() {
return collection[0];
};
this.size = function() {
return collection.length;
};
this.isEmpty = function() {
return (collection.length === 0);
};
}

var pq = new PriorityQueue();
pq.enqueue(['Beau Caranes', 2]);
pq.enqueue(['Quincy Larson', 3]);
pq.enqueue(['Ewa Mitulska-Wójcik', 1]);
pq.printCollection();
pq.dequeue();
pq.front();
pq.printCollection();
32 changes: 32 additions & 0 deletions Data Structures/BasicDataStructures/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Queue */

function Queue() {
collection = [];
this.print = function() {
console.log(collection);
};
this.enqueue = function(element) {
collection.push(element);
};
this.dequeue = function() {
return collection.shift();
};
this.front = function() {
return collection[0];
};
this.size = function() {
return collection.length;
};
this.isEmpty = function() {
return (collection.length === 0);
};
}

var q = new Queue();
q.enqueue('a');
q.enqueue('b');
q.enqueue('c');
q.print();
q.dequeue();
console.log(q.front());
q.print();
Loading