Title: [FEATURE]: Add Morris Traversal Algorithm · Issue #1806 · TheAlgorithms/JavaScript · GitHub
Open Graph Title: [FEATURE]: Add Morris Traversal Algorithm · Issue #1806 · TheAlgorithms/JavaScript
X Title: [FEATURE]: Add Morris Traversal Algorithm · Issue #1806 · TheAlgorithms/JavaScript
Description: Motivation Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Checklist: I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Javascript/blob/master/CONTRIBUTING.md). This ...
Open Graph Description: Motivation Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Checklist: I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Javasc...
X Description: Motivation Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Checklist: I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Javasc...
Opengraph URL: https://github.com/TheAlgorithms/JavaScript/issues/1806
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[FEATURE]: Add Morris Traversal Algorithm","articleBody":"### Motivation\n\n\n\u003e ### Describe your change:\n\u003e\n\u003e * [x] Add an algorithm?\n\u003e * [ ] Fix a bug or typo in an existing algorithm?\n\u003e * [ ] Documentation change?\n\u003e\n\u003e ### Checklist:\n\u003e\n\u003e * [x] I have read [[CONTRIBUTING.md](https://github.com/TheAlgorithms/Javascript/blob/master/CONTRIBUTING.md)](https://github.com/TheAlgorithms/Javascript/blob/master/CONTRIBUTING.md).\n\u003e * [x] This pull request is all my own work -- I have not plagiarized.\n\u003e * [x] I know that pull requests will not be merged if they fail the automated tests.\n\u003e * [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.\n\u003e * [x] All new JavaScript files are placed inside an existing directory.\n\u003e * [x] All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.\n\u003e **Example:** `UserProfile.js` is allowed but `userprofile.js`,`Userprofile.js`,`user-Profile.js`,`userProfile.js` are not.\n\u003e * [x] All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.\n\u003e **Example:** [[Morris Traversal - Wikipedia](https://en.wikipedia.org/wiki/Threaded_binary_tree#Morris_traversal)](https://en.wikipedia.org/wiki/Threaded_binary_tree#Morris_traversal)\n\u003e * [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.\n\u003e * [ ] closes\n\n### Examples\n\n 7\n / \\\n 5 8\n / \\\n 3 6\n \\\n 9\n\n\nTraversal order (Inorder: Left → Root → Right):\nStep 1: Visit leftmost node → 3\nStep 2: Back to parent → 5\nStep 3: Move to right subtree → 6\nStep 4: Right child of 6 → 9\nStep 5: Back to root → 7\nStep 6: Right child of root → 8\n\nFinal output sequence:\n3, 5, 6, 9, 7, 8\n\n\n### Possible workarounds\n\n\u003chtml\u003e\n\u003cbody\u003e\n\u003c!--StartFragment--\u003e\u003ch3 data-start=\"648\" data-end=\"706\"\u003e\u003cstrong data-start=\"652\" data-end=\"706\"\u003ePossible Workarounds – Conceptual Illustration\u003c/strong\u003e\u003c/h3\u003e\n\u003cdiv class=\"_tableContainer_1rjym_1\"\u003e\u003cdiv tabindex=\"-1\" class=\"group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse\"\u003e\nMethod | Space Complexity | Notes\n-- | -- | --\nRecursive Inorder Traversal | O(h) | Uses call stack; simple but not O(1)\nIterative Inorder with Stack | O(h) | Explicit stack; simulates recursion\nThreaded Binary Tree | O(1) | Permanently modifies tree pointers for threads\n\n\u003c/div\u003e\u003c/div\u003e\n\u003cp data-start=\"1211\" data-end=\"1232\"\u003e\u003cstrong data-start=\"1211\" data-end=\"1230\"\u003eVisual analogy:\u003c/strong\u003e\u003c/p\u003e\n\u003cul data-start=\"1233\" data-end=\"1515\"\u003e\n\u003cli data-start=\"1233\" data-end=\"1319\"\u003e\n\u003cp data-start=\"1235\" data-end=\"1319\"\u003e\u003cstrong data-start=\"1235\" data-end=\"1256\"\u003eRecursive / Stack\u003c/strong\u003e → climbing stairs with a backpack (stack grows with height).\u003c/p\u003e\n\u003c/li\u003e\n\u003cli data-start=\"1320\" data-end=\"1414\"\u003e\n\u003cp data-start=\"1322\" data-end=\"1414\"\u003e\u003cstrong data-start=\"1322\" data-end=\"1342\"\u003eMorris Traversal\u003c/strong\u003e → climbing stairs using only your hands on rails (no extra backpack).\u003c/p\u003e\n\u003c/li\u003e\n\u003cli data-start=\"1415\" data-end=\"1515\"\u003e\n\u003cp data-start=\"1417\" data-end=\"1515\"\u003e\u003cstrong data-start=\"1417\" data-end=\"1434\"\u003eThreaded Tree\u003c/strong\u003e → installing permanent rails on stairs (extra setup, but efficient traversal).\u003c/p\u003e\u003c/li\u003e\u003c/ul\u003e\u003c!--EndFragment--\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\n### Additional information\n\nAdditional Information – Illustration\n\nKey Points about Morris Traversal:\n\nMemory Efficient: Uses O(1) extra space, unlike recursion or stack.\n\nInorder Traversal: Left → Root → Right.\n\nTemporary Tree Modification: Creates “threads” to revisit nodes.\n\nFuture Extension: Preorder Morris Traversal is possible with minor changes.\n\nReference: Wikipedia → Threaded binary tree / Morris Traversal.\n\nConceptual Analogy:\n\nNormal Traversal (Stack/Recursion) → Carrying bags for each level\nMorris Traversal → Using built-in tree connections temporarily, no bags\nThreaded Tree → Installing permanent pathways for future traversals","author":{"url":"https://github.com/aniket866","@type":"Person","name":"aniket866"},"datePublished":"2025-09-29T20:54:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/1806/JavaScript/issues/1806"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:084f239e-6ab7-192a-d7fd-4c2e77688fa8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 98B0:1B5D7A:588B878:78E7FF8:696E41B8 |
| html-safe-nonce | 714a3b3367a2d55ba18d24aaedb3d8c82b73e594992228eb522e9a36f0dcfd59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEIwOjFCNUQ3QTo1ODhCODc4Ojc4RTdGRjg6Njk2RTQxQjgiLCJ2aXNpdG9yX2lkIjoiMTc5OTI3MDU1NDkzMDEzNTQ4MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5b52590145e3ca7e3196842ace5e32dcb69ce5d25c80a6e0588e20a6b390282e |
| hovercard-subject-tag | issue:3466587281 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/JavaScript/1806/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1cd0fe0bbc436640b1d25b4c3fa488e8d57b6fed540cb55aa2e8ec0d33830d30/TheAlgorithms/JavaScript/issues/1806 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1cd0fe0bbc436640b1d25b4c3fa488e8d57b6fed540cb55aa2e8ec0d33830d30/TheAlgorithms/JavaScript/issues/1806 |
| og:image:alt | Motivation Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Checklist: I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Javasc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aniket866 |
| hostname | github.com |
| expected-hostname | github.com |
| None | abd163b7d9c46b4a930c1f194b3178a41170cabf658619a75407b1f9208b96e4 |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/JavaScript git https://github.com/TheAlgorithms/JavaScript.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 97086543 |
| octolytics-dimension-repository_nwo | TheAlgorithms/JavaScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 97086543 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/JavaScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9072c2b7e26adf699c492532ab0d77c337ccb0ac |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width