Last active
April 1, 2017 20:22
-
-
Save C-Rodg/86b1d59349543b2a5edceee63912aabb to your computer and use it in GitHub Desktop.
An example of Trees implemented in Javascript. Using Depth-First Search (stack/DFS) and Breadth-First Search (queue/BFS).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Queue() { | |
this._storage = {}; | |
this._oldestIndex = 1; | |
this._newestIndex = 1; | |
} | |
Queue.prototype.size = function() { | |
return this._oldestIndex - this._newestIndex; | |
}; | |
Queue.prototype.enqueue = function(data) { | |
this._storage[this._newestIndex] = data; | |
this._newestIndex += 1; | |
}; | |
Queue.prototype.dequeue = function() { | |
if (this._oldestIndex !== this._newestIndex) { | |
const deletedData = this._storage[this._oldestIndex]; | |
delete this._storage[this._oldestIndex] | |
this._oldestIndex += 1; | |
return deletedData; | |
} | |
}; | |
function Node(data) { | |
this.data = data; | |
this.parent = null; | |
this.children = []; | |
} | |
function Tree(data) { | |
let node = new Node(data); | |
this._root = node; | |
} | |
Tree.prototype.traverseDF = function(callback) { | |
(function recurse(currentNode) { | |
for(let i = 0, j = currentNode.children.length; i < j; i++) { | |
recurse(currentNode.children[i]); | |
} | |
callback(currentNode); | |
})(this._root); | |
}; | |
Tree.prototype.traverseBF = function(callback) { | |
let queue = new Queue(); | |
queue.enqueue(this._root); | |
let currentTree = queue.dequeue(); | |
while(currentTree) { | |
for(let i = 0, j = currentTree.children.length; i < j; i++) { | |
queue.enqueue(currentTree.children[i]); | |
} | |
callback(currentTree); | |
currentTree = queue.dequeue(); | |
} | |
}; | |
Tree.prototype.contains = function(callback, traversal) { | |
traversal.call(this, callback); | |
}; | |
Tree.prototype.add = function(data, toData, traversal) { | |
let child = new Node(data), | |
parent = null, | |
callback = function(node) { | |
if (node.data = toData){ | |
parent = node; | |
} | |
}; | |
this.contains(callback, traversal); | |
if (parent) { | |
parent.children.push(child); | |
child.parent = parent; | |
} else { | |
throw new Error("Cannot add node to non-existent parent"); | |
} | |
}; | |
Tree.prototype.remove = function(data, fromData, traversal) { | |
let tree = this, | |
parent = null, | |
childToRemove = null, | |
index, | |
callback = function(node) { | |
if (node.data === fromData) { | |
parent = node; | |
} | |
}; | |
this.contains(callback, traversal); | |
if (parent) { | |
index = findIndex(parent.children, data); | |
if (index === undefined) { | |
throw new Error("Node to remove does not exist"); | |
} else { | |
childToRemove = parent.children.splice(index, 1); | |
} | |
} else { | |
throw new Error("Parent does not exist"); | |
} | |
return childToRemove; | |
}; | |
function findIndex(arr, data) { | |
let index; | |
for (let i = 0, j = arr.length; i < j; i++) { | |
if (arr[i].data === data) { | |
index = i; | |
} | |
} | |
return index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment