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 a98602e

Browse files
Contains method
1 parent 1ffca52 commit a98602e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

‎binarySearchTree.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ BST.prototype.insert = function(value) {
2020
}
2121
};
2222

23+
BST.prototype.contains = function(value) {
24+
if (value === this.value) return true;
25+
else if (value < this.value) {
26+
if (!this.left) return false;
27+
else return this.left.contains(value);
28+
} else if (value > this.value) {
29+
if (!this.right) return false;
30+
else return this.right.contains(value);
31+
}
32+
};
33+
2334
let bst = new BST(50);
2435

2536
bst.insert(30);
@@ -34,4 +45,4 @@ bst.insert(85);
3445
bst.insert(105);
3546
bst.insert(10);
3647

37-
console.log(bst.right.right);
48+
console.log(bst.contains(30));

0 commit comments

Comments
(0)

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