@@ -171,6 +171,47 @@ static PyObject* BinaryTreeTraversal__out_order(BinaryTreeTraversal* self, PyObj
171
171
return visit;
172
172
}
173
173
174
+ static PyObject* BinaryTreeTraversal_morris_in_order (BinaryTreeTraversal* self, PyObject *args) {
175
+ PyObject* node = NULL ;
176
+ if (!PyArg_ParseTuple (args, " |O" , &node)) return NULL ;
177
+
178
+ if (node == NULL ) node = self->tree ->root_idx ;
179
+
180
+ PyObject* visit = PyList_New (0 );
181
+ ArrayForTrees* tree = self->tree ->tree ;
182
+ PyObject* current = node;
183
+
184
+ if (current == Py_None || self->tree ->size == 0 ) return visit;
185
+
186
+ while (current != Py_None) {
187
+ TreeNode* curr_node = reinterpret_cast <TreeNode*>(tree->_one_dimensional_array ->_data [PyLong_AsLong (current)]);
188
+
189
+ if (curr_node->left == Py_None) {
190
+ PyList_Append (visit, reinterpret_cast <PyObject*>(curr_node));
191
+ current = curr_node->right ;
192
+ } else {
193
+ PyObject* pre_obj = curr_node->left ;
194
+ TreeNode* pre_node = reinterpret_cast <TreeNode*>(tree->_one_dimensional_array ->_data [PyLong_AsLong (pre_obj)]);
195
+
196
+ while (pre_node->right != Py_None && pre_node->right != current) {
197
+ pre_obj = pre_node->right ;
198
+ pre_node = reinterpret_cast <TreeNode*>(tree->_one_dimensional_array ->_data [PyLong_AsLong (pre_obj)]);
199
+ }
200
+
201
+ if (pre_node->right == Py_None) {
202
+ pre_node->right = current;
203
+ current = curr_node->left ;
204
+ } else {
205
+ pre_node->right = Py_None;
206
+ PyList_Append (visit, reinterpret_cast <PyObject*>(curr_node));
207
+ current = curr_node->right ;
208
+ }
209
+ }
210
+ }
211
+
212
+ return visit;
213
+ }
214
+
174
215
static PyObject* BinaryTreeTraversal_depth_first_search (BinaryTreeTraversal* self, PyObject *args, PyObject *kwds) {
175
216
Py_INCREF (Py_None);
176
217
PyObject* node = Py_None;
@@ -242,6 +283,7 @@ static struct PyMethodDef BinaryTreeTraversal_PyMethodDef[] = {
242
283
{" _in_order" , (PyCFunction) BinaryTreeTraversal__in_order, METH_VARARGS, NULL },
243
284
{" _out_order" , (PyCFunction) BinaryTreeTraversal__out_order, METH_VARARGS, NULL },
244
285
{" _post_order" , (PyCFunction) BinaryTreeTraversal__post_order, METH_VARARGS, NULL },
286
+ {" morris_in_order" , (PyCFunction) BinaryTreeTraversal_morris_in_order, METH_VARARGS, NULL },
245
287
{" depth_first_search" , (PyCFunction) BinaryTreeTraversal_depth_first_search, METH_VARARGS | METH_KEYWORDS, NULL },
246
288
{" breadth_first_search" , (PyCFunction) BinaryTreeTraversal_breadth_first_search, METH_VARARGS | METH_KEYWORDS, NULL },
247
289
{NULL }
0 commit comments