public class MyTreeSet extends Comparable implements Set int size Node

  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
public class MyTreeSet<T extends Comparable<T» implements Set<T> {
int size;
Node<T> root;
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean contains(Object o) {
Queue<Node<T» queue = new LinkedList<Node<T»();
queue.offer(this.root);
while (!queue.isEmpty()) {
Node<T> p = queue.poll();
if (p.getLeft() != null) {
queue.offer(p.getLeft());
}
if (p.getRight() != null) {
queue.offer(p.getRight());
}
if (p.getValue().equals(o)) {
return true;
}
}
return false;
}
@Override
public Iterator<T> iterator() {
return null;
}
@Override
public Object[] toArray() {
Object[] array = new Object[this.size];
int i = 0;
Queue<Node<T» queue = new LinkedList<Node<T»();
queue.offer(this.root);
while (!queue.isEmpty()) {
Node<T> p = queue.poll();
if (p.getLeft() != null) {
queue.offer(p.getLeft());
}
if (p.getRight() != null) {
queue.offer(p.getRight());
}
array[i] = p.getValue();
i++;
}
return array;
}
@Override
public <T1> T1[] toArray(T1[] a) {
return null;
}
@Override
public boolean add(T t) {
if (!this.contains(t)) {
Node<T> node = new Node<T>(t);
if (root == null) {
root = node;
return true;
} else {
root.add(t);
return true;
}
} else {
return false;
}
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!this.contains(o)) {
return false;
}
}
return true;
}
@Override
public boolean addAll(Collection<? extends T> c) {
for (T o : c) {
if (!this.add(o)) {
return false;
}
}
return true;
}
@Override
public boolean retainAll(Collection<?> c) {
boolean flag;
boolean flag1 = false;
for (T o : this) {
flag = !c.contains(o);
flag1 |= flag;
if (flag) {
this.remove(o);
}
}
return flag1;
}
@Override
public boolean removeAll(Collection<?> c) {
for (Object o : c) {
if (!this.remove(o)) {
return false;
}
}
return true;
}
@Override
public void clear() {
this.size = 0;
this.root = null;
}
}