Skip to content

Commit b21a67e

Browse files
committed
Created and registered RedBlackTrees, __new__() and __str__() done
1 parent 0bc3eb2 commit b21a67e

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef TREES_REDBLACKTREE_HPP
2+
#define TREES_REDBLACKTREE_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+
} RedBlackTree;
20+
21+
static void RedBlackTree_dealloc(RedBlackTree *self) {
22+
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
23+
}
24+
25+
static PyObject* RedBlackTree___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) {
26+
RedBlackTree *self;
27+
self = reinterpret_cast<RedBlackTree*>(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* RedBlackTree___str__(RedBlackTree *self) {
40+
return BinarySearchTree___str__(self->sbbt->bst);
41+
}
42+
43+
44+
45+
static struct PyMethodDef RedBlackTree_PyMethodDef[] = {
46+
{NULL}
47+
};
48+
49+
static PyMemberDef RedBlackTree_PyMemberDef[] = {
50+
{"tree", T_OBJECT_EX, offsetof(RedBlackTree, tree), 0, "tree"},
51+
{NULL} /* Sentinel */
52+
};
53+
54+
55+
static PyTypeObject RedBlackTreeType = {
56+
/* tp_name */ PyVarObject_HEAD_INIT(NULL, 0) "RedBlackTree",
57+
/* tp_basicsize */ sizeof(RedBlackTree),
58+
/* tp_itemsize */ 0,
59+
/* tp_dealloc */ (destructor) RedBlackTree_dealloc,
60+
/* tp_print */ 0,
61+
/* tp_getattr */ 0,
62+
/* tp_setattr */ 0,
63+
/* tp_reserved */ 0,
64+
/* tp_repr */ 0,
65+
/* tp_as_number */ 0,
66+
/* tp_as_sequence */ 0,
67+
/* tp_as_mapping */ 0,
68+
/* tp_hash */ 0,
69+
/* tp_call */ 0,
70+
/* tp_str */ (reprfunc) RedBlackTree___str__,
71+
/* tp_getattro */ 0,
72+
/* tp_setattro */ 0,
73+
/* tp_as_buffer */ 0,
74+
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
75+
/* tp_doc */ 0,
76+
/* tp_traverse */ 0,
77+
/* tp_clear */ 0,
78+
/* tp_richcompare */ 0,
79+
/* tp_weaklistoffset */ 0,
80+
/* tp_iter */ 0,
81+
/* tp_iternext */ 0,
82+
/* tp_methods */ RedBlackTree_PyMethodDef,
83+
/* tp_members */ RedBlackTree_PyMemberDef,
84+
/* tp_getset */ 0,
85+
/* tp_base */ &SelfBalancingBinaryTreeType,
86+
/* tp_dict */ 0,
87+
/* tp_descr_get */ 0,
88+
/* tp_descr_set */ 0,
89+
/* tp_dictoffset */ 0,
90+
/* tp_init */ 0,
91+
/* tp_alloc */ 0,
92+
/* tp_new */ RedBlackTree___new__,
93+
};
94+
95+
#endif

pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef TREES_SELFBALANCINGSelfBalancingBinaryTree_HPP
2-
#define TREES_SELFBALANCINGSelfBalancingBinaryTree_HPP
1+
#ifndef TREES_SELFBALANCINGBINARYTREE_HPP
2+
#define TREES_SELFBALANCINGBINARYTREE_HPP
33

44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>

pydatastructs/trees/_backend/cpp/trees.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "BinarySearchTree.hpp"
44
#include "BinaryTreeTraversal.hpp"
55
#include "SelfBalancingBinaryTree.hpp"
6+
#include "RedBlackTree.hpp"
67

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

44+
if (PyType_Ready(&RedBlackTreeType) < 0) {
45+
return NULL;
46+
}
47+
Py_INCREF(&RedBlackTreeType);
48+
PyModule_AddObject(trees, "RedBlackTree", reinterpret_cast<PyObject*>(&RedBlackTreeType));
49+
4350
return trees;
4451
}

pydatastructs/trees/binary_trees.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,18 @@ class RedBlackTree(SelfBalancingBinaryTree):
11991199
pydatastructs.trees.binary_trees.SelfBalancingBinaryTree
12001200
"""
12011201

1202+
def __new__(cls, key=None, root_data=None, comp=None,
1203+
is_order_statistic=False, **kwargs):
1204+
backend = kwargs.get('backend', Backend.PYTHON)
1205+
if backend == Backend.CPP:
1206+
if comp is None:
1207+
comp = lambda key1, key2: key1 < key2
1208+
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
1209+
return super().__new__(cls, key, root_data, comp, is_order_statistic, **kwargs)
1210+
12021211
@classmethod
12031212
def methods(cls):
1204-
return ['insert', 'delete']
1213+
return ['__new__', 'insert', 'delete']
12051214

12061215
def _get_parent(self, node_idx):
12071216
return self.tree[node_idx].parent

0 commit comments

Comments
 (0)