|
| 1 | +#ifndef TREES_BINARYSEARCHTREE_HPP |
| 2 | +#define TREES_BINARYSEARCHTREE_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 "BinaryTree.hpp" |
| 12 | + |
| 13 | +typedef struct { |
| 14 | + PyObject_HEAD |
| 15 | + BinaryTree* binary_tree; |
| 16 | +} BinarySearchTree; |
| 17 | + |
| 18 | +static void BinarySearchTree_dealloc(BinarySearchTree *self) { |
| 19 | + Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 20 | +} |
| 21 | + |
| 22 | +// How to allocate without __new__()? |
| 23 | + |
| 24 | +static int BinarySearchTree_left_size(BinarySearchTree* self, TreeNode* node) { |
| 25 | + if (node->left != Py_None) { |
| 26 | + return self->binary_tree->tree->_dynamic_one_dimensional_array->_one_dimensional_array->_data[node->left]->size; |
| 27 | + } else { |
| 28 | + return 0; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +static int BinarySearchTree_right_size(BinarySearchTree* self, TreeNode* node) { |
| 33 | + if (node->right != Py_None) { |
| 34 | + return self->binary_tree->tree->_dynamic_one_dimensional_array->_one_dimensional_array->_data[node->right]->size; |
| 35 | + } else { |
| 36 | + return 0; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +static PyObject* BinarySearchTree__update_size(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 41 | + PyObject *start_idx = PyObject_GetItem(args, PyZero); |
| 42 | + if(self->binary_tree->is_order_statistic){ |
| 43 | + PyObject* walk = start_idx; |
| 44 | + while(walk!=Py_None){ |
| 45 | + self->binary_tree->_dynamic_one_dimensional_array->_one_dimensional_array->_data[walk]->size = BinarySearchTree_left_size(self->binary_tree->_dynamic_one_dimensional_array->_one_dimensional_array->_data[walk]) + BinarySearchTree_right_size(self->binary_tree->_dynamic_one_dimensional_array->_one_dimensional_array->_data[walk]) + 1; |
| 46 | + walk = self->binary_tree->_dynamic_one_dimensional_array->_one_dimensional_array->_data[walk]->parent; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// static PyObject* BinarySearchTree_insert(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 52 | +// PyErr_SetString(PyExc_ValueError, "This is an abstract method."); // Currently of type ValueError, change type if needed later |
| 53 | +// return NULL; |
| 54 | +// } |
| 55 | + |
| 56 | +// static PyObject* BinaryTree_delete(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 57 | +// PyErr_SetString(PyExc_ValueError, "This is an abstract method."); // Currently of type ValueError, change type if needed later |
| 58 | +// return NULL; |
| 59 | +// } |
| 60 | + |
| 61 | +// static PyObject* BinaryTree_search(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 62 | +// PyErr_SetString(PyExc_ValueError, "This is an abstract method."); // Currently of type ValueError, change type if needed later |
| 63 | +// return NULL; |
| 64 | +// } |
| 65 | + |
| 66 | +// static PyObject* BinaryTree___str__(BinaryTree *self) { |
| 67 | +// long size = reinterpret_cast<ArrayForTrees*>(self->tree)->_last_pos_filled+1; |
| 68 | +// PyObject* list = PyList_New(size); |
| 69 | +// for(int i=0;i<size;i++){ |
| 70 | +// TreeNode* node = reinterpret_cast<TreeNode*>(reinterpret_cast<ArrayForTrees*>(self->tree)->_dynamic_one_dimensional_array->_one_dimensional_array->_data[i]); // check this |
| 71 | +// if(reinterpret_cast<PyObject*>(node) != Py_None){ |
| 72 | +// PyObject* out = Py_BuildValue("(OllO)", node->left, node->key, node->data, node->right); |
| 73 | +// Py_INCREF(out); |
| 74 | +// PyList_SET_ITEM(list, i, out); |
| 75 | +// } |
| 76 | +// else{ |
| 77 | +// PyObject* empty_string = PyUnicode_FromString(""); |
| 78 | +// PyList_SET_ITEM(list, i, empty_string); |
| 79 | +// } |
| 80 | +// } |
| 81 | +// return PyObject_Str(list); // use this or __str()__ (that is defined in utils)? |
| 82 | +// } |
| 83 | + |
| 84 | +// static struct PyMethodDef BinaryTree_PyMethodDef[] = { |
| 85 | +// {"insert", (PyCFunction) BinaryTree_insert, METH_VARARGS | METH_KEYWORDS, NULL}, |
| 86 | +// {"delete", (PyCFunction) BinaryTree_delete, METH_VARARGS | METH_KEYWORDS, NULL}, |
| 87 | +// {"search", (PyCFunction) BinaryTree_search, METH_VARARGS | METH_KEYWORDS, NULL}, |
| 88 | +// {NULL} |
| 89 | +// }; |
| 90 | + |
| 91 | +// // Check if PyMemberDef is actually needed: |
| 92 | +// static PyMemberDef BinaryTree_PyMemberDef[] = { |
| 93 | +// {"root_idx", T_PYSSIZET, offsetof(BinaryTree, root_idx), READONLY, "Index of the root node"}, |
| 94 | +// {"comparator", T_OBJECT, offsetof(BinaryTree, comparator), 0, "Comparator function"}, |
| 95 | +// {"tree", T_OBJECT, offsetof(BinaryTree, tree), 0, "Tree"}, |
| 96 | +// {"size", T_PYSSIZET, offsetof(BinaryTree, size), READONLY, "Size of the tree"}, |
| 97 | +// {"is_order_statistic", T_BOOL, offsetof(BinaryTree, is_order_statistic), 0, "Whether the tree is ordered statically or not"}, |
| 98 | +// {NULL} /* Sentinel */ |
| 99 | +// }; |
| 100 | + |
| 101 | + |
| 102 | +static PyTypeObject BinarySearchTreeType = { |
| 103 | + /* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "BinarySearchTree", |
| 104 | + /* tp_basicsize */ sizeof(BinarySearchTree), |
| 105 | + /* tp_itemsize */ 0, |
| 106 | + /* tp_dealloc */ (destructor) BinarySearchTree_dealloc, |
| 107 | + /* tp_print */ 0, |
| 108 | + /* tp_getattr */ 0, |
| 109 | + /* tp_setattr */ 0, |
| 110 | + /* tp_reserved */ 0, |
| 111 | + /* tp_repr */ 0, |
| 112 | + /* tp_as_number */ 0, |
| 113 | + /* tp_as_sequence */ 0, |
| 114 | + /* tp_as_mapping */ 0, |
| 115 | + /* tp_hash */ 0, |
| 116 | + /* tp_call */ 0, |
| 117 | + /* tp_str */ 0, |
| 118 | + /* tp_getattro */ 0, |
| 119 | + /* tp_setattro */ 0, |
| 120 | + /* tp_as_buffer */ 0, |
| 121 | + /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 122 | + /* tp_doc */ 0, |
| 123 | + /* tp_traverse */ 0, |
| 124 | + /* tp_clear */ 0, |
| 125 | + /* tp_richcompare */ 0, |
| 126 | + /* tp_weaklistoffset */ 0, |
| 127 | + /* tp_iter */ 0, |
| 128 | + /* tp_iternext */ 0, |
| 129 | + /* tp_methods */ BinarySearchTree_PyMethodDef, |
| 130 | + /* tp_members */ BinarySearchTree_PyMemberDef, |
| 131 | + /* tp_getset */ 0, |
| 132 | + /* tp_base */ &BinaryTreeType, |
| 133 | + /* tp_dict */ 0, |
| 134 | + /* tp_descr_get */ 0, |
| 135 | + /* tp_descr_set */ 0, |
| 136 | + /* tp_dictoffset */ 0, |
| 137 | + /* tp_init */ 0, |
| 138 | + /* tp_alloc */ 0, |
| 139 | + /* tp_new */ 0, |
| 140 | +}; |
| 141 | + |
| 142 | +#endif |
0 commit comments