Euler tour of Binary Tree

Here given code implementation process.

/*
    C Program 
    Euler tour of 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 *new_tree()
{
    // 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 *new_node(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;
}

//Recursively Printing the elements of euler path
void print_euler_path(struct TreeNode*node,struct TreeNode *parent)
{

    if(node==NULL)
    {
        // When tree node empty
        return;
    }

    printf("   %d",node->data);

    //Recursively executing the left and right subtree
    print_euler_path(node->left, node);
    print_euler_path(node->right, node);

    if(parent != NULL)
    {
        //When parent not null
        printf("  %d",parent->data);
    }

   
}

// Handles the request of printing the euler path
void euler_path(struct TreeNode*root)
{

    if(root==NULL)
    {
        printf("\n Empty Tree \n");  
    }
    else
    {
        print_euler_path(root,NULL);
    }
}

int main()
{
    struct BinaryTree *tree = new_tree();

    /*
            9
           / \
          5   7
         / \   \
        1   6   11
       /   / \   \
      10  4   8   20
             / \
            2   3
    -----------------------
        Binary Tree
    */

    tree->root = new_node(9);
    tree->root->left = new_node(5); 
    tree->root->right = new_node(7); 
    tree->root->left->left = new_node(1); 
    tree->root->left->left->left = new_node(10); 
    tree->root->left->right = new_node(6);
    tree->root->left->right->left = new_node(4);
    tree->root->left->right->right = new_node(8);
    tree->root->left->right->right->left = new_node(2);
    tree->root->left->right->right->right = new_node(3);
    tree->root->right->right = new_node(11);
    tree->root->right->right->right = new_node(20);  
    euler_path(tree->root);
    return 0;
}

Output

   9   5   1   10  1  5   6   4  6   8   2  8   3  8  6  5  9   7   11   20  11  7  9
/*
    Java Program 
    Euler tour of Binary Tree
*/
// Tree Node
class TreeNode
{
    public int data;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
// Binary Tree
public class BinaryTree
{
    public TreeNode root;
    public BinaryTree()
    {
        this.root = null;
    }
    //Recursively Printing the elements of euler path
    public void print_euler_path(TreeNode node, TreeNode parent)
    {
        if (node == null)
        {
            // When tree node empty
            return;
        }
        System.out.print("  " + node.data);
        //Recursively executing the left and right subtree
        print_euler_path(node.left, node);
        print_euler_path(node.right, node);
        if (parent != null)
        {
            //When parent not null
            System.out.print("   " + parent.data);
        }
    }
    // Handles the request of printing the euler path
    public void euler_path()
    {
        if (this.root == null)
        {
            System.out.print("\n Empty Tree \n");
        }
        else
        {
            print_euler_path(this.root, null);
        }
    }
    public static void main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
        /*
                9
               / \
              5   7
             / \   \
            1   6   11
           /   / \   \
          10  4   8   20
                 / \
                2   3
        -----------------------
            Binary Tree
        */
        tree.root = new TreeNode(9);
        tree.root.left = new TreeNode(5);
        tree.root.right = new TreeNode(7);
        tree.root.left.left = new TreeNode(1);
        tree.root.left.left.left = new TreeNode(10);
        tree.root.left.right = new TreeNode(6);
        tree.root.left.right.left = new TreeNode(4);
        tree.root.left.right.right = new TreeNode(8);
        tree.root.left.right.right.left = new TreeNode(2);
        tree.root.left.right.right.right = new TreeNode(3);
        tree.root.right.right = new TreeNode(11);
        tree.root.right.right.right = new TreeNode(20);
        tree.euler_path();
    }
}

Output

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

/*
    C++ Program 
    Euler tour of Binary Tree
*/

//  Tree Node
class TreeNode
{
	public: 
    int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
//  Binary Tree
class BinaryTree
{
	public: 
    TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Recursively Printing the elements of euler path
	void print_euler_path(TreeNode *node, TreeNode *parent)
	{
		//  When tree node empty
		if (node == NULL)
		{
			return;
		}
		cout << "  " << node->data;
		// Recursively executing the left and right subtree
		this->print_euler_path(node->left, node);
		this->print_euler_path(node->right, node);
		if (parent != NULL)
		{
			// When parent not null
			cout << "   " << parent->data;
		}
	}
	//  Handles the request of printing the euler path
	void euler_path()
	{
		if (this->root == NULL)
		{
			cout << "\n Empty Tree \n";
		}
		else
		{
			this->print_euler_path(this->root, NULL);
		}
	}
};
int main()
{
	BinaryTree tree = BinaryTree();
	/*
	                9
	               / \
	              5   7
	             / \   \
	            1   6   11
	           /   / \   \
	          10  4   8   20
	                 / \
	                2   3
	        -----------------------
	            Binary Tree
	        */
	tree.root = new TreeNode(9);
	tree.root->left = new TreeNode(5);
	tree.root->right = new TreeNode(7);
	tree.root->left->left = new TreeNode(1);
	tree.root->left->left->left = new TreeNode(10);
	tree.root->left->right = new TreeNode(6);
	tree.root->left->right->left = new TreeNode(4);
	tree.root->left->right->right = new TreeNode(8);
	tree.root->left->right->right->left = new TreeNode(2);
	tree.root->left->right->right->right = new TreeNode(3);
	tree.root->right->right = new TreeNode(11);
	tree.root->right->right->right = new TreeNode(20);
	tree.euler_path();
	return 0;
}

Output

  9  5  1  10   1   5  6  4   6  8  2   8  3   8   6   5   9  7  11  20   11   7   9
// Include namespace system
using System;
/*
    C# Program 
    Euler tour of Binary Tree
*/
//  Tree Node
public class TreeNode
{
    public int data;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
//  Binary Tree
public class BinaryTree
{
    public TreeNode root;
    public BinaryTree()
    {
        this.root = null;
    }
    // Recursively Printing the elements of euler path
    public void print_euler_path(TreeNode node, TreeNode parent)
    {
        //  When tree node empty
        if (node == null)
        {
            return;
        }
        Console.Write("  " + node.data);
        // Recursively executing the left and right subtree
        print_euler_path(node.left, node);
        print_euler_path(node.right, node);
        if (parent != null)
        {
            // When parent not null
            Console.Write("   " + parent.data);
        }
    }
    //  Handles the request of printing the euler path
    public void euler_path()
    {
        if (this.root == null)
        {
            Console.Write("\n Empty Tree \n");
        }
        else
        {
            print_euler_path(this.root, null);
        }
    }
    public static void Main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
        /*
                9
               / \
              5   7
             / \   \
            1   6   11
           /   / \   \
          10  4   8   20
                 / \
                2   3
        -----------------------
            Binary Tree
        */
        tree.root = new TreeNode(9);
        tree.root.left = new TreeNode(5);
        tree.root.right = new TreeNode(7);
        tree.root.left.left = new TreeNode(1);
        tree.root.left.left.left = new TreeNode(10);
        tree.root.left.right = new TreeNode(6);
        tree.root.left.right.left = new TreeNode(4);
        tree.root.left.right.right = new TreeNode(8);
        tree.root.left.right.right.left = new TreeNode(2);
        tree.root.left.right.right.right = new TreeNode(3);
        tree.root.right.right = new TreeNode(11);
        tree.root.right.right.right = new TreeNode(20);
        tree.euler_path();
    }
}

Output

  9  5  1  10   1   5  6  4   6  8  2   8  3   8   6   5   9  7  11  20   11   7   9
<?php
/*
    Php Program 
    Euler tour of Binary Tree
*/
//  Tree Node
class TreeNode
{
    public $data;
    public $left;
    public $right;

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

    function __construct()
    {
        $this->root = null;
    }
    // Recursively Printing the elements of euler path
    public  function print_euler_path($node, $parent)
    {
        //  When tree node empty
        if ($node == null)
        {
            return;
        }
        echo "  ". $node->data;
        // Recursively executing the left and right subtree
        $this->print_euler_path($node->left, $node);
        $this->print_euler_path($node->right, $node);
        if ($parent != null)
        {
            // When parent not null
            echo "   ". $parent->data;
        }
    }
    //  Handles the request of printing the euler path
    public  function euler_path()
    {
        if ($this->root == null)
        {
            echo "\n Empty Tree \n";
        }
        else
        {
            $this->print_euler_path($this->root, null);
        }
    }
}

function main()
{
    $tree = new BinaryTree();
    /*
            9
           / \
          5   7
         / \   \
        1   6   11
       /   / \   \
      10  4   8   20
             / \
            2   3
    -----------------------
        Binary Tree
    */
    $tree->root = new TreeNode(9);
    $tree->root->left = new TreeNode(5);
    $tree->root->right = new TreeNode(7);
    $tree->root->left->left = new TreeNode(1);
    $tree->root->left->left->left = new TreeNode(10);
    $tree->root->left->right = new TreeNode(6);
    $tree->root->left->right->left = new TreeNode(4);
    $tree->root->left->right->right = new TreeNode(8);
    $tree->root->left->right->right->left = new TreeNode(2);
    $tree->root->left->right->right->right = new TreeNode(3);
    $tree->root->right->right = new TreeNode(11);
    $tree->root->right->right->right = new TreeNode(20);
    $tree->euler_path();
}
main();

Output

  9  5  1  10   1   5  6  4   6  8  2   8  3   8   6   5   9  7  11  20   11   7   9
/*
    Node Js Program 
    Euler tour of Binary Tree
*/
//  Tree Node
class TreeNode
{
    constructor(data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
//  Binary Tree
class BinaryTree
{
    constructor()
    {
        this.root = null;
    }
    // Recursively Printing the elements of euler path
    print_euler_path(node, parent)
    {
        //  When tree node empty
        if (node == null)
        {
            return;
        }
        process.stdout.write("  " + node.data);
        // Recursively executing the left and right subtree
        this.print_euler_path(node.left, node);
        this.print_euler_path(node.right, node);
        if (parent != null)
        {
            // When parent not null
            process.stdout.write("   " + parent.data);
        }
    }
    //  Handles the request of printing the euler path
    euler_path()
    {
        if (this.root == null)
        {
            process.stdout.write("\n Empty Tree \n");
        }
        else
        {
            this.print_euler_path(this.root, null);
        }
    }
}

function main()
{
    var tree = new BinaryTree();
    /*
            9
           / \
          5   7
         / \   \
        1   6   11
       /   / \   \
      10  4   8   20
             / \
            2   3
    -----------------------
        Binary Tree
    */
    tree.root = new TreeNode(9);
    tree.root.left = new TreeNode(5);
    tree.root.right = new TreeNode(7);
    tree.root.left.left = new TreeNode(1);
    tree.root.left.left.left = new TreeNode(10);
    tree.root.left.right = new TreeNode(6);
    tree.root.left.right.left = new TreeNode(4);
    tree.root.left.right.right = new TreeNode(8);
    tree.root.left.right.right.left = new TreeNode(2);
    tree.root.left.right.right.right = new TreeNode(3);
    tree.root.right.right = new TreeNode(11);
    tree.root.right.right.right = new TreeNode(20);
    tree.euler_path();
}
main();

Output

  9  5  1  10   1   5  6  4   6  8  2   8  3   8   6   5   9  7  11  20   11   7   9
#  Python 3 Program 
#  Euler tour of Binary Tree

#  Tree Node
class TreeNode :
	
	def __init__(self, data) :
		self.data = data
		self.left = None
		self.right = None
	

#  Binary Tree
class BinaryTree :
	
	def __init__(self) :
		self.root = None
	
	# Recursively Printing the elements of euler path
	def print_euler_path(self, node, parent) :
		if (node == None) :
			#  When tree node empty
			return
		
		print("  ", node.data, end = "")
		# Recursively executing the left and right subtree
		self.print_euler_path(node.left, node)
		self.print_euler_path(node.right, node)
		if (parent != None) :
			# When parent not null
			print("   ", parent.data, end = "")
		
	
	#  Handles the request of printing the euler path
	def euler_path(self) :
		if (self.root == None) :
			print("\n Empty Tree \n", end = "")
		else :
			self.print_euler_path(self.root, None)
		
	

def main() :
	tree = BinaryTree()
	# 
	#                 9
	#                / \
	#               5   7
	#              / \   \
	#             1   6   11
	#            /   / \   \
	#           10  4   8   20
	#                  / \
	#                 2   3
	#         -----------------------
	#             Binary Tree
	#         
	
	tree.root = TreeNode(9)
	tree.root.left = TreeNode(5)
	tree.root.right = TreeNode(7)
	tree.root.left.left = TreeNode(1)
	tree.root.left.left.left = TreeNode(10)
	tree.root.left.right = TreeNode(6)
	tree.root.left.right.left = TreeNode(4)
	tree.root.left.right.right = TreeNode(8)
	tree.root.left.right.right.left = TreeNode(2)
	tree.root.left.right.right.right = TreeNode(3)
	tree.root.right.right = TreeNode(11)
	tree.root.right.right.right = TreeNode(20)
	tree.euler_path()

if __name__ == "__main__": main()

Output

   9   5   1   10    1    5   6   4    6   8   2    8   3    8    6    5    9   7   11   20    11    7    9
#  Ruby Program 
#  Euler tour of Binary Tree

#  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) 
		self.data = data
		self.left = nil
		self.right = nil
	end

end

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

	# Recursively Printing the elements of euler path
	def print_euler_path(node, parent) 
		if (node == nil) 
			#  When tree node empty
			return
		end

		print("  ", node.data)
		# Recursively executing the left and right subtree
		self.print_euler_path(node.left, node)
		self.print_euler_path(node.right, node)
		if (parent != nil) 
			# When parent not null
			print("   ", parent.data)
		end

	end

	#  Handles the request of printing the euler path
	def euler_path() 
		if (self.root == nil) 
			print("\n Empty Tree \n")
		else 
			self.print_euler_path(self.root, nil)
		end

	end

end

def main() 
	tree = BinaryTree.new()
	# 
	#                 9
	#                / \
	#               5   7
	#              / \   \
	#             1   6   11
	#            /   / \   \
	#           10  4   8   20
	#                  / \
	#                 2   3
	#         -----------------------
	#             Binary Tree
	#         
	
	tree.root = TreeNode.new(9)
	tree.root.left = TreeNode.new(5)
	tree.root.right = TreeNode.new(7)
	tree.root.left.left = TreeNode.new(1)
	tree.root.left.left.left = TreeNode.new(10)
	tree.root.left.right = TreeNode.new(6)
	tree.root.left.right.left = TreeNode.new(4)
	tree.root.left.right.right = TreeNode.new(8)
	tree.root.left.right.right.left = TreeNode.new(2)
	tree.root.left.right.right.right = TreeNode.new(3)
	tree.root.right.right = TreeNode.new(11)
	tree.root.right.right.right = TreeNode.new(20)
	tree.euler_path()
end

main()

Output

  9  5  1  10   1   5  6  4   6  8  2   8  3   8   6   5   9  7  11  20   11   7   9
/*
    Scala Program 
    Euler tour of Binary Tree
*/

//  Tree Node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
    def this(data: Int)
    {
        this(data, null, null);
    }
}
//  Binary Tree
class BinaryTree(var root: TreeNode)
{
    def this()
    {
        this(null);
    }
    // Recursively Printing the elements of euler path
    def print_euler_path(node: TreeNode, parent: TreeNode): Unit = {
        //  When tree node empty
        if (node == null)
        {
            return;
        }
        print("  " + node.data);
        // Recursively executing the left and right subtree
        print_euler_path(node.left, node);
        print_euler_path(node.right, node);
        if (parent != null)
        {
            // When parent not null
            print("   " + parent.data);
        }
    }
    //  Handles the request of printing the euler path
    def euler_path(): Unit = {
        if (this.root == null)
        {
            print("\n Empty Tree \n");
        }
        else
        {
            print_euler_path(this.root, null);
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var tree: BinaryTree = new BinaryTree();
        /*
                9
               / \
              5   7
             / \   \
            1   6   11
           /   / \   \
          10  4   8   20
                 / \
                2   3
        -----------------------
            Binary Tree
         */
        tree.root = new TreeNode(9);
        tree.root.left = new TreeNode(5);
        tree.root.right = new TreeNode(7);
        tree.root.left.left = new TreeNode(1);
        tree.root.left.left.left = new TreeNode(10);
        tree.root.left.right = new TreeNode(6);
        tree.root.left.right.left = new TreeNode(4);
        tree.root.left.right.right = new TreeNode(8);
        tree.root.left.right.right.left = new TreeNode(2);
        tree.root.left.right.right.right = new TreeNode(3);
        tree.root.right.right = new TreeNode(11);
        tree.root.right.right.right = new TreeNode(20);
        tree.euler_path();
    }
}

Output

  9  5  1  10   1   5  6  4   6  8  2   8  3   8   6   5   9  7  11  20   11   7   9
/*
    Swift 4 Program 
    Euler tour of Binary Tree
*/
//  Tree Node
class TreeNode
{
    var data: Int;
    var left: TreeNode? ;
    var right: TreeNode? ;
    init(_ data: Int)
    {
        self.data = data;
        self.left = nil;
        self.right = nil;
    }
}
//  Binary Tree
class BinaryTree
{
    var root: TreeNode? ;
    init()
    {
        self.root = nil;
    }
    // Recursively Printing the elements of euler path
    func print_euler_path(_ node: TreeNode? , _ parent : TreeNode? )
    {
        //  When tree node empty
        if (node == nil)
        {
            return;
        }
        print("  ", node!.data, terminator: "");
        // Recursively executing the left and right subtree
        self.print_euler_path(node!.left, node);
        self.print_euler_path(node!.right, node);
        if (parent != nil)
        {
            // When parent not null
            print("   ", parent!.data, terminator: "");
        }
    }
    //  Handles the request of printing the euler path
    func euler_path()
    {
        if (self.root == nil)
        {
            print("\n Empty Tree \n", terminator: "");
        }
        else
        {
            self.print_euler_path(self.root, nil);
        }
    }
}
func main()
{
    let tree: BinaryTree = BinaryTree();
    /*
           9
          / \
         5   7
        / \   \
       1   6   11
      /   / \   \
     10  4   8   20
            / \
           2   3
    -----------------------
       Binary Tree
    */
    tree.root = TreeNode(9);
    tree.root!.left = TreeNode(5);
    tree.root!.right = TreeNode(7);
    tree.root!.left!.left = TreeNode(1);
    tree.root!.left!.left!.left = TreeNode(10);
    tree.root!.left!.right = TreeNode(6);
    tree.root!.left!.right!.left = TreeNode(4);
    tree.root!.left!.right!.right = TreeNode(8);
    tree.root!.left!.right!.right!.left = TreeNode(2);
    tree.root!.left!.right!.right!.right = TreeNode(3);
    tree.root!.right!.right = TreeNode(11);
    tree.root!.right!.right!.right = TreeNode(20);
    tree.euler_path();
}
main();

Output

   9   5   1   10    1    5   6   4    6   8   2    8   3    8    6    5    9   7   11   20    11    7    9


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