BST
Sorting Algorithms
In-order Traversal
Constant Space Complexity
Computer Science

Sort BST in On using constant memory

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In problems related to binary search trees (BST), a common task is to sort or traverse the elements efficiently. The constraint of using constant memory while sorting a BST to a list is quite challenging, as typical in-order traversals use stacks or recursion, which require additional space proportional to the height of the tree. However, Morris Traversal offers an elegant solution, allowing us to perform an in-order traversal in O(n)O(n) time using O(1)O(1) additional memory. Let's delve deeper into this technique and its practical applications.

Morris Traversal: An Overview

Morris Traversal is a method to traverse a BST in in-order sequence using thread concepts. It cleverly uses the "unused" right pointer of leaf nodes to store temporary navigation pointers, known as "threads," to the successor of the node. By doing this, it avoids recursion or stack use entirely, achieving constant space usage.

Steps of Morris Traversal

  1. Initialization: Start from the root node of the BST.
  2. Traversal Loop:
    • If the left child of the current node is `None`, print the current node's value and move to the right child.
    • If the left child exists, find the inorder predecessor of the current node, which is the rightmost node in the current node's left subtree.
    • Modify the predecessor's right pointer:
      • If the predecessor’s right is `None`, set it to point back to the current node (creating a temporary thread), and move to the left child of the current node.
      • If the predecessor’s right is pointing to the current node (a thread exists), revert this change (remove the thread), print the current node’s value, and move to the right child.

This method allows us to visit each node in `O(1)` time outside the context of modifying the thread, leading to an overall time complexity of O(n)O(n) where nn is the number of nodes in the BST.

Example Execution

Consider the BST:

3 7 2 4 6 8 1

  • Constant Space Usage: Morris Traversal's primary advantage is space efficiency, using only `O(1)` additional space apart from the input tree structure.
  • No Stack/Recursion: It effectively eliminates the need for recursion or stack structures, reducing overhead and simplifying memory management on constrained systems.
  • Tree Modification: The technique temporarily modifies the tree, which may be risky in concurrent or parallel situations where the tree is shared among multiple processes.
  • Threading Management: Error-prone due to manual management of pointers, requiring careful programming to ensure no permanent alterations to the tree structure.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.