Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Morris Tree Traversal implementation #2892

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ritesh1408
Copy link

@Ritesh1408 Ritesh1408 commented Oct 30, 2024

Description of Change

Key Changes:
Morris traversal function for in-order traversal
Edge case handling for empty trees and single-node trees

Testing:
Included tests:

Simple binary trees
Skewed trees (left and right)
Complex trees with varying node configurations

Sample Test Cases
Basic Tree with Depth 2:

Input Tree:

1

/
2 3

Expected In-Order Traversal Output: 2 1 3
Complete Tree with Depth 3:

Input Tree:

  1
/   \

2 3
/ \ /
4 5 6 7

Expected In-Order Traversal Output: 4 2 5 1 6 3 7
Left-Skewed Tree:

Input Tree:

1

/
2
/
3
/ 4

  • Expected In-Order Traversal Output: 4 3 2 1

Right-Skewed Tree:

Input Tree:

1

2

3

4
Expected In-Order Traversal Output: 1 2 3 4
Tree with Only Leaf Nodes on Left Subtree:

Input Tree:

  1
/   \

2 3
/
4
Expected In-Order Traversal Output: 4 2 1 3
Tree with Multiple Levels and Mixed Branching:

Input Tree:

     1
   /   \
  2     3
 / \     \
4   5     6
     \
      7

Expected In-Order Traversal Output: 4 2 5 7 1 3 6

Validation Steps

Memory Efficiency Check: Ensure that the Morris traversal function is not allocating additional memory (stacks or recursion frames) and maintains constant space usage, as it modifies the tree structure for in-order traversal.

Edge Case Handling:

Empty Tree: Input -1 as the first data point to create an empty tree and confirm that Morris traversal handles it gracefully.

Single Node Tree: Input only one node, ensuring the traversal output is the value of that single node.

Output Verification: Compare the Morris traversal output to expected in-order traversal results for each case to confirm accuracy.
Notes: Please verify the in-order traversal output matches expected values for different binary tree structures.

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant