set实现

1
2
3
4
5
6
7
8

public interface <E> {
void add(E e); // 不能添加重复元素
void remove(E e);
boolean contains(E e);
int getSize();
boolean isEmpty();
}

BST实现集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//平均时间复杂度O(logn)
public class BSTSet<E extends Comparable<E>> implements <E> {
private BST<E> bst;
public BSTSet() {
bst = new BST<>();
}

public int getSize() {
return bst.size();
}

public boolean isEmpty() {
return bst.isEmpty();
}

public void add(E e) {
bst.add(e);
}

public boolean contains(E e) {
return bst.contains(e);
}
}

链表实现集合

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
// 平均时间复杂度O(n)
public class LinkedListSet<E> implements <E> {
private LinkedList<E> list;

public LinkListSet() {
list = new LinkedList<>();
}

public int getSize() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public void add(E e) {
if(!list.contains(e)) {
list.addFirst(e);
}
}
@Override
public boolean contains(E e) {
return list.contains(e);
}
@Override
public void remove(E e) {
list.removeElement(e);
}
}