forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (56 loc) · 1.63 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const Node = require('./Node');
const BSTUtils = require('./utils');
class BinarySearchTree {
constructor(value) {
if (!value) throw new Error('Root node value required');
this.root = new Node(value);
}
isEmpty() {
return this.root === null;
}
/** Layered methods to simplify the BST API using utils under the hood */
add(value) {
return BSTUtils.insert(this.root, value);
}
preorder() {
return BSTUtils.preorder(this.root, []);
}
postorder() {
return BSTUtils.postorder(this.root, []);
}
inorder() {
return BSTUtils.inorder(this.root, []);
}
search(value) {
return BSTUtils.search(this.root, value);
}
getMinimum() {
const minNode = BSTUtils.findMinNode(this.root);
return minNode.value;
}
getMaximum() {
const maxNode = BSTUtils.findMaxNode(this.root);
return maxNode.value;
}
remove(value) {
this.root = BSTUtils.delete(this.root, value);
}
}
// const bst = new BinarySearchTree(6);
// [4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
// const preorder = bst.preorder();
// console.log('Preorder Traversal - ', preorder);
// const inorder = bst.inorder();
// console.log('Inorder Traversal - ', inorder);
// const postorder = bst.postorder();
// console.log('Postorder Traversal - ', postorder);
// const search = 18;
// console.log(`Search for ${search}`, bst.search(search));
// const minNode = bst.getMinimum();
// console.log('Minimum value =>', minNode);
// const maxNode = bst.getMaximum();
// console.log('Maximum value =>', maxNode);
// bst.remove(4);
// console.log(bst.preorder());
// console.log(bst.root);
module.exports = BinarySearchTree;