@@ -275,6 +275,98 @@ def search(self, key, **kwargs):
275
275
walk = self .tree [walk ].right
276
276
return (walk , parent ) if ret_parent else walk
277
277
278
+ def _bound_helper (self , node_idx , bound_key , is_upper = False ):
279
+ if node_idx is None :
280
+ return None
281
+ if self .tree [node_idx ].key is None :
282
+ return None
283
+
284
+ if self .tree [node_idx ].key == bound_key :
285
+ if not is_upper :
286
+ return self .tree [node_idx ].key
287
+ else :
288
+ return self ._bound_helper (self .tree [node_idx ].right ,
289
+ bound_key , is_upper )
290
+
291
+ if self .comparator (self .tree [node_idx ].key , bound_key ):
292
+ return self ._bound_helper (self .tree [node_idx ].right ,
293
+ bound_key , is_upper )
294
+ else :
295
+ res_bound = self ._bound_helper (self .tree [node_idx ].left ,
296
+ bound_key , is_upper )
297
+ return res_bound if res_bound is not None else self .tree [node_idx ].key
298
+
299
+
300
+ def lower_bound (self , key , ** kwargs ):
301
+ """
302
+ Finds the lower bound of the given key in the tree
303
+
304
+ Parameters
305
+ ==========
306
+
307
+ key
308
+ The key for comparison
309
+
310
+ Examples
311
+ ========
312
+
313
+ >>> from pydatastructs.trees import BinarySearchTree as BST
314
+ >>> b = BST()
315
+ >>> b.insert(10, 10)
316
+ >>> b.insert(18, 18)
317
+ >>> b.insert(7, 7)
318
+ >>> b.lower_bound(9)
319
+ 10
320
+ >>> b.lower_bound(7)
321
+ 7
322
+ >>> b.lower_bound(20) is None
323
+ True
324
+
325
+ Returns
326
+ =======
327
+
328
+ value
329
+ The lower bound of the given key.
330
+ Returns None if the value doesn't exist
331
+ """
332
+ return self ._bound_helper (self .root_idx , key )
333
+
334
+
335
+ def upper_bound (self , key , ** kwargs ):
336
+ """
337
+ Finds the upper bound of the given key in the tree
338
+
339
+ Parameters
340
+ ==========
341
+
342
+ key
343
+ The key for comparison
344
+
345
+ Examples
346
+ ========
347
+
348
+ >>> from pydatastructs.trees import BinarySearchTree as BST
349
+ >>> b = BST()
350
+ >>> b.insert(10, 10)
351
+ >>> b.insert(18, 18)
352
+ >>> b.insert(7, 7)
353
+ >>> b.upper_bound(9)
354
+ 10
355
+ >>> b.upper_bound(7)
356
+ 10
357
+ >>> b.upper_bound(20) is None
358
+ True
359
+
360
+ Returns
361
+ =======
362
+
363
+ value
364
+ The upper bound of the given key.
365
+ Returns None if the value doesn't exist
366
+ """
367
+ return self ._bound_helper (self .root_idx , key , True )
368
+
369
+
278
370
def delete (self , key , ** kwargs ):
279
371
(walk , parent ) = self .search (key , parent = True )
280
372
a = None
0 commit comments