Skip to content

Commit

Permalink
Created and registered RedBlackTrees, __new__() and __str__() done
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed Jun 6, 2024
1 parent 0bc3eb2 commit b21a67e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
95 changes: 95 additions & 0 deletions pydatastructs/trees/_backend/cpp/RedBlackTree.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef TREES_REDBLACKTREE_HPP
#define TREES_REDBLACKTREE_HPP

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include <cstdlib>
#include "../../../utils/_backend/cpp/utils.hpp"
#include "../../../utils/_backend/cpp/TreeNode.hpp"
#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp"
#include "../../../linear_data_structures/_backend/cpp/arrays/DynamicOneDimensionalArray.hpp"
#include "BinarySearchTree.hpp"
#include "SelfBalancingBinaryTree.hpp"

typedef struct {
PyObject_HEAD
SelfBalancingBinaryTree* sbbt;
ArrayForTrees* tree;
} RedBlackTree;

static void RedBlackTree_dealloc(RedBlackTree *self) {
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}

static PyObject* RedBlackTree___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) {
RedBlackTree *self;
self = reinterpret_cast<RedBlackTree*>(type->tp_alloc(type, 0));

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.
return NULL;
}
PyObject* p = SelfBalancingBinaryTree___new__(&SelfBalancingBinaryTreeType, args, kwds);
self->sbbt = reinterpret_cast<SelfBalancingBinaryTree*>(p);
self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(p)->bst->binary_tree->tree;

return reinterpret_cast<PyObject*>(self);
}

static PyObject* RedBlackTree___str__(RedBlackTree *self) {
return BinarySearchTree___str__(self->sbbt->bst);
}



static struct PyMethodDef RedBlackTree_PyMethodDef[] = {
{NULL}
};

static PyMemberDef RedBlackTree_PyMemberDef[] = {
{"tree", T_OBJECT_EX, offsetof(RedBlackTree, tree), 0, "tree"},
{NULL} /* Sentinel */
};


static PyTypeObject RedBlackTreeType = {
/* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "RedBlackTree",
/* tp_basicsize */ sizeof(RedBlackTree),
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor) RedBlackTree_dealloc,
/* tp_print */ 0,
/* tp_getattr */ 0,
/* tp_setattr */ 0,
/* tp_reserved */ 0,
/* tp_repr */ 0,
/* tp_as_number */ 0,
/* tp_as_sequence */ 0,
/* tp_as_mapping */ 0,
/* tp_hash */ 0,
/* tp_call */ 0,
/* tp_str */ (reprfunc) RedBlackTree___str__,
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
/* tp_doc */ 0,
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
/* tp_iternext */ 0,
/* tp_methods */ RedBlackTree_PyMethodDef,
/* tp_members */ RedBlackTree_PyMemberDef,
/* tp_getset */ 0,
/* tp_base */ &SelfBalancingBinaryTreeType,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
/* tp_descr_set */ 0,
/* tp_dictoffset */ 0,
/* tp_init */ 0,
/* tp_alloc */ 0,
/* tp_new */ RedBlackTree___new__,
};

#endif
4 changes: 2 additions & 2 deletions pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef TREES_SELFBALANCINGSelfBalancingBinaryTree_HPP
#define TREES_SELFBALANCINGSelfBalancingBinaryTree_HPP
#ifndef TREES_SELFBALANCINGBINARYTREE_HPP
#define TREES_SELFBALANCINGBINARYTREE_HPP

#define PY_SSIZE_T_CLEAN
#include <Python.h>
Expand Down
7 changes: 7 additions & 0 deletions pydatastructs/trees/_backend/cpp/trees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "BinarySearchTree.hpp"
#include "BinaryTreeTraversal.hpp"
#include "SelfBalancingBinaryTree.hpp"
#include "RedBlackTree.hpp"

static struct PyModuleDef trees_struct = {
PyModuleDef_HEAD_INIT,
Expand Down Expand Up @@ -40,5 +41,11 @@ PyMODINIT_FUNC PyInit__trees(void) {
Py_INCREF(&SelfBalancingBinaryTreeType);
PyModule_AddObject(trees, "SelfBalancingBinaryTree", reinterpret_cast<PyObject*>(&SelfBalancingBinaryTreeType));

if (PyType_Ready(&RedBlackTreeType) < 0) {
return NULL;
}
Py_INCREF(&RedBlackTreeType);
PyModule_AddObject(trees, "RedBlackTree", reinterpret_cast<PyObject*>(&RedBlackTreeType));

return trees;
}
11 changes: 10 additions & 1 deletion pydatastructs/trees/binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,9 +1199,18 @@ class RedBlackTree(SelfBalancingBinaryTree):
pydatastructs.trees.binary_trees.SelfBalancingBinaryTree
"""

def __new__(cls, key=None, root_data=None, comp=None,
is_order_statistic=False, **kwargs):
backend = kwargs.get('backend', Backend.PYTHON)
if backend == Backend.CPP:
if comp is None:
comp = lambda key1, key2: key1 < key2
return _trees.RedBlackTree(key, root_data, comp, is_order_statistic, **kwargs) # If any argument is not given, then it is passed as None, except for comp
return super().__new__(cls, key, root_data, comp, is_order_statistic, **kwargs)

@classmethod
def methods(cls):
return ['insert', 'delete']
return ['__new__', 'insert', 'delete']

def _get_parent(self, node_idx):
return self.tree[node_idx].parent
Expand Down

0 comments on commit b21a67e

Please sign in to comment.