@@ -86,29 +86,51 @@ class BinarySearchTree {
86
86
isEmpty ( ) {
87
87
return this . root === null ;
88
88
}
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
+ }
89
111
}
90
112
91
113
// const bst = new BinarySearchTree(6);
92
114
// 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);
99
121
100
122
// console.log(bst.root);
101
123
102
- // const preorder = bst.preorder(bst.root );
124
+ // const preorder = bst.traversePreorder( );
103
125
// console.log('Preorder Traversal - ', preorder);
104
126
105
- // const inorder = bst.inorder(bst.root );
127
+ // const inorder = bst.traverseInorder( );
106
128
// console.log('Inorder Traversal - ', inorder);
107
129
108
- // const postorder = bst.postorder(bst.root );
130
+ // const postorder = bst.traversePostorder( );
109
131
// console.log('Postorder Traversal - ', postorder);
110
132
111
133
// const search = 18;
112
- // console.log(`Search for ${search}`, bst.search(bst.root, search));
134
+ // console.log(`Search for ${search}`, bst.searchFor( search));
113
135
114
136
module . exports = BinarySearchTree ;
0 commit comments