|
| 1 | +#ifndef TREES_BINARYTREETRAVERSAL_HPP |
| 2 | +#define TREES_BINARYTREETRAVERSAL_HPP |
| 3 | + |
| 4 | +#define PY_SSIZE_T_CLEAN |
| 5 | +#include <Python.h> |
| 6 | +#include <structmember.h> |
| 7 | +#include <cstdlib> |
| 8 | +#include <iostream> |
| 9 | +#include "../../../utils/_backend/cpp/utils.hpp" |
| 10 | +#include "../../../utils/_backend/cpp/TreeNode.hpp" |
| 11 | +#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp" |
| 12 | +#include "BinaryTree.hpp" |
| 13 | +#include "BinarySearchTree.hpp" |
| 14 | + |
| 15 | +typedef struct { |
| 16 | + PyObject_HEAD |
| 17 | + BinaryTree* tree; |
| 18 | +} BinaryTreeTraversal; |
| 19 | + |
| 20 | +static void BinaryTreeTraversal_dealloc(BinaryTreeTraversal *self) { |
| 21 | + BinaryTree_dealloc(self->tree); |
| 22 | + Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 23 | +} |
| 24 | + |
| 25 | +static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) { |
| 26 | + BinaryTreeTraversal *self; |
| 27 | + PyObject* tree = PyObject_GetItem(args, PyZero); |
| 28 | + if (PyType_Ready(&BinarySearchTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization. |
| 29 | + return NULL; |
| 30 | + } |
| 31 | + if (PyObject_IsInstance(tree, (PyObject *)&BinarySearchTreeType)) { |
| 32 | + self->tree = reinterpret_cast<BinarySearchTree*>(tree)->binary_tree; |
| 33 | + std::cout<<"here"<<std::endl; |
| 34 | + } |
| 35 | + else { |
| 36 | + PyErr_SetString(PyExc_ValueError, "Not a supported type for BinaryTreeTraversal."); |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + return reinterpret_cast<PyObject*>(self); |
| 40 | +} |
| 41 | + |
| 42 | +static struct PyMethodDef BinaryTreeTraversal_PyMethodDef[] = { |
| 43 | + {NULL} |
| 44 | +}; |
| 45 | + |
| 46 | + |
| 47 | +static PyTypeObject BinaryTreeTraversalType = { |
| 48 | + /* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "BinaryTreeTraversal", |
| 49 | + /* tp_basicsize */ sizeof(BinaryTreeTraversal), |
| 50 | + /* tp_itemsize */ 0, |
| 51 | + /* tp_dealloc */ (destructor) BinaryTreeTraversal_dealloc, |
| 52 | + /* tp_print */ 0, |
| 53 | + /* tp_getattr */ 0, |
| 54 | + /* tp_setattr */ 0, |
| 55 | + /* tp_reserved */ 0, |
| 56 | + /* tp_repr */ 0, |
| 57 | + /* tp_as_number */ 0, |
| 58 | + /* tp_as_sequence */ 0, |
| 59 | + /* tp_as_mapping */ 0, |
| 60 | + /* tp_hash */ 0, |
| 61 | + /* tp_call */ 0, |
| 62 | + /* tp_str */ 0, |
| 63 | + /* tp_getattro */ 0, |
| 64 | + /* tp_setattro */ 0, |
| 65 | + /* tp_as_buffer */ 0, |
| 66 | + /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 67 | + /* tp_doc */ 0, |
| 68 | + /* tp_traverse */ 0, |
| 69 | + /* tp_clear */ 0, |
| 70 | + /* tp_richcompare */ 0, |
| 71 | + /* tp_weaklistoffset */ 0, |
| 72 | + /* tp_iter */ 0, |
| 73 | + /* tp_iternext */ 0, |
| 74 | + /* tp_methods */ BinaryTreeTraversal_PyMethodDef, |
| 75 | + /* tp_members */ 0, |
| 76 | + /* tp_getset */ 0, |
| 77 | + /* tp_base */ &PyBaseObject_Type, |
| 78 | + /* tp_dict */ 0, |
| 79 | + /* tp_descr_get */ 0, |
| 80 | + /* tp_descr_set */ 0, |
| 81 | + /* tp_dictoffset */ 0, |
| 82 | + /* tp_init */ 0, |
| 83 | + /* tp_alloc */ 0, |
| 84 | + /* tp_new */ BinaryTreeTraversal___new__, |
| 85 | +}; |
| 86 | + |
| 87 | +#endif |
0 commit comments