@@ -171,6 +171,47 @@ static PyObject* BinaryTreeTraversal__out_order(BinaryTreeTraversal* self, PyObj
171171 return visit;
172172}
173173
174+ static PyObject* BinaryTreeTraversal_morris_in_order_traversal (BinaryTreeTraversal* self, PyObject *args) {
175+ PyObject* node = PyObject_GetItem (args, PyZero);
176+ if (node == Py_None) {
177+ node = self->tree ->root_idx ;
178+ }
179+
180+ PyObject* traversal = PyList_New (0 );
181+ ArrayForTrees* tree = self->tree ->tree ;
182+ long current = PyLong_AsLong (node);
183+
184+ while (current != Py_None) {
185+ TreeNode* current_node = reinterpret_cast <TreeNode*>(tree->_one_dimensional_array ->_data [current]);
186+ if (current_node->left == Py_None) {
187+ // If there's no left child, visit the current node
188+ PyList_Append (traversal, reinterpret_cast <PyObject*>(current_node));
189+ current = PyLong_AsLong (current_node->right );
190+ } else {
191+ // Find the in-order predecessor (rightmost node in the left subtree)
192+ long predecessor = PyLong_AsLong (current_node->left );
193+ TreeNode* predecessor_node = reinterpret_cast <TreeNode*>(tree->_one_dimensional_array ->_data [predecessor]);
194+ while (predecessor_node->right != Py_None && predecessor_node->right != node) {
195+ predecessor = PyLong_AsLong (predecessor_node->right );
196+ predecessor_node = reinterpret_cast <TreeNode*>(tree->_one_dimensional_array ->_data [predecessor]);
197+ }
198+
199+ if (predecessor_node->right == Py_None) {
200+ // Make the current node the right child of the predecessor
201+ predecessor_node->right = node;
202+ current = PyLong_AsLong (current_node->left );
203+ } else {
204+ // Revert the changes made to the tree
205+ predecessor_node->right = Py_None;
206+ PyList_Append (traversal, reinterpret_cast <PyObject*>(current_node));
207+ current = PyLong_AsLong (current_node->right );
208+ }
209+ }
210+ }
211+
212+ return traversal;
213+ }
214+
174215static PyObject* BinaryTreeTraversal_depth_first_search (BinaryTreeTraversal* self, PyObject *args, PyObject *kwds) {
175216 Py_INCREF (Py_None);
176217 PyObject* node = Py_None;
@@ -242,12 +283,12 @@ static struct PyMethodDef BinaryTreeTraversal_PyMethodDef[] = {
242283 {" _in_order" , (PyCFunction) BinaryTreeTraversal__in_order, METH_VARARGS, NULL },
243284 {" _out_order" , (PyCFunction) BinaryTreeTraversal__out_order, METH_VARARGS, NULL },
244285 {" _post_order" , (PyCFunction) BinaryTreeTraversal__post_order, METH_VARARGS, NULL },
286+ {" morris_in_order_traversal" , (PyCFunction) BinaryTreeTraversal_morris_in_order_traversal, METH_VARARGS, NULL },
245287 {" depth_first_search" , (PyCFunction) BinaryTreeTraversal_depth_first_search, METH_VARARGS | METH_KEYWORDS, NULL },
246288 {" breadth_first_search" , (PyCFunction) BinaryTreeTraversal_breadth_first_search, METH_VARARGS | METH_KEYWORDS, NULL },
247289 {NULL }
248290};
249291
250-
251292static PyTypeObject BinaryTreeTraversalType = {
252293 /* tp_name */ PyVarObject_HEAD_INIT (NULL , 0 ) " BinaryTreeTraversal" ,
253294 /* tp_basicsize */ sizeof (BinaryTreeTraversal),
0 commit comments