映射
字典
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 newValue);
int getSize();
boolean isEmpty();
}
用链表实现映射
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);}
public String toString() {
return key.toString()+":"+value.toString();
}
private Node dummyHead;
private int size;
public LinkedListMap() {
dummyHead=new Node();
size=0;
}
public int getSize() {return size;}
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;
}
public boolean contains(K key) {
return getNode(key)!=null;
}
public V get(K key) {
Node node=getNode(key);
return node==null?null:node.value;
}
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;
}
public void set(K key,V newValue) { //修改
Node node=getNode(key); //看是否有节点
if(node==null)
throw new IllegalArgumentException("doesn't exist!");
node.value=newValue;
}
public V remove(K key) {
Node prev=dummyHead;
while(prev.next!=null) {
if(prev.next.key.equals(key))
break;
prev=prev.next;//prev.next为要删除的节点
}
if(prev.next!=null) {
Node delNode=prev.next;
prev.next=delNode.next;
delNode.next=null;
size--;
return delNode.value;
}
return null;//如果没发现要删的
}
用BST实现映射
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;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size==0;
}
public void add(K key,V value) {
root=add(root,key,value);
}
private Node add(Node node,K key,V value) {//返回Node类型的值
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 //key.compareTO(node.key)==0
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
return getNode(node.right,key);
}
public boolean contains(K key) {
return getNode(root,key)!=null;
}
public V get(K key) {
Node node=getNode(root,key);
return node==null?null:node.value;
}
public void set(K key,V newValue) {
Node node=getNode(root,key);//看看key在不在
if(node==null)
throw new IllegalArgumentException(key+"doesn't exist");
node.value=newValue;
}
//返回以node为根的二分搜索树最小键值所在节点
private Node minimum(Node node) {
if(node.left==null)
return node;
return minimum(node.left);
}
//从二分搜索树中删除最小值所在节点,返回最小值
public E removeMin() {
E ret =minimum();
root=removeMin(root);
return ret;
}
//删除以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;
}
//从二分搜索树中删除元素为e的节点
public V remove(K key) {
Node node=getNode(root,key);
if(node!=null) {
root = remove(root,e);
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 { //key==node.key
//待删除节点左子树为空的情况
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;
}
// 测试:每个词的词频
public static void main(String[] args) {
System.out.println("Pride and Prejudice");
ArrayList<String> words=new ArrayList<>();
if(FileOperation.readFile("nnnn.txt",words)) {
System.out.println("totel words:"+words.size());
BSTMap<String,Integer> map =new BSTMap<>();
for(String word:words) {
if(map.contains(word))
map.set(word,map.get(word)+1);// 当前word频率+1
else
map.add(word,1); //没有就添加
}
System.out.println("totel different words:"+map.getSize());
System.out.println("frequency of pride"+map.get("pride");)
}
}