-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.java
More file actions
241 lines (218 loc) · 4.28 KB
/
TreeNode.java
File metadata and controls
241 lines (218 loc) · 4.28 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Definition for binary tree
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* TreeNode class from leetcode, with extra constructors and print functions
*
* @author Rutter
*
*/
public class TreeNode
{
int val;
TreeNode left;
TreeNode right;
static String _seprator = ",";
String chara = "null";
public TreeNode()
{
// TODO Auto-generated constructor stub
}
TreeNode( int x )
{
val = x;
}
/**
* Constructs Tree with input String in level order
* A "null" means no node added for root, and will be ignored in next level
*
* @param c
*/
TreeNode( String[] c )
{
// List<TreeNode> l = new ArrayList<TreeNode>();
Queue<TreeNode> queue = new LinkedList<>();
TreeNode tmp;
this.val = Integer.parseInt( c[0] );
queue.add( this );
int k = 1;
while ( k < c.length )
{
tmp = queue.poll();
if ( c[k] != chara )
{
tmp.left = new TreeNode( Integer.parseInt( c[k] ) );
queue.add( tmp.left );
}
k++;
if ( k < c.length && c[k] != chara )
{
tmp.right = new TreeNode( Integer.parseInt( c[k] ) );
queue.add( tmp.right );
}
k++;
}
}
/**
* Constructs Tree with input String in level order
* Constructs full tree
*
* @param c
*/
TreeNode( char[] c )
{
ArrayList<TreeNode> l = new ArrayList<TreeNode>();
TreeNode tmp;
this.val = c[0] - '0';
l.add( this );
for ( int i = 0; i < c.length / 2; i++ )
{
tmp = l.get( 0 );
l.remove( 0 );
if ( c[2 * i + 1] != '#' )
{
tmp.left = new TreeNode( c[2 * i + 1] - '0' );
l.add( tmp.left );
}
if ( 2 * i + 2 < c.length )
{
if ( c[2 * i + 2] != '#' )
{
tmp.right = new TreeNode( c[2 * i + 2] - '0' );
l.add( tmp.right );
}
}
}
}
TreeNode( int[] c )
{
ArrayList<TreeNode> l = new ArrayList<TreeNode>();
TreeNode tmp;
this.val = c[0];
l.add( this );
for ( int i = 0; i < c.length / 2; i++ )
{
tmp = l.get( 0 );
l.remove( 0 );
if ( c[2 * i + 1] != -999 )
{
tmp.left = new TreeNode( c[2 * i + 1] );
l.add( tmp.left );
}
if ( 2 * i + 2 < c.length )
{
if ( c[2 * i + 2] != -999 )
{
tmp.right = new TreeNode( c[2 * i + 2] );
l.add( tmp.right );
}
}
}
}
/**
* prints tree in level order
*/
public void print()
{
TreeNode tmp;
ArrayList<TreeNode> q = new ArrayList<TreeNode>();
TreeNode that = new TreeNode( 0 );
q.add( this );
q.add( that );
System.out.println( "\nLevel Order Tree:\n Root->" + this.val );
while ( !q.isEmpty() )
{
tmp = q.get( 0 );
if ( tmp == that )
{
System.out.println();
q.remove( 0 );
if ( !q.isEmpty() )
{
q.add( that );
continue;
}
else
break;
}
q.remove( 0 );
if ( tmp == null )
continue;
if ( tmp.left == null && tmp.right == null )
{
System.out.print( " # " );
System.out.print( " # " );
continue;
}
if ( tmp.left != null )
{
q.add( tmp.left );
System.out.print( tmp.val + "->" + tmp.left.val + " " );
}
else
System.out.print( " # " );
if ( tmp.right != null )
{
q.add( tmp.right );
System.out.print( tmp.val + "->" + tmp.right.val + " " );
}
else
System.out.print( " # " );
}
}
/**
* print tree in pre-order traversal
*
* @param root
*/
public static void printPreOrder( TreeNode root )
{
System.out.println( "\npre order: " );
preOrder( root );
}
/**
* print tree in in-order traversal
*
* @param root
*/
public static void printInOrder( TreeNode root )
{
System.out.println( "\nin order: " );
inOrder( root );
}
/**
* print tree in post-order traversal
*
* @param root
*/
public static void printPostOrder( TreeNode root )
{
System.out.println( "\npost order: " );
postOrder( root );
}
private static void preOrder( TreeNode root )
{
if ( root == null )
return;
System.out.print( root.val + _seprator );
preOrder( root.left );
preOrder( root.right );
}
private static void inOrder( TreeNode root )
{
if ( root == null )
return;
inOrder( root.left );
System.out.print( root.val + _seprator );
inOrder( root.right );
}
private static void postOrder( TreeNode root )
{
if ( root == null )
return;
postOrder( root.left );
postOrder( root.right );
System.out.print( root.val + _seprator );
}
}