Skip to content

Commit

Permalink
Added C++ backend for Node, TreeNode, ArrayForTrees, BinaryTree and B…
Browse files Browse the repository at this point in the history
…inarySearchTree and all tree traversals implemented (#556)
  • Loading branch information
Kishan-Ved authored Jun 3, 2024
1 parent 12d1de5 commit 557f86b
Show file tree
Hide file tree
Showing 32 changed files with 1,936 additions and 128 deletions.
2 changes: 1 addition & 1 deletion pydatastructs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .utils import *
from .linear_data_structures import *
from .trees import *
from .miscellaneous_data_structures import *
from .utils import *
from .graphs import *
from .strings import *

Expand Down
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from .arrays import (
OneDimensionalArray,
DynamicOneDimensionalArray,
MultiDimensionalArray
MultiDimensionalArray,
ArrayForTrees
)
__all__.extend(arrays.__all__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static bool is_ordered_impl(PyObject* array, size_t lower, size_t upper,
PyObject* i_item = PyObject_GetItem(array, i_PyObject);
PyObject* i1_item = PyObject_GetItem(array, i1_PyObject);
if (i_item == Py_None || i1_item == Py_None) continue;
if( _comp(i_item, i1_item, comp) == 1 ) {
if ( _comp(i_item, i1_item, comp) == 1 ) {
return false;
}
}
Expand All @@ -30,23 +30,23 @@ static PyObject* is_ordered(PyObject* self, PyObject* args, PyObject* kwds) {
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
Expand All @@ -66,26 +66,26 @@ static PyObject* linear_search(PyObject* self, PyObject* args, PyObject* kwds) {
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}
value = PyObject_GetItem(args, PyLong_FromSize_t(1));
if( value == NULL ) {
if ( value == NULL ) {
PyErr_Format(PyExc_ValueError,
"Expected Value to be not NULL");
}
Expand Down Expand Up @@ -120,30 +120,30 @@ static PyObject* binary_search(PyObject* self, PyObject* args, PyObject* kwds) {
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}
value = PyObject_GetItem(args, PyLong_FromSize_t(1));
if( value == NULL ) {
if ( value == NULL ) {
PyErr_Format(PyExc_ValueError,
"Expected Value to be not NULL");
}
Expand All @@ -169,7 +169,7 @@ static PyObject* binary_search(PyObject* self, PyObject* args, PyObject* kwds) {
Py_INCREF(res);
return res;
}
if( _comp(u, value, comp) == 1 ) {
if ( _comp(u, value, comp) == 1 ) {
left = middle + 1;
} else {
right = middle - 1;
Expand All @@ -187,30 +187,30 @@ static PyObject* jump_search(PyObject* self, PyObject* args, PyObject* kwds) {
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}
value = PyObject_GetItem(args, PyLong_FromSize_t(1));
if( value == NULL ) {
if ( value == NULL ) {
PyErr_Format(PyExc_ValueError,
"Expected Value to be not NULL");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static PyObject* bubble_sort_impl(PyObject* array, size_t lower, size_t upper,
for (size_t j = lower; j < upper; j++) {
PyObject* j_PyObject = PyLong_FromSize_t(j);
PyObject* j1_PyObject = PyLong_FromSize_t(j+1);
if( _comp(PyObject_GetItem(array, j_PyObject),
if ( _comp(PyObject_GetItem(array, j_PyObject),
PyObject_GetItem(array, j1_PyObject), comp) != 1 ) {
PyObject* tmp = PyObject_GetItem(array, j1_PyObject);
PyObject_SetItem(array, j1_PyObject,
Expand All @@ -34,23 +34,23 @@ static PyObject* bubble_sort(PyObject* self, PyObject* args, PyObject* kwds) {
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
Expand All @@ -59,7 +59,7 @@ static PyObject* bubble_sort(PyObject* self, PyObject* args, PyObject* kwds) {
arr_length = PyObject_Length(args0);

args0 = bubble_sort_impl(args0, lower, upper, comp, arr_length);
if( is_DynamicOneDimensionalArray ) {
if ( is_DynamicOneDimensionalArray ) {
PyObject_CallMethod(args0, "_modify", "O", Py_True);
}
Py_INCREF(args0);
Expand All @@ -75,7 +75,7 @@ static PyObject* selection_sort_impl(PyObject* array, size_t lower, size_t upper
PyObject* i_PyObject = PyLong_FromSize_t(i);
for (size_t j = i + 1; j < upper + 1; j++) {
PyObject* j_PyObject = PyLong_FromSize_t(j);
if( _comp(PyObject_GetItem(array, j_min_PyObject),
if ( _comp(PyObject_GetItem(array, j_min_PyObject),
PyObject_GetItem(array, j_PyObject), comp) != 1 ) {
j_min_PyObject = j_PyObject;
}
Expand All @@ -96,31 +96,31 @@ static PyObject* selection_sort(PyObject* self, PyObject* args, PyObject* kwds)
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}

args0 = selection_sort_impl(args0, lower, upper, comp);
if( is_DynamicOneDimensionalArray ) {
if ( is_DynamicOneDimensionalArray ) {
PyObject_CallMethod(args0, "_modify", "O", Py_True);
}
Py_INCREF(args0);
Expand Down Expand Up @@ -153,31 +153,31 @@ static PyObject* insertion_sort(PyObject* self, PyObject* args, PyObject* kwds)
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}

args0 = insertion_sort_impl(args0, lower, upper, comp);
if( is_DynamicOneDimensionalArray ) {
if ( is_DynamicOneDimensionalArray ) {
PyObject_CallMethod(args0, "_modify", "O", Py_True);
}
Py_INCREF(args0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
static PyObject* call_pick_pivot_element(PyObject* pick_pivot_element,
size_t low, size_t high, PyObject* array) {
PyObject* high_PyObject = PyLong_FromSize_t(high);
if( pick_pivot_element ) {
if ( pick_pivot_element ) {
return PyObject_CallFunctionObjArgs(pick_pivot_element,
PyLong_FromSize_t(low),
high_PyObject,
Expand All @@ -27,7 +27,7 @@ static size_t quick_sort_partition(size_t low, size_t high,
PyObject* x = call_pick_pivot_element(pick_pivot_element, low, high, array);
for( size_t j = low; j < high; j++ ) {
PyObject* j_PyObject = PyLong_FromSize_t(j);
if( _comp(PyObject_GetItem(array, j_PyObject), x, comp) == 1 ) {
if ( _comp(PyObject_GetItem(array, j_PyObject), x, comp) == 1 ) {
i = i + 1;
PyObject* i_PyObject = PyLong_FromLongLong(i);
PyObject* tmp = PyObject_GetItem(array, i_PyObject);
Expand All @@ -54,18 +54,18 @@ static PyObject* quick_sort_impl(PyObject* array, size_t lower, size_t upper,
rstack.push(lower);
rstack.push(upper);

while( !rstack.empty() ) {
while ( !rstack.empty() ) {
high = rstack.top();
rstack.pop();
low = rstack.top();
rstack.pop();
p = quick_sort_partition(low, high, pick_pivot_element,
comp, array);
if( p - 1 > low ) {
if ( p - 1 > low ) {
rstack.push(low);
rstack.push(p - 1);
}
if( p + 1 < high ) {
if ( p + 1 < high ) {
rstack.push(p + 1);
rstack.push(high);
}
Expand All @@ -81,35 +81,35 @@ static PyObject* quick_sort(PyObject* self, PyObject* args, PyObject* kwds) {
args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
raise_exception_if_not_array(args0);
return NULL;
}
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if( comp == NULL ) {
if ( comp == NULL ) {
PyErr_Clear();
}
pick_pivot_element = PyObject_GetItem(kwds, PyUnicode_FromString("pick_pivot_element"));
if( pick_pivot_element == NULL ) {
if ( pick_pivot_element == NULL ) {
PyErr_Clear();
}
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if( start == NULL ) {
if ( start == NULL ) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if( end == NULL ) {
if ( end == NULL ) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}

args0 = quick_sort_impl(args0, lower, upper, comp, pick_pivot_element);
if( is_DynamicOneDimensionalArray ) {
if ( is_DynamicOneDimensionalArray ) {
PyObject_CallMethod(args0, "_modify", "O", Py_True);
}
Py_INCREF(args0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static PyObject* Array___new__(PyTypeObject* type, PyObject *args,

static PyObject* Array___str__(Array *self) {
PyObject* self__data = PyObject_GetAttrString(reinterpret_cast<PyObject*>(self), "_data");
if( !self__data ) {
if ( !self__data ) {
return NULL;
}
return PyObject_Str(self__data);
Expand Down
Loading

0 comments on commit 557f86b

Please sign in to comment.