Skip to main content

Morris postorder traversal in c#

Csharp program for Morris postorder traversal. Here more information.

// Include namespace system
using System;
/* 
  Csharp program for
  Morris traversal for postorder
*/
// Binary Tree Node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set the initial value of root
		this.root = null;
	}
	// Recursive function
	// Display postorder view of binary tree
	public void postorder(TreeNode node)
	{
		if (node != null)
		{
			this.postorder(node.left);
			this.postorder(node.right);
			// Print node value
			Console.Write("  " + node.data.ToString());
		}
	}
	// iterative postorder tree traversal
	public void morrisPostorder()
	{
		if (this.root == null)
		{
			return;
		}
		// Create a dummy node
		var dummy_node = new TreeNode(0);
		dummy_node.left = this.root;
		var current = dummy_node;
		// Define some useful variables
		TreeNode parent = null;
		TreeNode middle = null;
		TreeNode auxiliary = null;
		TreeNode back = null;
		// iterating tree nodes
		while (current != null)
		{
			if (current.left == null)
			{
				// When left child are empty then
				// Visit to right child
				current = current.right;
			}
			else
			{
				// Get to left child
				auxiliary = current.left;
				while (auxiliary.right != null && auxiliary.right != current)
				{
					auxiliary = auxiliary.right;
				}
				if (auxiliary.right != current)
				{
					auxiliary.right = current;
					current = current.left;
				}
				else
				{
					parent = current;
					middle = current.left;
					// Update new path
					while (middle != current)
					{
						back = middle.right;
						middle.right = parent;
						parent = middle;
						middle = back;
					}
					parent = current;
					middle = auxiliary;
					// Print the resultant nodes.
					// And correct node link in current path
					while (middle != current)
					{
						Console.Write("  " + middle.data.ToString());
						back = middle.right;
						middle.right = parent;
						parent = middle;
						middle = back;
					}
					// Unlink previous bind element
					auxiliary.right = null;
					// Visit to right child
					current = current.right;
				}
			}
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		// Create new tree
		var tree = new BinaryTree();
		/*
		    Create Binary Tree
		         4
		       /   \
		      8     3
		     / \   / \
		    1   6 7   2
		       /   \   \
		      9     10  11
		*/
		// Add tree node
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(8);
		tree.root.left.left = new TreeNode(1);
		tree.root.right = new TreeNode(3);
		tree.root.right.right = new TreeNode(2);
		tree.root.right.right.right = new TreeNode(11);
		tree.root.right.left = new TreeNode(7);
		tree.root.right.left.right = new TreeNode(10);
		tree.root.left.right = new TreeNode(6);
		tree.root.left.right.left = new TreeNode(9);
		Console.WriteLine("\n Recursive Postorder");
		// Display Tree elements
		tree.postorder(tree.root);
		Console.WriteLine("\n Morris Postorder");
		tree.morrisPostorder();
	}
}

Output

 Recursive Postorder
  1  9  6  8  10  7  11  2  3  4
 Morris Postorder
  1  9  6  8  10  7  11  2  3  4




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment