-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTree.java
More file actions
217 lines (177 loc) · 5.15 KB
/
BinaryTree.java
File metadata and controls
217 lines (177 loc) · 5.15 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
import java.util.*;
/*
*
*/
public class BinaryTree<E> {
private int index; /* This is the index of the next node to insert
in the tree. */
private TreeNode<E> root; /* The absolute root of the tree. */
/*
* Default constructor. */
public BinaryTree() {
index = 0;
root = null;
}
/* ------------------------------------------------------------------
* Recursively calculates the size of the tree; i.e., the number of
* elements in the binary tree. */
public int size() { return size_p(root); }
private int size_p(TreeNode<E> t) {
if(t==null)
return 0;
else return(1+ size_p(t.get_left()) + size_p(t.get_right()));
}
/* -------------------------------------------------------------------
* Recursively does an inorder traversal of the tree
*/
public void inorder() { inorder_p(root); }
private void inorder_p(TreeNode<E> t) {
if (t != null) {
inorder_p(t.get_left());
System.out.print(t.get_info()+",");
inorder_p(t.get_right());
}
}
/*
*inorder traversal using iteration */
public void inorder_itr() {
Stack<TreeNode<E>> store = new Stack<TreeNode<E>>();
TreeNode<E> current;
current=root;
boolean done=true;
while(done){
if(current!=null){
store.push(current);
current=current.left;
} else {
if(!store.isEmpty()){
current=store.pop();
System.out.print(current.info+",");
current=current.right;
} else{
done=false;
}
}
}
}
/*
*size using iteration */
public int size_itr() {
Stack<TreeNode<E>> store = new Stack<TreeNode<E>>();
TreeNode<E> current;
current=root;
boolean done=true;
int size=0;
while(done){
if(current!=null){
store.push(current);
current=current.left;
} else {
if(!store.isEmpty()){
current=store.pop();
size++;
current=current.right;
} else{
done=false;
}
}
}
return size;
}
/* --------------------------------------------------------------------
* Recursively does an preorder traversal of the tree
*/
public void preorder() { preorder_p(root); }
private void preorder_p(TreeNode<E> t) {
if (t != null) {
System.out.print(t.get_info()+",");
preorder_p(t.get_left());
preorder_p(t.get_right());
}
}
/* -------------------------------------------------------------------
* Recursively does an postorder traversal of the tree
*/
public void postorder() { postorder_p(root); }
private void postorder_p(TreeNode<E> t) {
if (t != null) {
postorder_p(t.get_left());
postorder_p(t.get_right());
System.out.print(t.get_info()+",");
}
}
/*
* add method without recursion - till index 7
*/
public void add(E info)
{
TreeNode<E> node = new TreeNode<E>(info);
if (index == 0) {
root = node;
}
else if (index == 1) {
root.left = node;
}
else if (index == 2) {
root.right = node;
}
else if (index == 3) {
root.left.left = node;
}
else if (index == 4) {
root.left.right=node;
}
else if (index == 5) {
root.right.left=node;
}
else if (index == 6) {
root.right.right=node;
}
index++;
}
/* -------------------------------------------------------------------
* Each node in the tree is an object of this type. */
protected static class TreeNode<E> {
private TreeNode<E> left,
right;
private E info;
public TreeNode(E info) { left = right = null; this.info = info; }
public TreeNode<E> get_left() { return left; }
public TreeNode<E> get_right() { return right;}
public E get_info() { return info;}
}
public static void main(String[] args) {
BinaryTree<Integer> bt = new BinaryTree<Integer>();
bt.add(0);
bt.add(1);
bt.add(2);
bt.add(3);
bt.add(4);
bt.add(5);
bt.add(6);
/*
* The above tree will look like:
* 0
* / \
* / \
* 1 2
* / \ / \
* / \ / \
* 3 4 5 6
*/
System.out.println("The tree has " + bt.size() + " nodes.");
System.out.println("Inorder using rec");
bt.inorder();
System.out.println("");
System.out.println("Postorder using rec");
bt.postorder();
System.out.println("");
System.out.println("Preorder using rec");
bt.preorder();
System.out.println("");
System.out.println("Inorder using itr");
bt.inorder_itr();
System.out.println("");
System.out.println("The tree size using itr has " + bt.size_itr() + " nodes.");
}
}