Convert binary tree into min max product

Here given code implementation process.

/*
    C Program 
    Convert binary tree into min max product
*/
#include <stdio.h>

#include <stdlib.h>

//Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
struct Collector
{
	int min;
	int max;
};
//This is creating a binary tree node and return this
struct Node *add_node(int data)
{
	// Create dynamic node
	struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
	if (new_node != NULL)
	{
		//Set data and pointer values
		new_node->data = data;
		new_node->left = NULL;
		new_node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return new_node;
}
//Recursive function
//Display preorder view of binary tree
void preorder(struct Node *node)
{
	if (node != NULL)
	{
		//Print node value
		printf("  %d", node->data);
		preorder(node->left);
		preorder(node->right);
	}
}
// Returns a maximum value of given two numbers
int big(int a, int b)
{
	if (a > b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
// Returns a minimum value of given two numbers
int small(int a, int b)
{
	if (a < b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
//Replace the given binary tree nodes with its min-max product
struct Collector *min_max_product(struct Node *node)
{
	if (node != NULL)
	{
		// Get current node data
		int element = node->data;
		struct Collector *info = (struct Collector *) malloc(sizeof(struct Collector));
		if (node->left == NULL && node->right == NULL)
		{
			//When leaf node 
			info->max = element;
			info->min = element;
			return info;
		}
		struct Collector *left = min_max_product(node->left);
		struct Collector *right = min_max_product(node->right);
		if (node->left != NULL && node->right != NULL)
		{
			// When node have left and right subtree 
			// Change node value
			node->data = small(left->min, right->min) * big(right->max, left->max);
			// Update new min and max value
			info->min = small(left->min, small(right->min, element));
			info->max = big(left->max, big(right->max, element));
		}
		else if (node->right != NULL)
		{
			// When node have right subtree
			// Change node value
			node->data = right->min * right->max;
			// Update new min and max value
			info->min = small(right->min, element);
			info->max = big(right->max, element);
		}
		else
		{
			// When node have left subtree
			// Change node value
			node->data = left->min * left->max;
			// Update new min and max value
			info->min = small(left->min, element);
			info->max = big(left->max, element);
		}
		return info;
	}
	return NULL;
}
int main()
{
	struct Node *root = NULL;
	/*     
	        8
	      /   \
	     4     9
	    / \   /  \
	   7   3 5    2
	      /   \
	     6     7
	*/
	root = add_node(8);
	root->left = add_node(4);
	root->left->right = add_node(3);
	root->left->left = add_node(7);
	root->right = add_node(9);
	root->right->left = add_node(5);
	root->right->right = add_node(2);
	root->left->right->left = add_node(6);
	root->right->left->right = add_node(7);
	//Display Tree elements
	printf("\n Before \n");
	preorder(root);
	min_max_product(root);
	printf("\n After \n");
	preorder(root);
	return 0;
}

Output

 Before
  8  4  7  3  6  9  5  7  2
 After
  18  21  7  36  6  14  49  7  2
/*
    Java Program
    Convert binary tree into min max product
*/
//Binary Tree node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		//set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class Collector
{
	public int max;
	public int min;
	public Collector()
	{
		this.max = 0;
		this.min = 0;
	}
}
class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	//Display tree elements in preorder form
	public void preorder(Node node)
	{
		if (node != null)
		{
			//Print node value
			System.out.print(" " + node.data + " ");
			preorder(node.left);
			preorder(node.right);
		}
	}
	// Returns a maximum value of given two numbers
	int big(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns a minimum value of given two numbers
	int small(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	public Collector min_max_product(Node node)
	{
		if (node != null)
		{
			// Get current node data
			int element = node.data;
			Collector info = new Collector();
			if (node.left == null && node.right == null)
			{
				//When leaf node 
				info.max = element;
				info.min = element;
				return info;
			}
			Collector left = min_max_product(node.left);
			Collector right = min_max_product(node.right);
			if (node.left != null && node.right != null)
			{
				// When node have left and right subtree 
				// Change node value
				node.data = small(left.min, right.min) * big(right.max, left.max);
				// Update new min and max value
				info.min = small(left.min, small(right.min, element));
				info.max = big(left.max, big(right.max, element));
			}
			else if (node.right != null)
			{
				// When node have right subtree
				// Change node value
				node.data = right.min * right.max;
				// Update new min and max value
				info.min = small(right.min, element);
				info.max = big(right.max, element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				node.data = left.min * left.max;
				// Update new min and max value
				info.min = small(left.min, element);
				info.max = big(left.max, element);
			}
			return info;
		}
		return null;
	}
	public static void main(String[] args)
	{
		// Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/*
		Construct Binary Tree
		-----------------------         
		        8
		      /   \
		     4     9
		    / \   /  \
		   7   3 5    2
		      /   \
		     6     7
		*/
		tree.root = new Node(8);
		tree.root.left = new Node(4);
		tree.root.left.right = new Node(3);
		tree.root.left.left = new Node(7);
		tree.root.right = new Node(9);
		tree.root.right.left = new Node(5);
		tree.root.right.right = new Node(2);
		tree.root.left.right.left = new Node(6);
		tree.root.right.left.right = new Node(7);
		System.out.print("\n Before \n");
		//Display Tree elements
		tree.preorder(tree.root);
		tree.min_max_product(tree.root);
		System.out.print("\n After \n");
		tree.preorder(tree.root);
	}
}

Output

 Before
 8  4  7  3  6  9  5  7  2
 After
 18  21  7  36  6  14  49  7  2
//Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Convert binary tree into min max product
*/

//Binary Tree node
class Node
{
	public: int data;
	Node *left;
	Node *right;
	Node(int data)
	{
		//set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class Collector
{
	public: int max;
	int min;
	Collector()
	{
		this->max = 0;
		this->min = 0;
	}
};
class BinaryTree
{
	public: Node *root;
	BinaryTree()
	{
		//Set initial tree root to null
		this->root = NULL;
	}
	//Display tree elements in preorder form
	void preorder(Node *node)
	{
		if (node != NULL)
		{
			//Print node value
			cout << " " << node->data << " ";
			this->preorder(node->left);
			this->preorder(node->right);
		}
	}
	// Returns a maximum value of given two numbers
	int big(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns a minimum value of given two numbers
	int small(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	Collector *min_max_product(Node *node)
	{
		if (node != NULL)
		{
			// Get current node data
			int element = node->data;
			Collector *info = new Collector();
			if (node->left == NULL && node->right == NULL)
			{
				//When leaf node 
				info->max = element;
				info->min = element;
				return info;
			}
			Collector *left = this->min_max_product(node->left);
			Collector *right = this->min_max_product(node->right);
			if (node->left != NULL && node->right != NULL)
			{
				// When node have left and right subtree 
				// Change node value
				node->data = this->small(left->min, right->min) *this->big(right->max, left->max);
				// Update new min and max value
				info->min = this->small(left->min, this->small(right->min, element));
				info->max = this->big(left->max, this->big(right->max, element));
			}
			else if (node->right != NULL)
			{
				// When node have right subtree
				// Change node value
				node->data = right->min *right->max;
				// Update new min and max value
				info->min = this->small(right->min, element);
				info->max = this->big(right->max, element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				node->data = left->min *left->max;
				// Update new min and max value
				info->min = this->small(left->min, element);
				info->max = this->big(left->max, element);
			}
			return info;
		}
		return NULL;
	}
};
int main()
{
	// Make object of binary tree
	BinaryTree tree = BinaryTree();
	tree.root = new Node(8);
	tree.root->left = new Node(4);
	tree.root->left->right = new Node(3);
	tree.root->left->left = new Node(7);
	tree.root->right = new Node(9);
	tree.root->right->left = new Node(5);
	tree.root->right->right = new Node(2);
	tree.root->left->right->left = new Node(6);
	tree.root->right->left->right = new Node(7);
	cout << "\n Before \n";
	//Display Tree elements
	tree.preorder(tree.root);
	tree.min_max_product(tree.root);
	cout << "\n After \n";
	tree.preorder(tree.root);
	return 0;
}

Output

 Before
 8  4  7  3  6  9  5  7  2
 After
 18  21  7  36  6  14  49  7  2
//Include namespace system
using System;

/*
    C# Program
    Convert binary tree into min max product
*/

//Binary Tree node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class Collector
{
	public int max;
	public int min;
	public Collector()
	{
		this.max = 0;
		this.min = 0;
	}
}
class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	//Display tree elements in preorder form
	public void preorder(Node node)
	{
		if (node != null)
		{
			//Print node value
			Console.Write(" " + node.data + " ");
			preorder(node.left);
			preorder(node.right);
		}
	}
	// Returns a maximum value of given two numbers
	int big(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns a minimum value of given two numbers
	int small(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	public Collector min_max_product(Node node)
	{
		if (node != null)
		{
			// Get current node data
			int element = node.data;
			Collector info = new Collector();
			if (node.left == null && node.right == null)
			{
				//When leaf node 
				info.max = element;
				info.min = element;
				return info;
			}
			Collector left = min_max_product(node.left);
			Collector right = min_max_product(node.right);
			if (node.left != null && node.right != null)
			{
				// When node have left and right subtree 
				// Change node value
				node.data = small(left.min, right.min) * big(right.max, left.max);
				// Update new min and max value
				info.min = small(left.min, small(right.min, element));
				info.max = big(left.max, big(right.max, element));
			}
			else if (node.right != null)
			{
				// When node have right subtree
				// Change node value
				node.data = right.min * right.max;
				// Update new min and max value
				info.min = small(right.min, element);
				info.max = big(right.max, element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				node.data = left.min * left.max;
				// Update new min and max value
				info.min = small(left.min, element);
				info.max = big(left.max, element);
			}
			return info;
		}
		return null;
	}
	public static void Main(String[] args)
	{
		// Make object of binary tree
		BinaryTree tree = new BinaryTree();
		tree.root = new Node(8);
		tree.root.left = new Node(4);
		tree.root.left.right = new Node(3);
		tree.root.left.left = new Node(7);
		tree.root.right = new Node(9);
		tree.root.right.left = new Node(5);
		tree.root.right.right = new Node(2);
		tree.root.left.right.left = new Node(6);
		tree.root.right.left.right = new Node(7);
		Console.Write("\n Before \n");
		//Display Tree elements
		tree.preorder(tree.root);
		tree.min_max_product(tree.root);
		Console.Write("\n After \n");
		tree.preorder(tree.root);
	}
}

Output

 Before
 8  4  7  3  6  9  5  7  2
 After
 18  21  7  36  6  14  49  7  2
<?php
/*
    Php Program
    Convert binary tree into min max product
*/
//Binary Tree node
class Node
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class Collector
{
	public $max;
	public $min;

	function __construct()
	{
		$this->max = 0;
		$this->min = 0;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		//Set initial tree root to null
		$this->root = null;
	}
	//Display tree elements in preorder form
	public	function preorder($node)
	{
		if ($node != null)
		{
			//Print node value
			echo " ". $node->data ." ";
			$this->preorder($node->left);
			$this->preorder($node->right);
		}
	}
	// Returns a maximum value of given two numbers
	function big($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		else
		{
			return $b;
		}
	}
	// Returns a minimum value of given two numbers
	function small($a, $b)
	{
		if ($a < $b)
		{
			return $a;
		}
		else
		{
			return $b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	public	function min_max_product($node)
	{
		if ($node != null)
		{
			// Get current node data
			$element = $node->data;
			$info = new Collector();
			if ($node->left == null && $node->right == null)
			{
				//When leaf node 
				$info->max = $element;
				$info->min = $element;
				return $info;
			}
			$left = $this->min_max_product($node->left);
			$right = $this->min_max_product($node->right);
			if ($node->left != null && $node->right != null)
			{
				// When node have left and right subtree 
				// Change node value
				$node->data = $this->small($left->min, $right->min) * $this->big($right->max, $left->max);
				// Update new min and max value
				$info->min = $this->small($left->min, $this->small($right->min, $element));
				$info->max = $this->big($left->max, $this->big($right->max, $element));
			}
			else if ($node->right != null)
			{
				// When node have right subtree
				// Change node value
				$node->data = $right->min * $right->max;
				// Update new min and max value
				$info->min = $this->small($right->min, $element);
				$info->max = $this->big($right->max, $element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				$node->data = $left->min * $left->max;
				// Update new min and max value
				$info->min = $this->small($left->min, $element);
				$info->max = $this->big($left->max, $element);
			}
			return $info;
		}
		return null;
	}
}

function main()
{
	// Make object of binary tree
	$tree = new BinaryTree();
	/*
			Construct Binary Tree
			-----------------------         
			        8
			      /   \
			     4     9
			    / \   /  \
			   7   3 5    2
			      /   \
			     6     7
			*/
	$tree->root = new Node(8);
	$tree->root->left = new Node(4);
	$tree->root->left->right = new Node(3);
	$tree->root->left->left = new Node(7);
	$tree->root->right = new Node(9);
	$tree->root->right->left = new Node(5);
	$tree->root->right->right = new Node(2);
	$tree->root->left->right->left = new Node(6);
	$tree->root->right->left->right = new Node(7);
	echo "\n Before \n";
	//Display Tree elements
	$tree->preorder($tree->root);
	$tree->min_max_product($tree->root);
	echo "\n After \n";
	$tree->preorder($tree->root);
}
main();

Output

 Before
 8  4  7  3  6  9  5  7  2
 After
 18  21  7  36  6  14  49  7  2
/*
    Node Js Program
    Convert binary tree into min max product
*/
//Binary Tree node
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class Collector
{
	constructor()
	{
		this.max = 0;
		this.min = 0;
	}
}
class BinaryTree
{
	constructor()
	{
		//Set initial tree root to null
		this.root = null;
	}
	//Display tree elements in preorder form
	preorder(node)
	{
		if (node != null)
		{
			//Print node value
			process.stdout.write(" " + node.data + " ");
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
	// Returns a maximum value of given two numbers
	big(a, b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns a minimum value of given two numbers
	small(a, b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	min_max_product(node)
	{
		if (node != null)
		{
			// Get current node data
			var element = node.data;
			var info = new Collector();
			if (node.left == null && node.right == null)
			{
				//When leaf node 
				info.max = element;
				info.min = element;
				return info;
			}
			var left = this.min_max_product(node.left);
			var right = this.min_max_product(node.right);
			if (node.left != null && node.right != null)
			{
				// When node have left and right subtree 
				// Change node value
				node.data = this.small(left.min, right.min) * this.big(right.max, left.max);
				// Update new min and max value
				info.min = this.small(left.min, this.small(right.min, element));
				info.max = this.big(left.max, this.big(right.max, element));
			}
			else if (node.right != null)
			{
				// When node have right subtree
				// Change node value
				node.data = right.min * right.max;
				// Update new min and max value
				info.min = this.small(right.min, element);
				info.max = this.big(right.max, element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				node.data = left.min * left.max;
				// Update new min and max value
				info.min = this.small(left.min, element);
				info.max = this.big(left.max, element);
			}
			return info;
		}
		return null;
	}
}

function main()
{
	// Make object of binary tree
	var tree = new BinaryTree();
	/*
			Construct Binary Tree
			-----------------------         
			        8
			      /   \
			     4     9
			    / \   /  \
			   7   3 5    2
			      /   \
			     6     7
			*/
	tree.root = new Node(8);
	tree.root.left = new Node(4);
	tree.root.left.right = new Node(3);
	tree.root.left.left = new Node(7);
	tree.root.right = new Node(9);
	tree.root.right.left = new Node(5);
	tree.root.right.right = new Node(2);
	tree.root.left.right.left = new Node(6);
	tree.root.right.left.right = new Node(7);
	process.stdout.write("\n Before \n");
	//Display Tree elements
	tree.preorder(tree.root);
	tree.min_max_product(tree.root);
	process.stdout.write("\n After \n");
	tree.preorder(tree.root);
}
main();

Output

 Before
 8  4  7  3  6  9  5  7  2
 After
 18  21  7  36  6  14  49  7  2
#  Python 3 Program
#  Convert binary tree into min max product

# Binary Tree node
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		self.left = None
		self.right = None
	

class Collector :
	
	def __init__(self) :
		self.max = 0
		self.min = 0
	

class BinaryTree :
	
	def __init__(self) :
		# Set initial tree root to null
		self.root = None
	
	# Display tree elements in preorder form
	def preorder(self, node) :
		if (node != None) :
			# Print node value
			print(" ", node.data ," ", end = "")
			self.preorder(node.left)
			self.preorder(node.right)
		
	
	#  Returns a maximum value of given two numbers
	def big(self, a, b) :
		if (a > b) :
			return a
		else :
			return b
		
	
	#  Returns a minimum value of given two numbers
	def small(self, a, b) :
		if (a < b) :
			return a
		else :
			return b
		
	
	# Replace the given binary tree nodes with its min-max product
	def min_max_product(self, node) :
		if (node != None) :
			#  Get current node data
			element = node.data
			info = Collector()
			if (node.left == None and node.right == None) :
				# When leaf node 
				info.max = element
				info.min = element
				return info
			
			left = self.min_max_product(node.left)
			right = self.min_max_product(node.right)
			if (node.left != None and node.right != None) :
				#  When node have left and right subtree 
				#  Change node value
				node.data = self.small(left.min, right.min) * self.big(right.max, left.max)
				#  Update new min and max value
				info.min = self.small(left.min, self.small(right.min, element))
				info.max = self.big(left.max, self.big(right.max, element))
			
			elif(node.right != None) :
				#  When node have right subtree
				#  Change node value
				node.data = right.min * right.max
				#  Update new min and max value
				info.min = self.small(right.min, element)
				info.max = self.big(right.max, element)
			else :
				#  When node have left subtree
				#  Change node value
				node.data = left.min * left.max
				#  Update new min and max value
				info.min = self.small(left.min, element)
				info.max = self.big(left.max, element)
			
			return info
		
		return None
	

def main() :
	#  Make object of binary tree
	tree = BinaryTree()
	# 
	# 		Construct Binary Tree
	# 		-----------------------         
	# 		        8
	# 		      /   \
	# 		     4     9
	# 		    / \   /  \
	# 		   7   3 5    2
	# 		      /   \
	# 		     6     7
	# 		
	
	tree.root = Node(8)
	tree.root.left = Node(4)
	tree.root.left.right = Node(3)
	tree.root.left.left = Node(7)
	tree.root.right = Node(9)
	tree.root.right.left = Node(5)
	tree.root.right.right = Node(2)
	tree.root.left.right.left = Node(6)
	tree.root.right.left.right = Node(7)
	print("\n Before \n", end = "")
	# Display Tree elements
	tree.preorder(tree.root)
	tree.min_max_product(tree.root)
	print("\n After \n", end = "")
	tree.preorder(tree.root)

if __name__ == "__main__": main()

Output

 Before
  8    4    7    3    6    9    5    7    2
 After
  18    21    7    36    6    14    49    7    2
#  Ruby Program
#  Convert binary tree into min max product

# Binary Tree node
class Node  
	# Define the accessor and reader of class Node  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
 
	def initialize(data) 
		# Set node value
		self.data = data
		self.left = nil
		self.right = nil
	end

end

class Collector  
	# Define the accessor and reader of class Collector  
	attr_reader :max, :min
	attr_accessor :max, :min
 
	
	def initialize() 
		self.max = 0
		self.min = 0
	end

end

class BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root
	attr_accessor :root
 
	
	def initialize() 
		# Set initial tree root to null
		self.root = nil
	end

	# Display tree elements in preorder form
	def preorder(node) 
		if (node != nil) 
			# Print node value
			print(" ", node.data ," ")
			self.preorder(node.left)
			self.preorder(node.right)
		end

	end

	#  Returns a maximum value of given two numbers
	def big(a, b) 
		if (a > b) 
			return a
		else 
			return b
		end

	end

	#  Returns a minimum value of given two numbers
	def small(a, b) 
		if (a < b) 
			return a
		else 
			return b
		end

	end

	# Replace the given binary tree nodes with its min-max product
	def min_max_product(node) 
		if (node != nil) 
			#  Get current node data
			element = node.data
			info = Collector.new()
			if (node.left == nil && node.right == nil) 
				# When leaf node 
				info.max = element
				info.min = element
				return info
			end

			left = self.min_max_product(node.left)
			right = self.min_max_product(node.right)
			if (node.left != nil && node.right != nil) 
				#  When node have left and right subtree 
				#  Change node value
				node.data = self.small(left.min, right.min) * self.big(right.max, left.max)
				#  Update new min and max value
				info.min = self.small(left.min, self.small(right.min, element))
				info.max = self.big(left.max, self.big(right.max, element))
			elsif(node.right != nil) 
				#  When node have right subtree
				#  Change node value
				node.data = right.min * right.max
				#  Update new min and max value
				info.min = self.small(right.min, element)
				info.max = self.big(right.max, element)
			else 
				#  When node have left subtree
				#  Change node value
				node.data = left.min * left.max
				#  Update new min and max value
				info.min = self.small(left.min, element)
				info.max = self.big(left.max, element)
			end

			return info
		end

		return nil
	end

end

def main() 
	#  Make object of binary tree
	tree = BinaryTree.new()
	# 
	# 		Construct Binary Tree
	# 		-----------------------         
	# 		        8
	# 		      /   \
	# 		     4     9
	# 		    / \   /  \
	# 		   7   3 5    2
	# 		      /   \
	# 		     6     7
	# 		
	
	tree.root = Node.new(8)
	tree.root.left = Node.new(4)
	tree.root.left.right = Node.new(3)
	tree.root.left.left = Node.new(7)
	tree.root.right = Node.new(9)
	tree.root.right.left = Node.new(5)
	tree.root.right.right = Node.new(2)
	tree.root.left.right.left = Node.new(6)
	tree.root.right.left.right = Node.new(7)
	print("\n Before \n")
	# Display Tree elements
	tree.preorder(tree.root)
	tree.min_max_product(tree.root)
	print("\n After \n")
	tree.preorder(tree.root)
end

main()

Output

 Before 
 8  4  7  3  6  9  5  7  2 
 After 
 18  21  7  36  6  14  49  7  2 
/*
    Scala Program
    Convert binary tree into min max product
*/
//Binary Tree node
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class Collector(var max: Int,
	var min: Int)
{
	def this()
	{
		this(0, 0);
	}
}
class BinaryTree(var root: Node)
{
	def this()
	{
		this(null);
	}
	//Display tree elements in preorder form
	def preorder(node: Node): Unit = {
		if (node != null)
		{
			//Print node value
			print(" " + node.data + " ");
			preorder(node.left);
			preorder(node.right);
		}
	}
	// Returns a maximum value of given two numbers
	def big(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns a minimum value of given two numbers
	def small(a: Int, b: Int): Int = {
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	def min_max_product(node: Node): Collector = {
		if (node != null)
		{
			// Get current node data
			var element: Int = node.data;
			var info: Collector = new Collector();
			if (node.left == null && node.right == null)
			{
				//When leaf node 
				info.max = element;
				info.min = element;
				return info;
			}
			var left: Collector = min_max_product(node.left);
			var right: Collector = min_max_product(node.right);
			if (node.left != null && node.right != null)
			{
				// When node have left and right subtree 
				// Change node value
				node.data = small(left.min, right.min) * big(right.max, left.max);
				// Update new min and max value
				info.min = small(left.min, small(right.min, element));
				info.max = big(left.max, big(right.max, element));
			}
			else if (node.right != null)
			{
				// When node have right subtree
				// Change node value
				node.data = right.min * right.max;
				// Update new min and max value
				info.min = small(right.min, element);
				info.max = big(right.max, element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				node.data = left.min * left.max;
				// Update new min and max value
				info.min = small(left.min, element);
				info.max = big(left.max, element);
			}
			return info;
		}
		return null;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Make object of binary tree
		var tree: BinaryTree = new BinaryTree();
		/*
				Construct Binary Tree
				-----------------------         
				        8
				      /   \
				     4     9
				    / \   /  \
				   7   3 5    2
				      /   \
				     6     7
				*/
		tree.root = new Node(8);
		tree.root.left = new Node(4);
		tree.root.left.right = new Node(3);
		tree.root.left.left = new Node(7);
		tree.root.right = new Node(9);
		tree.root.right.left = new Node(5);
		tree.root.right.right = new Node(2);
		tree.root.left.right.left = new Node(6);
		tree.root.right.left.right = new Node(7);
		print("\n Before \n");
		//Display Tree elements
		tree.preorder(tree.root);
		tree.min_max_product(tree.root);
		print("\n After \n");
		tree.preorder(tree.root);
	}
}

Output

 Before
 8  4  7  3  6  9  5  7  2
 After
 18  21  7  36  6  14  49  7  2
/*
    Swift 4 Program
    Convert binary tree into min max product
*/
//Binary Tree node
class Node
{
	var data: Int;
	var left: Node? ;
	var right: Node? ;
	init(_ data: Int)
	{
		//Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class Collector
{
	var max: Int;
	var min: Int;
	init()
	{
		self.max = 0;
		self.min = 0;
	}
}
class BinaryTree
{
	var root: Node? ;
	init()
	{
		//Set initial tree root to null
		self.root = nil;
	}
	//Display tree elements in preorder form
	func preorder(_ node: Node? )
	{
		if (node != nil)
		{
			//Print node value
			print(" ", node!.data ," ", terminator: "");
			self.preorder(node!.left);
			self.preorder(node!.right);
		}
	}
	// Returns a maximum value of given two numbers
	func big(_ a: Int, _ b: Int) -> Int
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns a minimum value of given two numbers
	func small(_ a: Int, _ b: Int) -> Int
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	//Replace the given binary tree nodes with its min-max product
	func min_max_product(_ node: Node? ) -> Collector?
	{
		if (node != nil)
		{
			// Get current node data
			let element: Int = node!.data;
			let info: Collector? = Collector();
			if (node!.left == nil && node!.right == nil)
			{
				//When leaf node 
				info!.max = element;
				info!.min = element;
				return info;
			}
			let left: Collector? = self.min_max_product(node!.left);
			let right: Collector? = self.min_max_product(node!.right);
			if (node!.left != nil && node!.right != nil)
			{
				// When node have left and right subtree 
				// Change node value
				node!.data = self.small(left!.min, right!.min) * self.big(right!.max, left!.max);
				// Update new min and max value
				info!.min = self.small(left!.min, self.small(right!.min, element));
				info!.max = self.big(left!.max, self.big(right!.max, element));
			}
			else if (node!.right != nil)
			{
				// When node have right subtree
				// Change node value
				node!.data = right!.min * right!.max;
				// Update new min and max value
				info!.min = self.small(right!.min, element);
				info!.max = self.big(right!.max, element);
			}
			else
			{
				// When node have left subtree
				// Change node value
				node!.data = left!.min * left!.max;
				// Update new min and max value
				info!.min = self.small(left!.min, element);
				info!.max = self.big(left!.max, element);
			}
			return info;
		}
		return nil;
	}
}
func main()
{
	// Make object of binary tree
	let tree: BinaryTree = BinaryTree();
	tree.root = Node(8);
	tree.root!.left = Node(4);
	tree.root!.left!.right = Node(3);
	tree.root!.left!.left = Node(7);
	tree.root!.right = Node(9);
	tree.root!.right!.left = Node(5);
	tree.root!.right!.right = Node(2);
	tree.root!.left!.right!.left = Node(6);
	tree.root!.right!.left!.right = Node(7);
	print("\n Before \n", terminator: "");
	//Display Tree elements
	tree.preorder(tree.root);
	let _ = tree.min_max_product(tree.root);
	print("\n After \n", terminator: "");
	tree.preorder(tree.root);
}
main();

Output

 Before
  8    4    7    3    6    9    5    7    2
 After
  18    21    7    36    6    14    49    7    2


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







© 2021, kalkicode.com, All rights reserved