Skip to content

Commit f9b2ee8

Browse files
author
robert
committed
add avltree algorithm
1 parent 26febd6 commit f9b2ee8

File tree

6 files changed

+343
-70
lines changed

6 files changed

+343
-70
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[欢迎访问我的博客](http://blog.csdn.net/robertcpp)<br>[算法系列(一)基本概念](http://blog.csdn.net/robertcpp/article/details/51530518)<br>[算法系列(二)查找算法--基本查找和二分查找](http://blog.csdn.net/robertcpp/article/details/51532593)<br>[算法系列(三)排序算法上篇--冒泡排序插入排序和选择排序](http://blog.csdn.net/robertcpp/article/details/51534034)<br>[算法系列(四)排序算法中篇--归并排序和快速排序](http://blog.csdn.net/robertcpp/article/details/51540976)<br>[算法系列(五)排序算法下篇--如何超越排序算法下界](http://blog.csdn.net/robertcpp/article/details/51559333)<br># simplealgorithm##com.algorithm.math PrimeNumber 素数打表,快速判断一个数是不是素数<br>##com.algorithm.thread TestAsynTreadXunlei 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。<br>##com.algorithm.beanPerson 实现Comparable接口的一个类<br>##com.algorithm.cacheIcache 缓存接口<br>BaseCache 缓存基类<br>FIFOCache FIFO缓存算法实现<br>LRUCache LRU缓存算法实现<br>LFUCache LFU缓存算法实现<br>CacheTest 缓存测试类<br>##com.algorithm.digestDigestUtil 快速计算文件MD5信息,通用获取MD 5文件信息的封装<br>DigestMain MD5测试类<br>##com.algorithm.list MyArrayList 数组线性表的实现<br>MyLinkedList 链表的实现<br>Permutation 简单的组合算法<br>ArrayStack 栈的数组实现<br>LinkedStack 栈的链表实现<br>LinkedQueue 队列的链表实现<br>CircularArrayQueue 队列的循环数组实现<br>##com.algorithm.treeBinarySearch 二分查找的递归实现和非递归实现方式<br>FindAllFile 查找某一目录下的所有文件TreeNode 基本树结构BinaryTree二叉树实现和二叉树的遍历BinarySeachTree 二叉查找树BSTBinarySearchTreeTest 二叉查找树测试##com.algorithm.sortJavaSort Java Collections.sort的两种排序方式<br>Bubblesort 冒泡排序<br>SelectionSort 选择排序<br>InsertionSort 插入排序<br>MergeSort 归并排序<br>QuickSort 快速排序<br>CountSort 计数排序<br>##com.algorithm.graphicsUnionFind 并查集算法<br>关注我的微信公众号,获取更多技术分享,扫一扫下方二维码或搜索微信号le_coding即可关注![我的微信公众号](http://img.my.csdn.net/uploads/201605/08/1462674108_9582.jpg)
1+
[欢迎访问我的博客](http://blog.csdn.net/robertcpp)<br>[算法系列(一)基本概念](http://blog.csdn.net/robertcpp/article/details/51530518)<br>[算法系列(二)查找算法--基本查找和二分查找](http://blog.csdn.net/robertcpp/article/details/51532593)<br>[算法系列(三)排序算法上篇--冒泡排序插入排序和选择排序](http://blog.csdn.net/robertcpp/article/details/51534034)<br>[算法系列(四)排序算法中篇--归并排序和快速排序](http://blog.csdn.net/robertcpp/article/details/51540976)<br>[算法系列(五)排序算法下篇--如何超越排序算法下界](http://blog.csdn.net/robertcpp/article/details/51559333)<br># simplealgorithm##com.algorithm.math PrimeNumber 素数打表,快速判断一个数是不是素数<br>##com.algorithm.thread TestAsynTreadXunlei 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。<br>##com.algorithm.beanPerson 实现Comparable接口的一个类<br>##com.algorithm.cacheIcache 缓存接口<br>BaseCache 缓存基类<br>FIFOCache FIFO缓存算法实现<br>LRUCache LRU缓存算法实现<br>LFUCache LFU缓存算法实现<br>CacheTest 缓存测试类<br>##com.algorithm.digestDigestUtil 快速计算文件MD5信息,通用获取MD 5文件信息的封装<br>DigestMain MD5测试类<br>##com.algorithm.list MyArrayList 数组线性表的实现<br>MyLinkedList 链表的实现<br>Permutation 简单的组合算法<br>ArrayStack 栈的数组实现<br>LinkedStack 栈的链表实现<br>LinkedQueue 队列的链表实现<br>CircularArrayQueue 队列的循环数组实现<br>##com.algorithm.treeBinarySearch 二分查找的递归实现和非递归实现方式<br>FindAllFile 查找某一目录下的所有文件TreeNode 基本树结构BinaryTree二叉树实现和二叉树的遍历BinarySeachTree 二叉查找树BSTAVLTree AVL平衡二叉树BinarySearchTreeTest 二叉查找树测试##com.algorithm.sortJavaSort Java Collections.sort的两种排序方式<br>Bubblesort 冒泡排序<br>SelectionSort 选择排序<br>InsertionSort 插入排序<br>MergeSort 归并排序<br>QuickSort 快速排序<br>CountSort 计数排序<br>##com.algorithm.graphicsUnionFind 并查集算法<br>关注我的微信公众号,获取更多技术分享,扫一扫下方二维码或搜索微信号le_coding即可关注![我的微信公众号](http://img.my.csdn.net/uploads/201605/08/1462674108_9582.jpg)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.algorithm.tree;
2+
3+
import java.util.Comparator;
4+
5+
/**
6+
* AVL树
7+
*
8+
* @author chao
9+
*
10+
* @param <T>
11+
*/
12+
public class AVLTree<T> extends BinarySeachTree<T> {
13+
14+
public AVLTree(T root) {
15+
super(root);
16+
}
17+
18+
public AVLTree(T root, Comparator<T> comparator) {
19+
super(root, comparator);
20+
}
21+
22+
@Override
23+
protected BinaryTreeNode<T> insert(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
24+
if (cur == null) {
25+
return x;
26+
}
27+
int compareresult = compare(cur, x);
28+
if (compareresult < 0) {
29+
cur.right = insert(cur.right, x);
30+
if (getHeight(cur.right) - getHeight(cur.left) == 2) {
31+
if (compare(x, cur.right) > 0) {
32+
cur = roateWithRightChild(cur);
33+
} else {
34+
cur = doubleWithRightChild(cur);
35+
}
36+
}
37+
} else if (compareresult > 0) {
38+
cur.left = insert(cur.left, x);
39+
if (getHeight(cur.left) - getHeight(cur.right) == 2) {
40+
if (compare(x, cur.left) < 0) {
41+
cur = roateWithLeftChild(cur);
42+
} else {
43+
cur = doubleWithLeftChild(cur);
44+
}
45+
}
46+
} else {
47+
cur.hintcount++;
48+
}
49+
return cur;
50+
}
51+
52+
/**
53+
* 左子树单旋转
54+
*
55+
* @param node
56+
* @return
57+
*/
58+
protected BinaryTreeNode<T> roateWithLeftChild(BinaryTreeNode<T> k2) {
59+
BinaryTreeNode<T> k1 = k2.left;
60+
k2.left = k1.right;
61+
k1.right = k2;
62+
return k1;
63+
64+
}
65+
66+
/**
67+
* 右子树单旋转
68+
*
69+
* @param node
70+
* @return
71+
*/
72+
protected BinaryTreeNode<T> roateWithRightChild(BinaryTreeNode<T> k2) {
73+
BinaryTreeNode<T> k1 = k2.right;
74+
k2.right = k1.left;
75+
k1.left = k2;
76+
return k1;
77+
}
78+
79+
/**
80+
* 左子树双旋转
81+
*
82+
* @param node
83+
* @return
84+
*/
85+
protected BinaryTreeNode<T> doubleWithLeftChild(BinaryTreeNode<T> k3) {
86+
k3.left = roateWithRightChild(k3.left);
87+
return roateWithLeftChild(k3);
88+
89+
}
90+
91+
/**
92+
* 右子树双旋转
93+
*
94+
* @param node
95+
* @return
96+
*/
97+
protected BinaryTreeNode<T> doubleWithRightChild(BinaryTreeNode<T> k3) {
98+
k3.right = roateWithLeftChild(k3.right);
99+
return roateWithRightChild(k3);
100+
}
101+
}

src/com/algorithm/tree/BinarySeachTree.java

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,33 @@
1212
public class BinarySeachTree<T> extends BinaryTree<T> {
1313
private Comparator<T> comparator;
1414

15-
public BinarySeachTree(BinarySeachTree.BinaryTreeNode<T> root) {
15+
public BinarySeachTree(T root) {
1616
this(root, null);
1717
}
1818

19-
public BinarySeachTree(BinarySeachTree.BinaryTreeNode<T> root, Comparator<T> comparator) {
19+
public BinarySeachTree(T root, Comparator<T> comparator) {
2020
super(root);
2121
this.comparator = comparator;
2222
}
2323

24-
private int compare(BinarySeachTree.BinaryTreeNode<T> a, BinarySeachTree.BinaryTreeNode<T> b) {
24+
protected int compare(BinarySeachTree.BinaryTreeNode<T> a, BinarySeachTree.BinaryTreeNode<T> b) {
2525
if (comparator != null) {
2626
return comparator.compare(a.element, b.element);
2727
} else if (a.element instanceof Comparable && b.element instanceof Comparable) {
2828
return ((Comparable) a.element).compareTo(b.element);
2929
} else {
3030
throw new RuntimeException("can't compare " + a.element.getClass());
3131
}
32+
}
3233

34+
protected int compare(T a, T b) {
35+
if (comparator != null) {
36+
return comparator.compare(a, b);
37+
} else if (a instanceof Comparable && b instanceof Comparable) {
38+
return ((Comparable) a).compareTo(b);
39+
} else {
40+
throw new RuntimeException("can't compare " + a.getClass() + " " + b.getClass());
41+
}
3342
}
3443

3544
/**
@@ -38,8 +47,8 @@ private int compare(BinarySeachTree.BinaryTreeNode<T> a, BinarySeachTree.BinaryT
3847
* @param x
3948
* @return
4049
*/
41-
public boolean contains(BinaryTreeNode<T> x) {
42-
return contains(root, x);
50+
public boolean contains(T x) {
51+
return contains(root, new BinaryTreeNode<T>(x));
4352
}
4453

4554
/**
@@ -49,8 +58,8 @@ public boolean contains(BinaryTreeNode<T> x) {
4958
* @param x
5059
* @return
5160
*/
52-
public boolean contains(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
53-
if (x == null || cur == null) {
61+
protected boolean contains(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
62+
if (x == null || cur == null || cur.hintcount < 0) {
5463
return false;
5564
}
5665
int compareresult = compare(cur, x);
@@ -63,14 +72,38 @@ public boolean contains(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
6372
}
6473
}
6574

75+
/**
76+
* 查找节点
77+
*
78+
* @param cur
79+
* @param x
80+
* @return
81+
*/
82+
protected BinaryTreeNode<T> findNode(BinaryTreeNode<T> cur, T x) {
83+
if (x == null || cur == null || cur.hintcount < 0) {
84+
return null;
85+
}
86+
int compareresult = compare(cur.element, x);
87+
if (compareresult == 0) {
88+
return cur;
89+
} else if (compareresult < 0) {
90+
return findNode(cur.right, x);
91+
} else {
92+
return findNode(cur.left, x);
93+
}
94+
}
95+
6696
/**
6797
* 从根节点插入一个新数据
6898
*
6999
* @param x
70100
* @return
71101
*/
72-
public BinaryTreeNode<T> insert(BinaryTreeNode<T> x) {
73-
return insert(root, x);
102+
public void insert(T x) {
103+
if (x == null) {
104+
return;
105+
}
106+
insert(root, new BinaryTreeNode<T>(x));
74107
}
75108

76109
/**
@@ -89,6 +122,8 @@ protected BinaryTreeNode<T> insert(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
89122
cur.right = insert(cur.right, x);
90123
} else if (compareresult > 0) {
91124
cur.left = insert(cur.left, x);
125+
} else {
126+
cur.hintcount++;
92127
}
93128
return cur;
94129
}
@@ -98,8 +133,8 @@ protected BinaryTreeNode<T> insert(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
98133
*
99134
* @return
100135
*/
101-
public BinaryTreeNode<T> findMin() {
102-
return findMin(root);
136+
public T findMin() {
137+
return findMin(root).element;
103138
}
104139

105140
/**
@@ -108,7 +143,7 @@ public BinaryTreeNode<T> findMin() {
108143
* @param node
109144
* @return
110145
*/
111-
public BinaryTreeNode<T> findMin(BinaryTreeNode<T> node) {
146+
protected BinaryTreeNode<T> findMin(BinaryTreeNode<T> node) {
112147
if (node == null) {
113148
return null;
114149
} else if (node.left == null) {
@@ -122,8 +157,8 @@ public BinaryTreeNode<T> findMin(BinaryTreeNode<T> node) {
122157
*
123158
* @return
124159
*/
125-
public BinaryTreeNode<T> findMax() {
126-
return findMax(root);
160+
public T findMax() {
161+
return findMax(root).element;
127162
}
128163

129164
/**
@@ -132,7 +167,7 @@ public BinaryTreeNode<T> findMax() {
132167
* @param node
133168
* @return
134169
*/
135-
public BinaryTreeNode<T> findMax(BinaryTreeNode<T> node) {
170+
protected BinaryTreeNode<T> findMax(BinaryTreeNode<T> node) {
136171
if (node == null) {
137172
return null;
138173
} else if (node.right == null) {
@@ -147,8 +182,26 @@ public BinaryTreeNode<T> findMax(BinaryTreeNode<T> node) {
147182
* @param x
148183
* @return
149184
*/
150-
public BinaryTreeNode<T> remove(BinaryTreeNode<T> x) {
151-
return remove(root, x);
185+
public void remove(T x) {
186+
BinaryTreeNode<T> node = findNode(root, x);
187+
if (node == null) {
188+
return;
189+
} else if (node.hintcount <= 0) {
190+
remove(root, new BinaryTreeNode<T>(x));
191+
} else {
192+
lazyRemove(node);
193+
}
194+
}
195+
196+
public void lazyRemove(T x) {
197+
BinaryTreeNode<T> node = findNode(root, x);
198+
if (node != null) {
199+
node.hintcount--;
200+
}
201+
}
202+
203+
protected void lazyRemove(BinaryTreeNode<T> x) {
204+
x.hintcount--;
152205
}
153206

154207
/**
@@ -158,7 +211,7 @@ public BinaryTreeNode<T> remove(BinaryTreeNode<T> x) {
158211
* @param x
159212
* @return
160213
*/
161-
public BinaryTreeNode<T> remove(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
214+
protected BinaryTreeNode<T> remove(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
162215
if (cur == null || x == null) {
163216
return cur;
164217
}

src/com/algorithm/tree/BinarySearchTreeTest.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)