Open In App

Euler tour of Binary Tree

Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary tree where each node can have at most two child nodes, the task is to find the Euler tour of the binary tree. Euler tour is represented by a pointer to the topmost node in the tree. If the tree is empty, then value of root is NULL.

Examples:  

Input : 

Output: 1 5 4 2 4 3 4 5 1

Approach: 
 

  1. First, start with root node 1, Euler[0]=1 
  2. Go to left node i.e, node 5, Euler[1]=5 
  3. Go to left node i.e, node 4, Euler[2]=4 
  4. Go to left node i.e, node 2, Euler[3]=2 
  5. Go to left node i.e, NULL, go to parent node 4 Euler[4]=4 
  6. Go to right node i.e, node 3 Euler[5]=3 
  7. No child, go to parent, node 4 Euler[6]=4 
  8. All child discovered, go to parent node 5 Euler[7]=5 
  9. All child discovered, go to parent node 1 Euler[8]=1 

Euler tour of tree has been already discussed where it can be applied to N-ary tree which is represented by adjacency list. If a Binary tree is represented by the classical structured way by links and nodes, then there need to first convert the tree into adjacency list representation and then we can find the Euler tour if we want to apply method discussed in the original post. But this increases the space complexity of the program. Here, In this post, a generalized space-optimized version is discussed which can be directly applied to binary trees represented by structure nodes. 

This method : 

  1. Works without the use of Visited arrays. 
  2. Requires exactly 2*N-1 vertices to store Euler tour.

Implementation:

C++




// C++ program to find euler tour of binary tree
#include <bits/stdc++.h>
using namespace std;
 
/* A tree node structure */
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};
 
/* Utility function to create a new Binary Tree node */
struct Node* newNode(int data)
{
    struct Node* temp = new struct Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Find Euler Tour
void eulerTree(struct Node* root, vector<int> &Euler)
{
    // store current node's data
    Euler.push_back(root->data);
 
    // If left node exists
    if (root->left)
    {
        // traverse left subtree
        eulerTree(root->left, Euler);
 
        // store parent node's data
        Euler.push_back(root->data);
    }
 
    // If right node exists
    if (root->right)
    {
        // traverse right subtree
        eulerTree(root->right, Euler);
 
        // store parent node's data
        Euler.push_back(root->data);
    }
}
 
// Function to print Euler Tour of tree
void printEulerTour(Node *root)
{
    // Stores Euler Tour
    vector<int> Euler;
 
    eulerTree(root, Euler);
 
    for (int i = 0; i < Euler.size(); i++)
        cout << Euler[i] << " ";
}
 
/* Driver function to test above functions */
int main()
{
    // Constructing tree given in the above figure
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->right->left->right = newNode(8);
 
    // print Euler Tour
    printEulerTour(root);
 
    return 0;
}


Java




// Java program to find euler tour of binary tree
import java.util.*;
 
class GFG
{
 
    /* A tree node structure */
    static class Node
    {
        int data;
        Node left;
        Node right;
    };
 
    /* Utility function to create a new Binary Tree node */
    static Node newNode(int data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.left = temp.right = null;
        return temp;
    }
 
    // Find Euler Tour
    static Vector<Integer> eulerTree(Node root, Vector<Integer> Euler)
    {
        // store current node's data
        Euler.add(root.data);
 
        // If left node exists
        if (root.left != null)
        {
            // traverse left subtree
            Euler = eulerTree(root.left, Euler);
 
            // store parent node's data
            Euler.add(root.data);
        }
 
        // If right node exists
        if (root.right != null)
        {
            // traverse right subtree
            Euler = eulerTree(root.right, Euler);
 
            // store parent node's data
            Euler.add(root.data);
        }
        return Euler;
    }
 
    // Function to print Euler Tour of tree
    static void printEulerTour(Node root)
    {
        // Stores Euler Tour
        Vector<Integer> Euler = new Vector<Integer>();
 
        Euler = eulerTree(root, Euler);
 
        for (int i = 0; i < Euler.size(); i++)
            System.out.print(Euler.get(i) + " ");
    }
 
    /* Driver function to test above functions */
    public static void main(String[] args)
    {
        // Constructing tree given in the above figure
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(4);
        root.left.right = newNode(5);
        root.right.left = newNode(6);
        root.right.right = newNode(7);
        root.right.left.right = newNode(8);
 
        // print Euler Tour
        printEulerTour(root);
 
    }
}
 
// This code is contributed by Rajput-Ji


Python3




# python program to find euler tour of binary tree
# a Node of binary tree
class Node:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
         
# Find Euler Tree
def eulerTree(root, euler):
   
    #   store current node's data
    euler.append(root.data)
     
    # If left node exists
    if root.left:
       
        # traverse left subtree
        euler = eulerTree(root.left, euler)
         
        # store parent node's data
        euler.append(root.data)
         
    # If right node exists
    if root.right:
       
        # traverse right subtree
        euler = eulerTree(root.right, euler)
         
        # store parent node's data
        euler.append(root.data)
    return euler
   
# Function to print Euler Tour of tree
def printEulerTour(root):
   
    # Stores Euler Tour
    euler = []
    euler = eulerTree(root, euler)
    for i in range(len(euler)):
        print(euler[i], end=" ")
 
# Driver function to test above functions */
# Constructing tree given in the above figure
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.right.left.right = Node(8)
 
# print Euler Tour
printEulerTour(root)
 
# This code is contributed by RAJAT KUMAR...(GLA UNIVERSITY)....


C#




// C# program to find euler tour of binary tree
using System;
using System.Collections.Generic;
 
class GFG
{
 
    /* A tree node structure */
    public class Node
    {
        public int data;
        public Node left;
        public Node right;
    };
 
    /* Utility function to create a new Binary Tree node */
    static Node newNode(int data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.left = temp.right = null;
        return temp;
    }
 
    // Find Euler Tour
    static List<int> eulerTree(Node root, List<int> Euler)
    {
        // store current node's data
        Euler.Add(root.data);
 
        // If left node exists
        if (root.left != null)
        {
            // traverse left subtree
            Euler = eulerTree(root.left, Euler);
 
            // store parent node's data
            Euler.Add(root.data);
        }
 
        // If right node exists
        if (root.right != null)
        {
            // traverse right subtree
            Euler = eulerTree(root.right, Euler);
 
            // store parent node's data
            Euler.Add(root.data);
        }
        return Euler;
    }
 
    // Function to print Euler Tour of tree
    static void printEulerTour(Node root)
    {
        // Stores Euler Tour
        List<int> Euler = new List<int>();
 
        Euler = eulerTree(root, Euler);
 
        for (int i = 0; i < Euler.Count; i++)
            Console.Write(Euler[i] + " ");
    }
 
    /* Driver function to test above functions */
    public static void Main(String[] args)
    {
        // Constructing tree given in the above figure
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(4);
        root.left.right = newNode(5);
        root.right.left = newNode(6);
        root.right.right = newNode(7);
        root.right.left.right = newNode(8);
 
        // print Euler Tour
        printEulerTour(root);
 
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// Javascript program to find euler tour of binary tree
/* A tree node structure */
class Node
{
    constructor()
    {
        this.data = 0;
        this.left = null;
        this.right = null;
    }
};
 
/* Utility function to create a new Binary Tree node */
function newNode(data)
{
    var temp = new Node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
// Find Euler Tour
function eulerTree(root, Euler)
{
    // store current node's data
    Euler.push(root.data);
     
    // If left node exists
    if (root.left != null)
    {
        // traverse left subtree
        Euler = eulerTree(root.left, Euler);
         
        // store parent node's data
        Euler.push(root.data);
    }
     
    // If right node exists
    if (root.right != null)
    {
        // traverse right subtree
        Euler = eulerTree(root.right, Euler);
         
        // store parent node's data
        Euler.push(root.data);
    }
    return Euler;
}
 
// Function to print Euler Tour of tree
function printEulerTour(root)
{
 
    // Stores Euler Tour
    var Euler = [];
    Euler = eulerTree(root, Euler);
    for (var i = 0; i < Euler.length; i++)
        document.write(Euler[i] + " ");
}
 
/* Driver function to test above functions */
// Constructing tree given in the above figure
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
 
// print Euler Tour
printEulerTour(root);
 
// This code is contributed by itsok.
</script>


Output

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

Complexity Analysis:

  • Time Complexity: O(2*N-1) where N is number of nodes in the tree. 
  • Auxiliary Space : O(2*N-1) where N is number of nodes in the tree. 


Last Updated : 18 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads