Skip to content

Commit 1a2740f

Browse files
committed
[Function add]
1.Balanced binary tree
1 parent 4ce42eb commit 1a2740f

File tree

4 files changed

+153
-2
lines changed

4 files changed

+153
-2
lines changed

Diff for: Algorithm(4th_Edition)/src/ca/mcmaster/chapter/three/bitree/BinaryTree.java renamed to Algorithm(4th_Edition)/Notes/Tree/BinaryTree.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package ca.mcmaster.chapter.three.bitree;
2-
31
public class BinaryTree<K extends Comparable<K>, V> {
42
protected class Node{
53
protected Node left, right;
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
public class CompleteBinaryTree<K extends Comparable<K>> {
2+
private Object[] arr; //因为无法实现泛型数组,我们通过Object数组替代。第一个位置不存储元素
3+
private int N; //树中元素的个数。
4+
public CompleteBinaryTree(int N) {
5+
arr = new Object[N];
6+
}
7+
private void swap(int i, int j){ //交换两个元素的位置
8+
Object temp = arr[i];
9+
arr[i] = arr[j];
10+
arr[j] = temp;
11+
}
12+
@SuppressWarnings("unchecked")
13+
private boolean less(Integer i, Integer j){
14+
return ((K)arr[i]).compareTo(((K)arr[j])) < 0;
15+
}
16+
private void swin(int k){
17+
while(k > 1 && less(k/2, k)){
18+
swap(k/2, k);
19+
k /= 2;
20+
}
21+
}
22+
public void insert(K k){
23+
arr[++N] = k;
24+
swin(N);
25+
}
26+
public Integer size(){
27+
return N;
28+
}
29+
@SuppressWarnings("unchecked")
30+
public K get(int i){
31+
return (K)arr[i];
32+
}
33+
private void sink(int i){
34+
while(i * 2 <= N){
35+
int j = i * 2;
36+
if(j + 1 < N && less(j, j+1)) j++; //取两个子结点中更大的一个。
37+
swap(i, j);
38+
i = j; //在当前子树中继续进行下沉
39+
}
40+
}
41+
42+
public K delMax(){
43+
K max = (K)arr[1];
44+
swap(1, N + 1);
45+
arr[N + 1] = null;
46+
sink(1);
47+
N--;
48+
return max;
49+
}
50+
51+
public static void main(String[] args) {
52+
CompleteBinaryTree<Integer> cbtree = new CompleteBinaryTree<>(100);
53+
cbtree.insert(1);
54+
cbtree.insert(2);
55+
cbtree.insert(3);
56+
cbtree.insert(4);
57+
cbtree.insert(5);
58+
cbtree.insert(6);
59+
cbtree.delMax();
60+
for(int i = 1; i <= cbtree.size(); i++){
61+
System.out.println(cbtree.get(i));
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 平衡二叉树Balanced Binary Tree
2+
>平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树。平衡二叉树要求对于每一个节点来说,它的左右子树的高度之差不能超过1,如果插入或者删除一个节点使得高度之差大于1,就要进行节点之间的旋转,将二叉树重新维持在一个平衡状态。这个方案很好的解决了二叉查找树退化成链表的问题,把插入,查找,删除的时间复杂度最好情况和最坏情况都维持在O(logN)。但是频繁旋转会使插入和删除牺牲掉O(logN)左右的时间,不过相对二叉查找树来说,时间上稳定了很多。
3+
4+
## 定义
5+
>平衡二叉树定义(AVL):它或者是一颗空树,或者具有以下性质的二叉树:**它的左子树和右子树的深度之差(平衡因子)的绝对值不超过1**,且它的左子树和右子树都是一颗平衡二叉树。
6+
7+
* 旋转
8+
>通过对树进行简单的修复来让其重新恢复到平衡,而这样的简单操作我们就称之为旋转,当然旋转也有单旋转和双旋转之分。
9+
假设结点X是失衡点,它必须重新恢复平衡,由于任意结点的孩子结点最多有两个,而且导致失衡的必要条件是X结点的两棵子树高度差为2(大于1),因此一般只有以下4种情况可能导致X点失去平衡:
10+
① 在结点X的左孩子结点的左子树中插入元素
11+
② 在结点X的左孩子结点的右子树中插入元素
12+
③ 在结点X的右孩子结点的左子树中插入元素
13+
④ 在结点X的右孩子结点的右子树中插入元素
14+
15+
* 高度
16+
>高度是指当前结点到叶子结点的最长路径,如所有叶子结点的高度都为0。
17+
![height](https://i.imgur.com/dA8OWAs.png)
18+
19+
* 左左单旋转(LL)情景①分析
20+
![LL](https://i.imgur.com/kpMVlUs.png)
21+
```Java
22+
private AVLNode singleRotateLeft(AVLNode n){
23+
AVLNode left1 = n.left;
24+
n.left = left1.right;
25+
left1.right = n;
26+
left1.height = Math.max(left1.left.height, left1.right.height) + 1;
27+
n.height = Math.max(n.left.height, n.right.height) + 1;
28+
return left1;
29+
}
30+
```
31+
32+
* 右右单旋转(RR)情景④分析
33+
![RR](https://i.imgur.com/CkFlj4X.png)
34+
```Java
35+
private AVLNode singleRotateRight(AVLNode n){
36+
AVLNode right1 = n.right;
37+
n.left = right1.left;
38+
right1.left = n;
39+
right1.height = Math.max(right1.left.height, right1.right.height) + 1;
40+
n.height = Math.max(n.left.height, n.right.height) + 1;
41+
return right1;
42+
}
43+
```
44+
45+
* 平衡二叉树的双旋转算法与实现
46+
* 左右双旋转(LR)情景②分析
47+
![LR](https://i.imgur.com/926XxBS.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ca.mcmaster.chapter.three.bitree;
2+
3+
public class BalancedBinaryTree<V extends Comparable<V>> {
4+
private class AVLNode{
5+
V v;
6+
int height; //当前节点的子树的高度
7+
AVLNode left, right;
8+
public AVLNode(V v, AVLNode left, AVLNode right, int height) {
9+
this.v = v;
10+
this.left = left;
11+
this.right = right;
12+
this.height = height;
13+
}
14+
public AVLNode(V v, AVLNode left, AVLNode right) {
15+
this(v, left, right, 0);
16+
}
17+
public AVLNode(V v) {
18+
this(v, null, null);
19+
}
20+
}
21+
private int height(AVLNode n){
22+
return n.height;
23+
}
24+
25+
private AVLNode singleRotateLeft(AVLNode n){
26+
AVLNode left1 = n.left;
27+
n.left = left1.right;
28+
left1.right = n;
29+
left1.height = Math.max(left1.left.height, left1.right.height) + 1;
30+
n.height = Math.max(n.left.height, n.right.height) + 1;
31+
return left1;
32+
}
33+
34+
private AVLNode singleRotateRight(AVLNode n){
35+
AVLNode right1 = n.right;
36+
n.left = right1.left;
37+
right1.left = n;
38+
right1.height = Math.max(right1.left.height, right1.right.height) + 1;
39+
n.height = Math.max(n.left.height, n.right.height) + 1;
40+
return right1;
41+
}
42+
}

0 commit comments

Comments
 (0)