Skip to content

Commit

Permalink
xcode license
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzuber committed Sep 24, 2016
1 parent 9c0f3a0 commit 7a7278b
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bin/needle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/scripts/needle.min.js

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions examples/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
<script type="text/javascript" src="needle.min.js"></script>
<script>

var heap = new Needle.BinaryHeap(function(a, b){
return (a > b);
});
var tree = new Needle.BinarySearchTree();

heap.heapify([1, 2, 3, 4, 7, 8, 9, 10, 14, 16]);

window.heap = heap;
tree.insert(2);
tree.insert(1);
tree.insert(4);
tree.insert(3);
tree.insert(5);

console.log(JSON.stringify(tree, null, 4));

tree.delete(4);

console.log(JSON.stringify(tree, null, 4));

window.tree = tree;

</script>
</body>
</html>
</html>
6 changes: 4 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ var banner = ['/*!',

// Contacat & compress javascript files
gulp.task('dispatch', function(){
gulp.src(['src/*.js'])
.pipe(browserify({}))
gulp.src(['src/index.js'])
.pipe(browserify({
entries: ['./index.js']
}))
.pipe(uglify())
.pipe(rename({
basename: 'needle',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/needle.js",
"scripts": {
"build": "gulp",
"dev": "gulp watch",
"dev": "gulp && gulp watch || true",
"bench": "node --harmony benchmarks/runTests.js || true",
"unit": "node --harmony unit/runTests.js || true",
"lint": "./node_modules/.bin/eslint src/*.js || true",
Expand Down
63 changes: 59 additions & 4 deletions src/BinarySearchTree/binarySearchTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
* | search | O(n) |
* +------------------------------+
*
* TODO: let user set a custom `equal` function
*
*/

'use strict';
'use strict';

const Node = require('../Nodes/bidirectional_tree_node.js');

const Node = require('../__Nodes__/bidirectional_tree_node.js');
const Types = {
LEFT: 'left',
RIGHT: 'right'
}

// @TODO: let user set a custom `compare` function
/** @private @default
* Compares two elements and returns.
* @param {number} first index to compare
Expand Down Expand Up @@ -206,6 +210,57 @@ BinarySearchTree.prototype.insert = function(data, node){
}
}

/**
* Deletes a node from the tree with the value of `data`.
* @param {any} data The data of the node to delete.
* @return {boolean} Returns the success of the deletion success.
*/
BinarySearchTree.prototype.delete = function (data) {
deleteHelper = deleteHelper.bind(this);
return deleteHelper(data, this.root, null, null);
}

/**
* Deletes a node from the tree with the value of `data`.
* @param {any} data The data of the node to delete.
* @param {Node} node The current node being analyzed.
* @param {Node} nodeType The type of child of the current node being analyzed.
* @param {Node} parentNode The last node that was analyzed.
* @return {boolean} Returns the success of the deletion success.
*/
function deleteHelper (data, node, nodeType, parentNode) {
if (node === null) {
return false;
}

// @TODO handle object comparisons -- doing a stringify is horrible dont do that
if (data === node.data) {
if (nodeType === Types.RIGHT) {
parentNode.right = null;
} else {
parentNode.left = null;
}

// Fix tree
if (node.left) {
var nodeRightSubtree = node.right;
parentNode[nodeType] = node.left;
parentNode[nodeType].right = nodeRightSubtree;
} else if (node.right) {
var nodeLeftSubtree = node.left;
parentNode[nodeType] = node.right;
parentNode[nodeType].left = nodeLeftSubtree;
}
return true;
}

if (safeCompare(data, node.data, this.compare)) {
return deleteHelper(data, node.left, Types.LEFT, node);
} else {
return deleteHelper(data, node.right, Types.RIGHT, node);
}
}

/**
* Search for a node in the tree.
* @param {*} the data of the node being searched for
Expand Down
2 changes: 1 addition & 1 deletion src/DoublyLinkedList/doublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

'use strict';

const Node = require('../__Nodes__/bidirectional_node.js');
const Node = require('../Nodes/bidirectional_node.js');

/**
* Single argument constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/KaryTree/karyTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

'use strict';

const Node = require('../__Nodes__/multidirectional_tree_node.js');
const Node = require('../Nodes/multidirectional_tree_node.js');

/**
* Creates an empty k-ary tree.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Queue/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

const Node = require('../__Nodes__/unidirectional_node.js');
const Node = require('../Nodes/unidirectional_node.js');

/**
* Single argument constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/SinglyLinkedList/singlyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

'use strict';

const Node = require('../__Nodes__/unidirectional_node.js');
const Node = require('../Nodes/unidirectional_node.js');

/**
* Single argument constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Stack/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const Node = require('../__Nodes__/unidirectional_node.js');
const Node = require('../Nodes/unidirectional_node.js');

/**
* Single argument constructor.
Expand Down

0 comments on commit 7a7278b

Please sign in to comment.