11package trees ;
22
3+ import java .util .LinkedList ;
4+ import java .util .Queue ;
5+
36public class GenericTree {
47
58 //depth=0;
@@ -36,7 +39,7 @@ public static int countLeafNodes(GenericTreeNode<Integer> root){
3639 }
3740
3841
39- public static GenericTreeNode < Integer > findSecondLargest (GenericTreeNode <Integer > root ){
42+ public static SecondMax findSecondLargest (GenericTreeNode <Integer > root ){
4043
4144 if (root ==null ){
4245
@@ -52,17 +55,195 @@ public static GenericTreeNode<Integer> findSecondLargest(GenericTreeNode<Integer
5255
5356 for (GenericTreeNode <Integer >child :root .children ){
5457
58+ SecondMax pair =findSecondLargest (child );
59+ // pair > root
60+ if (pair .first .data >out .first .data ) {
61+
62+ //pair second is null
63+ if (pair .second == null || pair .second .data < out .first .data ) {
64+
65+ out .first = pair .first ;
66+ out .second = out .first ;
67+ } else {
68+
69+ out .first = pair .first ;
70+ out .second = pair .second ;
71+ }
72+ }
73+ // when pair and out is equal..
74+ else if (pair .first .data .equals (out .first .data ) &&
75+ pair .second !=null ){
76+ if (out .second ==null || pair .second .data >out .second .data ){
77+ out .second =pair .second ;
78+ }
79+
80+ }
81+
82+ //for second max. check if not equal to curr max and check currsecond<pair first
83+ else if (pair .first .data !=out .first .data &&
84+ (out .second ==null || out .second .data <pair .first .data ))
85+
86+ out .second =pair .first ;
87+
88+ }
89+ return out ;
90+ }
91+
92+
93+ public static GenericTreeNode <Integer > findNextLargerNode (GenericTreeNode <Integer > root , int n ){
94+
95+ if (root ==null ){
96+ return null ;
97+ }
98+
99+ GenericTreeNode <Integer > next =null ;
100+ if (root .data >n ){
101+ next =root ;
102+ }
103+
104+ for (GenericTreeNode <Integer > child : root .children ){
105+
106+ GenericTreeNode <Integer > nextpair =findNextLargerNode (child ,n );
107+ if (nextpair !=null ){
108+ if (next ==null || next .data >nextpair .data ){
109+ next =nextpair ;
110+ }
111+ }
112+ }
113+
114+ return next ;
115+ }
116+
55117
118+ public static boolean checkIdentical (GenericTreeNode <Integer > root1 , GenericTreeNode <Integer > root2 ){
119+
120+ if (root1 ==null && root2 ==null ){
121+ return true ;
122+ }
123+ if (root1 ==null || root2 ==null ){
124+ return false ;
125+ }
126+ if (!root1 .data .equals (root2 .data )){
127+ return false ;
128+ }
129+ if (root1 .children .size ()!=root2 .children .size ()){
56130
131+ return false ;
57132 }
133+ for (int i =0 ;i <root1 .children .size ();i ++){
134+
135+ if (!checkIdentical (root1 .children .get (i ),root2 .children .get (i ))){
136+ return false ;
137+ }
58138
59- return null ;
139+ }
60140
141+ return true ;
61142 }
62143
144+ //node having sum of children and node is max..
145+ public static MaxNode maxSumNode (GenericTreeNode <Integer > root ){
63146
147+ if (root ==null ){
148+ MaxNode pp =new MaxNode ();
149+ pp .node =null ;
150+ pp .sum =Integer .MIN_VALUE ;
151+ return pp ;
152+ }
64153
65- }
154+ int sum =root .data ;
155+ for (GenericTreeNode <Integer > child : root .children ) {
156+ sum +=child .data ;
157+ }
158+ MaxNode res =new MaxNode ();
159+ res .node =root ;
160+ res .sum =sum ;
161+
162+ for (GenericTreeNode <Integer > child : root .children ){
163+
164+ MaxNode nextpair =maxSumNode (child );
165+ if (nextpair .sum >res .sum ){
166+ res =nextpair ;
167+ }
168+ }
169+
170+ return res ;
171+ }
172+
173+ public static int getHeight (GenericTreeNode <Integer > root ){
174+
175+ if (root ==null ){
176+ return 0 ;
177+ }
178+ int height =0 ;
179+ for (GenericTreeNode <Integer >child :root .children ){
180+
181+ int childHeight =getHeight (child );
182+ if (childHeight >height ){
183+ height =childHeight ;
184+ }
185+
186+ }
187+
188+ return height +1 ;
189+
190+ }
191+
192+
193+
194+ public static int numNodeGreaterX (GenericTreeNode <Integer > root ,int x ){
195+ int count =0 ;
196+ if (root ==null ){
197+ return 0 ;
198+ }
199+ if (root .data >x ){
200+ count ++;
201+ }
202+ for (GenericTreeNode <Integer >child :root .children ){
203+ count +=numNodeGreaterX (child ,x );
204+ }
205+
206+ return count ;
207+
208+ }
209+
210+
211+ public static void printLevelWise (GenericTreeNode <Integer > root ) {
212+
213+ if (root ==null ){
214+ return ;
215+ }
216+
217+ Queue <GenericTreeNode <Integer >> q =new LinkedList <>();
218+ GenericTreeNode <Integer > nullNode =new GenericTreeNode <>(Integer .MIN_VALUE );
219+
220+ q .add (root );
221+ q .add (nullNode );
222+ System .out .println (root .data );
223+
224+ while (!q .isEmpty ()){
225+
226+ GenericTreeNode <Integer > temp =null ;
227+ try {
228+ }
229+ catch (Exception e ){
230+
231+ }
232+ if (temp ==nullNode ){
233+ q .add (nullNode );
234+ System .out .println ();
235+ continue ;
236+ }
237+
238+ for (GenericTreeNode <Integer > child :temp .children ){
239+ System .out .println (child .data );
240+ q .add (child );
241+ }
242+ }
243+ }
244+
245+
246+ }
66247
67248class SecondMax {
68249
@@ -75,3 +256,14 @@ class SecondMax{
75256
76257
77258 }
259+ class MaxNode {
260+
261+ GenericTreeNode <Integer > node ;
262+ int sum ;
263+
264+ MaxNode (){
265+
266+ }
267+
268+
269+ }
0 commit comments