-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
211 lines (184 loc) · 4.89 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
211 lines (184 loc) · 4.89 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
package proj5;
/**
* The Binary Search Tree ADT models a tree where all the values to the left of a given node
* are less than that node and all the values to the right are equal to or greater than
* the given node
*
* @author Chris Fernandes, Kristina Striegnitz, edited by Nick DeBaise
* @version Fall 2022
*/
public class BinarySearchTree<T extends Comparable<T>> {
private BSTNode<T> root;
/**
* Default constructor
*/
public BinarySearchTree() {
root = null;
}
/**
* inserts a new value into this BST
* @param newValue value to insert
*/
public void insert(T newValue) {
root = insert(root,newValue);
}
/**
* inserts value into tree rooted at subroot
*
* @param subroot subroot of tree to insert into
* @param value the value to insert
* @return root of the subtree I've just finished inserting into
*/
private BSTNode<T> insert(BSTNode<T> subroot, T value) {
if (subroot==null){
return new BSTNode<T>(value);
}
else if (value.compareTo(subroot.key) > 0){
subroot.rlink = insert(subroot.rlink,value);
return subroot;
}
else {
subroot.llink = insert(subroot.llink, value);
return subroot;
}
}
/**
* deletes value from tree. If value not there, do nothing.
* @param value value to delete
*/
public void delete(T value) {
root = delete(root, value);
}
/**
* deletes value from tree rooted at subroot
* @param subroot root of tree to be deleted from
* @param value element to delete
* @return pointer to tree rooted at subroot that has value removed from it
*/
private BSTNode<T> delete(BSTNode<T> subroot, T value) {
if(subroot == null) {
return null;
} else {
if(value.compareTo(subroot.key) < 0) {
subroot.llink = delete(subroot.llink, value);
} else if(value.compareTo(subroot.key) > 0) {
subroot.rlink = delete(subroot.rlink, value);
} else {
if(subroot.isLeaf()) {
return null;
} else if(subroot.hasRightChildOnly()) {
return subroot.rlink;
} else if(subroot.hasLeftChildOnly()) {
return subroot.llink;
} else {
BSTNode<T> replacement = findLargestInLeft(subroot);
subroot.key = replacement.key;
subroot.llink = delete(subroot.llink, subroot.key);
}
}
}
return subroot;
}
/**
* Given a BSTNode, find the largest value in the left subtree
* @param root the root of the tree to search for
* @return the largest node in the left subtree of root
*/
private BSTNode<T> findLargestInLeft(BSTNode<T> root) {
if(root == null) {
return null;
}
root = root.llink;
while(root.rlink != null) {
root = root.rlink;
}
return root;
}
/**
* checks whether the target value is in the tree
* @return true or false to indicate whether the target value is in the tree
*/
public boolean search(T target) {
return getNode(root, target) != null;
}
/**
* Search for and return the node at the given target value
*
* @param target the target value to search for
* @return null if not found or the node corresponding to the first instance of target
*/
public T getNode(T target) {
return getNode(root, target);
}
/**
* Recursively find the target node
*
* @param subroot the root of the tree to start the search from
* @param target the target value to search for
* @return null if not found or the node corresponding to the first instance of target
*/
private T getNode(BSTNode<T> subroot, T target) {
if(subroot == null) {
return null;
} else {
if(subroot.key.equals(target)) {
return subroot.key;
} else {
if(subroot.key.compareTo(target) > 0) {
//go left
return getNode(subroot.llink, target);
} else {
//go right
return getNode(subroot.rlink, target);
}
}
}
}
/**
* returns tree as printable string
* @return tree in string format with form (left subtree) value (right subtree)
*/
public String toString(){
return toString(root);
}
/**
* recursive helper method for toString()
*
* @param N root of subtree to make into a string
* @return string version of tree rooted at N
*/
private String toString(BSTNode<T> N){
String ret = "";
if (N != null){
ret += "(";
ret += toString(N.llink);
ret += " " + N + " ";
ret += toString(N.rlink);
ret += ")";
}
return ret;
}
/**
* Return the binary search tree as an array of String values
* @return the bst in a string array
*/
public String[] toArray() {
LinkedList<T> list = new LinkedList<>();
fillLinkedListInOrder(root, list);
return list.toArray();
}
/**
* Fill the given LinkedList in order (least to greatest)
* @param root the root of the BST
* @param list the given list to fill
*/
private void fillLinkedListInOrder(BSTNode<T> root, LinkedList<T> list) {
if (root.llink != null) {
fillLinkedListInOrder(root.llink, list);
}
list.add(root.key);
if (root.rlink != null) {
fillLinkedListInOrder(root.rlink, list);
}
}
}