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

Commit d1691c4

Browse files
Depth first traversal in-order
1 parent a98602e commit d1691c4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

‎binarySearchTree.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ BST.prototype.contains = function(value) {
3131
}
3232
};
3333

34+
BST.prototype.depthFirstTraversal = function(iteratorFunc) {
35+
if (this.left) this.left.depthFirstTraversal(iteratorFunc);
36+
iteratorFunc(this.value);
37+
if (this.right) this.right.depthFirstTraversal(iteratorFunc);
38+
};
39+
3440
let bst = new BST(50);
3541

3642
bst.insert(30);
@@ -45,4 +51,8 @@ bst.insert(85);
4551
bst.insert(105);
4652
bst.insert(10);
4753

48-
console.log(bst.contains(30));
54+
function log(value) {
55+
console.log(value);
56+
}
57+
58+
bst.depthFirstTraversal(log);

0 commit comments

Comments
(0)

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