Posted on by Kalkicode
Code Binary Tree

Flatten binary tree in order of postorder traversal in c

Flattening tree nodes in order of postorder traversal

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

// Include header file
#include <stdio.h>
#include <stdlib.h>
/* 
  C program for
  Flatten binary tree in order of post-order traversal
*/

// Node of Binary Tree
typedef struct TreeNode
{
	// Define useful field of TreeNode
	int data;
	struct TreeNode * left;
	struct TreeNode * right;
}TreeNode;

TreeNode * getTreeNode(int data)
{
	// Create dynamic memory of TreeNode
	TreeNode * ref = (TreeNode * ) malloc(sizeof(TreeNode));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Set node value
	ref->data = data;
	ref->left = NULL;
	ref->right = NULL;
	return ref;
}

typedef struct BinaryTree
{
	// Define useful field of BinaryTree
	struct TreeNode * root;
	struct TreeNode * back;
}BinaryTree;

BinaryTree * getBinaryTree()
{
	// Create dynamic memory of BinaryTree
	BinaryTree * ref = (BinaryTree * ) malloc(sizeof(BinaryTree));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Set initial tree root to null
	ref->root = NULL;
	ref->back = NULL;
	return ref;
}
// Recursive function
// Display postorder view of binary tree
void postOrder(TreeNode * node)
{
	if (node != NULL)
	{
		postOrder(node->left);
		postOrder(node->right);
		// Print node value
		printf("  %d", node->data);
	}
}
// This are flattening tree nodes in postorder from
void transform(BinaryTree * ref, TreeNode * node)
{
	if (node != NULL)
	{
		// Recursive executing left and right subtree
		transform(ref,node->left);
		transform(ref,node->right);
		if (ref->back == NULL)
		{
			// This is first node of postorder traversal
			// Get first node of transform tree
			ref->root = node;
		}
		else
		{
			// Next node
			ref->back->right = node;
			// We taking only one direction
			ref->back->left = NULL;
		}
		ref->back = node;
	}
}
// This are handling the request of
// flatten tree nodes in post order from.
void flattenNode(BinaryTree * ref)
{
	if (ref->root == NULL)
	{
		// When empty tree
		return;
	}
	// Set back node
	ref->back = NULL;
	// Perform flatten operation
	transform(ref,ref->root);
	if (ref->back != NULL)
	{
		// Set last node of post order
		ref->back->left = NULL;
		ref->back->right = NULL;
	}
	ref->back = NULL;
}
// Display flatten elements of tree
void showElement(BinaryTree * ref)
{
	if (ref->root == NULL)
	{
		printf("\n Empty Tree");
		return;
	}
	printf("\n Flatten Tree Node in Postorder : \n");
	TreeNode * temp = ref->root;
	// Iterate tree elements
	while (temp != NULL)
	{
		// Display node value
		printf("  %d", temp->data);
		// Visit to next node
		temp = temp->right;
	}
}
int main()
{
	// New tree
	BinaryTree * tree = getBinaryTree();
	/*
	    Construct Binary Tree
	    -----------------------
	           1
	          / \ 
	         /   \
	        6     8
	       / \   / \
	      2   3 7   5
	     /   /   \   \
	    9   4    -6   11
	*/
	// Add nodes
	tree->root = getTreeNode(1);
	tree->root->left = getTreeNode(6);
	tree->root->left->left = getTreeNode(2);
	tree->root->right = getTreeNode(8);
	tree->root->right->right = getTreeNode(5);
	tree->root->right->left = getTreeNode(7);
	tree->root->right->left->right = getTreeNode(-6);
	tree->root->left->right = getTreeNode(3);
	tree->root->left->right->left = getTreeNode(4);
	tree->root->left->left->left = getTreeNode(9);
	tree->root->right->right->right = getTreeNode(11);
	// Display tree elements
	printf("\n Postorder Nodes : \n");
	postOrder(tree->root);
	// Testing
	flattenNode(tree);
	// After transform
	showElement(tree);
	return 0;
}

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