Skip to content

Commit b0f44b2

Browse files
committed
Added TreeNode() to BinaryTree.hpp
1 parent da8fb74 commit b0f44b2

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <structmember.h>
77
#include <cstdlib>
88
#include "../../../utils/_backend/cpp/utils.hpp"
9+
#include "../../../utils/_backend/cpp/TreeNode.hpp"
910

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

4344
// Create TreeNode class
44-
// root = TreeNode(key, root_data)
45-
// root.is_root = True
45+
PyObject* root = TreeNode___new__(key, root_data);
46+
root->is_root = true;
4647

4748
self->root_idx = 0;
4849

pydatastructs/utils/_backend/cpp/TreeNode.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ typedef struct {
2020

2121
static void TreeNode_dealloc(TreeNode *self) {
2222
// Dealloc left and right TreeNodes
23-
TreeNode_dealloc(TreeNode->left);
24-
TreeNode_dealloc(TreeNode->right);
25-
// Check if other deallocs are needed
23+
TreeNode_dealloc(reinterpret_cast<TreeNode*>(TreeNode->left));
24+
TreeNode_dealloc(reinterpret_cast<TreeNode*>(TreeNode->right));
25+
// Check if other deallocs are needed using Py_XDECREF
2626
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
2727
}
2828

@@ -35,7 +35,7 @@ static PyObject* TreeNode___new__(PyTypeObject* type, PyObject *args, PyObject *
3535

3636
// Assume that arguments are in the order below. Modify the code such that this is true.
3737
self->key = PyObject_GetItem(args, PyZero);
38-
self->data *root_data = PyObject_GetItem(args, PyOne);
38+
self->data = PyObject_GetItem(args, PyOne);
3939

4040
Py_INCREF(Py_None);
4141
self->left = Py_None;

0 commit comments

Comments
 (0)