-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeBag.java
More file actions
284 lines (258 loc) · 9.76 KB
/
TreeBag.java
File metadata and controls
284 lines (258 loc) · 9.76 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Liz Crittenden
// Project 4 CSC 103
// File: TreeBag.java
// The implementation of most methods in this file is left as a student
// exercise from Section 9.5 of "Data Structures and Other Objects Using Java"
/******************************************************************************
* This class is a homework assignment;
* An <CODE>TreeBag</CODE> is a collection of int numbers.
*
* <dl><dt><b>Limitations:</b> <dd>
* Beyond <CODE>Integer.MAX_VALUE</CODE> elements, <CODE>countOccurrences</CODE>,
* and <CODE>size</CODE> are wrong.
*
* <dt><b>Note:</b><dd>
* This file contains only blank implementations ("stubs")
* because this is a Programming Project for my students.
*
* @version
* Jan 24, 2016
******************************************************************************/
public class TreeBag<E extends Comparable> implements Cloneable
{
// The Term E extends Comparable is letting the compiler know that any type
// used to instantiate E must implement Comparable. i. e. that means that whatever
// type E is must have a compareTo method so that elements can be compared against one another
// This is required becuase we are doing comparisons in our methods
// Invariant of the TreeBag class:
// 1. The elements in the bag are stored in a binary search tree.
// 2. The instance variable root is a reference to the root of the
// binary search tree (or null for an empty tree).
private BTNode<E> root;
/**
* Insert a new element into this bag.
* @param <CODE>element</CODE>
* the new element that is being inserted
* <dt><b>Postcondition:</b><dd>
* A new copy of the element has been added to this bag.
* @exception OutOfMemoryError
* Indicates insufficient memory a new BTNode.
**/
public void add(E element)
{
BTNode <E> cursor;
BTNode <E> newNode;
boolean done = false;
if(root==null){
root = new BTNode(element, null, null); //If root does not exist, create root with element as data
} else {
cursor = root;//set cursor to root
while(!done){
if(element.compareTo(cursor.getData()) <= 0){ //Move cursor left within the tree
if(cursor.getLeft()==null){ //If cursor has no left child
cursor.setLeft(new BTNode<E>(element, null, null)); // create node with element as data
done = true; //End while loop
} else {
cursor = cursor.getLeft(); //Keep advancing left
}
} else { //Move cursor right within the tree
if(cursor.getRight()==null){
cursor.setRight(new BTNode<E>(element, null, null));
done = true;
} else {
cursor = cursor.getRight();
}
}
}
}
}
/**
* Retrieve location of a specified element from this bag.
* @param <CODE>target</CODE>
* the element to locate in the bag
* @return
* the return value is a reference to the found element in the tree
* <dt><b>Postcondition:</b><dd>
* If <CODE>target</CODE> was found in the bag, then method returns
* a reference to a comparable element. If the target was not found then
* the method returns null.
* The bag remains unchanged.
**/
public E retrieve(E target)
{
BTNode <E> cursor;
boolean flag = true;
cursor = root;
while(flag == true && cursor != null){
if(target.compareTo(cursor.getData())==0){ //Use compareTo method to find node containing target element
flag = false;
return cursor.getData();
}else if(target.compareTo(cursor.getData()) < 0){ //If not found, advance left
cursor = cursor.getLeft();
}else if(target.compareTo(cursor.getData()) > 0){ //If not found, advance right
cursor = cursor.getRight();
}
}
return null;
}
/**
* Remove one copy of a specified element from this bag.
* @param <CODE>target</CODE>
* the element to remove from the bag
* <dt><b>Postcondition:</b><dd>
* If <CODE>target</CODE> was found in the bag, then one copy of
* <CODE>target</CODE> has been removed and the method returns true.
* Otherwise the bag remains unchanged and the method returns false.
**/
public boolean remove(E target)
{
BTNode <E> cursor;
BTNode <E> parentOfCursor;
cursor = root;
parentOfCursor = null;
if(cursor==null || target == null){ //If target is not found
System.out.println("The player you entered isn't within this database.");
return false;
}
while(cursor != null){
if(target.compareTo(cursor.getData()) < 0 ){ //Advance cursor left
parentOfCursor = cursor; //Set parent to previous node
cursor = cursor.getLeft();
} else if(target.compareTo(cursor.getData()) > 0 ){ //Advance cursor right
parentOfCursor = cursor;
cursor = cursor.getRight();
} else if(target.compareTo(cursor.getData())==0){//Indicates target has been found
if(cursor.isLeaf()){//If target is a leaf
parentOfCursor.setLeft(null);//Removes links to target
parentOfCursor.setRight(null);
} else if(cursor.getLeft()==null){ //If target has no left child
if(cursor== parentOfCursor.getLeft())
parentOfCursor.setLeft(cursor.getRight());//Sets link of remaining child to parent
} else if (cursor.getRight()==null){ //If target has no right child
if(cursor== parentOfCursor.getRight())
parentOfCursor.setRight(cursor.getLeft());
} else if(cursor.getLeft()!=null && cursor.getRight() != null){//If target has two children
cursor.setData(cursor.getLeft().getRightmostData());//Set links of both children to parent
cursor.setLeft(cursor.getLeft().removeRightmost());
}
return true;
}
}
return false;
}
/**
* Displays the entire tree of Node elements in a order specified
* by the elements compareTo method
*
* @param
* none
* <dt><b>Postcondition:</b><dd>
* Outputs all elements in the tree to Screen.
* Does not change the structure
**/
public void display()
{
if(root != null)
root.inorderPrint();
}
/**
* Displays the entire tree of Node elements using the
* built in print method of BTNode
* which displays the entire tree in tree format
*
* @param
* none
* <dt><b>Postcondition:</b><dd>
* Outputs all elements in the tree to Screen.
* Does not change the structure
**/
public void displayAsTree() {
if (root == null)
throw new IllegalArgumentException("The tree is empty");
root.print(0);
}
/**
* Generate a copy of this bag.
* @param - none
* @return
* The return value is a copy of this bag. Subsequent changes to the
* copy will not affect the original, nor vice versa. Note that the return
* value must be type cast to an <CODE>TreeBag</CODE> before it can be used.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public TreeBag<E> clone( )
{ // Clone an IntTreeBag object.
// Student will replace this return statement with their own code:
return null;
}
/**
* Accessor method to count the number of occurrences of a particular element
* in this bag.
* @param <CODE>target</CODE>
* the element that needs to be counted
* @return
* the number of times that <CODE>target</CODE> occurs in this bag
**/
public int countOccurrences(E target)
{
// Student will replace this return statement with their own code:
return 0;
}
/**
* Determine the number of elements in this bag.
* @param - none
* @return
* the number of elements in this bag
**/
public int size( )
{
return BTNode.treeSize(root);
}
/**
* Add the contents of another bag to this bag.
* @param <CODE>addend</CODE>
* a bag whose contents will be added to this bag
* <dt><b>Precondition:</b><dd>
* The parameter, <CODE>addend</CODE>, is not null.
* <dt><b>Postcondition:</b><dd>
* The elements from <CODE>addend</CODE> have been added to this bag.
* @exception IllegalArgumentException
* Indicates that <CODE>addend</CODE> is null.
* @exception OutOfMemoryError
* Indicates insufficient memory to increase the size of the bag.
**/
public void addAll(TreeBag<E> addend)
{
// Implemented by student.
}
/**
* Create a new bag that contains all the elements from two other bags.
* @param <CODE>b1</CODE>
* the first of two bags
* @param <CODE>b2</CODE>
* the second of two bags
* <dt><b>Precondition:</b><dd>
* Neither b1 nor b2 is null.
* @return
* the union of b1 and b2
* @exception IllegalArgumentException
* Indicates that one of the arguments is null.
* @exception OutOfMemoryError
* Indicates insufficient memory for the new bag.
**/
public static TreeBag union(TreeBag b1, TreeBag b2)
{
// Student will replace this return statement with their own code:
return null;
}
/**
* Accessor for root of the treeBag
* @param none
* @return
* the root node of the treeBag
**/
public BTNode setRoot(){
return root;
}
}