Skip to content

Commit

Permalink
some initial work on binary search trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed May 6, 2024
1 parent 72be356 commit 8ec0296
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
179 changes: 179 additions & 0 deletions pydatastructs/trees/_backend/cpp/binary_search_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include <Python.h>
#include <structmember.h>

// Define TreeNode struct.
// Defines a C struct TreeNode that represents a node in the binary search tree.
// It contains pointers to key, data, left, right, and parent.
typedef struct {
PyObject_HEAD
PyObject* key;
PyObject* data;
PyObject* left;
PyObject* right;
PyObject* parent;
PyObject* size;
} TreeNode;

// Define BinarySearchTree class.
// Defines a C struct BinarySearchTree that represents the binary search tree.
// It extends the BinaryTree class.
typedef struct {
PyObject_VAR_HEAD
PyObject* left_size;
PyObject* right_size;
PyObject* _update_size;
PyObject* insert;
PyObject* search;
PyObject* _bound_helper;
PyObject* lower_bound;
PyObject* tree; // Define tree attribute
} BinarySearchTree;

// Deallocation function for BinarySearchTree.
// Defines a deallocation function for BinarySearchTree.
static void BinarySearchTree_dealloc(BinarySearchTree* self) {
Py_XDECREF(self->left_size);
Py_XDECREF(self->right_size);
Py_XDECREF(self->_update_size);
Py_XDECREF(self->insert);
Py_XDECREF(self->search);
Py_XDECREF(self->_bound_helper);
Py_XDECREF(self->lower_bound);
Py_XDECREF(self->tree); // Release memory for the tree attribute

Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}

// Object creation function for BinarySearchTree.
// Defines a new object creation function for BinarySearchTree.
static PyObject* BinarySearchTree_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
BinarySearchTree* self;
self = reinterpret_cast<BinarySearchTree*>(type->tp_alloc(type, 0));

// Initialize tree attribute as an empty list
if (self != NULL) {
self->tree = PyList_New(0);
if (self->tree == NULL) {
Py_DECREF(self);
return NULL;
}
}
return reinterpret_cast<PyObject*>(self);
}

// Method to update the size of nodes in the tree.
static PyObject* BinarySearchTree_update_size(BinarySearchTree* self, PyObject* args) {
PyObject* start_idx;

if (!PyArg_ParseTuple(args, "O", &start_idx)) {
return NULL;
}

// Implement _update_size logic here

Py_RETURN_NONE;
}

// Method to insert a new node into the tree.
static PyObject* BinarySearchTree_insert(BinarySearchTree* self, PyObject* args) {
PyObject* key;
PyObject* data = NULL;

if (!PyArg_ParseTuple(args, "O|O", &key, &data)) {
return NULL;
}

// Implement insert logic here

Py_RETURN_NONE;
}

// Method to search for a key in the tree.
static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args) {
PyObject* key;

if (!PyArg_ParseTuple(args, "O", &key)) {
return NULL;
}

// Implement search logic here

Py_RETURN_NONE;
}

// Method definitions for BinarySearchTree
static PyMethodDef BinarySearchTree_methods[] = {
{"_update_size", (PyCFunction)BinarySearchTree_update_size, METH_VARARGS, "Updates the size of nodes in the tree."},
{"insert", (PyCFunction)BinarySearchTree_insert, METH_VARARGS, "Inserts a new node into the tree."},
{"search", (PyCFunction)BinarySearchTree_search, METH_VARARGS, "Searches for a key in the tree."},
{NULL}
};

// Type object for BinarySearchTree
static PyTypeObject BinarySearchTreeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"BinarySearchTree", /* tp_name */
sizeof(BinarySearchTree), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)BinarySearchTree_dealloc, /* tp_dealloc */
0, /* 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 */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"BinarySearchTree objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
BinarySearchTree_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
BinarySearchTree_new, /* tp_new */
};

// Module definition for the binarysearchtree module
static PyModuleDef binarysearchtreemodule = {
PyModuleDef_HEAD_INIT,
"binarysearchtree", // Module name
NULL, // Module documentation
-1,
NULL, NULL, NULL, NULL, NULL
};

// Module initialization function
PyMODINIT_FUNC PyInit_binarysearchtree(void) {
PyObject* m;
if (PyType_Ready(&BinarySearchTreeType) < 0) {
return NULL;
}

m = PyModule_Create(&binarysearchtreemodule);
if (m == NULL) {
return NULL;
}

Py_INCREF(&BinarySearchTreeType);
PyModule_AddObject(m, "BinarySearchTree", reinterpret_cast<PyObject*>(&BinarySearchTreeType));

return m;
}
24 changes: 24 additions & 0 deletions pydatastructs/trees/_extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from setuptools import Extension

project = 'pydatastructs'

module = 'trees'

backend = '_backend'

cpp = 'cpp'

trees = '.'.join([project, module, backend, cpp, '_trees'])
trees_sources = ['/'.join([project, module, backend, cpp,
'trees', 'trees.cpp'])]

# Add tree algorithms later

# algorithms = '.'.join([project, module, backend, cpp, '_algorithms'])
# algorithms_sources = ['/'.join([project, module, backend, cpp,
# 'algorithms', 'algorithms.cpp'])]

extensions = [
Extension(trees, sources=trees_sources),
# Extension(algorithms, sources=algorithms_sources)
]

0 comments on commit 8ec0296

Please sign in to comment.