Skip to content

Commit 51f36cb

Browse files
committed
BST rank()
1 parent 654c49f commit 51f36cb

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,24 @@ static PyObject* BinarySearchTree_lowest_common_ancestor(BinarySearchTree* self,
570570
}
571571
}
572572

573+
static PyObject* BinarySearchTree_rank(BinarySearchTree* self, PyObject* args) {
574+
PyObject* x = PyObject_GetItem(args, PyZero);
575+
PyObject* walk = BinarySearchTree_search(self, Py_BuildValue("(O)",x), PyDict_New());
576+
if (walk == Py_None) {
577+
Py_RETURN_NONE;
578+
}
579+
BinaryTree* bt = self->binary_tree;
580+
long r = BinarySearchTree_left_size(self, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])) + 1;
581+
while (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->key != reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(bt->root_idx)])->key) {
582+
PyObject* p = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->parent;
583+
if (walk == reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(p)])->right) {
584+
r = r + BinarySearchTree_left_size(self, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(p)])) + 1;
585+
}
586+
walk = p;
587+
}
588+
return PyLong_FromLong(r);
589+
}
590+
573591
static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {
574592
{"insert", (PyCFunction) BinarySearchTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
575593
{"delete", (PyCFunction) BinarySearchTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
@@ -580,6 +598,7 @@ static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {
580598
{"_lca_1", (PyCFunction) BinarySearchTree__lca_1, METH_VARARGS, NULL},
581599
{"_lca_2", (PyCFunction) BinarySearchTree__lca_2, METH_VARARGS, NULL},
582600
{"lowest_common_ancestor", (PyCFunction) BinarySearchTree_lowest_common_ancestor, METH_VARARGS, NULL},
601+
{"rank", (PyCFunction) BinarySearchTree_rank, METH_VARARGS, NULL},
583602
{NULL}
584603
};
585604

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ def test_cpp_BST2():
151151
assert raises(ValueError, lambda: bl2.lowest_common_ancestor(200, 60, 1))
152152
assert raises(ValueError, lambda: bl2.lowest_common_ancestor(-3, 4, 1))
153153

154+
assert bl2.rank(18) == 5
155+
assert bl2.rank(10) == 1
156+
rank_list = [2, 2, 4, 4, 5, 4, 5, 3, 2, 3, 2, 1, 3, 4, 5]
157+
for i,node in enumerate(nodes):
158+
assert bl2.rank(node) == rank_list[i]
159+
154160
b3 = BST(backend=Backend.CPP)
155161
# b3 = BST()
156162
b3.insert(10, 10)
@@ -166,7 +172,7 @@ def test_cpp_BST2():
166172
assert b3.lower_bound(-1) == 7
167173
assert b3.lower_bound(20) is None
168174

169-
# test_cpp_BST2()
175+
test_cpp_BST2()
170176

171177
def test_cpp_BST_speed():
172178
BST = BinarySearchTree

0 commit comments

Comments
 (0)