Tilt of binary tree

Here given code implementation process.

/*
  C Program  
  Tilt of binary tree
*/
#include <stdio.h>
#include <stdlib.h>

//Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
//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 pre_order(struct Node *node)
{
	if (node != NULL)
	{
		//Print node value
		printf("  %d", node->data);
		pre_order(node->left);
		pre_order(node->right);
	}
}
//Find tilt of given binary tree
int tilt_of_bt(struct Node *node, int *result)
{
	if (node != NULL)
	{
		//Recursive execute, left and right subtree
		int left = tilt_of_bt(node->left, result);
		int right = tilt_of_bt(node->right, result);
		//Find absolute result
		if (right > left)
		{
			*result += right - left;
		}
		else
		{
			*result += left - right;
		}
		return node->data + (left + right);
	}
	else
	{
		return 0;
	}
}
//This function are used to handle the request of find tilt of tree
void find_tilt(struct Node *root)
{
	if (root == NULL)
	{
		printf("\n Empty Tree");
	}
	else
	{
		//Display tree elements
		printf(" Tree Nodes : ");
		pre_order(root);
		int result = 0;
		tilt_of_bt(root, &result);
		//Display calculated result
		printf("\n Tilt : %d\n", result);
	}
}
int main()
{
	struct Node *root = NULL;
	/*
	Construct Binary Tree
	-----------------------
	      10
	     /   \
	    6     8
	   / \   / \
	  2   3 7   5
	 /       \   \
	9        -1   3
	*/
	//Add nodes
	root = add_node(10);
	root->left = add_node(6);
	root->left->left = add_node(2);
	root->right = add_node(8);
	root->right->right = add_node(5);
	root->right->left = add_node(7);
	root->right->left->right = add_node(-1);
	root->left->right = add_node(3);
	root->left->left->left = add_node(9);
	root->right->right->right = add_node(3);
	//find tilt
	find_tilt(root);
	return 0;
}

Output

 Tree Nodes :   10  6  2  9  3  8  7  -1  5  3
 Tilt : 25
//Java Program 
//Tilt of binary tree

//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 BinaryTree
{
	public Node root;
	public int tilt;
	public BinaryTree()
	{
		//set initial tree root to null
		this.root = null;
		//Set initial tilt
		this.tilt = 0;
	}
	//Recursive function
	//Display preorder view of binary tree
	public void pre_order(Node node)
	{
		if (node != null)
		{
			//Print node value
			System.out.print(" " + node.data);
			pre_order(node.left);
			pre_order(node.right);
		}
	}
	//Find tilt of given binary tree
	public int tilt_of_bt(Node node)
	{
		if (node != null)
		{
			//Recursive execute, left and right subtree
			int left = tilt_of_bt(node.left);
			int right = tilt_of_bt(node.right);
			//Find absolute result
			if (right > left)
			{
				this.tilt += right - left;
			}
			else
			{
				this.tilt += left - right;
			}
			return node.data + (left + right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	public void find_tilt()
	{
		if (this.root == null)
		{
			System.out.print("\n Empty Tree");
		}
		else
		{
			//Display tree elements
			System.out.print(" Tree Nodes : ");
			pre_order(this.root);
			this.tilt = 0;
			tilt_of_bt(this.root);
			//Display calculated result
			System.out.print("\n Tilt : " + this.tilt + "\n");
		}
	}
	public static void main(String[] args)
	{
		//Make object 
		BinaryTree obj = new BinaryTree();
		/* 
		Construct Binary Tree
		-----------------------
		      10
		     /   \
		    6     8
		   / \   / \
		  2   3 7   5
		 /       \   \
		9        -1   3
		*/
		//Add nodes
		obj.root = new Node(10);
		obj.root.left = new Node(6);
		obj.root.left.left = new Node(2);
		obj.root.right = new Node(8);
		obj.root.right.right = new Node(5);
		obj.root.right.left = new Node(7);
		obj.root.right.left.right = new Node(-1);
		obj.root.left.right = new Node(3);
		obj.root.left.left.left = new Node(9);
		obj.root.right.right.right = new Node(3);
		//find tilt
		obj.find_tilt();
	}
}

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
//Include header file
#include <iostream>
using namespace std;

//C++ Program 
//Tilt of binary tree

//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 BinaryTree
{
	public: 
    Node *root;
	int tilt;
	BinaryTree()
	{
		//set initial tree root to null
		this->root = NULL;
		//Set initial tilt
		this->tilt = 0;
	}
	//Recursive function
	//Display preorder view of binary tree
	void pre_order(Node *node)
	{
		if (node != NULL)
		{
			//Print node value
			cout << " " << node->data;
			this->pre_order(node->left);
			this->pre_order(node->right);
		}
	}
	//Find tilt of given binary tree
	int tilt_of_bt(Node *node)
	{
		if (node != NULL)
		{
			//Recursive execute, left and right subtree
			int left  = this->tilt_of_bt(node->left);
			int right = this->tilt_of_bt(node->right);
			//Find absolute result
			if (right > left)
			{
				this->tilt += right - left;
			}
			else
			{
				this->tilt += left - right;
			}
			return node->data + (left + right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	void find_tilt()
	{
		if (this->root == NULL)
		{
			cout << "\n Empty Tree";
		}
		else
		{
			//Display tree elements
			cout << " Tree Nodes : ";
			this->pre_order(this->root);
			this->tilt = 0;
			this->tilt_of_bt(this->root);
			//Display calculated result
			cout << "\n Tilt : " << this->tilt << "\n";
		}
	}
};
int main()
{
	//Make object 
	BinaryTree obj = BinaryTree();
	/*
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -1   3
			*/
	//Add nodes
	obj.root = new Node(10);
	obj.root->left = new Node(6);
	obj.root->left->left = new Node(2);
	obj.root->right = new Node(8);
	obj.root->right->right = new Node(5);
	obj.root->right->left = new Node(7);
	obj.root->right->left->right = new Node(-1);
	obj.root->left->right = new Node(3);
	obj.root->left->left->left = new Node(9);
	obj.root->right->right->right = new Node(3);
	//find tilt
	obj.find_tilt();
	return 0;
}

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
//Include namespace system
using System;

//C# Program 
//Tilt of binary tree

//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 BinaryTree
{
	public Node root;
	public int tilt;
	public BinaryTree()
	{
		//set initial tree root to null
		this.root = null;
		//Set initial tilt
		this.tilt = 0;
	}
	//Recursive function
	//Display preorder view of binary tree
	public void pre_order(Node node)
	{
		if (node != null)
		{
			//Print node value
			Console.Write(" " + node.data);
			pre_order(node.left);
			pre_order(node.right);
		}
	}
	//Find tilt of given binary tree
	public int tilt_of_bt(Node node)
	{
		if (node != null)
		{
			//Recursive execute, left and right subtree
			int left = tilt_of_bt(node.left);
			int right = tilt_of_bt(node.right);
			//Find absolute result
			if (right > left)
			{
				this.tilt += right - left;
			}
			else
			{
				this.tilt += left - right;
			}
			return node.data + (left + right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	public void find_tilt()
	{
		if (this.root == null)
		{
			Console.Write("\n Empty Tree");
		}
		else
		{
			//Display tree elements
			Console.Write(" Tree Nodes : ");
			pre_order(this.root);
			this.tilt = 0;
			tilt_of_bt(this.root);
			//Display calculated result
			Console.Write("\n Tilt : " + this.tilt + "\n");
		}
	}
	public static void Main(String[] args)
	{
		//Make object 
		BinaryTree obj = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				      10
				     /   \
				    6     8
				   / \   / \
				  2   3 7   5
				 /       \   \
				9        -1   3
				*/
		//Add nodes
		obj.root = new Node(10);
		obj.root.left = new Node(6);
		obj.root.left.left = new Node(2);
		obj.root.right = new Node(8);
		obj.root.right.right = new Node(5);
		obj.root.right.left = new Node(7);
		obj.root.right.left.right = new Node(-1);
		obj.root.left.right = new Node(3);
		obj.root.left.left.left = new Node(9);
		obj.root.right.right.right = new Node(3);
		//find tilt
		obj.find_tilt();
	}
}

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
<?php
//Php Program 
//Tilt of binary tree

//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 BinaryTree
{
	public $root;
	public $tilt;

	function __construct()
	{
		//set initial tree root to null
		$this->root = null;
		//Set initial tilt
		$this->tilt = 0;
	}
	//Recursive function
	//Display preorder view of binary tree
	public	function pre_order($node)
	{
		if ($node != null)
		{
			//Print node value
			echo " ". $node->data;
			$this->pre_order($node->left);
			$this->pre_order($node->right);
		}
	}
	//Find tilt of given binary tree
	public	function tilt_of_bt($node)
	{
		if ($node != null)
		{
			//Recursive execute, left and right subtree
			$left = $this->tilt_of_bt($node->left);
			$right = $this->tilt_of_bt($node->right);
			//Find absolute result
			if ($right > $left)
			{
				$this->tilt += $right - $left;
			}
			else
			{
				$this->tilt += $left - $right;
			}
			return $node->data + ($left + $right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	public	function find_tilt()
	{
		if ($this->root == null)
		{
			echo "\n Empty Tree";
		}
		else
		{
			//Display tree elements
			echo " Tree Nodes : ";
			$this->pre_order($this->root);
			$this->tilt = 0;
			$this->tilt_of_bt($this->root);
			//Display calculated result
			echo "\n Tilt : ". $this->tilt ."\n";
		}
	}
}

function main()
{
	//Make object 
	$obj = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -1   3
			*/
	//Add nodes
	$obj->root = new Node(10);
	$obj->root->left = new Node(6);
	$obj->root->left->left = new Node(2);
	$obj->root->right = new Node(8);
	$obj->root->right->right = new Node(5);
	$obj->root->right->left = new Node(7);
	$obj->root->right->left->right = new Node(-1);
	$obj->root->left->right = new Node(3);
	$obj->root->left->left->left = new Node(9);
	$obj->root->right->right->right = new Node(3);
	//find tilt
	$obj->find_tilt();
}
main();

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
//Node Js Program 
//Tilt of binary tree

//Binary Tree node
class Node
{
	constructor(data)
	{
		//set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		//set initial tree root to null
		this.root = null;
		//Set initial tilt
		this.tilt = 0;
	}
	//Recursive function
	//Display preorder view of binary tree
	pre_order(node)
	{
		if (node != null)
		{
			//Print node value
			process.stdout.write(" " + node.data);
			this.pre_order(node.left);
			this.pre_order(node.right);
		}
	}
	//Find tilt of given binary tree
	tilt_of_bt(node)
	{
		if (node != null)
		{
			//Recursive execute, left and right subtree
			var left = this.tilt_of_bt(node.left);
			var right = this.tilt_of_bt(node.right);
			//Find absolute result
			if (right > left)
			{
				this.tilt += right - left;
			}
			else
			{
				this.tilt += left - right;
			}
			return node.data + (left + right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	find_tilt()
	{
		if (this.root == null)
		{
			process.stdout.write("\n Empty Tree");
		}
		else
		{
			//Display tree elements
			process.stdout.write(" Tree Nodes : ");
			this.pre_order(this.root);
			this.tilt = 0;
			this.tilt_of_bt(this.root);
			//Display calculated result
			process.stdout.write("\n Tilt : " + this.tilt + "\n");
		}
	}
}

function main()
{
	//Make object 
	var obj = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -1   3
			*/
	//Add nodes
	obj.root = new Node(10);
	obj.root.left = new Node(6);
	obj.root.left.left = new Node(2);
	obj.root.right = new Node(8);
	obj.root.right.right = new Node(5);
	obj.root.right.left = new Node(7);
	obj.root.right.left.right = new Node(-1);
	obj.root.left.right = new Node(3);
	obj.root.left.left.left = new Node(9);
	obj.root.right.right.right = new Node(3);
	//find tilt
	obj.find_tilt();
}
main();

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
# Python 3 Program 
# Tilt of binary tree

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

class BinaryTree :
	
	def __init__(self) :
		# set initial tree root to null
		self.root = None
		# Set initial tilt
		self.tilt = 0
	
	# Recursive function
	# Display preorder view of binary tree
	def pre_order(self, node) :
		if (node != None) :
			# Print node value
			print(" ", node.data, end = "")
			self.pre_order(node.left)
			self.pre_order(node.right)
		
	
	# Find tilt of given binary tree
	def tilt_of_bt(self, node) :
		if (node != None) :
			# Recursive execute, left and right subtree
			left = self.tilt_of_bt(node.left)
			right = self.tilt_of_bt(node.right)
			# Find absolute result
			if (right > left) :
				self.tilt += right - left
			else :
				self.tilt += left - right
			
			return node.data + (left + right)
		else :
			return 0
		
	
	# This function are used to handle the request of find tilt of tree
	def find_tilt(self) :
		if (self.root == None) :
			print("\n Empty Tree", end = "")
		else :
			# Display tree elements
			print(" Tree Nodes : ", end = "")
			self.pre_order(self.root)
			self.tilt = 0
			self.tilt_of_bt(self.root)
			# Display calculated result
			print("\n Tilt : ", self.tilt ,"\n", end = "")
		
	

def main() :
	# Make object 
	obj = BinaryTree()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		      10
	# 		     /   \
	# 		    6     8
	# 		   / \   / \
	# 		  2   3 7   5
	# 		 /       \   \
	# 		9        -1   3
	# 		
	
	# Add nodes
	obj.root = Node(10)
	obj.root.left = Node(6)
	obj.root.left.left = Node(2)
	obj.root.right = Node(8)
	obj.root.right.right = Node(5)
	obj.root.right.left = Node(7)
	obj.root.right.left.right = Node(-1)
	obj.root.left.right = Node(3)
	obj.root.left.left.left = Node(9)
	obj.root.right.right.right = Node(3)
	# find tilt
	obj.find_tilt()

if __name__ == "__main__": main()

Output

 Tree Nodes :   10  6  2  9  3  8  7  -1  5  3
 Tilt :  25
# Ruby Program 
# Tilt of binary tree

# 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 BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root, :tilt
	attr_accessor :root, :tilt
 
	
	def initialize() 
		# set initial tree root to null
		self.root = nil
		# Set initial tilt
		self.tilt = 0
	end
	# Recursive function
	# Display preorder view of binary tree
	def pre_order(node) 
		if (node != nil) 
			# Print node value
			print(" ", node.data)
			self.pre_order(node.left)
			self.pre_order(node.right)
		end
	end
	# Find tilt of given binary tree
	def tilt_of_bt(node) 
		if (node != nil) 
			# Recursive execute, left and right subtree
			left = self.tilt_of_bt(node.left)
			right = self.tilt_of_bt(node.right)
			# Find absolute result
			if (right > left) 
				self.tilt += right - left
			else 
				self.tilt += left - right
			end
			return node.data + (left + right)
		else 
			return 0
		end
	end
	# This function are used to handle the request of find tilt of tree
	def find_tilt() 
		if (self.root == nil) 
			print("\n Empty Tree")
		else 
			# Display tree elements
			print(" Tree Nodes : ")
			self.pre_order(self.root)
			self.tilt = 0
			self.tilt_of_bt(self.root)
			# Display calculated result
			print("\n Tilt : ", self.tilt ,"\n")
		end
	end
end
def main() 
	# Make object 
	obj = BinaryTree.new()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		      10
	# 		     /   \
	# 		    6     8
	# 		   / \   / \
	# 		  2   3 7   5
	# 		 /       \   \
	# 		9        -1   3
	# 		
	
	# Add nodes
	obj.root = Node.new(10)
	obj.root.left = Node.new(6)
	obj.root.left.left = Node.new(2)
	obj.root.right = Node.new(8)
	obj.root.right.right = Node.new(5)
	obj.root.right.left = Node.new(7)
	obj.root.right.left.right = Node.new(-1)
	obj.root.left.right = Node.new(3)
	obj.root.left.left.left = Node.new(9)
	obj.root.right.right.right = Node.new(3)
	# find tilt
	obj.find_tilt()
end
main()

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
//Scala Program 
//Tilt of binary tree

//Binary Tree node
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: Node,
	var tilt: Int)
{
	def this()
	{
		this(null, 0);
	}
	//Recursive function
	//Display preorder view of binary tree
	def pre_order(node: Node): Unit = {
		if (node != null)
		{
			//Print node value
			print(" " + node.data);
			pre_order(node.left);
			pre_order(node.right);
		}
	}
	//Find tilt of given binary tree
	def tilt_of_bt(node: Node): Int = {
		if (node != null)
		{
			//Recursive execute, left and right subtree
			var left: Int = tilt_of_bt(node.left);
			var right: Int = tilt_of_bt(node.right);
			//Find absolute result
			if (right > left)
			{
				this.tilt += right - left;
			}
			else
			{
				this.tilt += left - right;
			}
			return node.data + (left + right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	def find_tilt(): Unit = {
		if (this.root == null)
		{
			print("\n Empty Tree");
		}
		else
		{
			//Display tree elements
			print(" Tree Nodes : ");
			pre_order(this.root);
			this.tilt = 0;
			tilt_of_bt(this.root);
			//Display calculated result
			print("\n Tilt : " + this.tilt + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		//Make object 
		var obj: BinaryTree = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				      10
				     /   \
				    6     8
				   / \   / \
				  2   3 7   5
				 /       \   \
				9        -1   3
				*/
		//Add nodes
		obj.root = new Node(10);
		obj.root.left = new Node(6);
		obj.root.left.left = new Node(2);
		obj.root.right = new Node(8);
		obj.root.right.right = new Node(5);
		obj.root.right.left = new Node(7);
		obj.root.right.left.right = new Node(-1);
		obj.root.left.right = new Node(3);
		obj.root.left.left.left = new Node(9);
		obj.root.right.right.right = new Node(3);
		//find tilt
		obj.find_tilt();
	}
}

Output

 Tree Nodes :  10 6 2 9 3 8 7 -1 5 3
 Tilt : 25
//Swift 4 Program 
//Tilt of binary tree

//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 BinaryTree
{
	var root: Node? ;
	var tilt: Int;
	init()
	{
		//set initial tree root to null
		self.root = nil;
		//Set initial tilt
		self.tilt = 0;
	}
	//Recursive function
	//Display preorder view of binary tree
	func pre_order(_ node: Node? )
	{
		if (node != nil)
		{
			//Print node value
			print(" ", node!.data, terminator: "");
			self.pre_order(node!.left);
			self.pre_order(node!.right);
		}
	}
	//Find tilt of given binary tree
	func tilt_of_bt(_ node: Node? ) -> Int
	{
		if (node != nil)
		{
			//Recursive execute, left and right subtree
			let left: Int = self.tilt_of_bt(node!.left);
			let right: Int = self.tilt_of_bt(node!.right);
			//Find absolute result
			if (right > left)
			{
				self.tilt += right - left;
			}
			else
			{
				self.tilt += left - right;
			}
			return node!.data + (left + right);
		}
		else
		{
			return 0;
		}
	}
	//This function are used to handle the request of find tilt of tree
	func find_tilt()
	{
		if (self.root == nil)
		{
			print("\n Empty Tree", terminator: "");
		}
		else
		{
			//Display tree elements
			print(" Tree Nodes : ", terminator: "");
			self.pre_order(self.root);
			self.tilt = 0;
			let _ = self.tilt_of_bt(self.root);
			//Display calculated result
			print("\n Tilt : ", self.tilt ,"\n", terminator: "");
		}
	}
}
func main()
{
	//Make object 
	let obj: BinaryTree = BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -1   3
			*/
	//Add nodes
	obj.root = Node(10);
	obj.root!.left = Node(6);
	obj.root!.left!.left = Node(2);
	obj.root!.right = Node(8);
	obj.root!.right!.right = Node(5);
	obj.root!.right!.left = Node(7);
	obj.root!.right!.left!.right = Node(-1);
	obj.root!.left!.right = Node(3);
	obj.root!.left!.left!.left = Node(9);
	obj.root!.right!.right!.right = Node(3);
	//find tilt
	obj.find_tilt();
}
main();

Output

 Tree Nodes :   10  6  2  9  3  8  7  -1  5  3
 Tilt :  25


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