Double order traversal of a binary tree

Here given code implementation process.

// C program for
// Double 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 doubleOrder(struct TreeNode *node)
{
	if (node != NULL)
	{
		// Print node value
		printf("  %d", node->data);
		// Visit left subtree
		doubleOrder(node->left);
		// Print node value
		printf("  %d", node->data);
		// Visit right subtree
		doubleOrder(node->right);
	}
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree = newTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \     \               
	    2   3     12
	   /   / \    / 
	  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->left->left->left = getNode(1);
	tree->root->right = getNode(7);
	tree->root->right->right = getNode(12);
	tree->root->right->right->left = getNode(5);
	doubleOrder(tree->root);
	return 0;
}

input

  4  -4  2  1  1  2  -4  3  6  9  9  6  3  8  8  4  7  7  12  5  5  12
/*
    Java Program
    Double 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 doubleOrder(TreeNode node)
    {
        if (node != null)
        {
            // Print node value
            System.out.print(" " + node.data );
            // Visit left subtree
            this.doubleOrder(node.left);
            // Print node value
            System.out.print(" " + node.data );
            // Visit right subtree
            this.doubleOrder(node.right);
        }
    }

    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 double order
        tree.doubleOrder(tree.root);
    }
}

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Double 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 doubleOrder(TreeNode *node)
	{
		if (node != NULL)
		{
			// Print node value
			cout << " " << node->data;
			// Visit left subtree
			this->doubleOrder(node->left);
			// Print node value
			cout << " " << node->data;
			// Visit right subtree
			this->doubleOrder(node->right);
		}
	}
};
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 double order
	tree->doubleOrder(tree->root);
	return 0;
}

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
// Include namespace system
using System;
/*
    Csharp Program
    Double 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 doubleOrder(TreeNode node)
	{
		if (node != null)
		{
			// Print node value
			Console.Write(" " + node.data);
			// Visit left subtree
			this.doubleOrder(node.left);
			// Print node value
			Console.Write(" " + node.data);
			// Visit right subtree
			this.doubleOrder(node.right);
		}
	}
	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 double order
		tree.doubleOrder(tree.root);
	}
}

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
<?php
/*
    Php Program
    Double 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 doubleOrder($node)
	{
		if ($node != NULL)
		{
			// Print node value
			echo(" ".$node->data);
			// Visit left subtree
			$this->doubleOrder($node->left);
			// Print node value
			echo(" ".$node->data);
			// Visit right subtree
			$this->doubleOrder($node->right);
		}
	}
}

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 double order
	$tree->doubleOrder($tree->root);
}
main();

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
/*
    Node JS Program
    Double 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;
	}
	doubleOrder(node)
	{
		if (node != null)
		{
			// Print node value
			process.stdout.write(" " + node.data);
			// Visit left subtree
			this.doubleOrder(node.left);
			// Print node value
			process.stdout.write(" " + node.data);
			// Visit right subtree
			this.doubleOrder(node.right);
		}
	}
}

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 double order
	tree.doubleOrder(tree.root);
}
main();

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
#    Python 3 Program
#    Double 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 doubleOrder(self, node) :
		if (node != None) :
			#  Print node value
			print(" ", node.data, end = "")
			#  Visit left subtree
			self.doubleOrder(node.left)
			#  Print node value
			print(" ", node.data, end = "")
			#  Visit right subtree
			self.doubleOrder(node.right)
		
	

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 double order
	tree.doubleOrder(tree.root)

if __name__ == "__main__": main()

input

  4  -4  2  2  -4  3  6  9  9  6  3  8  8  4  7  7  1  5  5  1
#    Ruby Program
#    Double 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 doubleOrder(node) 
		if (node != nil) 
			#  Print node value
			print(" ", node.data)
			#  Visit left subtree
			self.doubleOrder(node.left)
			#  Print node value
			print(" ", node.data)
			#  Visit right subtree
			self.doubleOrder(node.right)
		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 double order
	tree.doubleOrder(tree.root)
end

main()

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
/*
    Scala Program
    Double 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 doubleOrder(node: TreeNode): Unit = {
		if (node != null)
		{
			// Print node value
			print(" " + node.data);
			// Visit left subtree
			this.doubleOrder(node.left);
			// Print node value
			print(" " + node.data);
			// Visit right subtree
			this.doubleOrder(node.right);
		}
	}
}
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 double order
		tree.doubleOrder(tree.root);
	}
}

input

 4 -4 2 2 -4 3 6 9 9 6 3 8 8 4 7 7 1 5 5 1
/*
    Swift 4 Program
    Double 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 doubleOrder(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			// Print node value
			print(" ", node!.data, terminator: "");
			// Visit left subtree
			self.doubleOrder(node!.left);
			// Print node value
			print(" ", node!.data, terminator: "");
			// Visit right subtree
			self.doubleOrder(node!.right);
		}
	}
}
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 double order
	tree.doubleOrder(tree.root);
}
main();

input

  4  -4  2  2  -4  3  6  9  9  6  3  8  8  4  7  7  1  5  5  1
/*
    Kotlin Program
    Double 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 doubleOrder(node: TreeNode ? ): Unit
	{
		if (node != null)
		{
			// Print node value
			print(" " + node.data);
			// Visit left subtree
			this.doubleOrder(node.left);
			// Print node value
			print(" " + node.data);
			// Visit right subtree
			this.doubleOrder(node.right);
		}
	}
}
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 double order
	tree.doubleOrder(tree.root);
}

input

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


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