Triple order traversal of a binary tree in java

Java program for Triple order traversal of a binary tree. Here more solutions.

``````/*
Java program for
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
this.root = null;
}
public void tripleOrder(TreeNode node)
{
if (node != null)
{
// Print preorder node
System.out.print(" " + node.data);
// Visit left subtree
this.tripleOrder(node.left);
// Print inorder node
System.out.print(" " + node.data);
// Visit right subtree
this.tripleOrder(node.right);
// Print postorder node
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
// ----------------------------------
// 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
tree.tripleOrder(tree.root);
}
}``````

Output

`` 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``

Comment

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.