Skip to content

Commit

Permalink
_left_right_rotate() for SBBT
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed Jun 6, 2024
1 parent 4db9db5 commit 86e97d2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
42 changes: 42 additions & 0 deletions pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,47 @@ static PyObject* SelfBalancingBinaryTree__left_rotate(SelfBalancingBinaryTree* s
Py_RETURN_NONE;
}

static PyObject* SelfBalancingBinaryTree__left_right_rotate(SelfBalancingBinaryTree* self, PyObject *args) {
PyObject* j = PyObject_GetItem(args, PyZero);
PyObject* k = PyObject_GetItem(args, PyOne);
BinaryTree* bt = self->bst->binary_tree;

PyObject* i = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->right;
PyObject* v = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->left;
PyObject* w = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->right;

reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->right = v;
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->left = w;

if (v != Py_None) {
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(v)])->parent = k;
}
if (w != Py_None) {
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(w)])->parent = j;
}

reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->left = k;
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->right = j;
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;

reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(k)])->parent = i;
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(j)])->parent = i;

PyObject* ip = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(i)])->parent;
if (ip != Py_None) {
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->left == j) {
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->left = i;
}
else {
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(ip)])->right = i;
}
}
else {
bt->root_idx = i;
}
Py_RETURN_NONE;
}

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 @@ -151,6 +192,7 @@ static struct PyMethodDef SelfBalancingBinaryTree_PyMethodDef[] = {
{"select", (PyCFunction) SelfBalancingBinaryTree_select, METH_VARARGS, NULL},
{"_right_rotate", (PyCFunction) SelfBalancingBinaryTree__right_rotate, METH_VARARGS, NULL},
{"_left_rotate", (PyCFunction) SelfBalancingBinaryTree__left_rotate, METH_VARARGS, NULL},
{"_left_right_rotate", (PyCFunction) SelfBalancingBinaryTree__left_right_rotate, METH_VARARGS, NULL},
{NULL}
};

Expand Down
5 changes: 4 additions & 1 deletion pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,13 @@ def _test_SelfBalancingBinaryTree(backend):
tree.insert(4.56, 4.56)
tree._left_rotate(5, 8)
assert tree.tree[tree.tree[8].parent].left == 8
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)]"

tree._left_right_rotate(0, 2)
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)]"

def test_SelfBalancingBinaryTree():
_test_SelfBalancingBinaryTree(Backend.PYTHON)

def test_cpp_SelfBalancingBinaryTree():
_test_SelfBalancingBinaryTree(Backend.CPP)

Expand Down

0 comments on commit 86e97d2

Please sign in to comment.