Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Complete Implementation of a Tree #709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pseudonerd16 wants to merge 1 commit into TheAlgorithms:master from pseudonerd16:master
Closed
Changes from all commits
Commits
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
112 changes: 112 additions & 0 deletions Trees/TreeImplementation.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//Complete Implementation of a Tree
/* author Sreejita Roy (https://github.com/pseudonerd16)*/

function Node(data) {
this.data = data;
this.parent = null;
this.children = [];
}

function Tree(data) {
var node = new Node(data);
this._root = node;
}

Tree.prototype.traverseDF = function(callback) {

// this is a recurse and immediately-invoking function
(function recurse(currentNode) {
// step 2
for (var i = 0, length = currentNode.children.length; i < length; i++) {
// step 3
recurse(currentNode.children[i]);
}

// step 4
callback(currentNode);

// step 1
})(this._root);

};

Tree.prototype.traverseBF = function(callback) {
var queue = new Queue();

queue.enqueue(this._root);

currentTree = queue.dequeue();

while(currentTree){
for (var i = 0, length = currentTree.children.length; i < length; 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) {
var 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 a non-existent parent.');
}
};

Tree.prototype.remove = function(data, fromData, traversal) {
var tree = this,
parent = null,
childToRemove = null,
index;

var 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) {
var index;

for (var i = 0; i < arr.length; i++) {
if (arr[i].data === data) {
index = i;
}
}

return index;
}

AltStyle によって変換されたページ (->オリジナル) /