-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
194 lines (172 loc) · 3.33 KB
/
Copy pathBinaryTree.java
File metadata and controls
194 lines (172 loc) · 3.33 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
/**
This class describes a simple BinaryTree class. It was provided by Srini
(Dr. Srini Sampalli).
*/
import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree<T>
{
private T data;
private BinaryTree<T> parent;
private BinaryTree<T> left;
private BinaryTree<T> right;
public BinaryTree()
{
parent = left = right = null;
data = null;
}
public void makeRoot(T data)
{
if (!isEmpty())
{
System.out.println("Can't make root. Already exists");
}
else
this.data = data;
}
public void setData(T data)
{
this.data = data;
}
public void setLeft(BinaryTree<T> tree)
{
left = tree;
}
public void setRight(BinaryTree<T> tree)
{
right = tree;
}
public void setParent(BinaryTree<T> tree)
{
parent = tree;
}
public T getData()
{
return data;
}
public BinaryTree<T> getParent()
{
return parent;
}
public BinaryTree<T> getLeft()
{
return left;
}
public BinaryTree<T> getRight()
{
return right;
}
public void attachLeft(BinaryTree<T> tree)
{
if (tree==null) return;
else if (left!=null || tree.getParent()!=null)
{
System.out.println("Can't attach");
return;
}
else
{
tree.setParent(this);
this.setLeft(tree);
}
}
public void attachRight(BinaryTree<T> tree)
{
if (tree==null) return;
else if (right!=null || tree.getParent()!=null)
{
System.out.println("Can't attach");
return;
}
else
{
tree.setParent(this);
this.setRight(tree);
}
}
public BinaryTree<T> detachLeft()
{
if (this.isEmpty()) return null;
BinaryTree<T> retLeft = left;
left = null;
if (retLeft!=null) retLeft.setParent(null);
return retLeft;
}
public BinaryTree<T> detachRight()
{
if (this.isEmpty()) return null;
BinaryTree<T> retRight = right;
right =null;
if (retRight!=null) retRight.setParent(null);
return retRight;
}
public boolean isEmpty()
{
if (data == null)
return true;
else
return false;
}
public void clear()
{
left = right = parent =null;
data = null;
}
public BinaryTree<T> root()
{
if (parent == null)
return this;
else
{
BinaryTree<T> next = parent;
while (next.getParent()!=null)
next = next.getParent();
return next;
}
}
public static <T> void preorder(BinaryTree<T> t)
{
if (t!=null)
{
System.out.println(t.getData());
preorder(t.getLeft());
preorder(t.getRight());
}
}
public static <T> void inorder(BinaryTree<T> t)
{
if (t!=null)
{
inorder(t.getLeft());
System.out.print(t.getData() + "\t");
inorder(t.getRight());
}
}
public static <T> void postorder(BinaryTree<T> t)
{
if (t!=null)
{
postorder(t.getLeft());
postorder(t.getRight());
System.out.println(t.getData() + "\t");
}
}
// this method uses a modified BFS to print the data associated with all
// nodes/trees in level order
public static <T> void levelorder(BinaryTree<T> t){
if(t==null)
return;
// initialize Queue to store references to BinaryTrees and
// preload agenda
Queue<BinaryTree<T>> q = new LinkedList<BinaryTree<T>>();
q.add(t);
while(!q.isEmpty()){
BinaryTree<T> tmp = q.remove();
System.out.println(tmp.getData());
if(tmp.getLeft()!=null)
q.add(tmp.getLeft());
if(tmp.getRight()!=null)
q.add(tmp.getRight());
}
}
}