-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTernaryToTree.java
More file actions
48 lines (48 loc) · 828 Bytes
/
TernaryToTree.java
File metadata and controls
48 lines (48 loc) · 828 Bytes
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
package practice;
import java.util.*;
public class ternaryToTree
{
static class Node
{
Node left,right;
char data;
Node(char data)
{
this.data=data;
left=right=null;
}
}
Node root;
void print(Node r)
{
if(r!=null)
{
System.out.print(r.data+" ");
print(r.left);
print(r.right);
}
}
Node convert(String exp,int i)
{
if(i>=exp.length())
return null;
Node root=new Node(exp.charAt(i));
i++;
if(i<exp.length()&&exp.charAt(i)=='?')
root.left=convert(exp,i+1);
else if(i<exp.length())
{
root.right=convert(exp,i+1);
}
return root;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the expression");
String exp=sc.next();
ternaryToTree t=new ternaryToTree();
Node r=t.convert(exp,0);
t.print(r);
}
}