-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADSBSTSet.java
More file actions
191 lines (155 loc) · 4.49 KB
/
Copy pathADSBSTSet.java
File metadata and controls
191 lines (155 loc) · 4.49 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
import java.util.Iterator;
import java.util.Stack;
public class ADSBSTSet<E extends Comparable<E>> implements Iterable<E>{
private class node {
E data;
node left, right;
node(E data) {
this.data = data;
left = null;
right = null;
}
}
node root = null;
public boolean contains(E e) {
node currRoot = root;
while ( currRoot != null ) {
int comparison = e.compareTo(currRoot.data);
System.out.println(e + " compared to " + currRoot.data + " => " + comparison);
if ( comparison == 0 ) {
return true;
}
if ( comparison < 0 ) {
currRoot = currRoot.left;
}
if ( comparison > 0 ) {
currRoot = currRoot.right;
}
}
return false;
}
public void addR(E e) {
root = addRecursive(e, root);
}
private node addRecursive(E e, node currRoot) {
if ( currRoot == null ) {
return new node(e);
}
int comparison = e.compareTo(currRoot.data);
if ( comparison == 0 ) {
return currRoot;
} else if ( comparison < 0 ) {
currRoot.left = addRecursive(e, currRoot.left);
return currRoot;
} else /* comparison > 0 */ {
currRoot.right = addRecursive(e, currRoot.right);
return currRoot;
}
}
public void add(E e) {
node currRoot = root;
node newNode = new node(e);
boolean done = false;
if ( root == null ) {
root = newNode;
return;
}
while ( ! done ) {
int comparison = e.compareTo(currRoot.data);
if ( comparison == 0 ) {
done = true;
}
if ( comparison < 0 ) {
if ( currRoot.left == null ) {
currRoot.left = newNode;
done = true; }
else {
currRoot = currRoot.left;
}
}
if ( comparison > 0 ) {
if ( currRoot.right == null ) {
currRoot.right = newNode;
done = true; }
else {
currRoot = currRoot.right;
}
}
}
return;
}
public void delete(E e)
{
root = deleteR(root, e);
}
node deleteR(node root, E e)
{
/* Base Case: If the tree is empty */
if (root == null)
return root;
/* Otherwise, recur down the tree */
if (e.compareTo(root.data) < 0)
root.left = deleteR(root.left, e);
else if (e.compareTo(root.data) > 0)
root.right = deleteR(root.right, e);
// if key is same as root's
// key, then This is the
// node to be deleted
else {
// node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node with two children: Get the in order
// successor (smallest in the right subtree)
root.data = minValue(root.right);
// Delete the in order successor
root.right = deleteR(root.right, root.data);
}
return root;
}
E minValue(node root)
{
E minv = root.data;
while (root.left != null)
{
minv = root.left.data;
root = root.left;
}
return minv;
}
class ADSBSTSetIterator implements Iterator<E>{
Stack<node> myStack;
node r = root;
public ADSBSTSetIterator()
{
myStack = new Stack<node>();
while (r != null) {
myStack.push(r);
r = r.left;
}
}
public boolean hasNext(){
if(r.data != null)
return true;
else
return false;
}
public E next() {
node tnode = myStack.pop();
E result = tnode.data;
if (tnode.right != null) {
tnode = tnode.right;
while (tnode != null) {
myStack.push(tnode);
tnode = tnode.left;
}
}
return result;
}
}
public ADSBSTSetIterator iterator() {
return new ADSBSTSetIterator();
}
}