Skip to content

Commit b9c1974

Browse files
committed
added trees.cpp file
1 parent 0665403 commit b9c1974

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cstdlib>
88
#include "../../../utils/_backend/cpp/utils.hpp"
99
#include "../../../utils/_backend/cpp/TreeNode.hpp"
10+
#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp"
1011

1112
typedef struct {
1213
PyObject_HEAD
@@ -41,14 +42,15 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
4142
Py_INCREF(Py_None);
4243
key = root_data == Py_None ? Py_None : key; // This key is the argument, not self->key
4344

44-
// Create TreeNode class
45-
PyObject* root = TreeNode___new__(key, root_data);
45+
PyObject* root = TreeNode___new__(key, root_data); // check if this is correct
4646
root->is_root = true;
4747

4848
self->root_idx = 0;
4949

50-
// Create ArrayForTrees class
51-
// obj.tree, obj.size = ArrayForTrees(TreeNode, [root]), 1
50+
// obj.tree= ArrayForTrees(TreeNode, [root])
51+
PyObject* listroot = Py_BuildValue("[i]", root);
52+
self->tree = ArrayForTrees(&TreeNodeType, listroot); // check if this is correct
53+
self->size = 1;
5254

5355
if(comp == Py_None){
5456
self->comparator = PyObject_RichCompare(PyObject *key1, PyObject *key2, Py_LT);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <Python.h>
2+
#include "BinaryTree.hpp"
3+
4+
static struct PyModuleDef trees_struct = {
5+
PyModuleDef_HEAD_INIT,
6+
"_trees",
7+
0,
8+
-1,
9+
NULL,
10+
};
11+
12+
PyMODINIT_FUNC PyInit__trees(void) {
13+
Py_Initialize();
14+
PyObject *trees = PyModule_Create(&trees_struct);
15+
16+
if (PyType_Ready(&BinaryTreeType) < 0) {
17+
return NULL;
18+
}
19+
Py_INCREF(&BinaryTreeType);
20+
PyModule_AddObject(trees, "BinaryTree", reinterpret_cast<PyObject*>(&BinaryTreeType));
21+
22+
return trees;
23+
}

0 commit comments

Comments
 (0)