-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
178 lines (142 loc) · 4.75 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
178 lines (142 loc) · 4.75 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
/**
* This class contains various of methods that are needed to construct
* and change a binary search tree
* @param <T> - any data type can be used
*/
public class BinarySearchTree <T extends Comparable<T>> implements SortedCollection <T> {
/**
* The top value of the BST (root)
*/
protected BSTNode<T> root;
/**
* Performs the naive binary search tree insert algorithm to recursively
* insert the provided newNode (which has already been initialized with a
* data value) into the provided tree/subtree. When the provided subtree
* is null, this method does nothing.
*/
protected void insertHelper(BSTNode<T> newNode, BSTNode<T> subtree) {
//If the subtree is null then the method does nothing
if (subtree == null) {
return;
}
//If the new node is bigger or equal than subtree insert to the left
if (newNode.getData().compareTo(subtree.getData()) >= 0) {
if (subtree.getRight() == null) {
subtree.setRight(newNode);
newNode.setUp(subtree);
} else {
insertHelper(newNode, subtree.getRight());
}
}
//If the new node is smaller than the subtree insert to the right
if (newNode.getData().compareTo(subtree.getData()) < 0) {
if (subtree.getLeft() == null) {
subtree.setLeft(newNode);
newNode.setUp(subtree);
} else {
insertHelper(newNode, subtree.getLeft());
}
}
}
/**
* Inserts a new data value into the sorted collection.
*
* @param data the new value being insterted
* @throws NullPointerException if data argument is null, we do not allow
* null values to be stored within a SortedCollection
*/
@Override
public void insert(T data) throws NullPointerException {
if (data == null) {
throw new NullPointerException("There is no data to insert");
}
BSTNode<T> newNode = new BSTNode<T>(data);
// Insert data at root if root is null otherwise call the helper method to find
// a null value where data can be inserted
if (root == null) {
root = newNode;
} else {
insertHelper(newNode, root);
}
}
/**
* If the data value is in the BST then this method will be true, otherwise
* it will be false
*
* @param data - the data you want to see is in the BST
* @param currentNode - the node used to traverse through the BST
* @return - true if the method contains the data value you want to check
* and false if it doesn't
*/
public boolean containsHelper(Comparable<T> data, BSTNode<T> currentNode) {
//base case
if (currentNode == null) {
return false;
}
int comparison = data.compareTo(currentNode.getData());
if (comparison == 0) {
return true;
} else if (comparison > 0) {
return containsHelper(data, currentNode.getRight());
} else {
return containsHelper(data, currentNode.getLeft());
}
}
/**
* Check whether data is stored in the tree.
*
* @param data the value to check for in the collection
* @return true if the collection contains data one or more times,
* and false otherwise
*/
@Override
public boolean contains(Comparable<T> data) {
if (data == null) {
return false;
}
return containsHelper(data, root);
}
/**
* Checks how big the BST is
*
* @param currentNode - used to traverse through the BST
* @return the size of the BST
*/
private int sizeHelper(BSTNode<T> currentNode) {
if (currentNode == null) {
return 0;
}
//Add one for every node in the BST to the size
return 1 + sizeHelper(currentNode.getLeft()) + sizeHelper(currentNode.getRight());
}
/**
* Counts the number of values in the collection, with each duplicate value
* being counted separately within the value returned.
*
* @return the number of values in the collection, including duplicates
*/
@Override
public int size() {
return sizeHelper(root);
}
/**
* Checks if the collection is empty.
*
* @return true if the collection contains 0 values, false otherwise
*/
@Override
public boolean isEmpty() {
if (root == null) {
return true;
} else {
return false;
}
}
/**
* Removes all values and duplicates from the collection.
*/
@Override
public void clear() {
root = null;
}
}