Construct skewed binary tree

Here given code implementation process.

/*
    C Program 
    Construct skewed binary tree
*/
#include <stdio.h>
#include <stdlib.h>

 // Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *newTree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// returns a new node of tree
struct TreeNode *newNode(int data)
{
	// Create dynamic node
	struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (node != NULL)
	{
		//Set data and pointer values
		node->data = data;
		node->left = NULL;
		node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return node;
}
//Display Inorder view of binary tree
void inorder(struct TreeNode *node)
{
	if (node)
	{
		inorder(node->left);
		//Print node value
		printf("  %d", node->data);
		inorder(node->right);
	}
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree1 = newTree();
	struct BinaryTree *tree2 = newTree();
	/*
	    10
	     \
	      20
	       \
	        30
	         \ 
	          40
	-----------------------
	 Right skewed binary tree
	-----------------------
	*/
	tree1->root = newNode(10);
	tree1->root->right = newNode(20);
	tree1->root->right->right = newNode(30);
	tree1->root->right->right->right = newNode(40);
	inorder(tree1->root);
	/*
            10
           /
          20
         /
        30
       / 
      40
    -----------------------
     Left skewed binary tree
    -----------------------
    */
	tree2->root = newNode(10);
	tree2->root->left = newNode(20);
	tree2->root->left->left = newNode(30);
	tree2->root->left->left->left = newNode(40);
	printf("\n");
	inorder(tree2->root);
	return 0;
}

Output

  10  20  30  40
  40  30  20  10
/*
   Java Program 
   Construct skewed binary tree
*/
// Binary Tree node
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;
	}
}
// Define Binary Tree 
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	//Display Inorder view of binary tree
	public void inorder(TreeNode node)
	{
		if (node != null)
		{
			inorder(node.left);
			//Print node value
			System.out.print("  " + node.data);
			inorder(node.right);
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		    10
		     \
		      20
		       \
		        30
		         \ 
		          40
		-----------------------
		 Right skewed binary tree
		-----------------------
		*/
		tree1.root = new TreeNode(10);
		tree1.root.right = new TreeNode(20);
		tree1.root.right.right = new TreeNode(30);
		tree1.root.right.right.right = new TreeNode(40);
		tree1.inorder(tree1.root);
		/*
		        10
		       /
		      20
		     /
		    30
		   / 
		  40
		-----------------------
		 Left skewed binary tree
		-----------------------
		*/
		tree2.root = new TreeNode(10);
		tree2.root.left = new TreeNode(20);
		tree2.root.left.left = new TreeNode(30);
		tree2.root.left.left.left = new TreeNode(40);
		System.out.print("\n");
		tree2.inorder(tree2.root);
	}
}

Output

  10  20  30  40
  40  30  20  10
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program 
   Construct skewed binary tree
*/

// Binary Tree node
class TreeNode
{
	public: 
    int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		// Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
// Define Binary Tree
class BinaryTree
{
	public: 
    TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	//Display Inorder view of binary tree
	void inorder(TreeNode *node)
	{
		if (node != NULL)
		{
			this->inorder(node->left);
			//Print node value
			cout << "  " << node->data;
			this->inorder(node->right);
		}
	}
};
int main()
{
	BinaryTree tree1 = BinaryTree();
	BinaryTree tree2 = BinaryTree();
	/*
	    10
	     \
	      20
	       \
	        30
	         \ 
	          40
	-----------------------
	 Right skewed binary tree
	-----------------------
	*/
	tree1.root = new TreeNode(10);
	tree1.root->right = new TreeNode(20);
	tree1.root->right->right = new TreeNode(30);
	tree1.root->right->right->right = new TreeNode(40);
	tree1.inorder(tree1.root);
	/*
	        10
	       /
	      20
	     /
	    30
	   / 
	  40
	-----------------------
	 Left skewed binary tree
	-----------------------
	*/
	tree2.root = new TreeNode(10);
	tree2.root->left = new TreeNode(20);
	tree2.root->left->left = new TreeNode(30);
	tree2.root->left->left->left = new TreeNode(40);
	cout << "\n";
	tree2.inorder(tree2.root);
	return 0;
}

Output

  10  20  30  40
  40  30  20  10
// Include namespace system
using System;
/*
   C# Program 
   Construct skewed binary tree
*/
// 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;
	}
}
// Define Binary Tree
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	//Display Inorder view of binary tree
	public void inorder(TreeNode node)
	{
		if (node != null)
		{
			inorder(node.left);
			//Print node value
			Console.Write("  " + node.data);
			inorder(node.right);
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		    10
		     \
		      20
		       \
		        30
		         \ 
		          40
		-----------------------
		 Right skewed binary tree
		-----------------------
		*/
		tree1.root = new TreeNode(10);
		tree1.root.right = new TreeNode(20);
		tree1.root.right.right = new TreeNode(30);
		tree1.root.right.right.right = new TreeNode(40);
		tree1.inorder(tree1.root);
		/*
		        10
		       /
		      20
		     /
		    30
		   / 
		  40
		-----------------------
		 Left skewed binary tree
		-----------------------
		*/
		tree2.root = new TreeNode(10);
		tree2.root.left = new TreeNode(20);
		tree2.root.left.left = new TreeNode(30);
		tree2.root.left.left.left = new TreeNode(40);
		Console.Write("\n");
		tree2.inorder(tree2.root);
	}
}

Output

  10  20  30  40
  40  30  20  10
<?php
/*
   Php Program 
   Construct skewed binary tree
*/
// Binary Tree node
class TreeNode
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		// Set node value
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
// Define Binary Tree
class BinaryTree
{
	public $root;

	function __construct()
	{
		$this->root = null;
	}
	//Display Inorder view of binary tree
	public	function inorder($node)
	{
		if ($node != null)
		{
			$this->inorder($node->left);
			//Print node value
			echo "  ". $node->data;
			$this->inorder($node->right);
		}
	}
}

function main()
{
	$tree1 = new BinaryTree();
	$tree2 = new BinaryTree();
	/*
	    10
	     \
	      20
	       \
	        30
	         \ 
	          40
	-----------------------
	 Right skewed binary tree
	-----------------------
	*/
	$tree1->root = new TreeNode(10);
	$tree1->root->right = new TreeNode(20);
	$tree1->root->right->right = new TreeNode(30);
	$tree1->root->right->right->right = new TreeNode(40);
	$tree1->inorder($tree1->root);
	/*
	        10
	       /
	      20
	     /
	    30
	   / 
	  40
	-----------------------
	 Left skewed binary tree
	-----------------------
	*/
	$tree2->root = new TreeNode(10);
	$tree2->root->left = new TreeNode(20);
	$tree2->root->left->left = new TreeNode(30);
	$tree2->root->left->left->left = new TreeNode(40);
	echo "\n";
	$tree2->inorder($tree2->root);
}
main();

Output

  10  20  30  40
  40  30  20  10
/*
   Node Js Program 
   Construct skewed binary tree
*/
// Binary Tree node
class TreeNode
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
// Define Binary Tree
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	//Display Inorder view of binary tree
	inorder(node)
	{
		if (node != null)
		{
			this.inorder(node.left);
			//Print node value
			process.stdout.write("  " + node.data);
			this.inorder(node.right);
		}
	}
}

function main()
{
	var tree1 = new BinaryTree();
	var tree2 = new BinaryTree();
	/*
	    10
	     \
	      20
	       \
	        30
	         \ 
	          40
	-----------------------
	 Right skewed binary tree
	-----------------------
	*/
	tree1.root = new TreeNode(10);
	tree1.root.right = new TreeNode(20);
	tree1.root.right.right = new TreeNode(30);
	tree1.root.right.right.right = new TreeNode(40);
	tree1.inorder(tree1.root);
	/*
	        10
	       /
	      20
	     /
	    30
	   / 
	  40
	-----------------------
	 Left skewed binary tree
	-----------------------
	*/
	tree2.root = new TreeNode(10);
	tree2.root.left = new TreeNode(20);
	tree2.root.left.left = new TreeNode(30);
	tree2.root.left.left.left = new TreeNode(40);
	process.stdout.write("\n");
	tree2.inorder(tree2.root);
}
main();

Output

  10  20  30  40
  40  30  20  10
#  Python 3 Program 
#  Construct skewed binary tree

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

#  Define Binary Tree
class BinaryTree :
	
	def __init__(self) :
		self.root = None
	
	# Display Inorder view of binary tree
	def inorder(self, node) :
		if (node != None) :
			self.inorder(node.left)
			# Print node value
			print("  ", node.data, end = "")
			self.inorder(node.right)
		
	

def main() :
	tree1 = BinaryTree()
	tree2 = BinaryTree()
	# 
	#     10
	#      \
	#       20
	#        \
	#         30
	#          \ 
	#           40
	# -----------------------
	#  Right skewed binary tree
	# -----------------------
	
	tree1.root = TreeNode(10)
	tree1.root.right = TreeNode(20)
	tree1.root.right.right = TreeNode(30)
	tree1.root.right.right.right = TreeNode(40)
	tree1.inorder(tree1.root)
	# 
	#         10
	#        /
	#       20
	#      /
	#     30
	#    / 
	#   40
	# -----------------------
	#  Left skewed binary tree
	# -----------------------
	
	tree2.root = TreeNode(10)
	tree2.root.left = TreeNode(20)
	tree2.root.left.left = TreeNode(30)
	tree2.root.left.left.left = TreeNode(40)
	print(end = "\n")
	tree2.inorder(tree2.root)

if __name__ == "__main__": main()

Output

   10   20   30   40
   40   30   20   10
#  Ruby Program 
#  Construct skewed binary tree

#  Binary Tree node
class TreeNode  
	# Define the accessor and reader of class TreeNode  
	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

#  Define Binary Tree
class BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root
	attr_accessor :root
 
	
	def initialize() 
		self.root = nil
	end

	# Display Inorder view of binary tree
	def inorder(node) 
		if (node != nil) 
			self.inorder(node.left)
			# Print node value
			print("  ", node.data)
			self.inorder(node.right)
		end

	end

end

def main() 
	tree1 = BinaryTree.new()
	tree2 = BinaryTree.new()
	# 
	#     10
	#      \
	#       20
	#        \
	#         30
	#          \ 
	#           40
	# -----------------------
	#  Right skewed binary tree
	# -----------------------
	
	tree1.root = TreeNode.new(10)
	tree1.root.right = TreeNode.new(20)
	tree1.root.right.right = TreeNode.new(30)
	tree1.root.right.right.right = TreeNode.new(40)
	tree1.inorder(tree1.root)
	# 
	#         10
	#        /
	#       20
	#      /
	#     30
	#    / 
	#   40
	# -----------------------
	#  Left skewed binary tree
	# -----------------------
	
	tree2.root = TreeNode.new(10)
	tree2.root.left = TreeNode.new(20)
	tree2.root.left.left = TreeNode.new(30)
	tree2.root.left.left.left = TreeNode.new(40)
	print("\n")
	tree2.inorder(tree2.root)
end

main()

Output

  10  20  30  40
  40  30  20  10
/*
   Scala Program 
   Construct skewed binary tree
*/
// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
// Define Binary Tree
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	//Display Inorder view of binary tree
	def inorder(node: TreeNode): Unit = {
		if (node != null)
		{
			this.inorder(node.left);
			//Print node value
			print("  " + node.data);
			this.inorder(node.right);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree1: BinaryTree = new BinaryTree();
		var tree2: BinaryTree = new BinaryTree();
		/*
		    10
		     \
		      20
		       \
		        30
		         \ 
		          40
		-----------------------
		 Right skewed binary tree
		-----------------------
		*/
		tree1.root = new TreeNode(10);
		tree1.root.right = new TreeNode(20);
		tree1.root.right.right = new TreeNode(30);
		tree1.root.right.right.right = new TreeNode(40);
		tree1.inorder(tree1.root);
		/*
		        10
		       /
		      20
		     /
		    30
		   / 
		  40
		-----------------------
		 Left skewed binary tree
		-----------------------
		*/
		tree2.root = new TreeNode(10);
		tree2.root.left = new TreeNode(20);
		tree2.root.left.left = new TreeNode(30);
		tree2.root.left.left.left = new TreeNode(40);
		print("\n");
		tree2.inorder(tree2.root);
	}
}

Output

  10  20  30  40
  40  30  20  10
/*
   Swift 4 Program 
   Construct skewed binary tree
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
// Define Binary Tree
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	//Display Inorder view of binary tree
	func inorder(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			self.inorder(node!.left);
			//Print node value
			print("  ", node!.data, terminator: "");
			self.inorder(node!.right);
		}
	}
}
func main()
{
	let tree1: BinaryTree = BinaryTree();
	let tree2: BinaryTree = BinaryTree();
	/*
	    10
	     \
	      20
	       \
	        30
	         \ 
	          40
	-----------------------
	 Right skewed binary tree
	-----------------------
	*/
	tree1.root = TreeNode(10);
	tree1.root!.right = TreeNode(20);
	tree1.root!.right!.right = TreeNode(30);
	tree1.root!.right!.right!.right = TreeNode(40);
	tree1.inorder(tree1.root);
	/*
	        10
	       /
	      20
	     /
	    30
	   / 
	  40
	-----------------------
	 Left skewed binary tree
	-----------------------
	*/
	tree2.root = TreeNode(10);
	tree2.root!.left = TreeNode(20);
	tree2.root!.left!.left = TreeNode(30);
	tree2.root!.left!.left!.left = TreeNode(40);
	print(terminator: "\n");
	tree2.inorder(tree2.root);
}
main();

Output

   10   20   30   40
   40   30   20   10
/*
   Kotlin Program 
   Construct skewed binary tree
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
// Define Binary Tree
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	//Display Inorder view of binary tree
	fun inorder(node: TreeNode ? ): Unit
	{
		if (node != null)
		{
			this.inorder(node.left);
			//Print node value
			print("  " + node.data);
			this.inorder(node.right);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var tree1: BinaryTree = BinaryTree();
	var tree2: BinaryTree = BinaryTree();
	/*
	    10
	     \
	      20
	       \
	        30
	         \ 
	          40
	-----------------------
	 Right skewed binary tree
	-----------------------
	*/
	tree1.root = TreeNode(10);
	tree1.root?.right = TreeNode(20);
	tree1.root?.right?.right = TreeNode(30);
	tree1.root?.right?.right?.right = TreeNode(40);
	tree1.inorder(tree1.root);
	/*
	        10
	       /
	      20
	     /
	    30
	   / 
	  40
	-----------------------
	 Left skewed binary tree
	-----------------------
	*/
	tree2.root = TreeNode(10);
	tree2.root?.left = TreeNode(20);
	tree2.root?.left?.left = TreeNode(30);
	tree2.root?.left?.left?.left = TreeNode(40);
	print("\n");
	tree2.inorder(tree2.root);
}

Output

  10  20  30  40
  40  30  20  10

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