Triple order traversal of a binary tree

Here given code implementation process.

// C program for
// Triple order traversal of a 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 a dynamic tree 
	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;
}
// This is creates and returns the new binary tree node
struct TreeNode *getNode(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;
}
void tripleOrder(struct TreeNode *node)
{
	if (node != NULL)
	{
		// Print node value
		printf("  %d", node->data);
		// Visit left subtree
		tripleOrder(node->left);
		// Print node value
		printf("  %d", node->data);
		// Visit right subtree
		tripleOrder(node->right);
		// Print node value
		printf("  %d", node->data);
	}
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree = newTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /       
	    9          
	-----------------
	  Constructing binary tree

	*/
	tree->root = getNode(4);
	tree->root->left = getNode(-4);
	tree->root->left->right = getNode(3);
	tree->root->left->right->left = getNode(6);
	tree->root->left->right->left->left = getNode(9);
	tree->root->left->right->right = getNode(8);
	tree->root->left->left = getNode(2);
	tree->root->right = getNode(7);
	tree->root->right->right = getNode(1);
	tree->root->right->right->left = getNode(5);
	tripleOrder(tree->root);
	return 0;
}

input

  4  -4  2  2  2  -4  3  6  9  9  9  6  6  3  8  8  8  3  -4  4  7  7  1  5  5  5  1  1  7  4
/*
    Java Program
    Triple order traversal of a 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;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	public void tripleOrder(TreeNode node)
	{
		if (node != null)
		{
			// Print node value
			System.out.print(" " + node.data);
			// Visit left subtree
			this.tripleOrder(node.left);
			// Print node value
			System.out.print(" " + node.data);
			// Visit right subtree
			this.tripleOrder(node.right);
			// Print node value
			System.out.print(" " + node.data);
		}
	}
	public static void main(String[] args)
	{
		// Create new binary trees 
		BinaryTree tree = new BinaryTree();
		/*
		         4                            
		       /   \    
		     -4     7    
		     / \      \               
		    2   3      1
		       / \    / 
		      6   8  5
		     /       
		    9          
		-----------------
		  Constructing binary tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(-4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(1);
		tree.root.right.right.left = new TreeNode(5);
		// Print triple order
		tree.tripleOrder(tree.root);
	}
}

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Triple order traversal of a 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;
	}
};
class BinaryTree
{
	public: TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	void tripleOrder(TreeNode *node)
	{
		if (node != NULL)
		{
			// Print node value
			cout << " " << node->data;
			// Visit left subtree
			this->tripleOrder(node->left);
			// Print node value
			cout << " " << node->data;
			// Visit right subtree
			this->tripleOrder(node->right);
			// Print node value
			cout << " " << node->data;
		}
	}
};
int main()
{
	// Create new binary trees
	BinaryTree *tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /       
	    9          
	-----------------
	  Constructing binary tree
	*/
	tree->root = new TreeNode(4);
	tree->root->left = new TreeNode(-4);
	tree->root->left->right = new TreeNode(3);
	tree->root->left->right->left = new TreeNode(6);
	tree->root->left->right->left->left = new TreeNode(9);
	tree->root->left->right->right = new TreeNode(8);
	tree->root->left->left = new TreeNode(2);
	tree->root->right = new TreeNode(7);
	tree->root->right->right = new TreeNode(1);
	tree->root->right->right->left = new TreeNode(5);
	// Print triple order
	tree->tripleOrder(tree->root);
	return 0;
}

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
// Include namespace system
using System;
/*
    Csharp Program
    Triple order traversal of a 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;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	public void tripleOrder(TreeNode node)
	{
		if (node != null)
		{
			// Print node value
			Console.Write(" " + node.data);
			// Visit left subtree
			this.tripleOrder(node.left);
			// Print node value
			Console.Write(" " + node.data);
			// Visit right subtree
			this.tripleOrder(node.right);
			// Print node value
			Console.Write(" " + node.data);
		}
	}
	public static void Main(String[] args)
	{
		// Create new binary trees
		BinaryTree tree = new BinaryTree();
		/*
		         4                            
		       /   \    
		     -4     7    
		     / \      \               
		    2   3      1
		       / \    / 
		      6   8  5
		     /       
		    9          
		-----------------
		  Constructing binary tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(-4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(1);
		tree.root.right.right.left = new TreeNode(5);
		// Print triple order
		tree.tripleOrder(tree.root);
	}
}

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
<?php
/*
    Php Program
    Triple order traversal of a binary tree
*/
// Binary Tree node
class TreeNode
{
	public $data;
	public $left;
	public $right;
	public	function __construct($data)
	{
		// Set node value
		$this->data = $data;
		$this->left = NULL;
		$this->right = NULL;
	}
}
class BinaryTree
{
	public $root;
	public	function __construct()
	{
		$this->root = NULL;
	}
	public	function tripleOrder($node)
	{
		if ($node != NULL)
		{
			// Print node value
			echo(" ".$node->data);
			// Visit left subtree
			$this->tripleOrder($node->left);
			// Print node value
			echo(" ".$node->data);
			// Visit right subtree
			$this->tripleOrder($node->right);
			// Print node value
			echo(" ".$node->data);
		}
	}
}

function main()
{
	// Create new binary trees
	$tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /       
	    9          
	-----------------
	  Constructing binary tree
	*/
	$tree->root = new TreeNode(4);
	$tree->root->left = new TreeNode(-4);
	$tree->root->left->right = new TreeNode(3);
	$tree->root->left->right->left = new TreeNode(6);
	$tree->root->left->right->left->left = new TreeNode(9);
	$tree->root->left->right->right = new TreeNode(8);
	$tree->root->left->left = new TreeNode(2);
	$tree->root->right = new TreeNode(7);
	$tree->root->right->right = new TreeNode(1);
	$tree->root->right->right->left = new TreeNode(5);
	// Print triple order
	$tree->tripleOrder($tree->root);
}
main();

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
/*
    Node JS Program
    Triple order traversal of a binary tree
*/
// Binary Tree node
class TreeNode
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	tripleOrder(node)
	{
		if (node != null)
		{
			// Print node value
			process.stdout.write(" " + node.data);
			// Visit left subtree
			this.tripleOrder(node.left);
			// Print node value
			process.stdout.write(" " + node.data);
			// Visit right subtree
			this.tripleOrder(node.right);
			// Print node value
			process.stdout.write(" " + node.data);
		}
	}
}

function main()
{
	// Create new binary trees
	var tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /       
	    9          
	-----------------
	  Constructing binary tree
	*/
	tree.root = new TreeNode(4);
	tree.root.left = new TreeNode(-4);
	tree.root.left.right = new TreeNode(3);
	tree.root.left.right.left = new TreeNode(6);
	tree.root.left.right.left.left = new TreeNode(9);
	tree.root.left.right.right = new TreeNode(8);
	tree.root.left.left = new TreeNode(2);
	tree.root.right = new TreeNode(7);
	tree.root.right.right = new TreeNode(1);
	tree.root.right.right.left = new TreeNode(5);
	// Print triple order
	tree.tripleOrder(tree.root);
}
main();

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
#    Python 3 Program
#    Triple order traversal of a binary tree

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

class BinaryTree :
	def __init__(self) :
		self.root = None
	
	def tripleOrder(self, node) :
		if (node != None) :
			#  Print node value
			print(" ", node.data, end = "")
			#  Visit left subtree
			self.tripleOrder(node.left)
			#  Print node value
			print(" ", node.data, end = "")
			#  Visit right subtree
			self.tripleOrder(node.right)
			#  Print node value
			print(" ", node.data, end = "")
		
	

def main() :
	#  Create new binary trees
	tree = BinaryTree()
	#         4                            
	#       /   \    
	#     -4     7    
	#     / \      \               
	#    2   3      1
	#       / \    / 
	#      6   8  5
	#     /       
	#    9          
	# -----------------
	#  Constructing binary tree
	tree.root = TreeNode(4)
	tree.root.left = TreeNode(-4)
	tree.root.left.right = TreeNode(3)
	tree.root.left.right.left = TreeNode(6)
	tree.root.left.right.left.left = TreeNode(9)
	tree.root.left.right.right = TreeNode(8)
	tree.root.left.left = TreeNode(2)
	tree.root.right = TreeNode(7)
	tree.root.right.right = TreeNode(1)
	tree.root.right.right.left = TreeNode(5)
	#  Print triple order
	tree.tripleOrder(tree.root)

if __name__ == "__main__": main()

input

  4  -4  2  2  2  -4  3  6  9  9  9  6  6  3  8  8  8  3  -4  4  7  7  1  5  5  5  1  1  7  4
#    Ruby Program
#    Triple order traversal of a 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

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

	def tripleOrder(node) 
		if (node != nil) 
			#  Print node value
			print(" ", node.data)
			#  Visit left subtree
			self.tripleOrder(node.left)
			#  Print node value
			print(" ", node.data)
			#  Visit right subtree
			self.tripleOrder(node.right)
			#  Print node value
			print(" ", node.data)
		end

	end

end

def main() 
	#  Create new binary trees
	tree = BinaryTree.new()
	#         4                            
	#       /   \    
	#     -4     7    
	#     / \      \               
	#    2   3      1
	#       / \    / 
	#      6   8  5
	#     /       
	#    9          
	# -----------------
	#  Constructing binary tree
	tree.root = TreeNode.new(4)
	tree.root.left = TreeNode.new(-4)
	tree.root.left.right = TreeNode.new(3)
	tree.root.left.right.left = TreeNode.new(6)
	tree.root.left.right.left.left = TreeNode.new(9)
	tree.root.left.right.right = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(2)
	tree.root.right = TreeNode.new(7)
	tree.root.right.right = TreeNode.new(1)
	tree.root.right.right.left = TreeNode.new(5)
	#  Print triple order
	tree.tripleOrder(tree.root)
end

main()

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
/*
    Scala Program
    Triple order traversal of a binary tree
*/
// Binary Tree node
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)
{
	def this()
	{
		this(null);
	}
	def tripleOrder(node: TreeNode): Unit = {
		if (node != null)
		{
			// Print node value
			print(" " + node.data);
			// Visit left subtree
			this.tripleOrder(node.left);
			// Print node value
			print(" " + node.data);
			// Visit right subtree
			this.tripleOrder(node.right);
			// Print node value
			print(" " + node.data);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new binary trees
		var tree: BinaryTree = new BinaryTree();
		/*
		         4                            
		       /   \    
		     -4     7    
		     / \      \               
		    2   3      1
		       / \    / 
		      6   8  5
		     /       
		    9          
		-----------------
		  Constructing binary tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(-4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(1);
		tree.root.right.right.left = new TreeNode(5);
		// Print triple order
		tree.tripleOrder(tree.root);
	}
}

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4
/*
    Swift 4 Program
    Triple order traversal of a 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;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	func tripleOrder(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			// Print node value
			print(" ", node!.data, terminator: "");
			// Visit left subtree
			self.tripleOrder(node!.left);
			// Print node value
			print(" ", node!.data, terminator: "");
			// Visit right subtree
			self.tripleOrder(node!.right);
			// Print node value
			print(" ", node!.data, terminator: "");
		}
	}
}
func main()
{
	// Create new binary trees
	let tree = BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /       
	    9          
	-----------------
	  Constructing binary tree
	*/
	tree.root = TreeNode(4);
	tree.root!.left = TreeNode(-4);
	tree.root!.left!.right = TreeNode(3);
	tree.root!.left!.right!.left = TreeNode(6);
	tree.root!.left!.right!.left!.left = TreeNode(9);
	tree.root!.left!.right!.right = TreeNode(8);
	tree.root!.left!.left = TreeNode(2);
	tree.root!.right = TreeNode(7);
	tree.root!.right!.right = TreeNode(1);
	tree.root!.right!.right!.left = TreeNode(5);
	// Print triple order
	tree.tripleOrder(tree.root);
}
main();

input

  4  -4  2  2  2  -4  3  6  9  9  9  6  6  3  8  8  8  3  -4  4  7  7  1  5  5  5  1  1  7  4
/*
    Kotlin Program
    Triple order traversal of a 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;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	fun tripleOrder(node: TreeNode ? ): Unit
	{
		if (node != null)
		{
			// Print node value
			print(" " + node.data);
			// Visit left subtree
			this.tripleOrder(node.left);
			// Print node value
			print(" " + node.data);
			// Visit right subtree
			this.tripleOrder(node.right);
			// Print node value
			print(" " + node.data);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	// Create new binary trees
	val tree: BinaryTree = BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /       
	    9          
	-----------------
	  Constructing binary tree
	*/
	tree.root = TreeNode(4);
	tree.root?.left = TreeNode(-4);
	tree.root?.left?.right = TreeNode(3);
	tree.root?.left?.right?.left = TreeNode(6);
	tree.root?.left?.right?.left?.left = TreeNode(9);
	tree.root?.left?.right?.right = TreeNode(8);
	tree.root?.left?.left = TreeNode(2);
	tree.root?.right = TreeNode(7);
	tree.root?.right?.right = TreeNode(1);
	tree.root?.right?.right?.left = TreeNode(5);
	// Print triple order
	tree.tripleOrder(tree.root);
}

input

 4 -4 2 2 2 -4 3 6 9 9 9 6 6 3 8 8 8 3 -4 4 7 7 1 5 5 5 1 1 7 4


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