-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
147 lines (143 loc) · 5.4 KB
/
AVLTree.java
File metadata and controls
147 lines (143 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package myCollections;
import java.util.Comparator;
public class AVLTree<T extends Comparable<T>> {
/*
AVL树:
左右子树高度绝对值最多差1的二叉搜索树
子树也是AVL树
*/
private Node<T> root;
class Node<T extends Comparable<T>>{
T key;
int height;
Node<T> left;
Node<T> right;
public Node(T key, int height, Node<T> left, Node<T> right) {
this.key = key;
this.height = height;
this.left = left;
this.right = right;
}
public Node(T key){
this.key = key;
this.height = 0;
}
}
public int getHeight(Node<T> node){
return node == null ? 0 : node.height;
}
public Node<T> LL(Node<T> node){
//左子树插入节点在左导致不平衡,需要旋转,以下不再赘述
Node<T> leftNode = node.left;
node.left = leftNode.right;
leftNode.right = node;
node.height = Math.max(node.left.height,node.right.height) + 1;
leftNode.height = Math.max(leftNode.left.height, leftNode.right.height) + 1;
return leftNode;
}
public Node<T> RR(Node<T> node) {
Node<T> rightNode;
rightNode = node.right;
node.right = rightNode.left;
rightNode.left = node;
node.height = Math.max( node.left.height, node.right.height) + 1;
rightNode.height = Math.max( rightNode.left.height, rightNode.right.height) + 1;
return rightNode;
}
public Node<T> LR(Node<T> node){
//LR先对左子树RR再对本树LL
node.left = RR(node.left);
return LL(node);
}
public Node<T> RL(Node<T> node){
node.right = LL(node.right);
return RR(node);
}
public Node<T> insert(Node<T> root,T key){
/*
插入:先判断插入点,再判断是否需要翻转
*/
if(root == null){
return new Node<T>(key);
}
else{
if(key.compareTo(root.key)<0)
//T是实现了comparable接口的,所以这里可以比较,但不能用大小于号
{
root.left = insert(root.left,key);
if(root.left.height - root.right.height == 2){
if (key.compareTo(root.left.key) < 0)
root = LL(root);
else
root = LR(root);
}
}else if(key.compareTo(root.key)>0){
root.right = insert(root.right,key);
if(root.left.height - root.right.height == -2){
if (key.compareTo(root.right.key) < 0)
root = RL(root);
else
root = RR(root);
}
}else{
return root;//相同的节点不添加
}
}
return root;
}
public Node<T> delete(Node<T> root,T key){
/*
插入:先判断插入点,再判断是否需要翻转
*/
if(root == null || key == null){
return root;
}
else{
if(key.compareTo(root.key)<0)
//T是实现了comparable接口的,所以这里可以比较,但不能用大小于号
{
root.left = delete(root.left,key);
if(root.left.height - root.right.height == -2){
Node<T> rightNode = root.right;
if (rightNode.right.height>root.left.height)
root = RL(root);
else
root = RR(root);
}
}else if(key.compareTo(root.key)>0){
root.right = delete(root.right,key);
if(root.left.height - root.right.height == 2){
Node<T> leftNode = root.left;
if (leftNode.left.height>leftNode.right.height)
root = LL(root);
else
root = LR(root);
}
}else {
//找到了要删除的节点
if (root.left == null) root = root.right;
else if (root.right == null) root = root.left;
else {
if (root.left.height < root.right.height) {
Node<T> tempNode = root.right;
while (tempNode.left != null) {
tempNode = tempNode.left;
}//为保证二叉树的平衡性、搜索性
//这里选择了高度较高的子树,选取中序遍历与root相邻的节点作为root
root.key = tempNode.key;
delete(tempNode, key);
} else {
Node<T> tempNode = root.left;
while (tempNode.right != null) {
tempNode = tempNode.right;
}//为保证二叉树的平衡性、搜索性
//这里选择了高度较高的子树,选取中序遍历与root相邻的节点作为root
root.key = tempNode.key;
delete(tempNode, key);
}
}
}
}
return root;
}
}