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
|
/**
* 平衡二叉树
*/
class AVLTree {
AVLTreeNode head;
public AVLTreeNode add(int value) {
head = insert(head, value);
return head;
}
private AVLTreeNode insert(AVLTreeNode root, int val) {
if (root == null) {
root = new AVLTreeNode(val);
root.height = 0;
return root;
}
if (val < root.val) {
//左侧插入
root.left = insert(root.left, val);
} else if (val > root.val) {
//右侧插入
root.right = insert(root.right, val);
} else {
//更新值
root.val = val;
}
//检查失衡,左右节点的高度差绝对值大于 2 即为失衡
if (Math.abs(Height(root.left) - Height(root.right)) >= 2) {
//当左边树高时可能为 LL 型或 LR 型
if (Height(root.left) > Height(root.right)) {
//当新插入的值比 root.left 值小时为 LL 型,比 root.left 值大时为 LR 型
if (val < root.left.val) {
root = LL(root);
} else if (val > root.left.val) {
root = LR(root);
}
} else if (Height(root.right) > Height(root.left)) {
//当右边树高时可能为 RR 型或 RL 型
//当新插入的值比 root.right 值大时为 RR 型,比 root.right 值小时为 RL 型
if (val > root.right.val) {
root = RR(root);
} else if (val < root.right.val) {
root = RL(root);
}
}
}
root.height = Math.max(Height(root.left), Height(root.right)) + 1;
return root;
}
/*四种类型转换*/
public AVLTreeNode LL(AVLTreeNode node) {
//反转结构
AVLTreeNode result = node.left;
node.left = result.right;
result.right = node;
//修改高度
node.height = Math.max(Height(node.left), Height(node.right)) + 1;
result.height = Math.max(Height(result.left), Height(result.right)) + 1;
return result;
}
public AVLTreeNode LR(AVLTreeNode node) {
AVLTreeNode result = node.left.right;
node.left.right = result.left;
result.left = node.left;
node.left = result.right;
result.right = node;
//修改高度
node.height = Math.max(Height(node.left), Height(node.right)) + 1;
result.height = Math.max(Height(result.left), Height(result.right)) + 1;
return result;
}
public AVLTreeNode RL(AVLTreeNode node) {
AVLTreeNode result = node.right.left;
node.right.left = result.right;
result.right = node.right;
node.right = result.left;
result.left = node;
//修改高度
node.height = Math.max(Height(node.left), Height(node.right)) + 1;
result.height = Math.max(Height(result.left), Height(result.right)) + 1;
return result;
}
private AVLTreeNode RR(AVLTreeNode node) {
AVLTreeNode result = node.right;
node.right = result.left;
result.left = node;
//修改高度
node.height = Math.max(Height(node.left), Height(node.right)) + 1;
result.height = Math.max(Height(result.left), Height(result.right)) + 1;
return result;
}
// 中序遍历
public void print(AVLTreeNode node) {
if (node == null) {
return;
}
print(node.left);
System.out.print(node.val + " ");
print(node.right);
}
private int Height(AVLTreeNode avlTreeNode) {
if (avlTreeNode == null) {
return -1;
} else {
return avlTreeNode.height;
}
}
}
/**
* 二叉平衡树的数据类型
*/
class AVLTreeNode {
int val;
int height = -1;
AVLTreeNode left;
AVLTreeNode right;
public AVLTreeNode() {
}
public AVLTreeNode(int val) {
this.val = val;
}
}
|