-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_1_binary-search-tree.js
More file actions
47 lines (38 loc) · 895 Bytes
/
Copy path02_1_binary-search-tree.js
File metadata and controls
47 lines (38 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { performance } = require('perf_hooks');
const startingTime = performance.now();
// Start of code
class BinarSearchTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
BinarSearchTree.prototype.add = function(value) {
if (value <= this.value) {
if (!this.left) {
this.left = new BinarSearchTree(value);
} else {
this.left.add(value);
}
} else {
if (!this.right) {
this.right = new BinarSearchTree(value);
} else {
this.right.add(value);
}
}
};
let arr = [50, 100, 40, 12, 90, 98];
let BST;
for (let i = 0; i < arr.length; i++) {
if (i === 0) {
BST = new BinarSearchTree(arr[0]);
} else {
BST.add(arr[i]);
}
}
console.log(BST);
// End of code
const endingTime = performance.now();
console.log('Function took ' + (endingTime - startingTime) + ' milliseconds.');