-
Notifications
You must be signed in to change notification settings - Fork 0
/
TripleOrderTraversalOfBinaryTree.java
65 lines (56 loc) · 1.38 KB
/
TripleOrderTraversalOfBinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// TRIPLE ORDER Traversal of a Binary Tree(Every node printed 3 times)
package Trees;
class Node
{
int data;
Node left;
Node right;
public Node(int d)
{
data=d;
left=null;
right=null;
}
}
class TripleOrderTraversal
{
void tripleorder(Node root)
{
if(root==null)
{
return;
}
//Here ROOT means printing the data
//For TRIPLE ORDER we have-> ROOT, LEFT_SUBTREE, ROOT, RIGHT_SUBTREE, ROOT
System.out.print(root.data+" ");
tripleorder(root.left);
System.out.print(root.data+" ");
tripleorder(root.right);
System.out.print(root.data+" ");
}
public static void main(String...args)
{
Node first=new Node(10);
Node second=new Node(20);
Node third=new Node(30);
Node fourth=new Node(40);
Node fifth=new Node(50);
Node sixth=new Node(60);
Node seventh=new Node(70);
first.left=second;
first.right=third;
second.left=fourth;
second.right=fifth;
third.left=sixth;
third.right=seventh;
fourth.left=null;
fourth.right=null;
fifth.left=null;
fifth.right=null;
sixth.left=null;
sixth.right=null;
seventh.left=null;
seventh.right=null;
new TripleOrderTraversal().tripleorder(first);
}
}