1
1
package com .company ;
2
2
3
+ import java .util .LinkedList ;
4
+ import java .util .Queue ;
5
+ import java .util .Stack ;
6
+
3
7
class BST {
4
8
class Node {
5
9
int data ;
@@ -14,6 +18,7 @@ class Node{
14
18
}
15
19
16
20
Node root ;
21
+
17
22
void insert (int data ){
18
23
root =insertRec (root ,data );
19
24
}
@@ -30,8 +35,10 @@ private Node insertRec(Node root,int data){
30
35
}
31
36
return root ;
32
37
}
38
+
33
39
void inorder (){
34
40
inorderRec (root );
41
+ System .out .println ();
35
42
}
36
43
private void inorderRec (Node root ){
37
44
if (root !=null ){
@@ -40,6 +47,7 @@ private void inorderRec(Node root){
40
47
inorderRec (root .right );
41
48
}
42
49
}
50
+
43
51
void preorder (){
44
52
preorderRec (root );
45
53
}
@@ -50,6 +58,7 @@ private void preorderRec(Node root){
50
58
preorderRec (root .right );
51
59
}
52
60
}
61
+
53
62
void postorder (){
54
63
postorderRec (root );
55
64
}
@@ -60,5 +69,132 @@ private void postorderRec(Node root){
60
69
System .out .print (root .data +" " );
61
70
}
62
71
}
72
+
73
+ public int sum (){
74
+ return sumRec (root );
75
+ }
76
+ private int sumRec (Node root ){
77
+ if (root ==null )
78
+ return 0 ;
79
+ return root .data +sumRec (root .left )+sumRec (root .right );
80
+ }
81
+
82
+ public int getDiffEvenOddNodes (){
83
+ return getDiffEvenOddNodesRec (root );
84
+ }
85
+ private int getDiffEvenOddNodesRec (Node root ){
86
+ if (root ==null )
87
+ return 0 ;
88
+ return root .data -getDiffEvenOddNodesRec (root .left )-getDiffEvenOddNodesRec (root .right );
89
+ }
90
+
91
+ public int noOfNodes (){
92
+ return noofnodes (root );
93
+ }
94
+ private int noofnodes (Node root ){
95
+ if (root ==null )
96
+ return 0 ;
97
+ return 1 +noofnodes (root .left )+noofnodes (root .right );
98
+ }
99
+
100
+ public int noOfLeadNodes (){
101
+ return noofleafnodes (root );
102
+ }
103
+ private int noofleafnodes (Node root ){
104
+ if (root ==null )
105
+ return 0 ;
106
+ if (root .left ==null &&root .right ==null )
107
+ return 1 ;
108
+ return noofleafnodes (root .left )+noofleafnodes (root .right );
109
+ }
110
+
111
+ public int height (){
112
+ return heightRec (root );
113
+ }
114
+ private int heightRec (Node root ){
115
+ if (root ==null )
116
+ return -1 ;
117
+ return max (heightRec (root .left ),heightRec (root .right ))+1 ;
118
+ }
119
+ private int max (int a ,int b ){
120
+ return (a >b )?a :b ;
121
+ }
122
+ public void levelOrderTraversal () {
123
+ levelOrderTraversalRec (root );
124
+ }
125
+ private void levelOrderTraversalRec (Node root ){
126
+ if (root ==null )
127
+ return ;
128
+ Queue <Node > q = new LinkedList <>();
129
+ q .add (root );
130
+ while (!q .isEmpty ()){
131
+ Node node =q .remove ();
132
+ System .out .print (node .data +" " );
133
+ if (node .left !=null )
134
+ q .add (node .left );
135
+ if (node .right !=null )
136
+ q .add (node .right );
137
+ }
138
+ System .out .println ();
139
+ }
140
+ public void printAtGivenLevel (int level ){
141
+ printAtGivenLevelRec (root ,level );
142
+ }
143
+ private void printAtGivenLevelRec (Node root ,int level ){
144
+ if (root ==null )
145
+ return ;
146
+ if (level ==1 ) {
147
+ System .out .print (root .data +" " );
148
+ return ;
149
+ }
150
+ printAtGivenLevelRec (root .left ,level -1 );
151
+ printAtGivenLevelRec (root .right ,level -1 );
152
+ }
153
+ public void reverseLevelOrderTraversal () {
154
+ reverseLevelOrderTraversalRec (root );
155
+ }
156
+ private void reverseLevelOrderTraversalRec (Node root ){
157
+ if (root ==null )
158
+ return ;
159
+ Queue <Node > q = new LinkedList <>();
160
+ Stack <Integer > s =new Stack <>();
161
+ q .add (root );
162
+ while (!q .isEmpty ()){
163
+ Node node =q .remove ();
164
+ s .push (node .data );
165
+ if (node .left !=null )
166
+ q .add (node .left );
167
+ if (node .right !=null )
168
+ q .add (node .right );
169
+ }
170
+ while (!s .isEmpty ()){
171
+ System .out .print (s .pop ()+" " );
172
+ }
173
+ System .out .println ();
174
+ }
175
+ public void levelOrderTraversallinebyline () {
176
+ levelOrderTraversallinebylineRec (root );
177
+ }
178
+ private void levelOrderTraversallinebylineRec (Node root ) {
179
+ if (root == null )
180
+ return ;
181
+ Queue <Node > q = new LinkedList <>();
182
+ q .add (root );
183
+ while (true ) {
184
+ int count =q .size ();
185
+ if (count ==0 )
186
+ break ;
187
+ while (count >0 ) {
188
+ Node node = q .remove ();
189
+ System .out .print (node .data + " " );
190
+ if (node .left != null )
191
+ q .add (node .left );
192
+ if (node .right != null )
193
+ q .add (node .right );
194
+ count --;
195
+ }
196
+ System .out .println ();
197
+ }
198
+ }
63
199
}
64
200
0 commit comments