集合Set
- void add(E) 不能添加重复元素
- void remove(E)
- boolean contains(E
- int getSize()
- boolean isEmpty()
二分搜索树实现集合
public class BSTSet<E extends Comparable<E>> implements Set<E>{
private BST<E> bst;
public BSTSet(){
bst = new BST<>();
}
@Override
public int getSize(){
return bst.size();
}
@Override
public boolean isEmpty(){
return bst.isEmpty();
}
@Override
public void add(E e){
bst.add(e);
}
@Override
public boolean contains(E e){
return bst.contains(e);
}
@Override
public void remove(E e){
bst.remove(e);
}
}
链表实现集合
import java.util.LinkedList;
public class LinkedListSet<E> implements Set<E> {
private LinkedList<E> list;
public LinkedListSet(){
list = new LinkedList<>();
}
@Override
public void add(E e) {
if (!list.contains(e))
list.addFirst(e);
}
@Override
public void remove(E e) {
list.remove(e);
}
@Override
public boolean contains(E e) {
return list.contains(e);
}
@Override
public int getSize() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
}
集合的时间复杂度分析
LinkedListSet | BSTSet (平均) 最差 | |
---|---|---|
增 add | O(n) | O(h) O(logn) O(n) |
查 contains | O(n) | O(h) O(logn) O(n) |
删 remove | O(n) | O(h) O(logn) O(n) |
有序性 | 无序集合 | 有序集合 |
-
n存储元素个数,h二分搜索树的高度
-
满二叉树:
h = l o g 2 ( n + 1 ) h=log_2(n+1) h=log2(n+1)
多重集合
- 集合元素可以重复
映射(Map)
- 也称字典(dict), 单词 -----> 释义
- 存储(键, 值) 数据对的数据结构(Key, Value)
- 根据键(Key),寻找值(Value)
使用链表或者二分搜索树实现映射
// 二叉树
class Node {
K key;
V value;
Node left;
Node right;
}
// 链表
class Node {
K key;
V value;
Node next;
}
映射接口
Map<K, V>
- void add(K, V)
- V remove(K)
- boolean contains(K)
- V get(K)
- void set(K, V)
- int getSize()
- boolean isEmpty()
public interface Map<K, V> {
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<K, V> implements Map<K, V> {
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) {
this(key, null, null);
}
public Node() {
this(null, null, null);
}
@Override
public String toString() {
return key.toString() + " : " + value.toString();
}
}
private Node dummyHead;
private int size;
public LinkedListMap(){
dummyHead = new Node();
size = 0;
}
@Override
public int getSize() { return size; }
@Override
public boolean isEmpty() { return 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 newValue){
Node node = getNode(key);
if(node == null)
throw new IllegalArgumentException(key + "doesn't exist!");
node.value = newValue;
}
@Override
public V remove(K key){
Node prev = dummyHead;
while (prev.next != null){
if(prev.next.key.equals(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;
}
}
二分搜索树实现映射
public class BSTMap<K extends Comparable<K>, V> implements Map<K, V> {
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;
left = null;
right = null;
}
}
private Node root;
private int size;
public BSTMap(){
root = null;
size = 0;
}
@Override
public int getSize(){
return size;
}
@Override
public boolean isEmpty(){
return size == 0;
}
@Override
public void add(K key, V value){
root = add(root, 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;
}
// 返回键为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
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 newValue){
Node node = getNode(root, key);
if(node == null)
throw new IllegalArgumentException(key + "doesn't exist!");
node.value = newValue;
}
@Override
public V remove(K key){
Node node = getNode(root, key);
if(node != null){
root = remove(root, key);
return node.value;
}
return null;
}
// 返回以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;
}
// 删除键为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); // removeMin已经size--
successor.left = node.left;
node.left = node.right = null;
return successor;
}
}
}
映射的时间复杂度分析
LinkedListMap | BSTMap | 平均 | 最差 | |
---|---|---|---|---|
增 add | O(n) | O(h) | O(logn) | O(n) |
删 remove | O(n) | O(h) | O(logn) | O(n) |
改 set | O(n) | O(h) | O(logn) | O(n) |
查 set | O(n) | O(h) | O(logn) | O(n) |
查 contains | O(n) | O(h) | O(logn) | O(n) |
注: 该笔记为慕课网的课程笔记,若侵权立删。