package 树形结构;
import org.omg.PortableInterceptor.SUCCESSFUL;
import shixianClass.ArrayList;
import shixianClass02.LinkedList;
import shujujiegou_interface.List;
import shujujiegou_interface.Map;
import shujujiegou_interface.Set;
//底层由AVL树实现的映射
public class AVLTreeMap<K extends Comparable<K>,V> implements Map<K,V> {
private class Node{
private K key;
private V value;
public int height;//当前结点的高度,默认为1
private Node left;
private Node right;
public Node (K key,V value){
this.key=key;
this.value=value;
this.left=null;
this.right=null;
this.height=1;
}
@Override
public String toString() {
return "("+key+","+value+")";
}
}
private Node root;
private int size;
public AVLTreeMap(){
root=null;
size=0;
}
private Node getNode(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;
}
}
/**
* 获取某一个结点的高度,如果结点为空,则高度为0
*/
private int getHeight(Node node){
if(node==null){
return 0;
}
return node.height;
}
/**
*计算平衡因子:左子树的高度-右子树的高度
* >0左比右高
* =0左右一样高
* <0左比右低
*/
private int getBlancedFactor(Node node){
if(node==null){
return 0;
}
return getHeight(node.left)-getHeight(node.right);
}
/**
* 验证当前AVL树是否为一颗二分搜索树
*/
public boolean isBST(){
ArrayList<K> list=new ArrayList<>();
inOrder(root,list);
for (int i=1;i<list.size();i++){
if(list.get(i-1).compareTo(list.get(i))>0){
return false;
}
}
return true;
}
private void inOrder(Node node, ArrayList<K> list) {
if(node==null){
return;
}
inOrder(node.left,list);
list.add(node.key);
inOrder(node.right,list);
}
/**
*验证当前的AVL是否是一颗平衡树
*/
public boolean isBalanced(){
return isBalanced(root);
}
private boolean isBalanced(Node node) {
if(node==null){
return true;
}
int factor= getBlancedFactor(node);
if(Math.abs(factor)>1){
return false;
}
return isBalanced(node.left)&&isBalanced(node.right);
}
//右旋转
private Node rightRotate(Node y){
Node x=y.left;
Node T3=x.right;
x.right=y;
y.left=T3;
y.height=Math.max(getHeight(y.left),getHeight(y.right))+1;
x.height=Math.max(getHeight(x.left),getHeight(x.right))+1;
return x;
}
//左旋转
private Node leftRotate(Node y){
Node x=y.right;
Node T3=x.left;
x.left=y;
y.right=T3;
y.height=Math.max(getHeight(y.left),getHeight(y.right))+1;
x.height=Math.max(getHeight(x.left),getHeight(x.right))+1;
return x;
}
@Override
public void put(K key, V value) {
root=put(root,key,value);
}
private Node put(Node node, K key, V value) {
if(node==null){
size++;
return new Node(key,value);
}
if(key.compareTo(node.key)<0){
node.left= put(node.left,key,value);
}else if (key.compareTo(node.key)>0){
node.right=put(node.right,key,value);
}else {
node.value=value;
}
//更新node的高度
node.height=Math.max(getHeight(node.left),getHeight(node.right))+1;
int balancedFactor=getBlancedFactor(node);
//左侧的左侧 不平衡
if(balancedFactor>1&&getBlancedFactor(node.left)>=0){
return rightRotate(node);
}
//左侧的右侧 不平衡
if(balancedFactor>1&&getBlancedFactor(node.left)<0){
node.left= leftRotate(node.left);
return rightRotate(node);
}
//右侧的右侧 不平衡
if(balancedFactor<-1&&getBlancedFactor(node.right)<=0){
return leftRotate(node);
}
//右侧的左侧 不平衡
if(balancedFactor<-1&&getBlancedFactor(node.right)>0){
node.right= rightRotate(node.right);
return leftRotate(node);
}
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;
}
Node retNode=null;
if(key.compareTo(node.key)<0){
node.left=remove(node.left,key);
retNode=node;
}else if(key.compareTo(node.key)>0){
node.right=remove(node.right,key);
retNode=node;
}else{
if (node.left==null){
Node rightNode=node.right;
rightNode.right=null;
size--;
retNode=rightNode;
}
if(node.right==null){
Node leftNode=node.left;
leftNode.left=null;
size--;
retNode=leftNode;
}
Node successor=minnum(node.right);
successor.right= removeMin(node.right);
successor.left=node.left;
node.left=node.right=null;
retNode= successor;
}
retNode.height=Math.max(getHeight(retNode.left),getHeight(retNode.right))+1;
int balancedFactor=getBlancedFactor(retNode);
//左侧的左侧 不平衡
if(balancedFactor>1&&getBlancedFactor(retNode.left)>=0){
return rightRotate(retNode);
}
//左侧的右侧 不平衡
if(balancedFactor>1&&getBlancedFactor(retNode.left)<0){
retNode.left= leftRotate(retNode.left);
return rightRotate(retNode);
}
//右侧的右侧 不平衡
if(balancedFactor<-1&&getBlancedFactor(retNode.left)<=0){
return leftRotate(retNode);
}
//右侧的左侧 不平衡
if(balancedFactor<-1&&getBlancedFactor(retNode.left)>0){
retNode.right= rightRotate(retNode.right);
return leftRotate(retNode);
}
return retNode;
}
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;
}
private Node minnum(Node node) {
if (node.left==null){
return node;
}
return minnum(node.left);
}
@Override
public boolean contains(K key) {
return getNode(root,key)!=null;
}
@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("key is not exist");
}
n.value=value;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size==0&&root==null;
}
@Override
public Set<K> KeySet() {
TreeSet<K> set=new TreeSet<>();
inOrderKeySet(root,set);
return set;
}
private void inOrderKeySet(Node node, TreeSet<K> set) {
if(node==null){
return;
}
inOrderKeySet(node.left,set);
set.add(node.key);
inOrderKeySet(node.right,set);
}
@Override
public List<V> values() {
LinkedList<V> list=new LinkedList<>();
inOrderValue(root,list);
return list;
}
private void inOrderValue(Node node, LinkedList<V> list) {
if(node==null){
return;
}
inOrderValue(node.left,list);
list.add(node.value);
inOrderValue(node.right,list);
}
@Override
public Set<Entry<K, V>> entrySet() {
LinkedSet<Entry<K,V>> entries=new LinkedSet<Entry<K,V>>();
inOrderEnterSet(root,entries);
return entries;
}
private void inOrderEnterSet(Node node, LinkedSet<Entry<K,V>> entries) {
if(node==null){
return;
}
inOrderEnterSet(node.left,entries);
entries.add(new BSTEntry<>(node.key,node.value));
inOrderEnterSet(node.right,entries);
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append('[');
if(isEmpty()){
sb.append(']');
}else {
Set<Entry<K,V>> set=entrySet();
for(Entry<K,V> entry:set){
sb.append(entry);
sb.append(',');
}
sb.deleteCharAt(sb.length()-1);
sb.append(']');
}
return sb.toString();
}
private class BSTEntry<K ,V> implements Entry<K, V>{
private K key;
private V value;
public BSTEntry(K key,V value){
this.key=key;
this.value=value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public String toString() {
return "("+key.toString()+":"+value.toString()+")";
}
}
}
测试:
package ceshi;
import shujujiegou_interface.Set;
import 树形结构.AVLTreeMap;
import 树形结构.FileOperation;
import 树形结构.TreeMap;
import java.util.ArrayList;
public class TestAVL {
public static void main(String[] args) {
AVLTreeMap<String, Integer> map = new AVLTreeMap<>();
ArrayList<String> words = new ArrayList<>();
FileOperation.readFile("pride-and-prejudice.txt", words);
for (String word : words) {
if (!map.contains(word)) {
map.put(word, 1);
} else {
map.set(word, map.get(word) + 1);
}
}
System.out.println("总单词个数:" + map.size());
System.out.println(map.isBST());
System.out.println(map.isBalanced());
}
}
执行结果
总单词个数:6530
true
true