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 1ffca52

Browse files
Binary search tree
1 parent 258d700 commit 1ffca52

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎binarySearchTree.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function BST(value) {
2+
this.value = value;
3+
this.left = null;
4+
this.right = null;
5+
}
6+
7+
BST.prototype.insert = function(value) {
8+
if (value <= this.value) {
9+
if (!this.left) {
10+
this.left = new BST(value);
11+
} else {
12+
this.left.insert(value);
13+
}
14+
} else if (value > this.value) {
15+
if (!this.right) {
16+
this.right = new BST(value);
17+
} else {
18+
this.right.insert(value);
19+
}
20+
}
21+
};
22+
23+
let bst = new BST(50);
24+
25+
bst.insert(30);
26+
bst.insert(70);
27+
bst.insert(100);
28+
bst.insert(60);
29+
bst.insert(59);
30+
bst.insert(20);
31+
bst.insert(45);
32+
bst.insert(35);
33+
bst.insert(85);
34+
bst.insert(105);
35+
bst.insert(10);
36+
37+
console.log(bst.right.right);

0 commit comments

Comments
(0)

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