Skip to content

Commit 9b70665

Browse files
committed
_right_left_rotate() for SBBT. C++ backend for SelfBalancingBinaryTree done!
1 parent 86e97d2 commit 9b70665

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,47 @@ static PyObject* SelfBalancingBinaryTree__left_right_rotate(SelfBalancingBinaryT
178178
Py_RETURN_NONE;
179179
}
180180

181+
static PyObject* SelfBalancingBinaryTree__right_left_rotate(SelfBalancingBinaryTree* self, PyObject *args) {
182+
PyObject* j = PyObject_GetItem(args, PyZero);
183+
PyObject* k = PyObject_GetItem(args, PyOne);
184+
BinaryTree* bt = self->bst->binary_tree;
185+
186+
PyObject* i = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->left;
187+
PyObject* v = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->left;
188+
PyObject* w = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->right;
189+
190+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->left = w;
191+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->right = v;
192+
193+
if (v != Py_None) {
194+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->parent = j;
195+
}
196+
if (w != Py_None) {
197+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(w)])->parent = k;
198+
}
199+
200+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->right = k;
201+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->left = j;
202+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->parent = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->parent;
203+
204+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->parent = i;
205+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->parent = i;
206+
207+
PyObject* ip = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->parent;
208+
if (ip != Py_None) {
209+
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->left == j) {
210+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->left = i;
211+
}
212+
else {
213+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->right = i;
214+
}
215+
}
216+
else {
217+
bt->root_idx = i;
218+
}
219+
Py_RETURN_NONE;
220+
}
221+
181222
static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
182223
{"insert", (PyCFunction) SelfBalancingBinaryTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
183224
{"delete", (PyCFunction) SelfBalancingBinaryTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
@@ -193,6 +234,7 @@ static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
193234
{"_right_rotate", (PyCFunction) SelfBalancingBinaryTree__right_rotate, METH_VARARGS, NULL},
194235
{"_left_rotate", (PyCFunction) SelfBalancingBinaryTree__left_rotate, METH_VARARGS, NULL},
195236
{"_left_right_rotate", (PyCFunction) SelfBalancingBinaryTree__left_right_rotate, METH_VARARGS, NULL},
237+
{"_right_left_rotate", (PyCFunction) SelfBalancingBinaryTree__right_left_rotate, METH_VARARGS, NULL},
196238
{NULL}
197239
};
198240

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ def _test_SelfBalancingBinaryTree(backend):
474474
tree._left_right_rotate(0, 2)
475475
assert str(tree) == "[(6, 5, 5, 1), (None, 5.5, 5.5, None), (4, 4.5, 4.5, 8), (2, 4.6, 4.6, 0), (None, 4.4, 4.4, None), (7, 4.55, 4.55, None), (None, 4.65, 4.65, None), (None, 4.54, 4.54, None), (5, 4.56, 4.56, None)]"
476476

477+
tree._right_left_rotate(0, 2)
478+
assert str(tree) == "[(6, 5, 5, None), (None, 5.5, 5.5, None), (None, 4.5, 4.5, 8), (2, 4.6, 4.6, 4), (0, 4.4, 4.4, 2), (7, 4.55, 4.55, None), (None, 4.65, 4.65, None), (None, 4.54, 4.54, None), (5, 4.56, 4.56, None)]"
479+
477480
def test_SelfBalancingBinaryTree():
478481
_test_SelfBalancingBinaryTree(Backend.PYTHON)
479482
def test_cpp_SelfBalancingBinaryTree():

0 commit comments

Comments
 (0)