Skip to content

Commit

Permalink
Update TreeTraversal - BFS.cs
Browse files Browse the repository at this point in the history
Updated tree traversal with Morris Threaded Inorder and PreOrder traversal
  • Loading branch information
F4C3L3SS committed Feb 12, 2022
1 parent e170f7e commit a72bde3
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Binary Tree/TreeTraversal - BFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,94 @@ public static void printListElements()
Console.WriteLine();
}

/*
* Uses O(1) space and O(n) time
* 1. if left is null, print current node and go right
* 2. before going left, make right most node on left subtree connected to current node, then go left
* 3. if thread is already pointed to current node, then remove the thread
*/
public static void MorrisThreadedInorderTraversal(TreeNode root)
{
Console.WriteLine("\nMorris Threaded Inorder Traversal: ");
TreeNode? current, prev;

if (root == null)
return;

current = root;

while(current != null)
{
// if left is null, print current node and go right
if (current.left == null)
{
Console.Write(current.data + " ");
current = current.right;
}
else
{
// before going left, make right most node on left subtree connected to current node, then go left

prev = current.left;
while (prev.right != null && prev.right != current)
prev = prev.right; // finding the rightmost element in the left subtree

if(prev.right == null)
{
prev.right = current; // set the thread from rightmost element to the current (our way to return back)
current = current.left; // now we can go left safely withouth losing the current
}
else
{ // if thread already existed, we need to remove the link now
prev.right = null;
Console.Write(current.data + " "); // we are at root, print it
current = current.right;
}
}
}
}

public static void MorrisThreadedPreorderTraversal(TreeNode root)
{
Console.WriteLine("\nMorris Threaded Preorder Traversal: ");
TreeNode? current, prev;

if (root == null)
return;

current = root;

while (current != null)
{
// if left is null, print current node and go right
if (current.left == null)
{
Console.Write(current.data + " ");
current = current.right;
}
else
{
// before going left, make right most node on left subtree connected to current node, then go left

prev = current.left;
while (prev.right != null && prev.right != current)
prev = prev.right; // finding the rightmost element in the left subtree

if (prev.right == null)
{
prev.right = current; // set the thread from rightmost element to the current (our way to return back)
Console.Write(current.data + " "); // this is the only change, we are printing here only before going left
current = current.left; // now we can go left safely withouth losing the current
}
else
{ // if thread already existed, we need to remove the link now
prev.right = null;
current = current.right;
}
}
}
}

public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
Expand All @@ -96,6 +184,8 @@ public static void Main(String[] args)
tree.temp.right.left = new TreeNode(15);
LevelOrderRecursive(tree.temp);
LevelOrderIterative(tree.temp);
MorrisThreadedInorderTraversal(tree.temp);
MorrisThreadedPreorderTraversal(tree.temp);
}
}
}
Expand Down

0 comments on commit a72bde3

Please sign in to comment.