Skip to content

Commit 89a143a

Browse files
committed
Fixed TreeNode SegFault :)
1 parent 45ad909 commit 89a143a

File tree

4 files changed

+14
-139
lines changed

4 files changed

+14
-139
lines changed

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
2828
BinaryTree *self;
2929
self = reinterpret_cast<BinaryTree*>(type->tp_alloc(type, 0));
3030

31-
// Check what this is: (python code below:)
32-
// obj = object.__new__(cls)
31+
// // Check what this is: (python code below:)
32+
// // obj = object.__new__(cls)
3333

34-
// Assume that arguments are in the order below. Modify the python code such that this is true
34+
// Assume that arguments are in the order below. Modify the python code such that this is true (ie; pass None for other arguments)
3535
PyObject *key = PyObject_GetItem(args, PyZero);
3636
PyObject *root_data = PyObject_GetItem(args, PyOne);
3737
PyObject *comp = PyObject_GetItem(args, PyTwo);
@@ -42,20 +42,21 @@ static PyObject* BinaryTree___new__(PyTypeObject* type, PyObject *args, PyObject
4242
}
4343
Py_INCREF(Py_None);
4444
key = root_data == Py_None ? Py_None : key; // This key is the argument, not self->key
45-
std::cout<<"h1"<<std::endl;
4645

47-
TN* r = reinterpret_cast<TN*>(TN___new__(&TNType, args, kwds)); // check if this is correct
48-
std::cout<<"yay! Error solved! :)"<<std::endl;
49-
50-
TreeNode* root = reinterpret_cast<TreeNode*>(TreeNode___new__(&TreeNodeType, args, kwds)); // check if this is correct
51-
std::cout<<"h2"<<std::endl;
46+
if (PyType_Ready(&TreeNodeType) < 0) { // This has to be present to
47+
return NULL;
48+
}
49+
TreeNode* root = reinterpret_cast<TreeNode*>(TreeNode___new__(&TreeNodeType, args, kwds));
5250
root->is_root = true;
53-
5451
self->root_idx = 0;
5552

5653
// obj.tree= ArrayForTrees(TreeNode, [root])
5754
PyObject* listroot = Py_BuildValue("[i]", root);
58-
self->tree = PyObject_CallMethod(reinterpret_cast<PyObject*>(&ArrayForTreesType),"__new__", "OO", &TreeNodeType, listroot);
55+
if (PyType_Ready(&ArrayForTreesType) < 0) { // This has to be present to
56+
return NULL;
57+
}
58+
// TO DO: Fix the following line!
59+
// self->tree = PyObject_CallMethod(reinterpret_cast<PyObject*>(&ArrayForTreesType),"__new__", "OO", &TreeNodeType, listroot);
5960
self->size = 1;
6061

6162
if(comp == Py_None){

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
def test_cpp_BinaryTree():
1111
pass
12-
# b = _trees.BinaryTree(1,100);
12+
b = _trees.BinaryTree(1,100,None,None); # Pass None, passing 4 arguments is necessary
1313
# b = BinaryTree(1,1000)
1414
# print(str(b))
1515

16-
# test_cpp_BinaryTree()
16+
test_cpp_BinaryTree()
1717

1818
def test_BinarySearchTree():
1919
BST = BinarySearchTree

pydatastructs/utils/_backend/cpp/TN.hpp

Lines changed: 0 additions & 119 deletions
This file was deleted.

pydatastructs/utils/_backend/cpp/TreeNode.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#define PY_SSIZE_T_CLEAN
55
#include <Python.h>
66
#include <structmember.h>
7-
#include <iostream>
87
#include "Node.hpp"
98
#include "utils.hpp"
109

@@ -30,23 +29,17 @@ static void TreeNode_dealloc(TreeNode *self) {
3029

3130
static PyObject* TreeNode___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) {
3231
TreeNode *self;
33-
std::cout<<"h3"<<std::endl;
3432
self = reinterpret_cast<TreeNode*>(type->tp_alloc(type, 0));
35-
std::cout<<"h4"<<std::endl;
3633

3734
// Check what this is: (python code below:)
3835
// obj = Node.__new__(cls)
3936

4037
// Assume that arguments are in the order below. Modify the code such that this is true.
4138
self->key = PyLong_AsLong(PyObject_GetItem(args, PyZero));
42-
std::cout<<"h5"<<std::endl;
4339
self->data = PyLong_AsLong(PyObject_GetItem(args, PyOne));
44-
std::cout<<"h6"<<std::endl;
4540

4641
Py_INCREF(Py_None);
47-
std::cout<<"h7"<<std::endl;
4842
self->left = Py_None;
49-
std::cout<<"h8"<<std::endl;
5043
Py_INCREF(Py_None);
5144
self->right = Py_None;
5245
Py_INCREF(Py_None);

0 commit comments

Comments
 (0)