From 4890d84ddbc0d703510ac4a5ee129eb6f31a7935 Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Mon, 29 Jan 2018 20:03:33 -0800 Subject: [PATCH 1/7] tree complete --- package-lock.json | 88 +++++++++++++++++++++++++++++---------- package.json | 5 +-- src/binary-search-tree.js | 8 ++-- src/graph.js | 12 +++--- src/tree.js | 13 +++++- 5 files changed, 89 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 653b955..2eb9fde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -796,7 +796,8 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true }, "bcrypt-pbkdf": { "version": "1.0.1", @@ -821,6 +822,7 @@ "version": "1.1.8", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", + "dev": true, "requires": { "balanced-match": "1.0.0", "concat-map": "0.0.1" @@ -866,7 +868,8 @@ "builtin-modules": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", - "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=" + "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", + "dev": true }, "caller-path": { "version": "0.1.0", @@ -1006,7 +1009,8 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true }, "concat-stream": { "version": "1.6.0", @@ -1022,7 +1026,8 @@ "contains-path": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/contains-path/-/contains-path-0.1.0.tgz", - "integrity": "sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo=" + "integrity": "sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo=", + "dev": true }, "content-type-parser": { "version": "1.0.1", @@ -1193,6 +1198,7 @@ "version": "1.5.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", "integrity": "sha1-N53Ocw9hZvds76TmcHoVmwLFpvo=", + "dev": true, "requires": { "esutils": "2.0.2", "isarray": "1.0.0" @@ -1227,6 +1233,7 @@ "version": "1.3.1", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.1.tgz", "integrity": "sha1-+FWobOYa3E6GIcPNoh56dhLDqNw=", + "dev": true, "requires": { "is-arrayish": "0.2.1" } @@ -1439,6 +1446,7 @@ "version": "0.3.1", "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.1.tgz", "integrity": "sha512-yUtXS15gIcij68NmXmP9Ni77AQuCN0itXbCc/jWd8C6/yKZaSNXicpC8cgvjnxVdmfsosIXrjpzFq7GcDryb6A==", + "dev": true, "requires": { "debug": "2.6.8", "resolve": "1.4.0" @@ -1448,15 +1456,17 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.1.1.tgz", "integrity": "sha512-jDI/X5l/6D1rRD/3T43q8Qgbls2nq5km5KSqiwlyUbGo5+04fXhMKdCPhjwbqAa6HXWaMxj8Q4hQDIh7IadJQw==", + "dev": true, "requires": { "debug": "2.6.8", "pkg-dir": "1.0.0" } }, "eslint-plugin-import": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.7.0.tgz", - "integrity": "sha512-HGYmpU9f/zJaQiKNQOVfHUh2oLWW3STBrCgH0sHTX1xtsxYlH1zjLh8FlQGEIdZSdTbUMaV36WaZ6ImXkenGxQ==", + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.8.0.tgz", + "integrity": "sha512-Rf7dfKJxZ16QuTgVv1OYNxkZcsu/hULFnC+e+w0Gzi6jMC3guQoWQgxYxc54IDRinlb6/0v5z/PxxIKmVctN+g==", + "dev": true, "requires": { "builtin-modules": "1.1.1", "contains-path": "0.1.0", @@ -1680,6 +1690,7 @@ "version": "1.1.2", "resolved": "https://registry.npmjs.org/find-up/-/find-up-1.1.2.tgz", "integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=", + "dev": true, "requires": { "path-exists": "2.1.0", "pinkie-promise": "2.0.1" @@ -1744,7 +1755,8 @@ "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true }, "generate-function": { "version": "2.0.0", @@ -1839,7 +1851,8 @@ "graceful-fs": { "version": "4.1.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", - "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" + "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=", + "dev": true }, "growly": { "version": "1.3.0", @@ -1896,6 +1909,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/has/-/has-1.0.1.tgz", "integrity": "sha1-hGFzP1OLCDfJNh45qauelwTcLyg=", + "dev": true, "requires": { "function-bind": "1.1.1" } @@ -1945,7 +1959,8 @@ "hosted-git-info": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.5.0.tgz", - "integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg==" + "integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg==", + "dev": true }, "html-encoding-sniffer": { "version": "1.0.1", @@ -2045,7 +2060,8 @@ "is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", - "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=" + "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", + "dev": true }, "is-buffer": { "version": "1.1.5", @@ -2057,6 +2073,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz", "integrity": "sha1-VAVy0096wxGfj3bDDLwbHgN6/74=", + "dev": true, "requires": { "builtin-modules": "1.1.1" } @@ -2238,7 +2255,8 @@ "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", + "dev": true }, "isexe": { "version": "2.0.0", @@ -2796,6 +2814,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", "integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=", + "dev": true, "requires": { "graceful-fs": "4.1.11", "parse-json": "2.2.0", @@ -2807,6 +2826,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", + "dev": true, "requires": { "p-locate": "2.0.0", "path-exists": "3.0.0" @@ -2815,7 +2835,8 @@ "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true } } }, @@ -2827,7 +2848,8 @@ "lodash.cond": { "version": "4.5.2", "resolved": "https://registry.npmjs.org/lodash.cond/-/lodash.cond-4.5.2.tgz", - "integrity": "sha1-9HGh2khr5g9quVXRcRVSPdHSVdU=" + "integrity": "sha1-9HGh2khr5g9quVXRcRVSPdHSVdU=", + "dev": true }, "longest": { "version": "1.0.1", @@ -2898,6 +2920,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, "requires": { "brace-expansion": "1.1.8" } @@ -2956,6 +2979,7 @@ "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", "integrity": "sha512-9jjUFbTPfEy3R/ad/2oNbKtW9Hgovl5O1FvFWKkKblNXoN/Oou6+9+KKohPK13Yc3/TyunyWhJp6gvRNR/PPAw==", + "dev": true, "requires": { "hosted-git-info": "2.5.0", "is-builtin-module": "1.0.0", @@ -3094,12 +3118,14 @@ "p-limit": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.1.0.tgz", - "integrity": "sha1-sH/y2aXYi+yAYDWJWiurZqJ5iLw=" + "integrity": "sha1-sH/y2aXYi+yAYDWJWiurZqJ5iLw=", + "dev": true }, "p-locate": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", + "dev": true, "requires": { "p-limit": "1.1.0" } @@ -3120,6 +3146,7 @@ "version": "2.2.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz", "integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=", + "dev": true, "requires": { "error-ex": "1.3.1" } @@ -3134,6 +3161,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", "integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=", + "dev": true, "requires": { "pinkie-promise": "2.0.1" } @@ -3153,12 +3181,14 @@ "path-parse": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", - "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=" + "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=", + "dev": true }, "path-type": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-2.0.0.tgz", "integrity": "sha1-8BLMuEFbcJb8LaoQVMPXI4lZTHM=", + "dev": true, "requires": { "pify": "2.3.0" } @@ -3172,17 +3202,20 @@ "pify": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", - "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" + "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", + "dev": true }, "pinkie": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz", - "integrity": "sha1-clVrgM+g1IqXToDnckjoDtT3+HA=" + "integrity": "sha1-clVrgM+g1IqXToDnckjoDtT3+HA=", + "dev": true }, "pinkie-promise": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz", "integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=", + "dev": true, "requires": { "pinkie": "2.0.4" } @@ -3191,6 +3224,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-1.0.0.tgz", "integrity": "sha1-ektQio1bstYp1EcFb/TpyTFM89Q=", + "dev": true, "requires": { "find-up": "1.1.2" } @@ -3313,6 +3347,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", "integrity": "sha1-jvHAYjxqbbDcZxPEv6xGMysjaPg=", + "dev": true, "requires": { "load-json-file": "2.0.0", "normalize-package-data": "2.4.0", @@ -3323,6 +3358,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-2.0.0.tgz", "integrity": "sha1-a3KoBImE4MQeeVEP1en6mbO1Sb4=", + "dev": true, "requires": { "find-up": "2.1.0", "read-pkg": "2.0.0" @@ -3332,6 +3368,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", + "dev": true, "requires": { "locate-path": "2.0.0" } @@ -3509,6 +3546,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.4.0.tgz", "integrity": "sha512-aW7sVKPufyHqOmyyLzg/J+8606v5nevBgaliIlV7nUpVMsDnoBGV/cbSLNjZAg9q0Cfd/+easKVKQ8vOu8fn1Q==", + "dev": true, "requires": { "path-parse": "1.0.5" } @@ -3619,7 +3657,8 @@ "semver": { "version": "5.4.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.4.1.tgz", - "integrity": "sha512-WfG/X9+oATh81XtllIo/I8gOiY9EXRdv1cQdyykeXK17YcUW3EXUAi2To4pcH6nZtJPr7ZOpM5OMyWJZm+8Rsg==" + "integrity": "sha512-WfG/X9+oATh81XtllIo/I8gOiY9EXRdv1cQdyykeXK17YcUW3EXUAi2To4pcH6nZtJPr7ZOpM5OMyWJZm+8Rsg==", + "dev": true }, "set-blocking": { "version": "2.0.0", @@ -3684,6 +3723,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-1.0.2.tgz", "integrity": "sha1-SzBz2TP/UfORLwOsVRlJikFQ20A=", + "dev": true, "requires": { "spdx-license-ids": "1.2.2" } @@ -3691,12 +3731,14 @@ "spdx-expression-parse": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/spdx-expression-parse/-/spdx-expression-parse-1.0.4.tgz", - "integrity": "sha1-m98vIOH0DtRH++JzJmGR/O1RYmw=" + "integrity": "sha1-m98vIOH0DtRH++JzJmGR/O1RYmw=", + "dev": true }, "spdx-license-ids": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-1.2.2.tgz", - "integrity": "sha1-yd96NCRZSt5r0RkA1ZZpbcBrrFc=" + "integrity": "sha1-yd96NCRZSt5r0RkA1ZZpbcBrrFc=", + "dev": true }, "sprintf-js": { "version": "1.0.3", @@ -3774,7 +3816,8 @@ "strip-bom": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=" + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true }, "strip-json-comments": { "version": "2.0.1", @@ -4055,6 +4098,7 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.1.tgz", "integrity": "sha1-KAS6vnEq0zeUWaz74kdGqywwP7w=", + "dev": true, "requires": { "spdx-correct": "1.0.2", "spdx-expression-parse": "1.0.4" diff --git a/package.json b/package.json index 60e89dc..9be292f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "babel-jest": "^19.0.0", "eslint": "^3.17.1", "eslint-config-airbnb": "^14.1.0", - "eslint-plugin-import": "^2.2.0", + "eslint-plugin-import": "^2.8.0", "eslint-plugin-jsx-a11y": "^4.0.0", "eslint-plugin-react": "^6.10.3", "jest": "^19.0.2", @@ -30,7 +30,6 @@ }, "dependencies": { "babel-preset-es2015": "^6.24.0", - "eslint-config-airbnb-base": "^11.1.3", - "eslint-plugin-import": "^2.2.0" + "eslint-config-airbnb-base": "^11.1.3" } } diff --git a/src/binary-search-tree.js b/src/binary-search-tree.js index 262a838..ba1a9df 100644 --- a/src/binary-search-tree.js +++ b/src/binary-search-tree.js @@ -12,17 +12,17 @@ class BinarySearchTree { // assigns it to either the left or right subtree, // depending on its value insert(value) { - + this.value = value; } // Checks the binary search tree for the input target // Can be written recursively or iteratively contains(target) { - + this.target = target; } // Traverses the tree in a depth-first manner, i.e. from top to bottom // Applies the given callback to each tree node in the process depthFirstForEach(cb) { - + this.cb = cb; } // Traverses the tree in a breadth-first manner, i.e. in layers, starting // at the root node, going down to the root node's children, and iterating @@ -31,7 +31,7 @@ class BinarySearchTree { // You'll need the queue-helper file for this. Or could you roll your own queue // again. Whatever floats your boat. breadthFirstForEach(cb) { - + this.cb = cb; } } diff --git a/src/graph.js b/src/graph.js index 80eeb07..89697e7 100644 --- a/src/graph.js +++ b/src/graph.js @@ -40,18 +40,18 @@ class Graph { // Optionally accepts an array of other GraphNodes for the new vertex to be connected to // Returns the newly-added vertex addVertex(value, edges = []) { - + this.edges = value; } // Checks all the vertices of the graph for the target value // Returns true or false contains(value) { - + this.value = value; } // Checks the graph to see if a GraphNode with the specified value exists in the graph // and removes the vertex if it is found // This function should also handle the removing of all edge references for the removed vertex removeVertex(value) { - + this.value = value; } // Checks the two input vertices to see if each one references the other in their respective edges array // Both vertices must reference each other for the edge to be considered valid @@ -59,19 +59,19 @@ class Graph { // Note: You'll need to store references to each vertex's array of edges so that you can use // array methods on said arrays. There is no method to traverse the edge arrays built into the GraphNode class checkIfEdgeExists(fromVertex, toVertex) { - + this.fromVertex = this.toVertix; } // Adds an edge between the two given vertices if no edge already exists between them // Again, an edge means both vertices reference the other addEdge(fromVertex, toVertex) { - + this.fromVertex = this.toVertix; } // Removes the edge between the two given vertices if an edge already exists between them // After removing the edge, neither vertex should be referencing the other // If a vertex would be left without any edges as a result of calling this function, those // vertices should be removed as well removeEdge(fromVertex, toVertex) { - + this.fromVertex = this.toVertix; } } diff --git a/src/tree.js b/src/tree.js index b6b7908..e2d81ec 100644 --- a/src/tree.js +++ b/src/tree.js @@ -7,13 +7,22 @@ class Tree { } // Adds a new Tree node with the input value to the current Tree node addChild(value) { - + const node = new Tree(value); + this.children.push(node); } // Checks this node's children to see if any of them matches the given value // Continues recursively until the value has been found or all of the children // have been checked contains(value) { - + let result = false; + const transverse = (node) => { + if (node.value === value) result = true; + for (let i = 0; i < node.children.length; i++) { + transverse(node.children[i]); + } + }; + transverse(this); + return result; } } From 676d9bdb827a727c2bc01d38c05273ca4c9ae222 Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Mon, 29 Jan 2018 20:51:32 -0800 Subject: [PATCH 2/7] binary-tree --- src/binary-search-tree.js | 40 +++++++++++++++++++++++++++++++++++---- src/tree.js | 6 +++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/binary-search-tree.js b/src/binary-search-tree.js index ba1a9df..f87462e 100644 --- a/src/binary-search-tree.js +++ b/src/binary-search-tree.js @@ -1,3 +1,5 @@ +const Queue = require('./queue-helper'); + // https://msdn.microsoft.com/en-us/library/aa289150(v=vs.71).aspx /* eslint-disable global-require */ /* eslint-disable no-unused-vars */ @@ -12,17 +14,40 @@ class BinarySearchTree { // assigns it to either the left or right subtree, // depending on its value insert(value) { - this.value = value; + const node = new BinarySearchTree(value); + if (value < this.value) { + if (!this.left) this.left = node; + else this.left.insert(value); + return; + } + if (!this.right) this.right = node; + else this.right.insert(value); + return; } // Checks the binary search tree for the input target // Can be written recursively or iteratively contains(target) { - this.target = target; + if (this.value === target) return true; + else if (target < this.value) { + if (this.left) { + return this.left.contains(target); + } + } else if (target > this.value) { + if (this.right) { + return this.right.contains(target); + } + } + return false; } // Traverses the tree in a depth-first manner, i.e. from top to bottom // Applies the given callback to each tree node in the process depthFirstForEach(cb) { - this.cb = cb; + const traverse = (node) => { + cb(node.value); + if (node.left !== null) traverse(node.left); + if (node.right !== null) traverse(node.right); + }; + traverse(this); } // Traverses the tree in a breadth-first manner, i.e. in layers, starting // at the root node, going down to the root node's children, and iterating @@ -31,7 +56,14 @@ class BinarySearchTree { // You'll need the queue-helper file for this. Or could you roll your own queue // again. Whatever floats your boat. breadthFirstForEach(cb) { - this.cb = cb; + const queue = new Queue(); + queue.enqueue(this); + while (!queue.isEmpty()) { + const node = queue.dequeue(); + cb(node.value); + if (node.left) queue.enqueue(node.left); + if (node.right) queue.enqueue(node.right); + } } } diff --git a/src/tree.js b/src/tree.js index e2d81ec..b4a3cef 100644 --- a/src/tree.js +++ b/src/tree.js @@ -15,13 +15,13 @@ class Tree { // have been checked contains(value) { let result = false; - const transverse = (node) => { + const traverse = (node) => { if (node.value === value) result = true; for (let i = 0; i < node.children.length; i++) { - transverse(node.children[i]); + traverse(node.children[i]); } }; - transverse(this); + traverse(this); return result; } } From bafd37b3b119695e62480ab8ee778bfea7235d8f Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Tue, 30 Jan 2018 20:59:31 -0800 Subject: [PATCH 3/7] tree, graph, bst --- src/graph.js | 56 +++++++++++++++++++++++++++++++++++++++++++++------- src/tree.js | 14 ++++++------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/graph.js b/src/graph.js index 89697e7..770b5d6 100644 --- a/src/graph.js +++ b/src/graph.js @@ -34,45 +34,87 @@ class Graph { constructor() { this.vertices = []; } + // Wraps the input value in a new GraphNode and adds it to the array of vertices // If there are only two nodes in the graph, they need to be automatically // connected via an edge // Optionally accepts an array of other GraphNodes for the new vertex to be connected to // Returns the newly-added vertex addVertex(value, edges = []) { - this.edges = value; + const node = new GraphNode({ value, edges }); + if (edges.length > 0) { + for (let i = 0; i < edges.length; i++) { + this.addEdge(node, edges[i]); + } + } + this.vertices.push(node); + if (this.vertices.length === 2) { + this.addEdge(this.vertices[0], this.vertices[1]); + } + return node; } // Checks all the vertices of the graph for the target value // Returns true or false contains(value) { - this.value = value; + for (let i = 0; i < this.vertices.length; i++) { + if (this.vertices[i].value === value) return true; + } + return false; } // Checks the graph to see if a GraphNode with the specified value exists in the graph // and removes the vertex if it is found // This function should also handle the removing of all edge references for the removed vertex removeVertex(value) { - this.value = value; + const index = this.vertices.findIndex((node) => { + return node.value === value; + }); + if (index === -1) return; + const vertex = this.vertices.splice(index, 1)[0]; + const edges = vertex.edges; + for (let i = 0; i < edges.length; i++) { + this.removeEdge(vertex, edges[i]); + } } + // Checks the two input vertices to see if each one references the other in their respective edges array // Both vertices must reference each other for the edge to be considered valid // If only one vertex references the other but not vice versa, should not return true // Note: You'll need to store references to each vertex's array of edges so that you can use // array methods on said arrays. There is no method to traverse the edge arrays built into the GraphNode class checkIfEdgeExists(fromVertex, toVertex) { - this.fromVertex = this.toVertix; + const testSatisfier = this.vertices; + const fromEdges = fromVertex.edges; + const toEdges = toVertex.edges; + const fromIndex = fromEdges.findIndex((node) => { + return node.value === toVertex.value; + }); + const toIndex = toEdges.findIndex((node) => { + return node.value === fromVertex.value; + }); + return (fromIndex > -1) && (toIndex > -1); } // Adds an edge between the two given vertices if no edge already exists between them // Again, an edge means both vertices reference the other addEdge(fromVertex, toVertex) { - this.fromVertex = this.toVertix; + if (!this.checkIfEdgeExists(fromVertex, toVertex)) { + fromVertex.pushToEdges(toVertex); + toVertex.pushToEdges(fromVertex); + } } // Removes the edge between the two given vertices if an edge already exists between them // After removing the edge, neither vertex should be referencing the other // If a vertex would be left without any edges as a result of calling this function, those // vertices should be removed as well removeEdge(fromVertex, toVertex) { - this.fromVertex = this.toVertix; - } + if (this.checkIfEdgeExists(fromVertex, toVertex)) { + const fromEdges = fromVertex.edges; + const toEdges = toVertex.edges; + fromEdges.splice(toVertex, 1); + toEdges.splice(fromVertex, 1); + if (fromVertex.numberOfEdges === 0) this.removeVertex(fromVertex.value); + if (toVertex.numberOfEdges === 0) this.removeVertex(toVertex.value); + } + } } module.exports = Graph; diff --git a/src/tree.js b/src/tree.js index b4a3cef..1e4aac4 100644 --- a/src/tree.js +++ b/src/tree.js @@ -14,15 +14,13 @@ class Tree { // Continues recursively until the value has been found or all of the children // have been checked contains(value) { - let result = false; - const traverse = (node) => { - if (node.value === value) result = true; - for (let i = 0; i < node.children.length; i++) { - traverse(node.children[i]); + if (this.value === value) return true; + for (let i = 0; i < this.children.length; i++) { + if (this.children[i].contains(value)) { + return true; } - }; - traverse(this); - return result; + } + return false; } } From 6007737db4de4565f6bad7c5c835b6726932499c Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Wed, 31 Jan 2018 19:04:48 -0800 Subject: [PATCH 4/7] refactor graph --- src/graph.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/graph.js b/src/graph.js index 770b5d6..1876b31 100644 --- a/src/graph.js +++ b/src/graph.js @@ -85,13 +85,9 @@ class Graph { const testSatisfier = this.vertices; const fromEdges = fromVertex.edges; const toEdges = toVertex.edges; - const fromIndex = fromEdges.findIndex((node) => { - return node.value === toVertex.value; - }); - const toIndex = toEdges.findIndex((node) => { - return node.value === fromVertex.value; - }); - return (fromIndex > -1) && (toIndex > -1); + const fromExists = fromEdges.includes(toVertex); + const toExists = toEdges.includes(fromVertex); + return (fromExists && toExists); } // Adds an edge between the two given vertices if no edge already exists between them // Again, an edge means both vertices reference the other From 0b11f860c165649b148098269717965608641199 Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Wed, 31 Jan 2018 20:45:37 -0800 Subject: [PATCH 5/7] more graph refactoring --- src/graph.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/graph.js b/src/graph.js index 1876b31..84f4b13 100644 --- a/src/graph.js +++ b/src/graph.js @@ -42,10 +42,8 @@ class Graph { // Returns the newly-added vertex addVertex(value, edges = []) { const node = new GraphNode({ value, edges }); - if (edges.length > 0) { - for (let i = 0; i < edges.length; i++) { - this.addEdge(node, edges[i]); - } + for (let i = 0; i < edges.length; i++) { + this.addEdge(node, edges[i]); } this.vertices.push(node); if (this.vertices.length === 2) { From 74e4e8f75c05b809e4aefac6ff8c4fd7c909fb72 Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Thu, 1 Feb 2018 20:46:48 -0800 Subject: [PATCH 6/7] refactor 2 for-loops on graph --- src/binary-search-tree.js | 6 ++---- src/graph.js | 8 ++------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/binary-search-tree.js b/src/binary-search-tree.js index f87462e..2a3bca9 100644 --- a/src/binary-search-tree.js +++ b/src/binary-search-tree.js @@ -32,10 +32,8 @@ class BinarySearchTree { if (this.left) { return this.left.contains(target); } - } else if (target > this.value) { - if (this.right) { - return this.right.contains(target); - } + } else if (this.right) { + return this.right.contains(target); } return false; } diff --git a/src/graph.js b/src/graph.js index 84f4b13..0970ee4 100644 --- a/src/graph.js +++ b/src/graph.js @@ -42,9 +42,7 @@ class Graph { // Returns the newly-added vertex addVertex(value, edges = []) { const node = new GraphNode({ value, edges }); - for (let i = 0; i < edges.length; i++) { - this.addEdge(node, edges[i]); - } + edges.forEach(edge => this.addEdge(node, edge)); this.vertices.push(node); if (this.vertices.length === 2) { this.addEdge(this.vertices[0], this.vertices[1]); @@ -69,9 +67,7 @@ class Graph { if (index === -1) return; const vertex = this.vertices.splice(index, 1)[0]; const edges = vertex.edges; - for (let i = 0; i < edges.length; i++) { - this.removeEdge(vertex, edges[i]); - } + edges.forEach(edge => this.removeEdge(vertex, edge)); } // Checks the two input vertices to see if each one references the other in their respective edges array From cd862943d98a1826c268036533d010d635b2732b Mon Sep 17 00:00:00 2001 From: Lisa Cee Date: Mon, 19 Feb 2018 16:04:40 -0800 Subject: [PATCH 7/7] loose ends --- src/graph.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/graph.js b/src/graph.js index 0970ee4..b973d53 100644 --- a/src/graph.js +++ b/src/graph.js @@ -52,9 +52,7 @@ class Graph { // Checks all the vertices of the graph for the target value // Returns true or false contains(value) { - for (let i = 0; i < this.vertices.length; i++) { - if (this.vertices[i].value === value) return true; - } + if (this.vertices.find(vertice => vertice.value === value)) return true; return false; } // Checks the graph to see if a GraphNode with the specified value exists in the graph