-
Notifications
You must be signed in to change notification settings - Fork 0
/
jap2.java
149 lines (143 loc) · 2.35 KB
/
jap2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.BufferedReader;
import java.io.InputStreamReader;
class jap2
{
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//int n=Integer.parseInt(br.readLine());
tree tr=new tree();
tr.insert(5);
tr.insert(2);
tr.insert(8);
tr.insert(1);
tr.insert(3);
tr.insert(6);
tr.insert(10);
tr.level();
queue s=new queue();
//jap2 j=new jap2();
//j.print_depth(root);
}
queue q=null;
public void print_depth()
{
while(q.start!=q.end)
{
q.pop();
}
}
public void print_dep(tree root)
{
if(root==null)
return;
q.push(root);
print_dep(root.left);
print_dep(root.right);
}
}
class queue
{
tree v;
queue start=null,next=null,end=null;
void push(tree n)
{
if(start==null)
{
start=new queue();
start.v=n;
start.next=null;
end=start;
return;
}
queue temp=new queue();
temp.v=n;
temp.next=null;
end.next=temp;
end=temp;
}
tree pop()
{
if(start==null)
{
System.out.println("Emptyy");
return null;
}
tree v=start.v;
start=start.next;
return v;
}
}
class tree
{
int v;
tree left,right,root=null;
public tree add(int n)
{
tree t=new tree();
t.left=t.right=null;
t.v=n;
return t;
}
public void insert(int n)
{
insert(this.root,n);
}
public void insert(tree root,int n)
{
if(this.root==null)
{
this.root=add(n);
}
else if(n>root.v)
{
if(root.right==null)
{
root.right=add(n);
return;
}
insert(root.right,n);
}
else if(n<root.v)
{
if(root.left==null)
{
root.left=add(n);
return;
}
insert(root.left,n);
}
}
public void inorder()
{
inorder(root);
}
public void inorder(tree root)
{
if(root==null)
return;
inorder(root.left);
System.out.println(root.v);
inorder(root.right);
}
public void level()
{
queue q=new queue();
tree lroot=this.root;
q.push(lroot);
while(q.start!=null)
{
lroot=q.pop();
System.out.println(lroot.v);
if(lroot.left!=null)
{q.push(lroot.left);
//System.out.println("le="+lroot.left.v);
}
if(lroot.right!=null)
{
q.push(lroot.right);
//System.out.println("ri"+lroot.right.v);
}
}
}
}