Skip to content

Commit 807668c

Browse files
committed
update: Added layered methods to simplify BST APIs
1 parent 0111f5d commit 807668c

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/_DataStructures_/Trees/BST/index.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,51 @@ class BinarySearchTree {
8686
isEmpty() {
8787
return this.root === null;
8888
}
89+
90+
/** Layered methods to simplify the BST API */
91+
92+
add(value) {
93+
return this.insert(this.root, value);
94+
}
95+
96+
traversePreorder() {
97+
return this.preorder(this.root);
98+
}
99+
100+
traversePostorder() {
101+
return this.postorder(this.root);
102+
}
103+
104+
traverseInorder() {
105+
return this.inorder(this.root);
106+
}
107+
108+
searchFor(value) {
109+
return this.search(this.root, value);
110+
}
89111
}
90112

91113
// const bst = new BinarySearchTree(6);
92114
// console.log(bst.root);
93-
// bst.insert(bst.root, 4);
94-
// bst.insert(bst.root, 9);
95-
// bst.insert(bst.root, 2);
96-
// bst.insert(bst.root, 5);
97-
// bst.insert(bst.root, 8);
98-
// bst.insert(bst.root, 12);
115+
// bst.add(4);
116+
// bst.add(9);
117+
// bst.add(2);
118+
// bst.add(5);
119+
// bst.add(8);
120+
// bst.add(12);
99121

100122
// console.log(bst.root);
101123

102-
// const preorder = bst.preorder(bst.root);
124+
// const preorder = bst.traversePreorder();
103125
// console.log('Preorder Traversal - ', preorder);
104126

105-
// const inorder = bst.inorder(bst.root);
127+
// const inorder = bst.traverseInorder();
106128
// console.log('Inorder Traversal - ', inorder);
107129

108-
// const postorder = bst.postorder(bst.root);
130+
// const postorder = bst.traversePostorder();
109131
// console.log('Postorder Traversal - ', postorder);
110132

111133
// const search = 18;
112-
// console.log(`Search for ${search}`, bst.search(bst.root, search));
134+
// console.log(`Search for ${search}`, bst.searchFor(search));
113135

114136
module.exports = BinarySearchTree;

0 commit comments

Comments
 (0)