Triple Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find it’s Triple Order Traversal.
Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order:
- Visit the root node
- Traverse the left subtree
- Visit the root node
- Traverse the right subtree
- Visit the root node.
Examples:
Input:
A
/ \
B C
/ \ \
F D E
Output: A B F F F B D D D B A C C E E E C A
Input:
A
/ \
B C
/ \
E D
/
F
Output: A B E F F F E E B D D D B A C C C A
Approach:
Follow the steps below to solve the problem:
- Start the traversal from the root.
- If the current node does not exist, simply return from it.
- Otherwise:
- Print the value of the current node.
- Recursively traverse the left subtree.
- Again, print the current node.
- Recursively traverse the right subtree.
- Again, print the current node.
- Repeat the above steps until all nodes in the tree are visited.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct node {
char data;
struct node *left, *right;
};
struct node* newNode( char ch)
{
struct node* Node = new node();
Node->data = ch;
Node->left = NULL;
Node->right = NULL;
return Node;
}
void tripleOrderTraversal( struct node* root)
{
if (root) {
cout << root->data << " " ;
tripleOrderTraversal(root->left);
cout << root->data << " " ;
tripleOrderTraversal(root->right);
cout << root->data << " " ;
}
}
int main()
{
struct node* root = newNode( 'A' );
root->left = newNode( 'B' );
root->right = newNode( 'C' );
root->left->left = newNode( 'F' );
root->left->right = newNode( 'D' );
root->right->right = newNode( 'E' );
tripleOrderTraversal(root);
}
|
Java
import java.util.*;
class GFG{
static class node
{
char data;
node left, right;
};
static node newNode( char ch)
{
node n = new node();
n.data = ch;
n.left = null ;
n.right = null ;
return n;
}
static void tripleOrderTraversal(node root)
{
if (root != null )
{
System.out.print(root.data + " " );
tripleOrderTraversal(root.left);
System.out.print(root.data + " " );
tripleOrderTraversal(root.right);
System.out.print(root.data + " " );
}
}
public static void main(String[] args)
{
node root = newNode( 'A' );
root.left = newNode( 'B' );
root.right = newNode( 'C' );
root.left.left = newNode( 'F' );
root.left.right = newNode( 'D' );
root.right.right = newNode( 'E' );
tripleOrderTraversal(root);
}
}
|
Python3
class Node:
def __init__( self , ch):
self .data = ch
self .left = None
self .right = None
def tripleOrderTraversal(root):
if root:
print (root.data, end = ' ' )
tripleOrderTraversal(root.left)
print (root.data, end = ' ' )
tripleOrderTraversal(root.right)
print (root.data, end = ' ' )
root = Node( 'A' )
root.left = Node( 'B' )
root.right = Node( 'C' )
root.left.left = Node( 'F' )
root.left.right = Node( 'D' )
root.right.right = Node( 'E' )
tripleOrderTraversal(root)
|
C#
using System;
class GFG{
public class node
{
public char data;
public node left, right;
};
static node newNode( char ch)
{
node n = new node();
n.data = ch;
n.left = null ;
n.right = null ;
return n;
}
static void tripleOrderTraversal(node root)
{
if (root != null )
{
Console.Write(root.data + " " );
tripleOrderTraversal(root.left);
Console.Write(root.data + " " );
tripleOrderTraversal(root.right);
Console.Write(root.data + " " );
}
}
public static void Main(String[] args)
{
node root = newNode( 'A' );
root.left = newNode( 'B' );
root.right = newNode( 'C' );
root.left.left = newNode( 'F' );
root.left.right = newNode( 'D' );
root.right.right = newNode( 'E' );
tripleOrderTraversal(root);
}
}
|
Javascript
<script>
class node {
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
function newNode(ch)
{
var n = new node();
n.data = ch;
n.left = null ;
n.right = null ;
return n;
}
function tripleOrderTraversal(root)
{
if (root) {
document.write( root.data + " " );
tripleOrderTraversal(root.left);
document.write( root.data + " " );
tripleOrderTraversal(root.right);
document.write( root.data + " " );
}
}
var root = newNode( 'A' );
root.left = newNode( 'B' );
root.right = newNode( 'C' );
root.left.left = newNode( 'F' );
root.left.right = newNode( 'D' );
root.right.right = newNode( 'E' );
tripleOrderTraversal(root);
</script>
|
Output:
A B F F F B D D D B A C C E E E C A
Time Complexity: O(N), where N is the total number of nodes in the binary tree.
Auxiliary Space: O(N)
Applications: Euler tour of a tree is a modified version of triple order traversal.
Last Updated :
11 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...