|
| 1 | +#ifndef TREES_CARTESIANTREE_HPP |
| 2 | +#define TREES_CARTESIANTREE_HPP |
| 3 | + |
| 4 | +#define PY_SSIZE_T_CLEAN |
| 5 | +#include <Python.h> |
| 6 | +#include <structmember.h> |
| 7 | +#include <cstdlib> |
| 8 | +#include "../../../utils/_backend/cpp/utils.hpp" |
| 9 | +#include "../../../utils/_backend/cpp/TreeNode.hpp" |
| 10 | +#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp" |
| 11 | +#include "../../../linear_data_structures/_backend/cpp/arrays/DynamicOneDimensionalArray.hpp" |
| 12 | +#include "BinarySearchTree.hpp" |
| 13 | +#include "SelfBalancingBinaryTree.hpp" |
| 14 | + |
| 15 | +typedef struct { |
| 16 | + PyObject_HEAD |
| 17 | + SelfBalancingBinaryTree* sbbt; |
| 18 | + ArrayForTrees* tree; |
| 19 | +} CartesianTree; |
| 20 | + |
| 21 | +static void CartesianTree_dealloc(CartesianTree *self) { |
| 22 | + Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 23 | +} |
| 24 | + |
| 25 | +static PyObject* CartesianTree___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 26 | + CartesianTree *self; |
| 27 | + self = reinterpret_cast<CartesianTree*>(type->tp_alloc(type, 0)); |
| 28 | + |
| 29 | + if (PyType_Ready(&SelfBalancingBinaryTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization. |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + PyObject* p = SelfBalancingBinaryTree___new__(&SelfBalancingBinaryTreeType, args, kwds); |
| 33 | + self->sbbt = reinterpret_cast<SelfBalancingBinaryTree*>(p); |
| 34 | + self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(p)->bst->binary_tree->tree; |
| 35 | + |
| 36 | + return reinterpret_cast<PyObject*>(self); |
| 37 | +} |
| 38 | + |
| 39 | +static PyObject* CartesianTree___str__(CartesianTree *self) { |
| 40 | + return BinarySearchTree___str__(self->sbbt->bst); |
| 41 | +} |
| 42 | + |
| 43 | +static PyObject* CartesianTree_search(CartesianTree* self, PyObject *args, PyObject *kwds) { |
| 44 | + return BinarySearchTree_search(self->sbbt->bst, args, kwds); |
| 45 | +} |
| 46 | + |
| 47 | +static PyObject* Cartesian_Tree__bubble_up(CartesianTree* self, PyObject *args) { |
| 48 | + PyObject* node_idx = PyObject_GetItem(args, PyZero); |
| 49 | + BinaryTree* bt = self->sbbt->bst->binary_tree; |
| 50 | + TreeNode* node = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)]); |
| 51 | + PyObject* parent_idx = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->parent; |
| 52 | + TreeNode* parent = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(parent_idx)]); |
| 53 | + |
| 54 | + while ((node->parent != Py_None) && (parent->priority > node->priority)) { |
| 55 | + if (parent->right == node_idx) { |
| 56 | + SelfBalancingBinaryTree__left_rotate(self->sbbt, Py_BuildValue("OO", parent_idx, node_idx)); |
| 57 | + } |
| 58 | + else { |
| 59 | + SelfBalancingBinaryTree__right_rotate(self->sbbt, Py_BuildValue("OO", parent_idx, node_idx)); |
| 60 | + } |
| 61 | + node = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)]); |
| 62 | + parent_idx = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->parent; |
| 63 | + if (parent_idx != Py_None) { |
| 64 | + parent = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(parent_idx)]); |
| 65 | + } |
| 66 | + } |
| 67 | + if (node->parent == Py_None) { |
| 68 | + reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->is_root = true; |
| 69 | + } |
| 70 | + Py_RETURN_NONE; |
| 71 | +} |
| 72 | + |
| 73 | +static PyObject* Cartesian_Tree__trickle_down(CartesianTree* self, PyObject *args) { |
| 74 | + PyObject* node_idx = PyObject_GetItem(args, PyZero); |
| 75 | + BinaryTree* bt = self->sbbt->bst->binary_tree; |
| 76 | + TreeNode* node = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)]); |
| 77 | + while (node->left != Py_None || node->right != Py_None) { |
| 78 | + if (node->left == Py_None) { |
| 79 | + SelfBalancingBinaryTree__left_rotate(self->sbbt, Py_BuildValue("OO", node_idx, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->right)); |
| 80 | + } |
| 81 | + else if (node->right == Py_None) { |
| 82 | + SelfBalancingBinaryTree__right_rotate(self->sbbt, Py_BuildValue("OO", node_idx, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->left)); |
| 83 | + } |
| 84 | + else if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node->left)])->priority < reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node->right)])->priority) { |
| 85 | + SelfBalancingBinaryTree__right_rotate(self->sbbt, Py_BuildValue("OO", node_idx, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->left)); |
| 86 | + } |
| 87 | + else { |
| 88 | + SelfBalancingBinaryTree__left_rotate(self->sbbt, Py_BuildValue("OO", node_idx, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->right)); |
| 89 | + } |
| 90 | + node = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)]); |
| 91 | + } |
| 92 | + Py_RETURN_NONE; |
| 93 | +} |
| 94 | + |
| 95 | +static PyObject* CartesianTree_insert(CartesianTree *self, PyObject* args) { |
| 96 | + Py_INCREF(Py_None); |
| 97 | + PyObject* key = Py_None; |
| 98 | + Py_INCREF(Py_None); |
| 99 | + PyObject* priority = Py_None; |
| 100 | + Py_INCREF(Py_None); |
| 101 | + PyObject* data = Py_None; |
| 102 | + if (!PyArg_ParseTuple(args, "OO|O", &key, &priority, &data)) { // data is optional |
| 103 | + return NULL; |
| 104 | + } |
| 105 | + BinaryTree* bt = self->sbbt->bst->binary_tree; |
| 106 | + |
| 107 | + SelfBalancingBinaryTree_insert(self->sbbt, Py_BuildValue("(OO)", key, data)); |
| 108 | + PyObject* node_idx = SelfBalancingBinaryTree_search(self->sbbt, Py_BuildValue("(O)", key), PyDict_New()); |
| 109 | + TreeNode* node = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)]); |
| 110 | + if (PyType_Ready(&TreeNodeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization. |
| 111 | + return NULL; |
| 112 | + } |
| 113 | + TreeNode* new_node = reinterpret_cast<TreeNode*>(TreeNode___new__(&TreeNodeType, Py_BuildValue("(OO)", key, data), PyDict_New())); |
| 114 | + new_node->isCartesianTreeNode = true; |
| 115 | + new_node->priority = PyLong_AsLong(priority); |
| 116 | + new_node->parent = node->parent; |
| 117 | + new_node->left = node->left; |
| 118 | + new_node->right = node->right; |
| 119 | + bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)] = reinterpret_cast<PyObject*>(new_node); |
| 120 | + if (node->is_root) { |
| 121 | + reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(node_idx)])->is_root = true; |
| 122 | + } |
| 123 | + else { |
| 124 | + Cartesian_Tree__bubble_up(self, Py_BuildValue("(O)", node_idx)); |
| 125 | + } |
| 126 | + Py_RETURN_NONE; |
| 127 | +} |
| 128 | + |
| 129 | +static PyObject* CartesianTree_delete(CartesianTree* self, PyObject *args, PyObject *kwds) { |
| 130 | + Py_INCREF(Py_None); |
| 131 | + PyObject* key = Py_None; |
| 132 | + PyObject* balancing_info = PyZero; |
| 133 | + static char* keywords[] = {"key","balancing_info", NULL}; |
| 134 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", keywords, &key, &balancing_info)) { |
| 135 | + return NULL; |
| 136 | + } |
| 137 | + PyObject* node_idx = SelfBalancingBinaryTree_search(self->sbbt, Py_BuildValue("(O)", key), PyDict_New()); |
| 138 | + if (node_idx != Py_None) { |
| 139 | + Cartesian_Tree__trickle_down(self, Py_BuildValue("(O)", node_idx)); |
| 140 | + PyObject* kwd_bal = PyDict_New(); |
| 141 | + PyDict_SetItemString(kwd_bal, "balancing_info", balancing_info); |
| 142 | + return SelfBalancingBinaryTree_delete(self->sbbt, Py_BuildValue("(O)", key), kwd_bal); |
| 143 | + } |
| 144 | + Py_RETURN_NONE; |
| 145 | +} |
| 146 | + |
| 147 | +static PyObject* CartesianTree_root_idx(CartesianTree *self, void *closure) { |
| 148 | + return self->sbbt->bst->binary_tree->root_idx; |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +static struct PyMethodDef CartesianTree_PyMethodDef[] = { |
| 153 | + {"insert", (PyCFunction) CartesianTree_insert, METH_VARARGS, NULL}, |
| 154 | + {"delete", (PyCFunction) CartesianTree_delete, METH_VARARGS | METH_KEYWORDS, NULL}, |
| 155 | + {"search", (PyCFunction) CartesianTree_search, METH_VARARGS | METH_KEYWORDS, NULL}, |
| 156 | + {NULL} /* Sentinel */ |
| 157 | +}; |
| 158 | + |
| 159 | +static PyGetSetDef CartesianTree_GetterSetters[] = { |
| 160 | + {"root_idx", (getter) CartesianTree_root_idx, NULL, "returns the index of the tree's root", NULL}, |
| 161 | + {NULL} /* Sentinel */ |
| 162 | +}; |
| 163 | + |
| 164 | +static PyMemberDef CartesianTree_PyMemberDef[] = { |
| 165 | + {"tree", T_OBJECT_EX, offsetof(CartesianTree, tree), 0, "tree"}, |
| 166 | + {NULL} /* Sentinel */ |
| 167 | +}; |
| 168 | + |
| 169 | + |
| 170 | +static PyTypeObject CartesianTreeType = { |
| 171 | + /* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "CartesianTree", |
| 172 | + /* tp_basicsize */ sizeof(CartesianTree), |
| 173 | + /* tp_itemsize */ 0, |
| 174 | + /* tp_dealloc */ (destructor) CartesianTree_dealloc, |
| 175 | + /* tp_print */ 0, |
| 176 | + /* tp_getattr */ 0, |
| 177 | + /* tp_setattr */ 0, |
| 178 | + /* tp_reserved */ 0, |
| 179 | + /* tp_repr */ 0, |
| 180 | + /* tp_as_number */ 0, |
| 181 | + /* tp_as_sequence */ 0, |
| 182 | + /* tp_as_mapping */ 0, |
| 183 | + /* tp_hash */ 0, |
| 184 | + /* tp_call */ 0, |
| 185 | + /* tp_str */ (reprfunc) CartesianTree___str__, |
| 186 | + /* tp_getattro */ 0, |
| 187 | + /* tp_setattro */ 0, |
| 188 | + /* tp_as_buffer */ 0, |
| 189 | + /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 190 | + /* tp_doc */ 0, |
| 191 | + /* tp_traverse */ 0, |
| 192 | + /* tp_clear */ 0, |
| 193 | + /* tp_richcompare */ 0, |
| 194 | + /* tp_weaklistoffset */ 0, |
| 195 | + /* tp_iter */ 0, |
| 196 | + /* tp_iternext */ 0, |
| 197 | + /* tp_methods */ CartesianTree_PyMethodDef, |
| 198 | + /* tp_members */ CartesianTree_PyMemberDef, |
| 199 | + /* tp_getset */ CartesianTree_GetterSetters, |
| 200 | + /* tp_base */ &SelfBalancingBinaryTreeType, |
| 201 | + /* tp_dict */ 0, |
| 202 | + /* tp_descr_get */ 0, |
| 203 | + /* tp_descr_set */ 0, |
| 204 | + /* tp_dictoffset */ 0, |
| 205 | + /* tp_init */ 0, |
| 206 | + /* tp_alloc */ 0, |
| 207 | + /* tp_new */ CartesianTree___new__, |
| 208 | +}; |
| 209 | + |
| 210 | +#endif |
0 commit comments