-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbintree.java
More file actions
51 lines (49 loc) · 1.15 KB
/
Copy pathbintree.java
File metadata and controls
51 lines (49 loc) · 1.15 KB
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
class node{
char key;
node left;
node right;
node(char key){
this.key=key;
}
}
class treetraversal{
node root;
void pretrav(node n){
if(n!=null){
System.out.print(n.key+" ");
pretrav(n.left);
pretrav(n.right);
}
}
void postrav(node n){
if(n!=null){
postrav(n.left);
postrav(n.right);
System.out.print(n.key+" ");
}
}
void inotrav(node n){
if(n!=null){
inotrav(n.left);
System.out.print(n.key+" ");
inotrav(n.right);
}
}
}
public class bintree {
public static void main(String[] args) {
treetraversal ob=new treetraversal();
ob.root=new node('a');
ob.root.right=new node('b');
ob.root.left=new node('c');
ob.root.right.left=new node('d');
ob.root.right.right=new node('e');
ob.root.left.left=new node('f');
ob.root.left.right=new node('g');
ob.pretrav(ob.root);
System.out.println();
ob.inotrav(ob.root);
System.out.println();
ob.postrav(ob.root);
}
}