From 316210549c9582fed2e21572c84e4d7f2fc1262e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fode=CC=81=20Diop?= Date: Mon, 15 May 2017 11:12:51 -0700 Subject: [PATCH 1/7] Initial commit --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 74e5c30..82ee366 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,15 @@ Tests and implementations for common data structures. See the full list in the [data-structures.md](data-structures.md) file. -Base repository for the [Core Data Structures](http://jsdev.learnersguild.org/goals/128) goal. +Project Goal: [Core Data Structures - Basic] (http://jsdev.learnersguild.org/goals/156-Core_Data_Structures-Basic.html) + +Base repository for the [Core Data Structures](Core Data Structures--Basic) goal. + +Team Name: ten-seal + +Project Members: +[Jonathan Pool](https://github.com/jrpool) +[Fodé Diop](https://github.com/diop) ## Installation and Setup From 7cc5606d79beccc909eb7ce70fd1f4b85e08068b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fode=CC=81=20Diop?= Date: Mon, 15 May 2017 11:14:58 -0700 Subject: [PATCH 2/7] Fix README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 82ee366..2bc135c 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,16 @@ Tests and implementations for common data structures. See the full list in the [data-structures.md](data-structures.md) file. -Project Goal: [Core Data Structures - Basic] (http://jsdev.learnersguild.org/goals/156-Core_Data_Structures-Basic.html) +Project Goal: [Core Data Structures - Basic](http://jsdev.learnersguild.org/goals/156-Core_Data_Structures-Basic.html) Base repository for the [Core Data Structures](Core Data Structures--Basic) goal. Team Name: ten-seal Project Members: + [Jonathan Pool](https://github.com/jrpool) + [Fodé Diop](https://github.com/diop) ## Installation and Setup From 7e0175353059ac7233b7c70ff2fd818f661e4e00 Mon Sep 17 00:00:00 2001 From: Jonathan Pool Date: Mon, 15 May 2017 13:57:57 -0700 Subject: [PATCH 3/7] Added installation and setup to README file and deleted advanced section from data-structures file. --- README.md | 22 ++++++++++++- data-structures.md | 81 ---------------------------------------------- 2 files changed, 21 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 2bc135c..e0ba10b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,27 @@ Project Members: ## Installation and Setup -_Fill this out_ +0. These instructions presuppose that npm (https://nodejs.org/en/) is installed. + +1. Clone this repository into a local directory. + +2. In the local directory, install required dependencies (see package.json) by executing: + + npm i + +3. In the local directory, perform the provided tests by executing: + + npm test + +4. Install ESLint (http://eslint.org) by executing: + + npm install -gS eslint + + cd + + eslint --init + +5. Edit .eslintrc.json in your home directory to customize. ## Usage and Examples diff --git a/data-structures.md b/data-structures.md index 7b2e43d..8ea46d0 100644 --- a/data-structures.md +++ b/data-structures.md @@ -156,87 +156,6 @@ set.clone() // returns a cloned set. _Note: if you haven't worked with sets before, you may want to read about [sets](https://www.mathsisfun.com/sets/sets-introduction.html) and [subsets](https://www.mathsisfun.com/activity/subsets.html)._ -## Advanced Data Structures - -### Hash Table ( using a LinkedList for collision chaining ) - -Maps keys to values, like a dictionary or a phone book. Or an object in JavaScript... - -From [Wikipedia](https://en.wikipedia.org/wiki/Hash_table) [edited]: - -> A data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of _buckets_ or _slots_, from which the desired value can be found. - -Collision Chaining: [Wikipedia](http://www.cs.rmit.edu.au/online/blackboard/chapter/05/documents/contribute/chapter/05/chaining.html) -> Instead of storing the data directly inside the structure, have a linked list structure at each hash element. That way, all the collision, retrieval and deletion functions can be handled by the list, and the hash function's role is limited mainly to that of a guide to the algorithms, as to which hash element's list to operate on. - -```javascript -const ht = new HashTable() -ht.put("name", "Zanzibar") // adds a key-value pair to the hash table, deal with collisions using chaining -ht.get("name") // returns the data associated with key. -ht.contains("name") // returns true if the hash table contains the key. -ht.iterate((k, v) => console.log(`${k}: ${v}`)) // takes a callback function and passes it each key and value in sequence. -ht.remove("name") // removes a key-value pair by key. -ht.size() // returns the number of key-value pairs in the hash table. -HashTable.hash("name") // generates a hash for the key "name" -``` - -### Binary (Search) Tree - -A sorted binary tree for fast lookup, addition and removal of items. - -From [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree) [edited]: - -> A particular type of container that allows fast lookup, addition and removal of items, and can be used to implement either dynamic sets of items, or lookup tables that allow finding an item by its key (e.g., finding the phone number of a person by name). -> -> Binary search trees keep their keys in sorted order, so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, based on the comparison, to continue searching in the left or right subtrees. - -```javascript -const bst = new BinarySearchTree() -bst.insert(3) // inserts a node with the specified value into the tree. -bst.search(3) // returns a node object or null if not found. -bst.remove(3) // removes an value's node (if exists) from the tree. -bst.traverse((val) => console.log(val)) // traverse the tree using in-order traversal and apply function on each node's value. -bst.count() // return the number of nodes in the tree. -``` - -#### Tree Node - -To implement a _standard_ binary search tree, use a **tree node** data structure in your implementation. Use this interface as a reference: - -```javascript -const leastNode = new TreeNode({data: 3}) -const moreNode = new TreeNode({data: 10}) -const midNode = new TreeNode({data: 7, left: leastNode, right: moreNode}) - -midNode.getData() // returns the node's data -midNode.getLeft() // returns the left node or null if none -midNode.setLeft(leastNode) // changes the reference to the left node and returns the original node -midNode.getRight() // returns the right node or null if none -midNode.setRight(moreNode) // changes the reference to the right node and returns the original node -``` - -### Directed Graph - -Nodes connected by vertices with a direction. - -From [Wikipedia](https://en.wikipedia.org/wiki/Directed_graph) [edited]: - -> A graph (that is a set of vertices connected by edges), where the edges have a direction associated with them. - -```javascript -const diGraph = new DirectedGraph() -diGraph.addVertex('v1') // adds a vertex to the graph. -diGraph.hasVertex('v1') // returns true if the graph contains the vertex or false if not. -diGraph.addDirection('v1', 'v2') // adds a direction from 'v1' to 'v2'. -diGraph.hasDirection('v1', 'v2') // returns true if there's a direction from 'v1' to 'v2'. -diGraph.visit('v1', vertex => console.log(vertex)) // visit all the connected vertices in the graph starting with v1 and apply function on the reached vertex. -diGraph.findPaths('v1', 'v2') // returns an array of all the paths between two vertices. -diGraph.removeDirection('v1', 'v2') // removes an existing direction between 'v1' and 'v2'. -diGraph.getSeparatedVertices() // returns an array of all the vertices that are unconnected to the graph (have no direction linking them to another vertex). -diGraph.removeVertex('v1') // removes an existing vertex and all its directions (the incoming and outgoing). -diGraph.count() // returns the number of vertices in the graph. -``` - ### Sources Most of the above was shamelessly borrowed from Wikipedia and these libraries: From 99dafb767910699a650cc02c717c556d5970e305 Mon Sep 17 00:00:00 2001 From: Jonathan Pool Date: Mon, 15 May 2017 17:03:17 -0700 Subject: [PATCH 4/7] Wrote and began testing PriorityNode class. --- .gitignore | 1 + package.json | 15 +++++++++--- spec/priority_node.js | 23 ++++++++++++++++++ spec/{stack.js => stack.js-template} | 0 src/priority_node.js | 36 ++++++++++++++++++++++++++++ src/{stack.js => stack.js-template} | 0 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 spec/priority_node.js rename spec/{stack.js => stack.js-template} (100%) create mode 100644 src/priority_node.js rename src/{stack.js => stack.js-template} (100%) diff --git a/.gitignore b/.gitignore index c2658d7..2752eb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.DS_Store diff --git a/package.json b/package.json index d47a351..158a12c 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,24 @@ { "name": "core-data-structures", - "description": "Tests and implementations for common data structures.", + "description": "Tests and implementations for basic core data structures.", "private": false, "version": "0.0.0", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/diop/core-data-structures" + }, + "dependencies": { + "babel-preset-es2015": "^6.24.1", + "babel-register": "^6.24.1" + }, "devDependencies": { "babel-cli": "^6.18.0", "babel-preset-env": "^1.1.4", "babel-register": "^6.18.0", - "chai": "~1.8.0", + "chai": "^1.8.1", "chai-change": "^2.1.2", - "mocha": "2.0.1" + "mocha": "^2.0.1" }, "scripts": { "build": "babel src -d lib", diff --git a/spec/priority_node.js b/spec/priority_node.js new file mode 100644 index 0000000..73eb5d1 --- /dev/null +++ b/spec/priority_node.js @@ -0,0 +1,23 @@ +import chai, { expect } from 'chai' +import chaiChange from 'chai-change' +import PriorityNode from '../src/priority_node' + +chai.use(chaiChange) + +describe('PriorityNode', () => { + 'use strict' + + it('is a function', () => { + expect(PriorityNode).to.be.a('function') + }) + + context('getData()', () => { + it('returns a string when given detData().', () => { + const levelZero = new PriorityNode( + {data: "level0", priority: 0} + ) + const data = levelZero.getData() + expect(data).to.be.a('string') + }) + }) +}) diff --git a/spec/stack.js b/spec/stack.js-template similarity index 100% rename from spec/stack.js rename to spec/stack.js-template diff --git a/src/priority_node.js b/src/priority_node.js new file mode 100644 index 0000000..4bf3de5 --- /dev/null +++ b/src/priority_node.js @@ -0,0 +1,36 @@ +'use strict' + +/* + Class declaration for PriorityNode and export statement making that + object the default export from this module. +*/ +export default class PriorityNode { + constructor(nodeProps) { + this.data = nodeProps.data; + this.priority = nodeProps.priorty; + this.next = nodeProps.next; + } + // Returns the node’s data. + getData() { + return this.data; + } + // Returns the node’s priority. + getPriority() { + return this.priority; + } + // Returns the next node, or null if none. + getNext() { + return this.next || null; + } + // Changes the node’s priority and returns the post-change node. + setPriority(newPriority) { + this.priority = newPriority; + return this; + } + // Changes the next node and returns the pre-change node. + setNext(newNext) { + const origThis = this; + this.next = newNext; + return origThis; + } +} diff --git a/src/stack.js b/src/stack.js-template similarity index 100% rename from src/stack.js rename to src/stack.js-template From 974bf6c8521699b633314f6d091a6741fd2e9675 Mon Sep 17 00:00:00 2001 From: Jonathan Pool Date: Mon, 15 May 2017 18:05:11 -0700 Subject: [PATCH 5/7] Added tests and organized them into contexts. --- spec/priority_node.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/spec/priority_node.js b/spec/priority_node.js index 73eb5d1..74ed6d2 100644 --- a/spec/priority_node.js +++ b/spec/priority_node.js @@ -12,12 +12,25 @@ describe('PriorityNode', () => { }) context('getData()', () => { - it('returns a string when given detData().', () => { - const levelZero = new PriorityNode( - {data: "level0", priority: 0} - ) + it('returns the correct type, if string', () => { + const levelZero = new PriorityNode({data: "level0", priority: 0}) const data = levelZero.getData() expect(data).to.be.a('string') }) + it('returns the correct value, if a string', () => { + const levelZero = new PriorityNode({data: "level0", priority: 0}) + const data = levelZero.getData() + expect(data).to.be.equal('level0') + }) + it('returns the correct type, if number', () => { + const levelZero = new PriorityNode({data: 0, priority: 0}) + const data = levelZero.getData() + expect(data).to.be.a('number') + }) + it('returns the correct value, if a number', () => { + const levelZero = new PriorityNode({data: 0, priority: 0}) + const data = levelZero.getData() + expect(data).to.be.equal(0) + }) }) }) From 4c1716ef497216ecbdc988f0bac872a0c2108ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fode=CC=81=20Diop?= Date: Mon, 15 May 2017 23:24:04 -0700 Subject: [PATCH 6/7] add node class --- src/node.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/node.js diff --git a/src/node.js b/src/node.js new file mode 100644 index 0000000..76e2d7b --- /dev/null +++ b/src/node.js @@ -0,0 +1,36 @@ +// Node -- A very basic data structure that can contain some value and a reference to another node. +function Node(data, next){ + this.data = data; + this.next = next; +}; + +// returns the data of the node +Node.prototype.getData = function(){ + console.log(this.data); +}; + +// returns the next node, or null if no next node +Node.prototype.getNext = function(){ + if (this.next === null){ + return null; + } else { + console.log(this.next); + }; +}; + +// changes the reference to the next node and returns the original node +Node.prototype.setNext = function(element){ + this.next = element; + return this; +}; + +// const head = new Node(0, null); +const nodeA = new Node("apple", null); +const nodeB = new Node("banana", null); + +nodeA.getData(); +nodeA.setNext(nodeB); +nodeA.next = nodeB; +nodeA.getNext(); +// nodeB.next.getData(); +// NodeA.data.getData(); From 7fb584ce04aed2972370d485472081542a988e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fode=CC=81=20Diop?= Date: Tue, 16 May 2017 01:31:03 -0700 Subject: [PATCH 7/7] add node test --- spec/nodeTest.js | 43 ++++++++++++++++++++++++++++++++++++++ src/node.js | 54 +++++++++++++++++++++--------------------------- 2 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 spec/nodeTest.js diff --git a/spec/nodeTest.js b/spec/nodeTest.js new file mode 100644 index 0000000..4fe610d --- /dev/null +++ b/spec/nodeTest.js @@ -0,0 +1,43 @@ + +import chai, { expect } from 'chai' +import chaiChange from 'chai-change' +import Node from '../src/node' + +chai.use(chaiChange) + +describe('Node', () => { + 'use strict' + const nodeA = new Node( {data: 'apple'} ) + const nodeB = new Node( {data: 'banana'} ) + + it('is a function', () => { + expect(Node).to.be.a('function') + }) + + context('getData()', () => { + it('gets the data from the node.', () => { + expect( nodeA.getData() ).to.equal('apple') + }) + }) + + context('setNext()', () => { + it('points the reference to the next node.', () => { + expect( () => nodeA.setNext(nodeB) ).to.alter( () => nodeA.data.next, { from: undefined, to: nodeB } ) + }) + + it('returns the original.', () => { + expect( nodeA.setNext(nodeB) ).to.equal(nodeA) + }) + }) + + context('getNext()', () => { + it('returns the next node', () => { + nodeA.setNext(nodeB) + expect( nodeA.getNext() ).to.equal(nodeB) + }) + + it('returns null if no next node.', () => { + expect( nodeB.getNext() ).to.equal(null) + }) + }) +}) diff --git a/src/node.js b/src/node.js index 76e2d7b..3ef7bd6 100644 --- a/src/node.js +++ b/src/node.js @@ -1,36 +1,28 @@ -// Node -- A very basic data structure that can contain some value and a reference to another node. -function Node(data, next){ - this.data = data; - this.next = next; -}; +'use strict' -// returns the data of the node -Node.prototype.getData = function(){ - console.log(this.data); -}; +// A very basic data structure that can contain some value and a reference to another node. +export default class Node { + constructor(data) { + this.data = data + this.next = undefined + } -// returns the next node, or null if no next node -Node.prototype.getNext = function(){ - if (this.next === null){ - return null; - } else { - console.log(this.next); - }; -}; +// returns the data ("apple") of the node + getData() { + return this.data.data + } // changes the reference to the next node and returns the original node -Node.prototype.setNext = function(element){ - this.next = element; - return this; -}; + setNext(element) { + this.data.next = element + return this + } -// const head = new Node(0, null); -const nodeA = new Node("apple", null); -const nodeB = new Node("banana", null); - -nodeA.getData(); -nodeA.setNext(nodeB); -nodeA.next = nodeB; -nodeA.getNext(); -// nodeB.next.getData(); -// NodeA.data.getData(); + // returns the next node, or null if no next node + getNext() { + if (this.data.next) { + return this.data.next + } + return null + } +}