forked from Dennis-ty/cs110ProjectC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTreeWithDups.java
85 lines (64 loc) · 2.28 KB
/
BinarySearchTreeWithDups.java
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
import java.util.*;
public class BinarySearchTreeWithDups<T extends Comparable<? super T>> extends BinarySearchTree<T> {
public BinarySearchTreeWithDups() {
super();
}
public BinarySearchTreeWithDups(T rootEntry) {
super(rootEntry);
}
@Override
public boolean add(T newEntry) {
if (isEmpty()) {
return super.add(newEntry);
} else {
return addEntryHelperNonRecursive(newEntry);
}
}
// IMPLEMENT THIS METHOD; THIS METHOD CANNOT BE RECURSIVE
private boolean addEntryHelperNonRecursive(T newEntry) {
// YOUR CODE HERE!
return false; // placeholder: replace with your own code
}
// THIS METHOD CANNOT BE RECURSIVE.
// Make sure to take advantage of the sorted nature of the BST!
public int countIterative(T target) {
// YOUR CODE HERE!
// this initial code is meant as a suggestion to get your started- use it or delete it!
int count = 0;
BinaryNode<T> currentNode = root;
// consider a loop!
return count;
}
// THIS METHOD MUST BE RECURSIVE!
// You are allowed to create a private helper.
// Make sure to take advantage of the sorted nature of the BST!
public int countGreaterRecursive(T target) {
// YOUR CODE HERE!
// this initial code is meant as a suggestion to get your started- use it or delete it!
int count = 0;
BinaryNode<T> rootNode = root;
// consider a helper method!
return count;
}
// THIS METHOD CANNOT BE RECURSIVE.
// Hint: use a stack!
// Make sure to take advantage of the sorted nature of the BST!
public int countGreaterIterative(T target) {
// YOUR CODE HERE!
// this initial code is meant as a suggestion to get your started- use it or delete it!
int count = 0;
BinaryNode<T> rootNode = root;
Stack<BinaryNode<T>> nodeStack = new Stack<BinaryNode<T>>();
nodeStack.push(rootNode);
// consider a loop based on the stack!
return count;
}
// For full credit, the method should be O(n).
// You are allowed to use a helper method.
// The method can be iterative or recursive.
// If you make the method recursive, you might need to comment out the call to the method in Part B.
public int countUniqueValues() {
// YOUR EXTRA CREDIT CODE HERE!
return 0; // placeholder: replace with your own code
}
}