Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e755882
finished adding autocmplete, left over from last week
chelseadole Dec 12, 2017
c1a9d65
adding two new tests to autocmplete in trie.
chelseadole Dec 12, 2017
ceba63c
one more test
chelseadole Dec 12, 2017
6ebce10
assert StopIteration when done.
chelseadole Dec 12, 2017
9cae6ed
adding error handling for non string input.
chelseadole Dec 12, 2017
27d365c
last insertion sort stuff.
chelseadole Dec 13, 2017
7658d49
finished insertion sort.
chelseadole Dec 14, 2017
917a87a
renaming insertion sort files to use underscore.
chelseadole Dec 14, 2017
aa86758
adding first tests for insertion sort.
chelseadole Dec 14, 2017
3ff4b28
changing test paramerization.
chelseadole Dec 14, 2017
5364b43
adding last test for randomized long lsts.
chelseadole Dec 14, 2017
40f09c3
adding test to check for negative numbers.
chelseadole Dec 14, 2017
1f40505
adding to README.
chelseadole Dec 14, 2017
591ff2e
adding timeit and if name is main to insertion sort.
chelseadole Dec 14, 2017
63d10db
creating files for LL and DLL in js-src file
chelseadole Dec 14, 2017
f406f80
adding constructor to LL.
chelseadole Dec 15, 2017
7145bdc
added semi colon bois
chelseadole Dec 15, 2017
94ac35e
adding more curlybois and adding push and pop fns.
chelseadole Dec 15, 2017
842cc34
added pop function completed.
chelseadole Dec 15, 2017
826cd56
added size() on LL and DLL
chelseadole Dec 15, 2017
43bdd12
adding remove and serach to DLL and LL.
chelseadole Dec 15, 2017
9505bd5
more remove method work.
chelseadole Dec 15, 2017
1c63770
adding more methods to DLL, fixing tail method of LL.
chelseadole Dec 15, 2017
e51d0d5
adding first two sketchy af chai tests.
chelseadole Dec 15, 2017
300e9d4
adding more tests to js-linked-list.
chelseadole Dec 15, 2017
fd73506
adding more to linked-list test.
chelseadole Dec 15, 2017
29004cf
adding node_modules to gitignore and also more test bois
chelseadole Dec 15, 2017
27073c0
fixing tests for linked-list in JS.
chelseadole Dec 15, 2017
b4503b2
fixing syntax of JS errors.
chelseadole Dec 15, 2017
8948b6f
broke some shit
chelseadole Dec 15, 2017
28037ac
all tests passing :squidab: :ho-ho-ho:
chelseadole Dec 15, 2017
bd158a0
adding changes to README.
chelseadole Dec 15, 2017
95993a7
changing throws to returns.
chelseadole Dec 15, 2017
33c5861
return not throw shit
chelseadole Dec 15, 2017
567ba98
changing dll tests
chelseadole Dec 15, 2017
759b2d9
adding module.exports, extra tests.
chelseadole Dec 15, 2017
4704bd6
trouble shooting remove() method to work with a one-node list.
chelseadole Dec 15, 2017
2818165
deleting semicolon bois
chelseadole Dec 16, 2017
f59a17a
fixing tests and throwing errors.
chelseadole Dec 16, 2017
0790f12
two extra empty files
chelseadole Dec 16, 2017
a98ced0
adding js-stack contents. Copying linked-list tests to edit.
chelseadole Dec 16, 2017
c07cfe9
fixing js-stack tests.
chelseadole Dec 16, 2017
b4fb3c2
adding last stack stuff.
chelseadole Dec 16, 2017
a70da4d
adding to README.
chelseadole Dec 16, 2017
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ ENV/

# mypy
.mypy_cache/
node_modules/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@

**Testing Tools**: pytest, pytest-cov

## Sorting Algorithms:

* **Insertion Sort** - *The insertion sort algorithm sorts a list of numbers by looping through each individual number in the list one by one, and (if they are smaller than the last index) swapping the two numbers. Unlike bubble sort, after this swap the sort continues to swap the number down into lower indexed positions until it finds the right position. The runtime for this function is at worst-case O(n^2), because in that scenario the list would be exactly the reverse of what it should be.*

## Data Structures:

* **Binary Search Tree** — *a BST is a "tree shaped" data structure containing nodes. Each node can have a maximum of two children or "leaves," and all node values are properly located based on its parents and siblings values. Nodes to the left of the "root"/head node have values smaller than the root. Those to the right have values larger than the root. There are no duplicate values.*

* **Trie Tree** - *a Trie Tree is a "tree shaped" data structure containing nodes with references to letters. These nodes string together (using each node's "children" and "parent" attriutes) to form words. This tree allows for quick lookup time of words, and is used for things such as word suggestion/auto-complete.*

* **Linked List (JavaScript)** - *A singly linked list is a data structure made of nodes, all of which have a single "next" pointer pointing from the head to the tail. The runtime of traversing the LL is O(n) because it grows proportionally with the length of the list.*

* **Stack (JavaScript)** - *A stack is much like a singly linked list. It can be visualized as a stack of cards, and it is a "last in, first out" data structure that pops and pushes to the same end (or 'top') of the structure. It has O(n) runtime.*

## Time Complexities:

* balance() = *This BST function returns the balance (or size difference) between the left and right parts of the tree. Its runtime is O(1), because it always takes the same amount of time to run regardless of tree size, and only performs simple subtraction.*
Expand Down
138 changes: 138 additions & 0 deletions js-src/js-doubly-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"use strict";

class Node {
constructor(data, nextNode=null, prevNode=null) {
this.data = data
this.nextNode = nextNode
this.prevNode = prevNode
}
}

class DLL {
constructor(iter=null) {
this.head = null
this.tail = null
this.counter = 0
if (Array.isArray(iter)) {
iter.forEach(item => this.push(item))
}
else if (iter !== null) {
throw 'LinkedList only takes arrays as inputs.'
}
}

push(val) {
this.head = new Node(val, this.head)
this.counter ++
if (this.size() === 1) {
this.tail = this.head
} else {
this.head.nextNode.prevNode = this.head
}
}

pop() {
if (this.size() === 0) {
throw 'Cannot pop() from empty DoublyLinkedList.'
}
this.counter --
var output = this.head.data
this.head = this.head.nextNode
if (this.head) {
this.head.prevNode = null
}
return output
}

size() {
return this.counter
}

search(val) {
var curr = this.head
while (curr.data !== val) {
if (curr.nextNode === null) {
throw 'This value is not in the LinkedList.'
}
curr = curr.nextNode
}
return curr
}

remove(val) {
var curr = this.head
var prev = null
while (curr) {
if (curr.data == val) {
this.counter --
if (curr == this.head) {
if (this.head == this.tail) {
this.head = null
this.tail = null
return curr.data
}
this.head = curr.nextNode
this.head.prevNode = null
return curr.data
}
prev.nextNode = curr.nextNode
curr.nextNode.prevNode = prev
return curr.data
}
if (curr.nextNode == null) {
throw 'Values not in the DoublyLinkedList cannot be removed.'
}
prev = curr
curr = curr.nextNode
}
return curr
}

append(val){
var firstTail = this.tail
this.tail = new Node(val, this.prevNode=this.tail)
this.counter ++
if(firstTail){
firstTail.nextNode = this.tail
} else {
this.head = this.tail
}
this.tail.nextNode = null
}

shift(){
if (!this.tail){
throw "Cannot shift() from empty DLL."
}
var output = this.tail.data
this.tail = this.tail.prevNode
if(this.tail) {
this.tail.nextNode = null
} else {
this.head = null
}
this.counter --
return output
}


display() {
var start_paren = "("
if (this.head === null) {
return '()'
}
var curr = this.head
while (curr) {
if (curr.nextNode === null) {
start_paren += curr.data + ')'
return start_paren
}
else {
start_paren += curr.data + ', '
curr = curr.nextNode
}
}
}
}

module.exports = DLL
95 changes: 95 additions & 0 deletions js-src/js-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

class Node {
constructor(data, nextNode=null) {
this.data = data;
this.nextNode = nextNode;
}
}

class LL {
constructor(iter=null) {
this.head = null;
this.counter = 0;
if (Array.isArray(iter)) {
iter.forEach(item => this.push(item));
}
else if (iter !== null) {
return 'LinkedList only takes arrays as inputs.';
}
}

push(val) {
this.head = new Node(val, this.head);
this.counter ++;
}

pop() {
if (this.counter === 0) {
return 'Cannot pop() from empty LinkedList.';
}
var output = this.head.data;
this.head = this.head.nextNode;
this.counter --;
return output;
}

size() {
return this.counter;
}

search(val) {
var curr = this.head;
while (curr.data !== val) {
if (curr.nextNode === null) {
return 'This value is not in the LinkedList.';
}
curr = curr.nextNode;
}
return curr;
}

remove(val) {
var curr = this.head;
if (this.head.data === val) {
this.head = this.head.nextNode;
this.counter --;
return val;
}
if (this.head === null) {
return 'Cannot remove vals from empty LinkedList.';
}
while (curr) {
if (curr.nextNode === null) {
return 'Values not in the LinkedList cannot be removed.';
}
else if (curr.nextNode.data === val) {
curr.nextNode = curr.nextNode.nextNode;
this.counter --;
return val;
}
curr = curr.nextNode;
}
}

display() {
var start_paren = '(';
if (this.head === null) {
return '()';
}
var curr = this.head;
while (curr) {
if (curr.nextNode === null) {
start_paren += curr.data + ')';
return start_paren;
}
else {
start_paren += curr.data + ', ';
curr = curr.nextNode;
}
}
}
}

module.exports = LL;

Empty file added js-src/js-que.js
Empty file.
28 changes: 28 additions & 0 deletions js-src/js-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

const LL = require("./js-linked-list")
// import {LL} from "js-linked-list.js"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't need this line get rid of it


class Stack {
constructor(iter=null){
this.linked = new LL()
if(Array.isArray(iter)){
iter.forEach(item => this.push(item))
}
}

size(){
return this.linked.size()
}

push(val){
this.linked.push(val)
}

pop(){
return this.linked.pop()
}

}

module.exports = Stack
17 changes: 17 additions & 0 deletions js-src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "js-data-structures",
"version": "1.0.0",
"description": "",
"main": "js-linked-list.js",
"dependencies": {
"chai": "^4.1.2"
},
"devDependencies": {
"mocha": "^4.0.1"
},
"scripts": {
"test": "mocha"
},
"author": "Ya bish",
"license": "ISC"
}
Loading