Skip to main content

Flatten binary tree in order of postorder traversal in scala

Flattening tree nodes in order of postorder traversal

Scala program for Flatten binary tree in order of postorder traversal. Here mentioned other language solution.

/* 
  Scala program for
  Flatten binary tree in order of post-order traversal
*/
// Node of Binary Tree
class TreeNode(var data: Int,
	var left: TreeNode,
		var right: TreeNode)
{
	def this(data: Int)
	{
		// Set node value
		this(data, null, null);
	}
}
class BinaryTree(var root: TreeNode,
	var back: TreeNode)
{
	def this()
	{
		this(null, null);
	}
	// Recursive function
	// Display postorder view of binary tree
	def postOrder(node: TreeNode): Unit = {
		if (node != null)
		{
			postOrder(node.left);
			postOrder(node.right);
			// Print node value
			print("  " + node.data);
		}
	}
	// This are flattening tree nodes in postorder from
	def transform(node: TreeNode): Unit = {
		if (node != null)
		{
			// Recursive executing left and right subtree
			transform(node.left);
			transform(node.right);
			if (this.back == null)
			{
				// This is first node of postorder traversal
				// Get first node of transform tree
				this.root = node;
			}
			else
			{
				// Next node
				this.back.right = node;
				// We taking only one direction
				this.back.left = null;
			}
			this.back = node;
		}
	}
	// This are handling the request of
	// flatten tree nodes in post order from.
	def flattenNode(): Unit = {
		if (this.root == null)
		{
			// When empty tree
			return;
		}
		// Set back node
		this.back = null;
		// Perform flatten operation
		transform(this.root);
		if (this.back != null)
		{
			// Set last node of post order
			this.back.left = null;
			this.back.right = null;
		}
		this.back = null;
	}
	// Display flatten elements of tree
	def showElement(): Unit = {
		if (this.root == null)
		{
			println("\n Empty Tree");
			return;
		}
		println("\n Flatten Tree Node in Postorder : ");
		var temp: TreeNode = this.root;
		// Iterate tree elements
		while (temp != null)
		{
			// Display node value
			print("  " + temp.data);
			// Visit to next node
			temp = temp.right;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// New tree
		var tree: BinaryTree = new BinaryTree();
		/*
		    Construct Binary Tree
		    -----------------------
		           1
		          / \ 
		         /   \
		        6     8
		       / \   / \
		      2   3 7   5
		     /   /   \   \
		    9   4    -6   11
		*/
		// Add nodes
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(6);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left = new TreeNode(7);
		tree.root.right.left.right = new TreeNode(-6);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(4);
		tree.root.left.left.left = new TreeNode(9);
		tree.root.right.right.right = new TreeNode(11);
		// Display tree elements
		println("\n Postorder Nodes : ");
		tree.postOrder(tree.root);
		// Testing
		tree.flattenNode();
		// After transform
		tree.showElement();
	}
}

Output

 Postorder Nodes :
  9  2  4  3  6  -6  7  11  5  8  1
 Flatten Tree Node in Postorder :
  9  2  4  3  6  -6  7  11  5  8  1




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