集合的实现
set集合的接口:包括添加,移除,包含,获取大小,判断是否为空
public interface Set {
void add(E e);
void remove(E e);
boolean contains(E e);
int getSize();
boolean isEmpty();
}
第一种方式,以链表的形式实现集合
我用的是自己之前写的文章java实现链表中的代码
public class LinkedListSet implements Set {
private LinkList list;
public LinkedListSet() {
this.list = new LinkList<>();
}
@Override
public boolean contains(E e) {
return list.constants(e);
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public void add(E e) {
if(!list.constants(e)){
list.addFirst(e);
}
}
@Override
public void remove(E e) {
list.removeElement(e);
}
}
第二种方式,以二分搜索树的形式实现
以上篇文章java实现二分搜索树中的代码
public class BSTSet> implements Set {
private BST bst;
public BSTSet() {
bst = new BST<>();
}
@Override
public boolean contains(E e) {
return bst.contains(e);
}
@Override
public int getSize() {
return bst.getSize();
}
@Override
public boolean isEmpty() {
return bst.isEmpty();
}
@Override
public void add(E e) {
bst.add(e);
}
@Override
public void remove(E e) {
bst.remove(e);
}
}
映射Map的实现
Map接口:添加,删除,包含,根据key获得值,根据key设置新value进去,获得大小,判断是否为空
public interface Map {
void add(K key, V value);
V remove(K key);
boolean contains(K key);
V get(K key);
void set(K key, V value);
int getSize();
boolean isEmpty();
}
第一种方法,以链表的形式实现映射
public class LinkedListMap implements Map {
private class Node {
public K key;
public V value;
public Node next;
public Node(K key, V value, Node next) {
this.key = key;
this.value = value;
this.next = next;
}
public Node(K key, V value) {
this(key, value, null);
}
public Node() {
this(null, null, null);
}
@Override
public String toString() {
return key.toString() + " : " + value.toString();
}
}
private Node dummyHead;
private int size;
public LinkedListMap() {
this.dummyHead = new Node();
size = 0;
}
private Node getNode(K key) {
Node cur = dummyHead.next;
while (cur != null) {
if (cur.key.equals(key)) {
return cur;
}
cur = cur.next;
}
return null;
}
@Override
public boolean contains(K key) {
return getNode(key) != null;
}
@Override
public V get(K key) {
Node node = getNode(key);
return node == null ? null : node.value;
}
@Override
public void add(K key, V value) {
Node node = getNode(key);
if (node == null) {
dummyHead.next = new Node(key, value, dummyHead.next);
size++;
} else
node.value = value;
}
@Override
public void set(K key, V value) {
Node node = getNode(key);
if (node == null)
throw new IllegalArgumentException(key + "doesn't exist");
node.value = value;
}
@Override
public V remove(K key) {
Node prev = dummyHead;
while (prev.next != null) {
if (prev.next.key.equals(key)) {//key相同将不进行处理
break;
}
prev = prev.next;
}
if (prev.next != null) {
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size--;
return delNode.value;
}
return null;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
}
第二种方法,以二分搜索树的形式实现映射
public class BSTMap implements Map {
private class Node {
public K key;
public V value;
public Node left, right;
public Node(K key, V value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
}
}
private Node root;
private int size;
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
//向二分搜索树中添加新元素value
@Override
public void add(K key, V value) {
root = add(root, key, value);
}
//向以node为根的二分搜索树中插入(key,value),递归算法
//返回插入新节点后二分搜索树的根
private Node add(Node node, K key, V value) {
if (node == null) {
size++;
return new Node(key, value);
}
if (key.compareTo(node.key) < 0) {
node.left = add(node.left, key, value);
} else if (key.compareTo(node.key) > 0) {
node.right = add(node.right, key, value);
} else {
node.value = value;
}
return node;
}
//返回以node为根节点的二分搜索树中,key所在的节点
private Node getNode(Node node, K key) {
if (node == null)
return null;
if (key.compareTo(node.key) == 0)
return node;
else if (key.compareTo(node.key) < 0)
return getNode(node.left, key);
else //if (key.compareTo(node.key) > 0)
return getNode(node.right, key);
}
@Override
public boolean contains(K key) {
return getNode(root, key) != null;
}
@Override
public V get(K key) {
Node node = getNode(root, key);
return node == null ? null : node.value;
}
@Override
public void set(K key, V value) {
Node node = getNode(root, key);
if (node == null)
throw new IllegalArgumentException(key + "doesn't exist!");
node.value = value;
}
//返回以node为根节点的二分搜索树的最小值的节点
private Node minimum(Node node) {
if (node.left == null)
return node;
return minimum(node.left);
}
//删除以node为根的二分搜索树中的最小节点
//返回删除节点后新的二分搜索树的根
private Node removeMin(Node node) {
if (node.left == null) {
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
//返回以node为根节点的二分搜索树的最大值的节点
private Node maximum(Node node) {
if (node.right == null)
return node;
return maximum(node.right);
}
//删除以node为根的二分搜索树中的最大节点
//返回删除节点后新的二分搜索树的根
private Node removeMax(Node node) {
if (node.right == null) {
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
node.right = removeMin(node.right);
return node;
}
//从二分搜索树删除元素
@Override
public V remove(K key) {
Node node = getNode(root, key);
if (node != null) {
root = remove(root, key);
return node.value;
}
return null;
}
//删除以node为根的二分 搜索树中键为key的节点,递归算法
//返回删除节点后新的二分搜索树的根
private Node remove(Node node, K key) {
if (node == null) {
return null;
}
if (key.compareTo(node.key) < 0) {
node.left = remove(node.left, key);
return node;
} else if (key.compareTo(node.key) > 0) {
node.right = remove(node.right, key);
return node;
} else {
//待删除节点左子树为空的情况
if (node.left == null) {
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
//待删除节点右子树为空的情况
if (node.right == null) {
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
//待删除的节点左右子树均不为空的情况
//找到比待删除节点大的最小节点,即待删除节点右子树的最小节点
//用这个结点顶替待删除的节点的位置
Node successor = minimum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.right = null;
return successor;
}
}
}