package com.cl.map;
import com.cl.set.FileOperation;
import java.util.ArrayList;
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;
this.left = this.right=null;
}
}
private Node root;
private int size;
public BSTMap() {
root = null;
size = 0;
}
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 void add(K key,V value){
root = add(root,key,value);
}
/**递归
* 向以node为根节点的树插入元素
* @return:返回插入新结点后二分搜索树的根
*/
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为根的树的最小结点
* @param node
* @return 返回最小的结点
*/
private Node minmum(Node node) {
if(node.left==null){
return node;
}
return minmum(node.left);
}
/**
* 删除以node为根节点的树的最小节点
* @param node
* @return:返回删除后的子树的根
*/
private Node removeMin(Node node) {
if(node.left==null){//找到最小值的结点
Node rigthNode = node.right;//null或者非空
node.right=null;
size--;
return rigthNode;
}
node.left = removeMin(node.left);
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;
}
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 {//e==node.e,找到删除的结点
//左子树空
if(node.left==null){
Node rightNoe = node.right;
node.right=null;
size--;
return rightNoe;
}else if(node.right==null){//右子树空
Node leftNode = node.left;
node.left=null;
size--;
return leftNode;
}else{//都不空
//找到比待删除结点的右子树中最小的结点
//用这个结点删除待删除的结点
Node successor = minmum(node.right);
//removeMin里有一次size--
successor.right = removeMin(node.right);
successor.left = node.left;
node.left=node.right=null;
return successor;
}
}
}
@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(node+" not exist");
}
node.value=value;
}
@Override
public int getSise() {
return size;
}
@Override
public boolean isEmpty() {
return size==0;
}
public static void main(String[] args) {
String filename = "pride-and-prejudice.txt";
ArrayList<String> words1 = new ArrayList<>();
FileOperation.readFile("src/"+filename,words1);
System.out.println("Total words:"+words1.size());
BSTMap<String,Integer> map = new BSTMap<>();
for (String word:words1) {
if(map.contains(word)){
map.set(word,map.get(word)+1);
}
else map.add(word,1);
}
System.out.println("Total diffrent words:"+map.getSise());
System.out.println("Frenquncy of pride:"+map.get("pride"));
}
}