parent
359d69732b
commit
946360f90f
@ -0,0 +1,48 @@
|
|||||||
|
package class33;
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.Security;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
public class Hash {
|
||||||
|
|
||||||
|
private MessageDigest hash;
|
||||||
|
|
||||||
|
public Hash(String algorithm) {
|
||||||
|
try {
|
||||||
|
hash = MessageDigest.getInstance(algorithm);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String hashCode(String input) {
|
||||||
|
return DatatypeConverter.printHexBinary(hash.digest(input.getBytes())).toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("支持的算法 : ");
|
||||||
|
for (String str : Security.getAlgorithms("MessageDigest")) {
|
||||||
|
System.out.println(str);
|
||||||
|
}
|
||||||
|
System.out.println("=======");
|
||||||
|
|
||||||
|
String algorithm = "SHA";
|
||||||
|
Hash hash = new Hash(algorithm);
|
||||||
|
|
||||||
|
String input1 = "zuochengyunzuochengyun1";
|
||||||
|
String input2 = "zuochengyunzuochengyun2";
|
||||||
|
String input3 = "zuochengyunzuochengyun3";
|
||||||
|
String input4 = "zuochengyunzuochengyun4";
|
||||||
|
String input5 = "zuochengyunzuochengyun5";
|
||||||
|
System.out.println(hash.hashCode(input1));
|
||||||
|
System.out.println(hash.hashCode(input2));
|
||||||
|
System.out.println(hash.hashCode(input3));
|
||||||
|
System.out.println(hash.hashCode(input4));
|
||||||
|
System.out.println(hash.hashCode(input5));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,228 @@
|
|||||||
|
package class37;
|
||||||
|
|
||||||
|
public class Code02_SlidingWindowMedian {
|
||||||
|
|
||||||
|
public static class SBTNode<K extends Comparable<K>> {
|
||||||
|
public K key;
|
||||||
|
public SBTNode<K> l;
|
||||||
|
public SBTNode<K> r;
|
||||||
|
public int size;
|
||||||
|
|
||||||
|
public SBTNode(K k) {
|
||||||
|
key = k;
|
||||||
|
size = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SizeBalancedTreeMap<K extends Comparable<K>> {
|
||||||
|
private SBTNode<K> root;
|
||||||
|
|
||||||
|
private SBTNode<K> rightRotate(SBTNode<K> cur) {
|
||||||
|
SBTNode<K> leftNode = cur.l;
|
||||||
|
cur.l = leftNode.r;
|
||||||
|
leftNode.r = cur;
|
||||||
|
leftNode.size = cur.size;
|
||||||
|
cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
|
||||||
|
return leftNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SBTNode<K> leftRotate(SBTNode<K> cur) {
|
||||||
|
SBTNode<K> rightNode = cur.r;
|
||||||
|
cur.r = rightNode.l;
|
||||||
|
rightNode.l = cur;
|
||||||
|
rightNode.size = cur.size;
|
||||||
|
cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
|
||||||
|
return rightNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SBTNode<K> maintain(SBTNode<K> cur) {
|
||||||
|
if (cur == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int leftSize = cur.l != null ? cur.l.size : 0;
|
||||||
|
int leftLeftSize = cur.l != null && cur.l.l != null ? cur.l.l.size : 0;
|
||||||
|
int leftRightSize = cur.l != null && cur.l.r != null ? cur.l.r.size : 0;
|
||||||
|
int rightSize = cur.r != null ? cur.r.size : 0;
|
||||||
|
int rightLeftSize = cur.r != null && cur.r.l != null ? cur.r.l.size : 0;
|
||||||
|
int rightRightSize = cur.r != null && cur.r.r != null ? cur.r.r.size : 0;
|
||||||
|
if (leftLeftSize > rightSize) {
|
||||||
|
cur = rightRotate(cur);
|
||||||
|
cur.r = maintain(cur.r);
|
||||||
|
cur = maintain(cur);
|
||||||
|
} else if (leftRightSize > rightSize) {
|
||||||
|
cur.l = leftRotate(cur.l);
|
||||||
|
cur = rightRotate(cur);
|
||||||
|
cur.l = maintain(cur.l);
|
||||||
|
cur.r = maintain(cur.r);
|
||||||
|
cur = maintain(cur);
|
||||||
|
} else if (rightRightSize > leftSize) {
|
||||||
|
cur = leftRotate(cur);
|
||||||
|
cur.l = maintain(cur.l);
|
||||||
|
cur = maintain(cur);
|
||||||
|
} else if (rightLeftSize > leftSize) {
|
||||||
|
cur.r = rightRotate(cur.r);
|
||||||
|
cur = leftRotate(cur);
|
||||||
|
cur.l = maintain(cur.l);
|
||||||
|
cur.r = maintain(cur.r);
|
||||||
|
cur = maintain(cur);
|
||||||
|
}
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SBTNode<K> findLastIndex(K key) {
|
||||||
|
SBTNode<K> pre = root;
|
||||||
|
SBTNode<K> cur = root;
|
||||||
|
while (cur != null) {
|
||||||
|
pre = cur;
|
||||||
|
if (key.compareTo(cur.key) == 0) {
|
||||||
|
break;
|
||||||
|
} else if (key.compareTo(cur.key) < 0) {
|
||||||
|
cur = cur.l;
|
||||||
|
} else {
|
||||||
|
cur = cur.r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pre;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SBTNode<K> add(SBTNode<K> cur, K key) {
|
||||||
|
if (cur == null) {
|
||||||
|
return new SBTNode<K>(key);
|
||||||
|
} else {
|
||||||
|
cur.size++;
|
||||||
|
if (key.compareTo(cur.key) < 0) {
|
||||||
|
cur.l = add(cur.l, key);
|
||||||
|
} else {
|
||||||
|
cur.r = add(cur.r, key);
|
||||||
|
}
|
||||||
|
return maintain(cur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SBTNode<K> delete(SBTNode<K> cur, K key) {
|
||||||
|
cur.size--;
|
||||||
|
if (key.compareTo(cur.key) > 0) {
|
||||||
|
cur.r = delete(cur.r, key);
|
||||||
|
} else if (key.compareTo(cur.key) < 0) {
|
||||||
|
cur.l = delete(cur.l, key);
|
||||||
|
} else {
|
||||||
|
if (cur.l == null && cur.r == null) {
|
||||||
|
// free cur memory -> C++
|
||||||
|
cur = null;
|
||||||
|
} else if (cur.l == null && cur.r != null) {
|
||||||
|
// free cur memory -> C++
|
||||||
|
cur = cur.r;
|
||||||
|
} else if (cur.l != null && cur.r == null) {
|
||||||
|
// free cur memory -> C++
|
||||||
|
cur = cur.l;
|
||||||
|
} else {
|
||||||
|
SBTNode<K> pre = null;
|
||||||
|
SBTNode<K> des = cur.r;
|
||||||
|
des.size--;
|
||||||
|
while (des.l != null) {
|
||||||
|
pre = des;
|
||||||
|
des = des.l;
|
||||||
|
des.size--;
|
||||||
|
}
|
||||||
|
if (pre != null) {
|
||||||
|
pre.l = des.r;
|
||||||
|
des.r = cur.r;
|
||||||
|
}
|
||||||
|
des.l = cur.l;
|
||||||
|
des.size = des.l.size + (des.r == null ? 0 : des.r.size) + 1;
|
||||||
|
// free cur memory -> C++
|
||||||
|
cur = des;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SBTNode<K> getIndex(SBTNode<K> cur, int kth) {
|
||||||
|
if (kth == (cur.l != null ? cur.l.size : 0) + 1) {
|
||||||
|
return cur;
|
||||||
|
} else if (kth <= (cur.l != null ? cur.l.size : 0)) {
|
||||||
|
return getIndex(cur.l, kth);
|
||||||
|
} else {
|
||||||
|
return getIndex(cur.r, kth - (cur.l != null ? cur.l.size : 0) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return root == null ? 0 : root.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean containsKey(K key) {
|
||||||
|
if (key == null) {
|
||||||
|
throw new RuntimeException("invalid parameter.");
|
||||||
|
}
|
||||||
|
SBTNode<K> lastNode = findLastIndex(key);
|
||||||
|
return lastNode != null && key.compareTo(lastNode.key) == 0 ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(K key) {
|
||||||
|
if (key == null) {
|
||||||
|
throw new RuntimeException("invalid parameter.");
|
||||||
|
}
|
||||||
|
SBTNode<K> lastNode = findLastIndex(key);
|
||||||
|
if (lastNode == null || key.compareTo(lastNode.key) != 0) {
|
||||||
|
root = add(root, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(K key) {
|
||||||
|
if (key == null) {
|
||||||
|
throw new RuntimeException("invalid parameter.");
|
||||||
|
}
|
||||||
|
if (containsKey(key)) {
|
||||||
|
root = delete(root, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public K getIndexKey(int index) {
|
||||||
|
if (index < 0 || index >= this.size()) {
|
||||||
|
throw new RuntimeException("invalid parameter.");
|
||||||
|
}
|
||||||
|
return getIndex(root, index + 1).key;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Node implements Comparable<Node> {
|
||||||
|
public int index;
|
||||||
|
public int value;
|
||||||
|
|
||||||
|
public Node(int i, int v) {
|
||||||
|
index = i;
|
||||||
|
value = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Node o) {
|
||||||
|
return value != o.value ? Integer.valueOf(value).compareTo(o.value)
|
||||||
|
: Integer.valueOf(index).compareTo(o.index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double[] medianSlidingWindow(int[] nums, int k) {
|
||||||
|
SizeBalancedTreeMap<Node> map = new SizeBalancedTreeMap<>();
|
||||||
|
for (int i = 0; i < k - 1; i++) {
|
||||||
|
map.add(new Node(i, nums[i]));
|
||||||
|
}
|
||||||
|
double[] ans = new double[nums.length - k + 1];
|
||||||
|
int index = 0;
|
||||||
|
for (int i = k - 1; i < nums.length; i++) {
|
||||||
|
map.add(new Node(i, nums[i]));
|
||||||
|
if (map.size() % 2 == 0) {
|
||||||
|
Node upmid = map.getIndexKey(map.size() / 2 - 1);
|
||||||
|
Node downmid = map.getIndexKey(map.size() / 2);
|
||||||
|
ans[index++] = ((double) upmid.value + (double) downmid.value) / 2;
|
||||||
|
} else {
|
||||||
|
Node mid = map.getIndexKey(map.size() / 2);
|
||||||
|
ans[index++] = (double) mid.value;
|
||||||
|
}
|
||||||
|
map.remove(new Node(i - k + 1, nums[i - k + 1]));
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,408 @@
|
|||||||
|
package class37;
|
||||||
|
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import class35.Code01_AVLTreeMap.AVLTreeMap;
|
||||||
|
import class36.Code01_SizeBalancedTreeMap.SizeBalancedTreeMap;
|
||||||
|
import class36.Code02_SkipListMap.SkipListMap;
|
||||||
|
|
||||||
|
// 本文件为avl、sbt、skiplist三种结构的测试文件
|
||||||
|
public class Compare {
|
||||||
|
|
||||||
|
public static void functionTest() {
|
||||||
|
System.out.println("功能测试开始");
|
||||||
|
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
|
||||||
|
AVLTreeMap<Integer, Integer> avl = new AVLTreeMap<>();
|
||||||
|
SizeBalancedTreeMap<Integer, Integer> sbt = new SizeBalancedTreeMap<>();
|
||||||
|
SkipListMap<Integer, Integer> skip = new SkipListMap<>();
|
||||||
|
int maxK = 500;
|
||||||
|
int maxV = 50000;
|
||||||
|
int testTime = 1000000;
|
||||||
|
for (int i = 0; i < testTime; i++) {
|
||||||
|
int addK = (int) (Math.random() * maxK);
|
||||||
|
int addV = (int) (Math.random() * maxV);
|
||||||
|
treeMap.put(addK, addV);
|
||||||
|
avl.put(addK, addV);
|
||||||
|
sbt.put(addK, addV);
|
||||||
|
skip.put(addK, addV);
|
||||||
|
|
||||||
|
int removeK = (int) (Math.random() * maxK);
|
||||||
|
treeMap.remove(removeK);
|
||||||
|
avl.remove(removeK);
|
||||||
|
sbt.remove(removeK);
|
||||||
|
skip.remove(removeK);
|
||||||
|
|
||||||
|
int querryK = (int) (Math.random() * maxK);
|
||||||
|
if (treeMap.containsKey(querryK) != avl.containsKey(querryK)
|
||||||
|
|| sbt.containsKey(querryK) != skip.containsKey(querryK)
|
||||||
|
|| treeMap.containsKey(querryK) != sbt.containsKey(querryK)) {
|
||||||
|
System.out.println("containsKey Oops");
|
||||||
|
System.out.println(treeMap.containsKey(querryK));
|
||||||
|
System.out.println(avl.containsKey(querryK));
|
||||||
|
System.out.println(sbt.containsKey(querryK));
|
||||||
|
System.out.println(skip.containsKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (treeMap.containsKey(querryK)) {
|
||||||
|
int v1 = treeMap.get(querryK);
|
||||||
|
int v2 = avl.get(querryK);
|
||||||
|
int v3 = sbt.get(querryK);
|
||||||
|
int v4 = skip.get(querryK);
|
||||||
|
if (v1 != v2 || v3 != v4 || v1 != v3) {
|
||||||
|
System.out.println("get Oops");
|
||||||
|
System.out.println(treeMap.get(querryK));
|
||||||
|
System.out.println(avl.get(querryK));
|
||||||
|
System.out.println(sbt.get(querryK));
|
||||||
|
System.out.println(skip.get(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Integer f1 = treeMap.floorKey(querryK);
|
||||||
|
Integer f2 = avl.floorKey(querryK);
|
||||||
|
Integer f3 = sbt.floorKey(querryK);
|
||||||
|
Integer f4 = skip.floorKey(querryK);
|
||||||
|
if (f1 == null && (f2 != null || f3 != null || f4 != null)) {
|
||||||
|
System.out.println("floorKey Oops");
|
||||||
|
System.out.println(treeMap.floorKey(querryK));
|
||||||
|
System.out.println(avl.floorKey(querryK));
|
||||||
|
System.out.println(sbt.floorKey(querryK));
|
||||||
|
System.out.println(skip.floorKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null && (f2 == null || f3 == null || f4 == null)) {
|
||||||
|
System.out.println("floorKey Oops");
|
||||||
|
System.out.println(treeMap.floorKey(querryK));
|
||||||
|
System.out.println(avl.floorKey(querryK));
|
||||||
|
System.out.println(sbt.floorKey(querryK));
|
||||||
|
System.out.println(skip.floorKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null) {
|
||||||
|
int ans1 = f1;
|
||||||
|
int ans2 = f2;
|
||||||
|
int ans3 = f3;
|
||||||
|
int ans4 = f4;
|
||||||
|
if (ans1 != ans2 || ans3 != ans4 || ans1 != ans3) {
|
||||||
|
System.out.println("floorKey Oops");
|
||||||
|
System.out.println(treeMap.floorKey(querryK));
|
||||||
|
System.out.println(avl.floorKey(querryK));
|
||||||
|
System.out.println(sbt.floorKey(querryK));
|
||||||
|
System.out.println(skip.floorKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f1 = treeMap.ceilingKey(querryK);
|
||||||
|
f2 = avl.ceilingKey(querryK);
|
||||||
|
f3 = sbt.ceilingKey(querryK);
|
||||||
|
f4 = skip.ceilingKey(querryK);
|
||||||
|
if (f1 == null && (f2 != null || f3 != null || f4 != null)) {
|
||||||
|
System.out.println("ceilingKey Oops");
|
||||||
|
System.out.println(treeMap.ceilingKey(querryK));
|
||||||
|
System.out.println(avl.ceilingKey(querryK));
|
||||||
|
System.out.println(sbt.ceilingKey(querryK));
|
||||||
|
System.out.println(skip.ceilingKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null && (f2 == null || f3 == null || f4 == null)) {
|
||||||
|
System.out.println("ceilingKey Oops");
|
||||||
|
System.out.println(treeMap.ceilingKey(querryK));
|
||||||
|
System.out.println(avl.ceilingKey(querryK));
|
||||||
|
System.out.println(sbt.ceilingKey(querryK));
|
||||||
|
System.out.println(skip.ceilingKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null) {
|
||||||
|
int ans1 = f1;
|
||||||
|
int ans2 = f2;
|
||||||
|
int ans3 = f3;
|
||||||
|
int ans4 = f4;
|
||||||
|
if (ans1 != ans2 || ans3 != ans4 || ans1 != ans3) {
|
||||||
|
System.out.println("ceilingKey Oops");
|
||||||
|
System.out.println(treeMap.ceilingKey(querryK));
|
||||||
|
System.out.println(avl.ceilingKey(querryK));
|
||||||
|
System.out.println(sbt.ceilingKey(querryK));
|
||||||
|
System.out.println(skip.ceilingKey(querryK));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer f1 = treeMap.firstKey();
|
||||||
|
Integer f2 = avl.firstKey();
|
||||||
|
Integer f3 = sbt.firstKey();
|
||||||
|
Integer f4 = skip.firstKey();
|
||||||
|
if (f1 == null && (f2 != null || f3 != null || f4 != null)) {
|
||||||
|
System.out.println("firstKey Oops");
|
||||||
|
System.out.println(treeMap.firstKey());
|
||||||
|
System.out.println(avl.firstKey());
|
||||||
|
System.out.println(sbt.firstKey());
|
||||||
|
System.out.println(skip.firstKey());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null && (f2 == null || f3 == null || f4 == null)) {
|
||||||
|
System.out.println("firstKey Oops");
|
||||||
|
System.out.println(treeMap.firstKey());
|
||||||
|
System.out.println(avl.firstKey());
|
||||||
|
System.out.println(sbt.firstKey());
|
||||||
|
System.out.println(skip.firstKey());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null) {
|
||||||
|
int ans1 = f1;
|
||||||
|
int ans2 = f2;
|
||||||
|
int ans3 = f3;
|
||||||
|
int ans4 = f4;
|
||||||
|
if (ans1 != ans2 || ans3 != ans4 || ans1 != ans3) {
|
||||||
|
System.out.println("firstKey Oops");
|
||||||
|
System.out.println(treeMap.firstKey());
|
||||||
|
System.out.println(avl.firstKey());
|
||||||
|
System.out.println(sbt.firstKey());
|
||||||
|
System.out.println(skip.firstKey());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f1 = treeMap.lastKey();
|
||||||
|
f2 = avl.lastKey();
|
||||||
|
f3 = sbt.lastKey();
|
||||||
|
f4 = skip.lastKey();
|
||||||
|
if (f1 == null && (f2 != null || f3 != null || f4 != null)) {
|
||||||
|
System.out.println("lastKey Oops");
|
||||||
|
System.out.println(treeMap.lastKey());
|
||||||
|
System.out.println(avl.lastKey());
|
||||||
|
System.out.println(sbt.lastKey());
|
||||||
|
System.out.println(skip.lastKey());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null && (f2 == null || f3 == null || f4 == null)) {
|
||||||
|
System.out.println("firstKey Oops");
|
||||||
|
System.out.println(treeMap.lastKey());
|
||||||
|
System.out.println(avl.lastKey());
|
||||||
|
System.out.println(sbt.lastKey());
|
||||||
|
System.out.println(skip.lastKey());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (f1 != null) {
|
||||||
|
int ans1 = f1;
|
||||||
|
int ans2 = f2;
|
||||||
|
int ans3 = f3;
|
||||||
|
int ans4 = f4;
|
||||||
|
if (ans1 != ans2 || ans3 != ans4 || ans1 != ans3) {
|
||||||
|
System.out.println("lastKey Oops");
|
||||||
|
System.out.println(treeMap.lastKey());
|
||||||
|
System.out.println(avl.lastKey());
|
||||||
|
System.out.println(sbt.lastKey());
|
||||||
|
System.out.println(skip.lastKey());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (treeMap.size() != avl.size() || sbt.size() != skip.size() || treeMap.size() != sbt.size()) {
|
||||||
|
System.out.println("size Oops");
|
||||||
|
System.out.println(treeMap.size());
|
||||||
|
System.out.println(avl.size());
|
||||||
|
System.out.println(sbt.size());
|
||||||
|
System.out.println(skip.size());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("功能测试结束");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void performanceTest() {
|
||||||
|
System.out.println("性能测试开始");
|
||||||
|
TreeMap<Integer, Integer> treeMap;
|
||||||
|
AVLTreeMap<Integer, Integer> avl;
|
||||||
|
SizeBalancedTreeMap<Integer, Integer> sbt;
|
||||||
|
SkipListMap<Integer, Integer> skip;
|
||||||
|
long start;
|
||||||
|
long end;
|
||||||
|
int max = 1000000;
|
||||||
|
treeMap = new TreeMap<>();
|
||||||
|
avl = new AVLTreeMap<>();
|
||||||
|
sbt = new SizeBalancedTreeMap<>();
|
||||||
|
skip = new SkipListMap<>();
|
||||||
|
System.out.println("顺序递增加入测试,数据规模 : " + max);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
treeMap.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("treeMap 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
avl.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("avl 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
sbt.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("sbt 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
skip.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("skip 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
System.out.println("顺序递增删除测试,数据规模 : " + max);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
treeMap.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("treeMap 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
avl.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("avl 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
sbt.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("sbt 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
skip.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("skip 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
System.out.println("顺序递减加入测试,数据规模 : " + max);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
treeMap.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("treeMap 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
avl.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("avl 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
sbt.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("sbt 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
skip.put(i, i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("skip 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
System.out.println("顺序递减删除测试,数据规模 : " + max);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
treeMap.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("treeMap 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
avl.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("avl 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
sbt.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("sbt 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
skip.remove(i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("skip 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
System.out.println("随机加入测试,数据规模 : " + max);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
treeMap.put((int) (Math.random() * i), i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("treeMap 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
avl.put((int) (Math.random() * i), i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("avl 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
sbt.put((int) (Math.random() * i), i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("sbt 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
skip.put((int) (Math.random() * i), i);
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("skip 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
System.out.println("随机删除测试,数据规模 : " + max);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
treeMap.remove((int) (Math.random() * i));
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("treeMap 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
avl.remove((int) (Math.random() * i));
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("avl 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
sbt.remove((int) (Math.random() * i));
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("sbt 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
for (int i = max; i >= 0; i--) {
|
||||||
|
skip.remove((int) (Math.random() * i));
|
||||||
|
}
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
System.out.println("skip 运行时间 : " + (end - start) + "ms");
|
||||||
|
|
||||||
|
System.out.println("性能测试结束");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
functionTest();
|
||||||
|
System.out.println("======");
|
||||||
|
performanceTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue