Skip to content
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

added inorder using node #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 19 additions & 5 deletions src/bstree.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ buckets.BSTree = function (compareFunction) {
* Executes the provided function once per element present in the tree in in-order.
* @param {function(Object):*} callback Function to execute, invoked with an element as
* argument. To break the iteration you can optionally return false in the callback.
* @param {Object} element Indicates the node to start traversing from. If is undefined,
* the function starts traversing from the root
*/
tree.inorderTraversal = function (callback) {
tree.inorderTraversal = function (callback, element = undefined) {

function inorderRecursive(node, callback, signal) {
if (node === undefined || signal.stop) {
Expand All @@ -225,9 +227,21 @@ buckets.BSTree = function (compareFunction) {
inorderRecursive(node.rightCh, callback, signal);
}

inorderRecursive(root, callback, {
stop: false
});
if (element === undefined) {
inorderRecursive(root, callback, {
stop: false
});
} else {
var node = searchNode(root, element);
if (node === undefined) {
return node;
} else {
return inorderRecursive(node, callback, {
stop: false
});
}
}

};

/**
Expand Down Expand Up @@ -418,4 +432,4 @@ buckets.BSTree = function (compareFunction) {
};

return tree;
};
};
1 change: 0 additions & 1 deletion src/linkedlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ buckets.LinkedList = function () {
}
} else if (currentNode === lastNode) {
lastNode = previous;
previous.next = currentNode.next;
currentNode.next = undefined;
} else {
previous.next = currentNode.next;
Expand Down