-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.java
More file actions
executable file
·152 lines (121 loc) · 2.63 KB
/
Copy pathSet.java
File metadata and controls
executable file
·152 lines (121 loc) · 2.63 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
import java.util.Iterator;
import java.util.Stack;
public class Set<Key extends Comparable<Key>> implements Iterable<Key> {
public Node root;
public Key get(Key key) {
return get(root, key);
}
private Key get(Node x, Key key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp < 0)
return get(x.left, key);
else if (cmp > 0)
return get(x.right, key);
else
return x.key;
}
public boolean contains(Key key) {
return (get(key) != null);
}
public void put(Key key) {
root = put(root, key);
}
private Node put(Node x, Key key) {
if (x == null) {
Node n = new Node(key);
return n;
}
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key);
if (cmp > 0)
x.right = put(x.right, key);
return x;
}
public class Node {
Key key;
Node left, right;
public Node(Key key) {
this.key = key;
}
}
public Set<Key> intersection(Set<Key> t) {
Set<Key> answer = new Set<Key>();
Iterator<Key> sItr = iterator();
while (sItr.hasNext()) {
Key currentkey = sItr.next();
if (!answer.contains(currentkey) && t.contains(currentkey)) {
answer.put(currentkey);
}
}
return answer;
}
public Set<Key> union(Set<Key> t) {
Set<Key> answer = new Set<Key>();
Iterator<Key> itrthis = iterator();
Iterator<Key> tItr = t.iterator();
while (itrthis.hasNext()) {
Key currentkey = itrthis.next();
if (!answer.contains(currentkey)) {
answer.put(currentkey);
}
}
while (tItr.hasNext()) {
Key currentkey = tItr.next();
if (!answer.contains(currentkey)) {
answer.put(currentkey);
}
}
return answer;
}
public Set<Key> minus(Set<Key> t) {
Iterator<Key> sItr = iterator();
Iterator<Key> tItr = t.iterator();
Set<Key> answer = new Set<Key>();
while (sItr.hasNext()) {
Key current = sItr.next();
if (!t.contains(current)) {
answer.put(current);
}
}
return answer;
}
public Iterator<Key> iterator() {
return new BSTIterator();
}
public class BSTIterator implements Iterator<Key> {
Stack<Node> stack = new Stack<Node>();
private void pushLeft(Node x) {
while (x != null) {
stack.push(x);
x = x.left;
}
}
BSTIterator() {
pushLeft(root);
}
public boolean hasNext() {
return !stack.isEmpty();
}
public Key next() {
Node x = stack.pop();
pushLeft(x.right);
return x.key;
}
// public void remove(Key key) {
// if (contains(key)){
//
// }
//
// }
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
public static void main(String args[]){
System.out.print("enter the values for the first set (S): ");
}
}