# Inorder Tree Traversal without recursion and stack!

With inorder tree traversal, nodes are visited in the following order: left child, current node, then right child. Commonly, this sequence is referred to as "LNR."

A systematic method for exploring and processing each node in a binary tree is provided by inorder tree traversal, which enables actions like printing, searching, or processing the nodes in a particular order (left, root, right).

A detailed explanation of the recursive inorder tree traversal is provided below:

i) Base Case: Return in the event that the current node is null (empty). The recursion uses this as its basic case.

ii) Visit Left Child: Repeat the process for the left subtree by walking via the left child.

iii) Current Node Visit: Visit or perform operations on the current node. This entails taking some action on the current node, such as printing its value, in the case of in-order traversal.

iv) Visit Right Child: Continue to navigate to the right child of the subtree.

In order to obtain the required traversal order while employing norder Tree Traversal without recursion or a stack, the tree structure is typically temporarily modified. Using threaded binary trees, where some nodes have threads (points) that "thread" together nodes in the appropriate traversal order, is one popular strategy.

### In order, tree traversal has several benefits.

1. Ordered in reverse: For binary search trees, inorder traversal traverses nodes in ascending order, making it handy for activities like looking for a specific node in a BST.
2. In many circumstances, in-order traversal offers a balanced traversal of the tree, which is useful for a variety of applications.
3. Memory Efficient: Inorder traversal, notably Morris traversal, can be implemented without the use of additional memory structures like stacks.
4. Non-Recursive Approach: In-order traversal can be implemented without the use of recursion, which is crucial in languages or systems where recursion may be limited or wasteful.

### Negative effects of inorder tree traversal

1. Temporal Difficulty: O(n), where n is the number of nodes in the tree, is the temporal complexity of the inorder traversal. This is due to the traversal's requirement to visit each node just once.
2. Space Complexity (Using Stack): In the case of using a stack for recursion or iteration, the space complexity might range from O(h), where h is the height of the tree, to O(n), in the worst case (for skewed trees).
3. Alters the Tree Structure (Morris Traversal): The Morris Traversal approach alters the tree structure momentarily, which may not be appropriate in circumstances where such adjustments are prohibited.
4. Inorder traversal is excellent for finding nodes in sorted order, but it may not be the most effective method for other operations, such as tree rebuilding or some searches.

### Recursion

Recursion is a programming method where a function calls itself to solve an issue. Recursive functions divide a larger problem into smaller, related subproblems and call themselves to resolve those subproblems. When a base case is reached, the recursion stops, and the answers are merged to solve the original problem. Each iteration of the recursive call breaks the original problem down into simpler examples.

### Typically, recursive functions consist of two fundamental parts:

Base Case: A circumstance outlining the point at which the recursion should end. It is the most straightforward version of the issue that can be resolved without further recursion.

Case Recursive: a set of guidelines outlining how to separate the issue into smaller issues and make

Tree traversals, factorial computations, and the creation of mathematical sequences are a few examples of issues that may be broken down into similar subproblems and are frequently solved via recursion.

### Stack

The Last-In-First-Out (LIFO) principle, which states that the last item inserted is the first one withdrawn, is used to organize data in a stack. It performs these two functions:

Push: Adds a piece to the stack's top.

Pop: Takes the top item off the stack and replaces it.

In the real world, a stack of objects, like a stack of plates, where only the top plate may be reached, is analogous to the stack structure. To manage control flow, local variables, and function calls, a stack is commonly used in programming.

Within the environment of recurrence, a stack of functions is used to keep a record of the functions it calls. Whenever a method is called, the stack of data is pushed up with variables that are local and the address of the return value or the location in the program wherever the function in question is performed. The program can continue where it left off when a function completes by popping its local variables and returning the address of the stack.

In conclusion, recursion is a programming technique that entails decomposing a problem into smaller, related subproblems and recursively solving each one. A stack is a data structure that adheres to the LIFO principle and is frequently employed to control function calls and the contexts in which they are made. At the same time, a program is being executed, notably in recursive algorithms.

## Types Of Tree Traversal

LNR (Left, Node, Right) Inorder Traversal:

An inorder traversal involves visiting a node's left subtree, the node itself, and then the right subtree.

- It is helpful for actions like searching, printing, or processing nodes in sorted order since this traversal order is frequently used in binary search trees (BSTs) to visit nodes in ascending order.

- Traversal (NLR - Node, Left, Right) preorder:

A preorder traversal involves visiting the node, the left subtree, and then the right subtree in that order.

- Preorder traversal is excellent for actions that need a top-down view of the tree since it can be used to duplicate the tree or to represent a tree structure.

LRN (Left, Right, Node) Postorder Traversal:

- A postorder traversal involves visiting the left subtree, the right subtree, and then the node itself in that order.

- When doing operations that call for a bottom-up view of the tree, including deleting every node in the tree or analyzing expressions the tree is a representation of, postorder traversal is frequently utilized.

The following is a general method to perform an inorder traverse without recursion or a stack:

Modify the Tree to Thread Nodes: You can traverse the tree without using a stack or recursion by altering the tree structure to include threading (points) to each node. One should be directed to the following node to visit the traversal in sequence by the threading.

Perform inorder traversal: by moving the threads through the updated tree in the inorder traversal order.

Code

Output:

## Key Points of Inorder Tree Traversal without Recursion and Stack

• Morris Traversal's efficiency: eliminates the need for additional data structures like recursion or a stack and enables efficient in-order tree traversal. With an O(n) time complexity and an O(1) space complexity, it is memory-efficient and appropriate for trees with many nodes.
• Morris Traversal does not require additional data structures, in contrast to conventional recursive or stack-based techniques, which makes it beneficial in situations where memory utilization is an issue or when recursion depth may be constrained.
• Temporary Tree Modification: Using pointers (threads) to move between nodes, this technique briefly modifies the tree structure. Despite the fact that this adjustment is only temporary and does not permanently change the tree, it is still vital to exercise caution when working with it.
• Applications: Finding nodes in a binary search tree in sorted order is one of several applications for which in-order tree traversal is a fundamental operation. An effective non-recursive method to do this traversal is the Morris Traversal.
• Complexity: Although the Morris Traversal algorithm initially appears to be complicated, it is a tried-and-true method that makes the in-order traversal procedure simple.

## Conclusion

In conclusion, the Morris traverse method can be used to build an inorder tree traverse without recursion and the use of a stack. To efficiently traverse the nodes in the inorder sequence (left child, current node, right child), Morris Traversal momentarily threads the binary tree.

Overall, Morris Traversal is a strong and effective method that balances memory use with traversal efficiency, making it a vital tool for tree traversal operations in computer programming. Morris Traversal supports inorder tree traversal without recursion and a stack.