1+ //BinaryTree.java : Program for Binary Tree.
2+
3+ import java .util .Stack ;
4+ import java .util .Queue ;
5+ import java .util .LinkedList ;
6+
7+ class Node
8+ {
9+ public char info ;
10+ public Node lchild ;
11+ public Node rchild ;
12+
13+ public Node (char data )
14+ {
15+ info = data ;
16+ lchild = null ;
17+ rchild = null ;
18+ }
19+ }//End of class Node
20+
21+ class BinaryTree
22+ {
23+ private Node root ;
24+
25+ public BinaryTree ()
26+ {
27+ root = null ;
28+ }//End of BinaryTree()
29+
30+ public boolean isEmpty ()
31+ {
32+ return root ==null ;
33+ }//End of isEmpty()
34+
35+ public void createTree ()
36+ {
37+ root = new Node ('P' );
38+ root .lchild = new Node ('Q' );
39+ root .rchild = new Node ('R' );
40+
41+ root .lchild .lchild = new Node ('A' );
42+ root .lchild .rchild = new Node ('B' );
43+
44+ root .rchild .lchild = new Node ('X' );
45+ }//End of createTree()
46+
47+ private void preorder (Node p )
48+ {
49+ if (p == null ) //Base case
50+ return ;
51+
52+ System .out .print (p .info + " " );
53+ preorder (p .lchild );
54+ preorder (p .rchild );
55+ }//End of preorder()
56+
57+ public void preorder ()
58+ {
59+ preorder (root );
60+ System .out .println ();
61+ }//End of preorder()
62+
63+ private void inorder (Node p )
64+ {
65+ if (p == null ) //Base case
66+ return ;
67+
68+ inorder (p .lchild );
69+ System .out .print (p .info + " " );
70+ inorder (p .rchild );
71+ }//End of inorder()
72+
73+ public void inorder ()
74+ {
75+ inorder (root );
76+ System .out .println ();
77+ }//End of inorder()
78+
79+ private void postorder (Node p )
80+ {
81+ if (p == null ) //Base case
82+ return ;
83+
84+ postorder (p .lchild );
85+ postorder (p .rchild );
86+ System .out .print (p .info + " " );
87+ }//End of postorder()
88+
89+ public void postorder ()
90+ {
91+ postorder (root );
92+ System .out .println ();
93+ }//End of postorder()
94+
95+ public void levelOrder ()
96+ {
97+ Queue <Node > qu = new LinkedList <Node >();
98+ Node p ;
99+
100+ qu .add (root );
101+ while (!qu .isEmpty ())
102+ {
103+ p = qu .remove ();
104+ System .out .print (p .info + " " );
105+
106+ if (p .lchild != null )
107+ qu .add (p .lchild );
108+
109+ if (p .rchild != null )
110+ qu .add (p .rchild );
111+ }
112+ System .out .println ();
113+ }//End of levelOrder()
114+
115+ private int height (Node p )
116+ {
117+ int hLeft , hRight ;
118+
119+ if (p == null ) //Base case
120+ return 0 ;
121+
122+ hLeft = height (p .lchild );
123+ hRight = height (p .rchild );
124+
125+ if (hLeft > hRight )
126+ return 1 +hLeft ;
127+ else
128+ return 1 +hRight ;
129+ }//End of height()
130+
131+ public int height ()
132+ {
133+ return height (root );
134+ }//End of height()
135+
136+ //Non recursive Preorder traversal
137+ public void nrecPreorder ()
138+ {
139+ Stack <Node > st = new Stack <Node >();
140+ Node p = root ;
141+
142+ if (p != null )
143+ {
144+ st .push (p );
145+ while (!st .empty ())
146+ {
147+ p = st .pop ();
148+ System .out .print (p .info + " " );
149+ if (p .rchild != null )
150+ st .push (p .rchild );
151+ if (p .lchild != null )
152+ st .push (p .lchild );
153+ }
154+ System .out .println ();
155+ }
156+ else
157+ System .out .println ("Tree is empty" );
158+ }//End of nrecPreorder()
159+
160+ //Non recursive Inorder traversal
161+ public void nrecInorder ()
162+ {
163+ Stack <Node > st = new Stack <Node >();
164+ Node p = root ;
165+
166+ if (p != null )
167+ {
168+ while (true )
169+ {
170+ while (p .lchild != null )
171+ {
172+ st .push (p );
173+ p = p .lchild ;
174+ }
175+
176+ while (p .rchild == null )
177+ {
178+ System .out .print (p .info + " " );
179+ if (st .empty ())
180+ {
181+ System .out .println ();
182+ return ;
183+ }
184+ p = st .pop ();
185+ }
186+ System .out .print (p .info + " " );
187+ p = p .rchild ;
188+ }
189+ }
190+ else
191+ System .out .println ("Tree is empty" );
192+ }//End of nrecInorder()
193+
194+ //Non recursive Postorder traversal
195+ public void nrecPostorder ()
196+ {
197+ Stack <Node > st = new Stack <Node >();
198+ Node p = root ;
199+ Node visited = root ;
200+
201+ if (p != null )
202+ {
203+ while (true )
204+ {
205+ while (p .lchild != null )
206+ {
207+ st .push (p );
208+ p = p .lchild ;
209+ }
210+
211+ while (p .rchild ==null || p .rchild ==visited )
212+ {
213+ System .out .print (p .info + " " );
214+ visited = p ;
215+ if (st .empty ())
216+ {
217+ System .out .println ();
218+ return ;
219+ }
220+ p = st .pop ();
221+ }
222+ st .push (p );
223+ p = p .rchild ;
224+ }
225+ }
226+ else
227+ System .out .print ("Tree is empty" );
228+ }//End of nrecPostorder()
229+
230+ private void display (Node p , int level )
231+ {
232+ if (p == null )
233+ return ;
234+
235+ display (p .rchild , level +1 );
236+ System .out .println ();
237+
238+ for (int i =0 ; i <level ; i ++)
239+ System .out .print (" " );
240+ System .out .print (p .info );
241+
242+ display (p .lchild , level +1 );
243+ }//End of display()
244+
245+ public void display ()
246+ {
247+ display (root , 0 );
248+ System .out .println ();
249+ }//End of display()
250+
251+ //Creation of binary tree from inorder and preorder traversal
252+ private Node construct (String in , String pre , int num )
253+ {
254+ Node temp ;
255+
256+ if (num == 0 )
257+ return null ;
258+
259+ temp = new Node (pre .charAt (0 ));
260+
261+ if (num == 1 ) //if only one node in tree
262+ return temp ;
263+
264+ int i ;
265+ for (i =0 ; in .charAt (i )!=pre .charAt (0 ); i ++)
266+ ;
267+
268+ //Number of nodes in its left subtree is i
269+ //For left subtree
270+ temp .lchild = construct (in , pre .substring (1 ), i );
271+
272+ //For right subtree
273+ int j ;
274+ for (j =1 ; j <=i +1 ; j ++)
275+ ;
276+ temp .rchild = construct (in .substring (i +1 ), pre .substring (j -1 ), num -i -1 );
277+
278+ return temp ;
279+ }//End of construct()
280+
281+ public void construct (String in , String pre )
282+ {
283+ root = construct (in , pre , in .length ());
284+ }//End of construct()
285+
286+ //Creation of binary tree from inorder and postorder traversal
287+ private Node construct1 (String in , String post , int num )
288+ {
289+ Node temp ;
290+ int i , j , k ;
291+
292+ if (num == 0 )
293+ return null ;
294+
295+ for (i =1 ; i <num ; i ++)
296+ ;
297+
298+ temp = new Node (post .charAt (i -1 ));
299+
300+ if (num == 1 ) //if only one node in tree
301+ return temp ;
302+
303+ for (k =0 ; in .charAt (k )!=post .charAt (i -1 ); k ++)
304+ ;
305+
306+ //Now k denotes the number of nodes in left subtree
307+ //For left subtree
308+ temp .lchild = construct1 (in , post , k );
309+
310+ //For right subtree
311+ for (j =1 ; j <=k ; j ++)
312+ ;
313+
314+ temp .rchild = construct1 (in .substring (k +1 ), post .substring (j -1 ), num -k -1 );
315+
316+ return temp ;
317+ }//End of construct1()
318+
319+ public void construct1 (String in , String post )
320+ {
321+ root = construct1 (in , post , in .length ());
322+ }//End of construct1()
323+
324+ }//End of class BinaryTree
325+
326+ class BinaryTreeDemo
327+ {
328+ public static void main (String [] args )
329+ {
330+ BinaryTree bnTree = new BinaryTree ();
331+
332+ bnTree .createTree ();
333+ bnTree .display ();
334+ System .out .println ();
335+
336+ System .out .println ("Preorder traversal :" );
337+ bnTree .preorder ();
338+
339+ System .out .println ("Inorder traversal :" );
340+ bnTree .inorder ();
341+
342+ System .out .println ("Postorder traversal :" );
343+ bnTree .postorder ();
344+
345+ System .out .println ("Level order traversal :" );
346+ bnTree .levelOrder ();
347+
348+ System .out .println ("Height = " + bnTree .height ());
349+
350+ System .out .println ("Non Recursive Preorder traversal :" );
351+ bnTree .nrecPreorder ();
352+
353+ System .out .println ("Non Recursive Inorder traversal :" );
354+ bnTree .nrecInorder ();
355+
356+ System .out .println ("Non Recursive Postorder traversal :" );
357+ bnTree .nrecPostorder ();
358+
359+ String inorder = "GDHBEIACJFK" ;
360+
361+ String preorder = "ABDGHEICFJK" ;
362+ BinaryTree bnTree1 = new BinaryTree ();
363+ System .out .println ("Creation of binary tree from Inorder = " + inorder + ", Preorder = " + preorder );
364+ bnTree1 .construct (inorder , preorder );
365+ bnTree1 .display ();
366+ System .out .println ();
367+
368+ String postorder = "GHDIEBJKFCA" ;
369+ BinaryTree bnTree2 = new BinaryTree ();
370+ System .out .println ("Creation of binary tree from Inorder = " + inorder + ", Postorder = " + postorder );
371+ bnTree2 .construct1 (inorder , postorder );
372+ bnTree2 .display ();
373+ System .out .println ();
374+
375+ }//End of main()
376+ }//End of class BinaryTreeDemo
0 commit comments