Skip to content

Commit a591549

Browse files
committed
lower_bound() and upper_bound()
1 parent bc02f34 commit a591549

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

pydatastructs/trees/_backend/cpp/RedBlackTree.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,18 @@ static PyObject* RedBlackTree_insert(RedBlackTree *self, PyObject* args) {
188188
Py_RETURN_NONE;
189189
}
190190

191+
static PyObject* RedBlackTree_lower_bound(RedBlackTree* self, PyObject *args, PyObject *kwds) {
192+
return BinarySearchTree_lower_bound(self->sbbt->bst, args, kwds);
193+
}
194+
195+
static PyObject* RedBlackTree_upper_bound(RedBlackTree* self, PyObject *args, PyObject *kwds) {
196+
return BinarySearchTree_upper_bound(self->sbbt->bst, args, kwds);
197+
}
198+
191199
static struct PyMethodDef RedBlackTree_PyMethodDef[] = {
192200
{"insert", (PyCFunction) RedBlackTree_insert, METH_VARARGS, NULL},
201+
{"lower_bound", (PyCFunction) RedBlackTree_lower_bound, METH_VARARGS | METH_KEYWORDS, NULL},
202+
{"upper_bound", (PyCFunction) RedBlackTree_upper_bound, METH_VARARGS | METH_KEYWORDS, NULL},
193203
{NULL}
194204
};
195205

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -565,25 +565,25 @@ def test_RedBlackTree():
565565
assert [node.key for node in in_order] == [2, 6, 7, 10, 15, 16, 17, 18, 25, 30, 40, 60]
566566
assert [node.key for node in pre_order] == [16, 10, 6, 2, 7, 15, 25, 18, 17, 40, 30, 60]
567567

568-
# assert tree.lower_bound(0) == 2
569-
# assert tree.lower_bound(2) == 2
570-
# assert tree.lower_bound(3) == 6
571-
# assert tree.lower_bound(7) == 7
572-
# assert tree.lower_bound(25) == 25
573-
# assert tree.lower_bound(32) == 40
574-
# assert tree.lower_bound(41) == 60
575-
# assert tree.lower_bound(60) == 60
576-
# assert tree.lower_bound(61) is None
577-
578-
# assert tree.upper_bound(0) == 2
579-
# assert tree.upper_bound(2) == 6
580-
# assert tree.upper_bound(3) == 6
581-
# assert tree.upper_bound(7) == 10
582-
# assert tree.upper_bound(25) == 30
583-
# assert tree.upper_bound(32) == 40
584-
# assert tree.upper_bound(41) == 60
585-
# assert tree.upper_bound(60) is None
586-
# assert tree.upper_bound(61) is None
568+
assert tree.lower_bound(0) == 2
569+
assert tree.lower_bound(2) == 2
570+
assert tree.lower_bound(3) == 6
571+
assert tree.lower_bound(7) == 7
572+
assert tree.lower_bound(25) == 25
573+
assert tree.lower_bound(32) == 40
574+
assert tree.lower_bound(41) == 60
575+
assert tree.lower_bound(60) == 60
576+
assert tree.lower_bound(61) is None
577+
578+
assert tree.upper_bound(0) == 2
579+
assert tree.upper_bound(2) == 6
580+
assert tree.upper_bound(3) == 6
581+
assert tree.upper_bound(7) == 10
582+
assert tree.upper_bound(25) == 30
583+
assert tree.upper_bound(32) == 40
584+
assert tree.upper_bound(41) == 60
585+
assert tree.upper_bound(60) is None
586+
assert tree.upper_bound(61) is None
587587

588588
# tree = RedBlackTree()
589589

0 commit comments

Comments
 (0)