(一) 基本介绍看我上一篇写的linklist实现的map
map介绍
(二)map接口实现
import p02.动态链表.SingleLinkList;
import 顺序表.List;
public class BstMap<K extends Comparable<K>,V> implements Map<K, V> {
private class Node{
Node left,right;
public K key;
public V value;
public Node(K key,V value) {
this.key=key;
this.value=value;
left=null;
right=null;
}
public Node() {
this(null,null);
}
@Override
public String toString() {
return key.toString()+":"+value.toString();
}
}
public Node root;
int size;
public BstMap() {
root=null;
size=0;
}
@Override
public void add(K key, V value) {
root=add(root,key,value);
}
private BstMap<K, V>.Node add(BstMap<K, V>.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;
}
@Override
public V remove(K key) {
Node node=getNode(root,key);
if(node==null) {
return null;
}
root=remove(root,key);
return node.value;
}
private BstMap<K, V>.Node remove(BstMap<K, V>.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;
size--;
node.right=null;
return rightnode;
}else if(node.right==null) {
Node leftnode=node.left;
size--;
node.left=null;
return leftnode;
}
Node temp=getmin(node.right);
temp.right=removemin(node.right);
temp.left=node.left;
node.left=null;
node.right=null;
return temp;
}
}
private BstMap<K, V>.Node removemin(BstMap<K, V>.Node node) {
if(node.left==null) {
Node rightnode=node.right;
size--;
node.right=null;
return rightnode;
}
node.left=removemin(node.left);
return node;
}
private BstMap<K, V>.Node getmin(BstMap<K, V>.Node node) {
if(node.left==null) {
return node;
}else {
return getmin(node.left);
}
}
private BstMap<K, V>.Node getNode(BstMap<K, V>.Node node, K key) {
if(node==null) {
return null;
}if(key.compareTo(node.key)<0) {
return getNode(node.left, key);
}else if(key.compareTo(node.key)>0) {
return getNode(node.right, key);
}else {
return node;
}
}
@Override
public boolean contains(K key) {
Node n=getNode(root, key);
return n==null?false:true;
}
@Override
public V get(K key) {
Node n=getNode(root, key);
return n==null?null:n.value;
}
@Override
public void set(K key, V value) {
Node n=getNode(root,key);
if(n==null){
throw new IllegalArgumentException("元素不存在!");
}
n.value=value;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return getSize()==0;
}
@Override
public Set keys() {
BstSet<K> set=new BstSet<>();
Inorder(root,set);
return set;
}
private void Inorder(Node node, Set<K> set) {
if(node==null) {
return ;
}
Inorder(node.left, set);
set.add(node.key);
Inorder(node.right, set);
}
@Override
public List values() {
SingleLinkList<V> list=new SingleLinkList<>();
Inorder(root,list);
return list;
}
private void Inorder(BstMap<K, V>.Node node, SingleLinkList<V> list) {
if(node==null) {
return;
}
Inorder(node.left, list);
list.addLast(node.value);
Inorder(node.right, list);
}
}
(三)测试:用bstmap计算一本书中一个出现的次数
import java.util.ArrayList;
public class Test3 {
public static void main(String[] args) {
ArrayList<String> words=new ArrayList<>();
FileOperation.readFile("a-tale-of-two-cities.txt", words);
long start=System.currentTimeMillis();
BstMap<String, Integer> map=new BstMap<>();
for(int i=0;i<words.size();i++){
String word=words.get(i);
if(map.contains(word)){
map.set(word, map.get(word)+1);
}else{
map.add(word, 1);
}
}
long end=System.currentTimeMillis();
double second=(end-start)/1000.0;
System.out.println("用时:"+second);
System.out.println("所有单词的个数"+words.size());
System.out.println("map的长度(不重复的单词数):"+map.getSize());
System.out.println("she:"+map.get("she"));
System.out.println("不重复的单词:"+map.keys());
}
}
(四)测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f249fe47a47ec1f415c6d38f7bb7300e.png)