-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
226 lines (209 loc) · 6.03 KB
/
BinarySearchTree.java
File metadata and controls
226 lines (209 loc) · 6.03 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
/**
* This class implements a binary search tree whose
* nodes hold objects that implement the Comparable
* interface.
*
* @author Michael Chadwick
* @version 5/13/2020 Final Student Search
*/
import java.util.ArrayList;
public class BinarySearchTree
{
/** The Node at the root of a tree */
private Node root;
/**
* Constructs an empty tree.
*/
public BinarySearchTree()
{
root = null;
}
/**
* Inserts a new node into the tree.
*
* @param obj the object to insert
*/
public void add(Comparable obj)
{
Node newNode = new Node();
newNode.data = obj;
newNode.left = null;
newNode.right = null;
if (root == null)
root = newNode;
else
root.addNode(newNode);
}
/**
* Tries to find a specific object in the tree. It uses only the data
* from the input object to search the tree using the compareTo() method
* specified for the class
*
* @param obj - object with the data (key of) the object to be found
* @return reference to the object, if it is contained in the tree,
* null if not
*/
public Comparable find(Comparable obj)
{
Node current = root;
while (current != null)
{
int d = current.data.compareTo(obj);
if (d == 0)
{
return current.data;
}
else if (d > 0)
current = current.left;
else
current = current.right;
}
return null;
}
/**
* Tries to find and remove an object from the tree. It uses only the data
* from the input object to search the tree using the compareTo() method
* specified for the class. Rearranges the tree members after removal.
* Does nothing if the object is not contained in the tree.
*
* @param obj the object to remove
*/
public void remove(Comparable obj)
{
// Find node to be removed
Node toBeRemoved = root;
Node parent = null;
boolean found = false;
while (!found && toBeRemoved != null)
{
int d = toBeRemoved.data.compareTo(obj);
if (d == 0)
found = true;
else
{
parent = toBeRemoved;
if (d > 0)
toBeRemoved = toBeRemoved.left;
else
toBeRemoved = toBeRemoved.right;
}
}
if (!found)
return;
// toBeRemoved contains obj
// If one of the children is empty, use the other
if (toBeRemoved.left == null || toBeRemoved.right == null)
{
Node newChild;
if (toBeRemoved.left == null)
newChild = toBeRemoved.right;
else
newChild = toBeRemoved.left;
if (parent == null) // Found in root
root = newChild;
else if (parent.left == toBeRemoved)
parent.left = newChild;
else
parent.right = newChild;
return;
}
// Neither subtree is empty
// Find smallest element of the right subtree
Node smallestParent = toBeRemoved;
Node smallest = toBeRemoved.right;
while (smallest.left != null)
{
smallestParent = smallest;
smallest = smallest.left;
}
// smallest contains smallest child in right subtree
// Move contents, unlink child
toBeRemoved.data = smallest.data;
if (smallestParent == toBeRemoved)
smallestParent.right = smallest.right;
else
smallestParent.left = smallest.right;
}
/**
* Prints the contents of the tree in sorted order.
*/
public void print()
{
print(root);
System.out.println();
}
/**
* Prints a node and all of its descendants in sorted order.
*
* @param parent the root of the subtree to print
*/
private void print(Node parent)
{
if (parent == null)
return;
print(parent.left);
System.out.print(parent.data.toString());
print(parent.right);
}
/**
* Returns a new ArrayList of objects
*
* @return an Arraylist Object with / without node components
*/
public ArrayList<Object> getList()
{
ArrayList<Object> list = new ArrayList<Object>();
getList(list, root);
return list;
}
/**
* Returns an empty or complete tree based on the root node
*
* @param list - the list to which elements are to be added
* @param parent - the root of the tree to be created
*/
private void getList(ArrayList<Object> list, Node parent)
{
if (parent == null)
return;
getList(list,parent.left);
list.add(parent.data);
getList(list, parent.right);
}
/**
* A node of a tree stores a data item and references
* to the left and right child nodes.
*/
class Node
{
/** the data object in the node */
public Comparable data;
/** Reference to the node to this node's left */
public Node left;
/** Reference to the node to this node's right */
public Node right;
/**
* Inserts a new node as a descendant of this node.
*
* @param newNode the node to insert
*/
public void addNode(Node newNode)
{
int comp = newNode.data.compareTo(data);
if (comp < 0)
{
if (left == null)
left = newNode;
else
left.addNode(newNode);
}
else if (comp > 0)
{
if (right == null)
right = newNode;
else
right.addNode(newNode);
}
}
}
}