-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
219 lines (194 loc) · 5.39 KB
/
BinaryTree.java
File metadata and controls
219 lines (194 loc) · 5.39 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
package code;
/*
Codigo extraído del EJERCICIO 96 del cuaderno de ejercicios
*/
public class BinaryTree {
// Root node pointer. Will be null for an empty tree.
private Node root;
/*
--Node--
The binary tree is built using this nested node class.
Each node stores one data element, and has left and right
sub-tree pointer which may be null.
The node is a "dumb" nested class -- we just use it for storage; it does not have any methods.
*/
private static class Node {
Node left;
Node right;
int data;
Node(int newData) {
left = null;
right = null;
data = newData;
}
}
/**
* Creates an empty binary tree -- a null root pointer.
*/
public void BinaryTree() {
root = null;
}
/**
* Returns true if the given target is in the binary tree. Uses a recursive
* helper.
*
* @param data
* @return
*/
public boolean lookup(int data) {
return (lookup(root, data));
}
/**
* Recursive lookup -- given a node, recur down searching for the given
* data.
*/
private boolean lookup(Node node, int data) {
if (node == null) {
return (false);
}
if (data == node.data) {
return (true);
} else if (data < node.data) {
return (lookup(node.left, data));
} else {
return (lookup(node.right, data));
}
}
/**
* Inserts the given data into the binary tree. Uses a recursive helper.
*
* @param data
*/
public void insert(int data) {
root = insert(root, data);
}
/**
* Recursive insert -- given a node pointer, recur down and insert the given
* data into the tree. Returns the new node pointer (the standard way to
* communicate a changed pointer back to the caller).
*/
private Node insert(Node node, int data) {
if (node == null) {
node = new Node(data);
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
}
return (node); // in any case, return the new pointer to the caller
}
public int size() {
return (size(root));
}
private int size(Node node) {
if (node == null) {
return (0);
} else {
return (size(node.left) + 1 + size(node.right));
}
}
/**
* Returns the max root-to-leaf depth of the tree. Uses a recursive helper
* that recurs down to find the max depth.
*
* @return
*/
public int maxDepth() {
return (maxDepth(root));
}
private int maxDepth(Node node) {
if (node == null) {
return (0);
} else {
int lDepth = maxDepth(node.left);
int rDepth = maxDepth(node.right);
// use the larger + 1
return (Math.max(lDepth, rDepth) + 1);
}
}
/**
* Returns the min value in a non-empty binary search tree. Uses a helper
* method that iterates to the left to find the min value.
*
* @return
*/
public int minValue() {
return (minValue(root));
}
/**
* Finds the min value in a non-empty binary search tree.
*/
private int minValue(Node node) {
Node current = node;
while (current.left != null) {
current = current.left;
}
return (current.data);
}
/**
* Prints the node values in the "inorder" order. Uses a recursive helper to
* do the traversal.
*
* @return
*/
@Override
public String toString() {
return aStringBuilder(root).toString();
}
private StringBuilder aStringBuilder(Node node) {
StringBuilder sb = new StringBuilder("");
if (node == null) {
return sb;
}
// left, node itself, right
return aStringBuilder(node.left).
append(new StringBuilder(" " + node.data + " ")).
append(aStringBuilder(node.right));
}
@Override
public boolean equals(Object other) {
/*
Compares the receiver to another tree to
see if they are structurally identical.
*/
return (sameTree(root, ((BinaryTree) other).root));
}
/**
* Recursive helper -- recurs down two trees in parallel, checking to see if
* they are identical.
*/
boolean sameTree(Node a, Node b) {
// 1. both empty -> true
if (a == null && b == null) {
return (true);
} // 2. both non-empty -> compare them
else if (a != null && b != null) {
return (a.data == b.data
&& sameTree(a.left, b.left)
&& sameTree(a.right, b.right));
} // 3. one empty, one not -> false
else {
return (false);
}
}
/**
* Prints the node values in the "postorder" order. Uses a recursive helper
* to do the traversal.
*/
public void printPostorder() {
printPostorder(root);
System.out.println();
}
private void printPostorder(Node node) {
if (node == null) {
return;
}
// first recur on both subtrees
printPostorder(node.left);
printPostorder(node.right);
// then deal with the node
System.out.print(node.data + " ");
}
}