-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLString.java
More file actions
62 lines (50 loc) · 1.54 KB
/
AVLString.java
File metadata and controls
62 lines (50 loc) · 1.54 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
/**
* String AVL class extends AVL tree and provides extra method
* that allows user to search for prefixes in AVL String objects.
* @author Adisa Narula
*/
public class AVLString extends AVLTree<String>{
/**
* Constructor calls super() constructor of parent AVL class.
*/
public AVLString() {
super();
}
/**
* Contains prefix helper method accessed by the user in order
* to check whether AVL tree contains a certain prefix.
* @param data prefix to search for in AVL tree.
* @return tree if prefix exists.
*/
public boolean containsPrefix (String data) {
// tree if prefix exists.
return recContainsPrefix(super.root, data);
}
/**
* Recursive contains prefix method recursively searchers through the tree structure
* in order to find whether prefix exists in the tree. Uses compareTo method to
* check whether the method should go left or right to find the data.
* @param node tree that is being searched.
* @param data prefix that is searched for in the tree.
* @return true if prefix is found.
*/
private boolean recContainsPrefix(AVLNode<String> node, String data) {
// empty tree
if (node == null) {
return false;
}
// return true if data in node starts with the prefix
else if (node.getData().startsWith(data)) {
return true;
}
// keep comparing
else if (data.compareTo(node.getData()) < 0) {
// go left
return recContainsPrefix(node.getLeft(), data);
}
else {
// go right
return recContainsPrefix(node.getRight(), data);
}
}
}