Skip to content

Commit 557f86b

Browse files
authored
Added C++ backend for Node, TreeNode, ArrayForTrees, BinaryTree and BinarySearchTree and all tree traversals implemented (#556)
1 parent 12d1de5 commit 557f86b

32 files changed

+1936
-128
lines changed

pydatastructs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
from .utils import *
12
from .linear_data_structures import *
23
from .trees import *
34
from .miscellaneous_data_structures import *
4-
from .utils import *
55
from .graphs import *
66
from .strings import *
77

pydatastructs/linear_data_structures/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from .arrays import (
1111
OneDimensionalArray,
1212
DynamicOneDimensionalArray,
13-
MultiDimensionalArray
13+
MultiDimensionalArray,
14+
ArrayForTrees
1415
)
1516
__all__.extend(arrays.__all__)
1617

pydatastructs/linear_data_structures/_backend/cpp/algorithms/misc_algorithms.hpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static bool is_ordered_impl(PyObject* array, size_t lower, size_t upper,
1616
PyObject* i_item = PyObject_GetItem(array, i_PyObject);
1717
PyObject* i1_item = PyObject_GetItem(array, i1_PyObject);
1818
if (i_item == Py_None || i1_item == Py_None) continue;
19-
if( _comp(i_item, i1_item, comp) == 1 ) {
19+
if ( _comp(i_item, i1_item, comp) == 1 ) {
2020
return false;
2121
}
2222
}
@@ -30,23 +30,23 @@ static PyObject* is_ordered(PyObject* self, PyObject* args, PyObject* kwds) {
3030
args0 = PyObject_GetItem(args, PyZero);
3131
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
3232
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
33-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
33+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
3434
raise_exception_if_not_array(args0);
3535
return NULL;
3636
}
3737
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
38-
if( comp == NULL ) {
38+
if ( comp == NULL ) {
3939
PyErr_Clear();
4040
}
4141
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
42-
if( start == NULL ) {
42+
if ( start == NULL ) {
4343
PyErr_Clear();
4444
lower = 0;
4545
} else {
4646
lower = PyLong_AsSize_t(start);
4747
}
4848
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
49-
if( end == NULL ) {
49+
if ( end == NULL ) {
5050
PyErr_Clear();
5151
upper = PyObject_Length(args0) - 1;
5252
} else {
@@ -66,26 +66,26 @@ static PyObject* linear_search(PyObject* self, PyObject* args, PyObject* kwds) {
6666
args0 = PyObject_GetItem(args, PyZero);
6767
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
6868
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
69-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
69+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
7070
raise_exception_if_not_array(args0);
7171
return NULL;
7272
}
7373
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
74-
if( start == NULL ) {
74+
if ( start == NULL ) {
7575
PyErr_Clear();
7676
lower = 0;
7777
} else {
7878
lower = PyLong_AsSize_t(start);
7979
}
8080
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
81-
if( end == NULL ) {
81+
if ( end == NULL ) {
8282
PyErr_Clear();
8383
upper = PyObject_Length(args0) - 1;
8484
} else {
8585
upper = PyLong_AsSize_t(end);
8686
}
8787
value = PyObject_GetItem(args, PyLong_FromSize_t(1));
88-
if( value == NULL ) {
88+
if ( value == NULL ) {
8989
PyErr_Format(PyExc_ValueError,
9090
"Expected Value to be not NULL");
9191
}
@@ -120,30 +120,30 @@ static PyObject* binary_search(PyObject* self, PyObject* args, PyObject* kwds) {
120120
args0 = PyObject_GetItem(args, PyZero);
121121
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
122122
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
123-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
123+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
124124
raise_exception_if_not_array(args0);
125125
return NULL;
126126
}
127127
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
128-
if( comp == NULL ) {
128+
if ( comp == NULL ) {
129129
PyErr_Clear();
130130
}
131131
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
132-
if( start == NULL ) {
132+
if ( start == NULL ) {
133133
PyErr_Clear();
134134
lower = 0;
135135
} else {
136136
lower = PyLong_AsSize_t(start);
137137
}
138138
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
139-
if( end == NULL ) {
139+
if ( end == NULL ) {
140140
PyErr_Clear();
141141
upper = PyObject_Length(args0) - 1;
142142
} else {
143143
upper = PyLong_AsSize_t(end);
144144
}
145145
value = PyObject_GetItem(args, PyLong_FromSize_t(1));
146-
if( value == NULL ) {
146+
if ( value == NULL ) {
147147
PyErr_Format(PyExc_ValueError,
148148
"Expected Value to be not NULL");
149149
}
@@ -169,7 +169,7 @@ static PyObject* binary_search(PyObject* self, PyObject* args, PyObject* kwds) {
169169
Py_INCREF(res);
170170
return res;
171171
}
172-
if( _comp(u, value, comp) == 1 ) {
172+
if ( _comp(u, value, comp) == 1 ) {
173173
left = middle + 1;
174174
} else {
175175
right = middle - 1;
@@ -187,30 +187,30 @@ static PyObject* jump_search(PyObject* self, PyObject* args, PyObject* kwds) {
187187
args0 = PyObject_GetItem(args, PyZero);
188188
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
189189
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
190-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
190+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
191191
raise_exception_if_not_array(args0);
192192
return NULL;
193193
}
194194
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
195-
if( comp == NULL ) {
195+
if ( comp == NULL ) {
196196
PyErr_Clear();
197197
}
198198
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
199-
if( start == NULL ) {
199+
if ( start == NULL ) {
200200
PyErr_Clear();
201201
lower = 0;
202202
} else {
203203
lower = PyLong_AsSize_t(start);
204204
}
205205
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
206-
if( end == NULL ) {
206+
if ( end == NULL ) {
207207
PyErr_Clear();
208208
upper = PyObject_Length(args0) - 1;
209209
} else {
210210
upper = PyLong_AsSize_t(end);
211211
}
212212
value = PyObject_GetItem(args, PyLong_FromSize_t(1));
213-
if( value == NULL ) {
213+
if ( value == NULL ) {
214214
PyErr_Format(PyExc_ValueError,
215215
"Expected Value to be not NULL");
216216
}

pydatastructs/linear_data_structures/_backend/cpp/algorithms/quadratic_time_sort.hpp

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static PyObject* bubble_sort_impl(PyObject* array, size_t lower, size_t upper,
1414
for (size_t j = lower; j < upper; j++) {
1515
PyObject* j_PyObject = PyLong_FromSize_t(j);
1616
PyObject* j1_PyObject = PyLong_FromSize_t(j+1);
17-
if( _comp(PyObject_GetItem(array, j_PyObject),
17+
if ( _comp(PyObject_GetItem(array, j_PyObject),
1818
PyObject_GetItem(array, j1_PyObject), comp) != 1 ) {
1919
PyObject* tmp = PyObject_GetItem(array, j1_PyObject);
2020
PyObject_SetItem(array, j1_PyObject,
@@ -34,23 +34,23 @@ static PyObject* bubble_sort(PyObject* self, PyObject* args, PyObject* kwds) {
3434
args0 = PyObject_GetItem(args, PyZero);
3535
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
3636
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
37-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
37+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
3838
raise_exception_if_not_array(args0);
3939
return NULL;
4040
}
4141
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
42-
if( comp == NULL ) {
42+
if ( comp == NULL ) {
4343
PyErr_Clear();
4444
}
4545
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
46-
if( start == NULL ) {
46+
if ( start == NULL ) {
4747
PyErr_Clear();
4848
lower = 0;
4949
} else {
5050
lower = PyLong_AsSize_t(start);
5151
}
5252
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
53-
if( end == NULL ) {
53+
if ( end == NULL ) {
5454
PyErr_Clear();
5555
upper = PyObject_Length(args0) - 1;
5656
} else {
@@ -59,7 +59,7 @@ static PyObject* bubble_sort(PyObject* self, PyObject* args, PyObject* kwds) {
5959
arr_length = PyObject_Length(args0);
6060

6161
args0 = bubble_sort_impl(args0, lower, upper, comp, arr_length);
62-
if( is_DynamicOneDimensionalArray ) {
62+
if ( is_DynamicOneDimensionalArray ) {
6363
PyObject_CallMethod(args0, "_modify", "O", Py_True);
6464
}
6565
Py_INCREF(args0);
@@ -75,7 +75,7 @@ static PyObject* selection_sort_impl(PyObject* array, size_t lower, size_t upper
7575
PyObject* i_PyObject = PyLong_FromSize_t(i);
7676
for (size_t j = i + 1; j < upper + 1; j++) {
7777
PyObject* j_PyObject = PyLong_FromSize_t(j);
78-
if( _comp(PyObject_GetItem(array, j_min_PyObject),
78+
if ( _comp(PyObject_GetItem(array, j_min_PyObject),
7979
PyObject_GetItem(array, j_PyObject), comp) != 1 ) {
8080
j_min_PyObject = j_PyObject;
8181
}
@@ -96,31 +96,31 @@ static PyObject* selection_sort(PyObject* self, PyObject* args, PyObject* kwds)
9696
args0 = PyObject_GetItem(args, PyZero);
9797
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
9898
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
99-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
99+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
100100
raise_exception_if_not_array(args0);
101101
return NULL;
102102
}
103103
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
104-
if( comp == NULL ) {
104+
if ( comp == NULL ) {
105105
PyErr_Clear();
106106
}
107107
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
108-
if( start == NULL ) {
108+
if ( start == NULL ) {
109109
PyErr_Clear();
110110
lower = 0;
111111
} else {
112112
lower = PyLong_AsSize_t(start);
113113
}
114114
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
115-
if( end == NULL ) {
115+
if ( end == NULL ) {
116116
PyErr_Clear();
117117
upper = PyObject_Length(args0) - 1;
118118
} else {
119119
upper = PyLong_AsSize_t(end);
120120
}
121121

122122
args0 = selection_sort_impl(args0, lower, upper, comp);
123-
if( is_DynamicOneDimensionalArray ) {
123+
if ( is_DynamicOneDimensionalArray ) {
124124
PyObject_CallMethod(args0, "_modify", "O", Py_True);
125125
}
126126
Py_INCREF(args0);
@@ -153,31 +153,31 @@ static PyObject* insertion_sort(PyObject* self, PyObject* args, PyObject* kwds)
153153
args0 = PyObject_GetItem(args, PyZero);
154154
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
155155
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
156-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
156+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
157157
raise_exception_if_not_array(args0);
158158
return NULL;
159159
}
160160
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
161-
if( comp == NULL ) {
161+
if ( comp == NULL ) {
162162
PyErr_Clear();
163163
}
164164
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
165-
if( start == NULL ) {
165+
if ( start == NULL ) {
166166
PyErr_Clear();
167167
lower = 0;
168168
} else {
169169
lower = PyLong_AsSize_t(start);
170170
}
171171
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
172-
if( end == NULL ) {
172+
if ( end == NULL ) {
173173
PyErr_Clear();
174174
upper = PyObject_Length(args0) - 1;
175175
} else {
176176
upper = PyLong_AsSize_t(end);
177177
}
178178

179179
args0 = insertion_sort_impl(args0, lower, upper, comp);
180-
if( is_DynamicOneDimensionalArray ) {
180+
if ( is_DynamicOneDimensionalArray ) {
181181
PyObject_CallMethod(args0, "_modify", "O", Py_True);
182182
}
183183
Py_INCREF(args0);

pydatastructs/linear_data_structures/_backend/cpp/algorithms/quick_sort.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
static PyObject* call_pick_pivot_element(PyObject* pick_pivot_element,
1212
size_t low, size_t high, PyObject* array) {
1313
PyObject* high_PyObject = PyLong_FromSize_t(high);
14-
if( pick_pivot_element ) {
14+
if ( pick_pivot_element ) {
1515
return PyObject_CallFunctionObjArgs(pick_pivot_element,
1616
PyLong_FromSize_t(low),
1717
high_PyObject,
@@ -27,7 +27,7 @@ static size_t quick_sort_partition(size_t low, size_t high,
2727
PyObject* x = call_pick_pivot_element(pick_pivot_element, low, high, array);
2828
for( size_t j = low; j < high; j++ ) {
2929
PyObject* j_PyObject = PyLong_FromSize_t(j);
30-
if( _comp(PyObject_GetItem(array, j_PyObject), x, comp) == 1 ) {
30+
if ( _comp(PyObject_GetItem(array, j_PyObject), x, comp) == 1 ) {
3131
i = i + 1;
3232
PyObject* i_PyObject = PyLong_FromLongLong(i);
3333
PyObject* tmp = PyObject_GetItem(array, i_PyObject);
@@ -54,18 +54,18 @@ static PyObject* quick_sort_impl(PyObject* array, size_t lower, size_t upper,
5454
rstack.push(lower);
5555
rstack.push(upper);
5656

57-
while( !rstack.empty() ) {
57+
while ( !rstack.empty() ) {
5858
high = rstack.top();
5959
rstack.pop();
6060
low = rstack.top();
6161
rstack.pop();
6262
p = quick_sort_partition(low, high, pick_pivot_element,
6363
comp, array);
64-
if( p - 1 > low ) {
64+
if ( p - 1 > low ) {
6565
rstack.push(low);
6666
rstack.push(p - 1);
6767
}
68-
if( p + 1 < high ) {
68+
if ( p + 1 < high ) {
6969
rstack.push(p + 1);
7070
rstack.push(high);
7171
}
@@ -81,35 +81,35 @@ static PyObject* quick_sort(PyObject* self, PyObject* args, PyObject* kwds) {
8181
args0 = PyObject_GetItem(args, PyZero);
8282
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
8383
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
84-
if( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
84+
if ( !is_DynamicOneDimensionalArray && !is_OneDimensionalArray ) {
8585
raise_exception_if_not_array(args0);
8686
return NULL;
8787
}
8888
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
89-
if( comp == NULL ) {
89+
if ( comp == NULL ) {
9090
PyErr_Clear();
9191
}
9292
pick_pivot_element = PyObject_GetItem(kwds, PyUnicode_FromString("pick_pivot_element"));
93-
if( pick_pivot_element == NULL ) {
93+
if ( pick_pivot_element == NULL ) {
9494
PyErr_Clear();
9595
}
9696
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
97-
if( start == NULL ) {
97+
if ( start == NULL ) {
9898
PyErr_Clear();
9999
lower = 0;
100100
} else {
101101
lower = PyLong_AsSize_t(start);
102102
}
103103
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
104-
if( end == NULL ) {
104+
if ( end == NULL ) {
105105
PyErr_Clear();
106106
upper = PyObject_Length(args0) - 1;
107107
} else {
108108
upper = PyLong_AsSize_t(end);
109109
}
110110

111111
args0 = quick_sort_impl(args0, lower, upper, comp, pick_pivot_element);
112-
if( is_DynamicOneDimensionalArray ) {
112+
if ( is_DynamicOneDimensionalArray ) {
113113
PyObject_CallMethod(args0, "_modify", "O", Py_True);
114114
}
115115
Py_INCREF(args0);

pydatastructs/linear_data_structures/_backend/cpp/arrays/Array.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static PyObject* Array___new__(PyTypeObject* type, PyObject *args,
2222

2323
static PyObject* Array___str__(Array *self) {
2424
PyObject* self__data = PyObject_GetAttrString(reinterpret_cast<PyObject*>(self), "_data");
25-
if( !self__data ) {
25+
if ( !self__data ) {
2626
return NULL;
2727
}
2828
return PyObject_Str(self__data);

0 commit comments

Comments
 (0)