Skip to content

Commit 86e97d2

Browse files
committed
_left_right_rotate() for SBBT
1 parent 4db9db5 commit 86e97d2

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,47 @@ static PyObject* SelfBalancingBinaryTree__left_rotate(SelfBalancingBinaryTree* s
137137
Py_RETURN_NONE;
138138
}
139139

140+
static PyObject* SelfBalancingBinaryTree__left_right_rotate(SelfBalancingBinaryTree* self, PyObject *args) {
141+
PyObject* j = PyObject_GetItem(args, PyZero);
142+
PyObject* k = PyObject_GetItem(args, PyOne);
143+
BinaryTree* bt = self->bst->binary_tree;
144+
145+
PyObject* i = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->right;
146+
PyObject* v = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->left;
147+
PyObject* w = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->right;
148+
149+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->right = v;
150+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->left = w;
151+
152+
if (v != Py_None) {
153+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->parent = k;
154+
}
155+
if (w != Py_None) {
156+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(w)])->parent = j;
157+
}
158+
159+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->left = k;
160+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->right = j;
161+
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;
162+
163+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->parent = i;
164+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->parent = i;
165+
166+
PyObject* ip = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->parent;
167+
if (ip != Py_None) {
168+
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->left == j) {
169+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->left = i;
170+
}
171+
else {
172+
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->right = i;
173+
}
174+
}
175+
else {
176+
bt->root_idx = i;
177+
}
178+
Py_RETURN_NONE;
179+
}
180+
140181
static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
141182
{"insert", (PyCFunction) SelfBalancingBinaryTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
142183
{"delete", (PyCFunction) SelfBalancingBinaryTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
@@ -151,6 +192,7 @@ static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
151192
{"select", (PyCFunction) SelfBalancingBinaryTree_select, METH_VARARGS, NULL},
152193
{"_right_rotate", (PyCFunction) SelfBalancingBinaryTree__right_rotate, METH_VARARGS, NULL},
153194
{"_left_rotate", (PyCFunction) SelfBalancingBinaryTree__left_rotate, METH_VARARGS, NULL},
195+
{"_left_right_rotate", (PyCFunction) SelfBalancingBinaryTree__left_right_rotate, METH_VARARGS, NULL},
154196
{NULL}
155197
};
156198

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,13 @@ def _test_SelfBalancingBinaryTree(backend):
469469
tree.insert(4.56, 4.56)
470470
tree._left_rotate(5, 8)
471471
assert tree.tree[tree.tree[8].parent].left == 8
472+
assert str(tree) == "[(2, 5, 5, 1), (None, 5.5, 5.5, None), (4, 4.5, 4.5, 3), (8, 4.6, 4.6, 6), (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)]"
473+
474+
tree._left_right_rotate(0, 2)
475+
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)]"
472476

473477
def test_SelfBalancingBinaryTree():
474478
_test_SelfBalancingBinaryTree(Backend.PYTHON)
475-
476479
def test_cpp_SelfBalancingBinaryTree():
477480
_test_SelfBalancingBinaryTree(Backend.CPP)
478481

0 commit comments

Comments
 (0)