Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ backend for all trees in binary_trees.py file complete #569

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Harsheet <[email protected]>
Pratik Goyal <[email protected]>
Jay Thorat <[email protected]>
Rajveer Singh Bharadwaj <[email protected]>
Kishan Ved <[email protected]>
16 changes: 13 additions & 3 deletions pydatastructs/trees/_backend/cpp/AVLTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static long AVLTree_left_height(AVLTree* self, PyObject *args) {
BinaryTree* bt = self->sbbt->bst->binary_tree;
return reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node->left)])->height;
}
else{
else {
return (-1);
}
}
Expand All @@ -68,7 +68,7 @@ static long AVLTree_right_height(AVLTree* self, PyObject *args) {
BinaryTree* bt = self->sbbt->bst->binary_tree;
return reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node->right)])->height;
}
else{
else {
return -1;
}
}
Expand Down Expand Up @@ -306,6 +306,11 @@ static PyObject* AVLTree_delete(AVLTree* self, PyObject *args, PyObject *kwds) {
Py_RETURN_TRUE;
}

static PyObject* AVLTree_root_idx(AVLTree *self, void *closure) {
return self->sbbt->bst->binary_tree->root_idx;
}


static struct PyMethodDef AVLTree_PyMethodDef[] = {
{"search", (PyCFunction) AVLTree_search, METH_VARARGS | METH_KEYWORDS, NULL},
{"insert", (PyCFunction) AVLTree_insert, METH_VARARGS, NULL},
Expand All @@ -321,6 +326,11 @@ static struct PyMethodDef AVLTree_PyMethodDef[] = {
{NULL}
};

static PyGetSetDef AVLTree_GetterSetters[] = {
{"root_idx", (getter) AVLTree_root_idx, NULL, "returns the index of the tree's root", NULL},
{NULL} /* Sentinel */
};

static PyMemberDef AVLTree_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(AVLTree, tree), 0, "tree"},
{NULL} /* Sentinel */
Expand Down Expand Up @@ -356,7 +366,7 @@ static PyTypeObject AVLTreeType = {
/* tp_iternext */ 0,
/* tp_methods */ AVLTree_PyMethodDef,
/* tp_members */ AVLTree_PyMemberDef,
/* tp_getset */ 0,
/* tp_getset */ AVLTree_GetterSetters,
/* tp_base */ &SelfBalancingBinaryTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
Expand Down
12 changes: 11 additions & 1 deletion pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,11 @@ static PyObject* BinarySearchTree_select(BinarySearchTree* self, PyObject* args)
Py_RETURN_NONE; // dummy return statement, never executed
}

static PyObject* BinarySearchTree_root_idx(BinarySearchTree *self, void *closure) {
return self->binary_tree->root_idx;
}


static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {
{"insert", (PyCFunction) BinarySearchTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
{"delete", (PyCFunction) BinarySearchTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
Expand All @@ -674,6 +679,11 @@ static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {
{NULL}
};

static PyGetSetDef BinarySearchTree_GetterSetters[] = {
{"root_idx", (getter) BinarySearchTree_root_idx, NULL, "returns the index of the tree's root", NULL},
{NULL} /* Sentinel */
};

static PyMemberDef BinarySearchTree_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(BinarySearchTree, tree), 0, "tree"},
{NULL} /* Sentinel */
Expand Down Expand Up @@ -709,7 +719,7 @@ static PyTypeObject BinarySearchTreeType = {
/* tp_iternext */ 0,
/* tp_methods */ BinarySearchTree_PyMethodDef,
/* tp_members */ BinarySearchTree_PyMemberDef,
/* tp_getset */ 0,
/* tp_getset */ BinarySearchTree_GetterSetters,
/* tp_base */ &BinaryTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
Expand Down
11 changes: 10 additions & 1 deletion pydatastructs/trees/_backend/cpp/RedBlackTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ static PyObject* RedBlackTree_search(RedBlackTree* self, PyObject *args, PyObjec
return BinarySearchTree_search(self->sbbt->bst, args, kwds);
}

static PyObject* RedBlackTree_root_idx(RedBlackTree *self, void *closure) {
return self->sbbt->bst->binary_tree->root_idx;
}


static struct PyMethodDef RedBlackTree_PyMethodDef[] = {
{"insert", (PyCFunction) RedBlackTree_insert, METH_VARARGS, NULL},
Expand All @@ -592,6 +596,11 @@ static struct PyMethodDef RedBlackTree_PyMethodDef[] = {
{NULL}
};

static PyGetSetDef RedBlackTree_GetterSetters[] = {
{"root_idx", (getter) RedBlackTree_root_idx, NULL, "returns the index of the tree's root", NULL},
{NULL} /* Sentinel */
};

static PyMemberDef RedBlackTree_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(RedBlackTree, tree), 0, "tree"},
{NULL} /* Sentinel */
Expand Down Expand Up @@ -627,7 +636,7 @@ static PyTypeObject RedBlackTreeType = {
/* tp_iternext */ 0,
/* tp_methods */ RedBlackTree_PyMethodDef,
/* tp_members */ RedBlackTree_PyMemberDef,
/* tp_getset */ 0,
/* tp_getset */ RedBlackTree_GetterSetters,
/* tp_base */ &SelfBalancingBinaryTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
Expand Down
12 changes: 11 additions & 1 deletion pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ static PyObject* SelfBalancingBinaryTree__right_left_rotate(SelfBalancingBinaryT
Py_RETURN_NONE;
}

static PyObject* SelfBalancingBinaryTree_root_idx(SelfBalancingBinaryTree *self, void *closure) {
return self->bst->binary_tree->root_idx;
}


static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
{"insert", (PyCFunction) SelfBalancingBinaryTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
{"delete", (PyCFunction) SelfBalancingBinaryTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
Expand All @@ -238,6 +243,11 @@ static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
{NULL}
};

static PyGetSetDef SelfBalancingBinaryTree_GetterSetters[] = {
{"root_idx", (getter) SelfBalancingBinaryTree_root_idx, NULL, "returns the index of the tree's root", NULL},
{NULL} /* Sentinel */
};

static PyMemberDef SelfBalancingBinaryTree_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(SelfBalancingBinaryTree, tree), 0, "tree"},
{NULL} /* Sentinel */
Expand Down Expand Up @@ -273,7 +283,7 @@ static PyTypeObject SelfBalancingBinaryTreeType = {
/* tp_iternext */ 0,
/* tp_methods */ SelfBalancingBinaryTree_PyMethodDef,
/* tp_members */ SelfBalancingBinaryTree_PyMemberDef,
/* tp_getset */ 0,
/* tp_getset */ SelfBalancingBinaryTree_GetterSetters,
/* tp_base */ &BinarySearchTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
Expand Down
11 changes: 10 additions & 1 deletion pydatastructs/trees/_backend/cpp/SplayTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ static PyObject* SplayTree_split(SplayTree *self, PyObject* args) {
return reinterpret_cast<PyObject*>(other);
}

static PyObject* SplayTree_root_idx(SplayTree *self, void *closure) {
return self->sbbt->bst->binary_tree->root_idx;
}


static struct PyMethodDef SplayTree_PyMethodDef[] = {
{"insert", (PyCFunction) SplayTree_insert, METH_VARARGS, NULL},
{"delete", (PyCFunction) SplayTree_delete, METH_VARARGS, NULL},
Expand All @@ -292,6 +297,10 @@ static struct PyMethodDef SplayTree_PyMethodDef[] = {
{NULL}
};

static PyGetSetDef SplayTree_GetterSetters[] = {
{"root_idx", (getter) SplayTree_root_idx, NULL, "returns the index of the tree's root", NULL},
{NULL} /* Sentinel */
};

static PyMemberDef SplayTree_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(SplayTree, tree), 0, "tree"},
Expand Down Expand Up @@ -328,7 +337,7 @@ static PyTypeObject SplayTreeType = {
/* tp_iternext */ 0,
/* tp_methods */ SplayTree_PyMethodDef,
/* tp_members */ SplayTree_PyMemberDef,
/* tp_getset */ 0,
/* tp_getset */ SplayTree_GetterSetters,
/* tp_base */ &SelfBalancingBinaryTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
Expand Down
11 changes: 10 additions & 1 deletion pydatastructs/trees/_backend/cpp/Treap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ static PyObject* Treap_insert(Treap *self, PyObject* args) {
return CartesianTree_insert(self->ct, Py_BuildValue("(OOO)", key, priority, data));
}

static PyObject* Treap_root_idx(Treap *self, void *closure) {
return self->ct->sbbt->bst->binary_tree->root_idx;
}


static struct PyMethodDef Treap_PyMethodDef[] = {
{"insert", (PyCFunction) Treap_insert, METH_VARARGS, NULL},
Expand All @@ -70,6 +74,11 @@ static struct PyMethodDef Treap_PyMethodDef[] = {
{NULL} /* Sentinel */
};

static PyGetSetDef Treap_GetterSetters[] = {
{"root_idx", (getter) Treap_root_idx, NULL, "returns the index of the tree's root", NULL},
{NULL} /* Sentinel */
};

static PyMemberDef Treap_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(Treap, tree), 0, "tree"},
{NULL} /* Sentinel */
Expand Down Expand Up @@ -105,7 +114,7 @@ static PyTypeObject TreapType = {
/* tp_iternext */ 0,
/* tp_methods */ Treap_PyMethodDef,
/* tp_members */ Treap_PyMemberDef,
/* tp_getset */ 0,
/* tp_getset */ Treap_GetterSetters,
/* tp_base */ &CartesianTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
Expand Down
7 changes: 6 additions & 1 deletion pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _test_BinarySearchTree(backend):
("[(1, 8, 8, 2), (3, 3, 3, 4), (None, 10, 10, 7), (None, 1, 1, None), "
"(5, 6, 6, 6), (None, 4, 4, None), (None, 7, 7, None), (8, 14, 14, None), "
"(None, 13, 13, None)]")
assert b.root_idx == 0

assert b.tree[0].left == 1
assert b.tree[0].key == 8
Expand Down Expand Up @@ -171,6 +172,7 @@ def _test_AVLTree(backend):
a.insert('H', 'H')
a.insert('I', 'I')
a.insert('A', 'A')
assert a.root_idx == 1

trav = BinaryTreeTraversal(a, backend=backend)
in_order = trav.depth_first_search(order='in_order')
Expand Down Expand Up @@ -465,7 +467,7 @@ def _test_Treap(backend):
tree.insert(3, 3)
tree.insert(4, 4)
tree.insert(5, 5)
print(str(tree))

assert isinstance(tree.tree[0].priority, float)
tree.delete(1)
assert tree.search(1) is None
Expand Down Expand Up @@ -497,6 +499,7 @@ def _test_SelfBalancingBinaryTree(backend):
assert tree.tree[3].parent == 5
assert tree.tree[2].right != 3
assert tree.tree[tree.tree[5].parent].right == 5
assert tree.root_idx == 0

trav = BinaryTreeTraversal(tree, backend=backend)
in_order = trav.depth_first_search(order='in_order')
Expand Down Expand Up @@ -533,6 +536,7 @@ def _test_SplayTree(backend):
t.insert(20, 20)
t.insert(55, 55)
assert str(t) == "[(None, 100, 100, None), (None, 50, 50, None), (0, 200, 200, None), (None, 40, 40, 1), (5, 30, 30, 3), (None, 20, 20, None), (4, 55, 55, 2)]"
assert t.root_idx == 6

trav = BinaryTreeTraversal(t, backend=backend)
in_order = trav.depth_first_search(order='in_order')
Expand Down Expand Up @@ -610,6 +614,7 @@ def _test_RedBlackTree(backend):
tree.insert(17, 17)
tree.insert(6, 6)
assert str(tree) == "[(11, 10, 10, 3), (10, 18, 18, None), (None, 7, 7, None), (None, 15, 15, None), (0, 16, 16, 6), (None, 30, 30, None), (1, 25, 25, 7), (5, 40, 40, 8), (None, 60, 60, None), (None, 2, 2, None), (None, 17, 17, None), (9, 6, 6, 2)]"
assert tree.root_idx == 4

trav = BinaryTreeTraversal(tree, backend=backend)
in_order = trav.depth_first_search(order='in_order')
Expand Down
Loading