@@ -32,23 +32,62 @@ let utils
32
32
* If no argument is sent it uses utils.comparator
33
33
*/
34
34
const AVLTree = ( function ( ) {
35
- function _avl ( comp ) {
36
- /** @public comparator function */
37
- this . _comp = undefined
38
- this . _comp = comp !== undefined ? comp : utils . comparator ( )
35
+ class _avl {
36
+ constructor ( comp ) {
37
+ /** @public comparator function */
38
+ this . _comp = undefined
39
+ this . _comp = comp !== undefined ? comp : utils . comparator ( )
39
40
40
- /** @public root of the AVL Tree */
41
- this . root = null
42
- /** @public number of elements in AVL Tree */
43
- this . size = 0
41
+ /** @public root of the AVL Tree */
42
+ this . root = null
43
+ /** @public number of elements in AVL Tree */
44
+ this . size = 0
45
+ }
46
+
47
+ /* Public Functions */
48
+ /**
49
+ * For Adding Elements to AVL Tree
50
+ * @param {any } _val
51
+ * Since in AVL Tree an element can only occur once so
52
+ * if a element exists it return false
53
+ * @returns {Boolean } element added or not
54
+ */
55
+ add ( _val ) {
56
+ const prevSize = this . size
57
+ this . root = insert ( this . root , _val , this )
58
+ return this . size !== prevSize
59
+ }
60
+ /**
61
+ * TO check is a particular element exists or not
62
+ * @param {any } _val
63
+ * @returns {Boolean } exists or not
64
+ */
65
+ find ( _val ) {
66
+ const temp = searchAVLTree ( this . root , _val , this )
67
+ return temp != null
68
+ }
69
+ /**
70
+ *
71
+ * @param {any } _val
72
+ * It is possible that element doesn't exists in tree
73
+ * in that case it return false
74
+ * @returns {Boolean } if element was found and deleted
75
+ */
76
+ remove ( _val ) {
77
+ const prevSize = this . size
78
+ this . root = deleteElement ( this . root , _val , this )
79
+ return prevSize !== this . size
80
+ }
44
81
}
45
82
46
83
// creates new Node Object
47
- const Node = function ( val ) {
48
- this . _val = val
49
- this . _left = null
50
- this . _right = null
51
- this . _height = 1
84
+ class Node {
85
+ constructor ( val ) {
86
+ this . _val = val
87
+ this . _left = null
88
+ this . _right = null
89
+ this . _height = 1
90
+ }
52
91
}
53
92
54
93
// get height of a node
@@ -199,40 +238,6 @@ const AVLTree = (function () {
199
238
return searchAVLTree ( root . _left , val , tree )
200
239
}
201
240
202
- /* Public Functions */
203
- /**
204
- * For Adding Elements to AVL Tree
205
- * @param {any } _val
206
- * Since in AVL Tree an element can only occur once so
207
- * if a element exists it return false
208
- * @returns {Boolean } element added or not
209
- */
210
- _avl . prototype . add = function ( _val ) {
211
- const prevSize = this . size
212
- this . root = insert ( this . root , _val , this )
213
- return this . size !== prevSize
214
- }
215
- /**
216
- * TO check is a particular element exists or not
217
- * @param {any } _val
218
- * @returns {Boolean } exists or not
219
- */
220
- _avl . prototype . find = function ( _val ) {
221
- const temp = searchAVLTree ( this . root , _val , this )
222
- return temp != null
223
- }
224
- /**
225
- *
226
- * @param {any } _val
227
- * It is possible that element doesn't exists in tree
228
- * in that case it return false
229
- * @returns {Boolean } if element was found and deleted
230
- */
231
- _avl . prototype . remove = function ( _val ) {
232
- const prevSize = this . size
233
- this . root = deleteElement ( this . root , _val , this )
234
- return prevSize !== this . size
235
- }
236
241
return _avl
237
242
} ) ( )
238
243
0 commit comments