-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
206 lines (172 loc) · 3.14 KB
/
BinaryTree.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package tree;
class node{
node left;
node right;
Object data;
node(Object value){
left=null;
right=null;
data=value;
}
node getLeft(){
return left;
}
void setLeft(node value){
left=value;
}
node getRight(){
return right;
}
void setRight(node value){
right=value;
}
Object getData(){
return data;
}
void setData(Object value){
data=value;
}
}
class BinaryTreeDS{
node root;
Object data;
void add(Object data){
node temp=root;
node curr=new node(data);
node prev=temp;
//System.out.println("Root="+root);
if ( root == null ){
root=curr;
}
else{
while(temp!=null){
prev=temp;
if ((int)temp.getData() > (int)data){
temp=temp.getLeft();
}
else{
temp=temp.getRight();
}
}
if ((int)prev.getData() > (int)data)
prev.setLeft(curr);
else
prev.setRight(curr);
}
}
void inorder(){
r_inorder(root);
}
void r_inorder(node p){
if (p== null)
return;
else{
r_inorder(p.getLeft());
System.out.println(p.getData());
r_inorder(p.getRight());
}
}
void preorder(){
r_preorder(root);
}
void r_preorder(node p){
if (p== null)
return;
else{
System.out.println(p.getData());
r_preorder(p.getLeft());
r_preorder(p.getRight());
}
}
void postorder(){
r_postorder(root);
}
void r_postorder(node p){
if (p==null){
return;
}
else{
r_postorder(p.getLeft());
r_postorder(p.getRight());
System.out.println(p.getData());
}
}
Object getMax(){
node temp=root;
while(temp.getRight()!=null){
temp=temp.getRight();
}
return temp.getData();
}
Object getMin(){
node temp=root;
while(temp.getLeft()!=null){
temp=temp.getLeft();
}
return temp.getData();
}
int getHeight(){
return r_getHeight(root);
}
int r_getHeight(node p){
if (p==null){
return 0;
}
else{
int x = r_getHeight(p.getLeft());
int y = r_getHeight(p.getRight());
if (x>y)
return x+1;
else
return y+1;
}
}
int countElements(){
return r_countElements(root);
}
int r_countElements(node p){
if (p==null){
return 0;
}
else{
int x=r_countElements(p.getLeft());
int y=r_countElements(p.getRight());
return 1+x+y;
}
}
boolean search(Object value){
node temp=root;
while(temp!=null){
if ((int)temp.getData()==(int) value)
return true;
else if ((int)temp.getData()<(int)value)
temp=temp.getRight();
else
temp=temp.getLeft();
}
return false;
}
}
public class BinaryTree {
public static void main (String args[]){
BinaryTreeDS bt=new BinaryTreeDS();
bt.add(50);
bt.add(75);
bt.add(25);
bt.add(60);
bt.add(90);
bt.add(80);
System.out.println("Inorder Traversal:");
bt.inorder();
System.out.println("Preorder Traversal:");
bt.preorder();
System.out.println("Postorder Traversal:");
bt.postorder();
System.out.println("Max element is "+bt.getMax());
System.out.println("Min element is "+bt.getMin());
System.out.println("Height of B tree is "+bt.getHeight());
System.out.println("Total Elements in the tree is "+bt.countElements());
System.out.println(bt.search(80));
System.out.println(bt.search(51));
}
}