|
|
|
@ -20,13 +20,15 @@
|
|
|
|
|
|
|
|
|
|
/* 更新结点高度 */
|
|
|
|
|
private void updateHeight(TreeNode node) {
|
|
|
|
|
// 结点高度等于最高子树高度 + 1
|
|
|
|
|
node.height = Math.max(height(node.left), height(node.right)) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取平衡因子 */
|
|
|
|
|
public int balanceFactor(TreeNode node) {
|
|
|
|
|
if (node == null)
|
|
|
|
|
return 0;
|
|
|
|
|
// 空结点平衡因子为 0
|
|
|
|
|
if (node == null) return 0;
|
|
|
|
|
// 结点平衡因子 = 左子树高度 - 右子树高度
|
|
|
|
|
return height(node.left) - height(node.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -34,10 +36,13 @@
|
|
|
|
|
private TreeNode rightRotate(TreeNode node) {
|
|
|
|
|
TreeNode child = node.left;
|
|
|
|
|
TreeNode grandChild = child.right;
|
|
|
|
|
// 以 child 为原点,将 node 向右旋转
|
|
|
|
|
child.right = node;
|
|
|
|
|
node.left = grandChild;
|
|
|
|
|
// 更新结点高度
|
|
|
|
|
updateHeight(node);
|
|
|
|
|
updateHeight(child);
|
|
|
|
|
// 返回旋转后子树的根节点
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -45,17 +50,21 @@
|
|
|
|
|
private TreeNode leftRotate(TreeNode node) {
|
|
|
|
|
TreeNode child = node.right;
|
|
|
|
|
TreeNode grandChild = child.left;
|
|
|
|
|
// 以 child 为原点,将 node 向左旋转
|
|
|
|
|
child.left = node;
|
|
|
|
|
node.right = grandChild;
|
|
|
|
|
// 更新结点高度
|
|
|
|
|
updateHeight(node);
|
|
|
|
|
updateHeight(child);
|
|
|
|
|
// 返回旋转后子树的根节点
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 执行旋转操作,使该子树重新恢复平衡 */
|
|
|
|
|
private TreeNode rotate(TreeNode node) {
|
|
|
|
|
// 获取结点 node 的平衡因子
|
|
|
|
|
int balanceFactor = balanceFactor(node);
|
|
|
|
|
// 根据失衡情况分为四种操作:右旋、左旋、先左后右、先右后左
|
|
|
|
|
// 左偏树
|
|
|
|
|
if (balanceFactor > 1) {
|
|
|
|
|
if (balanceFactor(node.left) >= 0) {
|
|
|
|
|
// 右旋
|
|
|
|
@ -66,6 +75,7 @@
|
|
|
|
|
return rightRotate(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 右偏树
|
|
|
|
|
if (balanceFactor < -1) {
|
|
|
|
|
if (balanceFactor(node.right) <= 0) {
|
|
|
|
|
// 左旋
|
|
|
|
@ -76,6 +86,7 @@
|
|
|
|
|
return leftRotate(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 平衡树,无需旋转,直接返回
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -85,22 +96,20 @@
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 递归插入结点 */
|
|
|
|
|
/* 递归插入结点(辅助函数) */
|
|
|
|
|
private TreeNode insertHelper(TreeNode node, int val) {
|
|
|
|
|
// 1. 查找插入位置,并插入结点
|
|
|
|
|
if (node == null)
|
|
|
|
|
return new TreeNode(val);
|
|
|
|
|
if (node == null) return new TreeNode(val);
|
|
|
|
|
/* 1. 查找插入位置,并插入结点 */
|
|
|
|
|
if (val < node.val)
|
|
|
|
|
node.left = insertHelper(node.left, val);
|
|
|
|
|
else if (val > node.val)
|
|
|
|
|
node.right = insertHelper(node.right, val);
|
|
|
|
|
else
|
|
|
|
|
return node; // 重复结点则直接返回
|
|
|
|
|
// 2. 更新结点高度
|
|
|
|
|
updateHeight(node);
|
|
|
|
|
// 3. 执行旋转操作,使该子树重新恢复平衡
|
|
|
|
|
return node; // 重复结点不插入,直接返回
|
|
|
|
|
updateHeight(node); // 更新结点高度
|
|
|
|
|
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
|
|
|
|
node = rotate(node);
|
|
|
|
|
// 返回该子树的根节点
|
|
|
|
|
// 返回子树的根节点
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -110,11 +119,10 @@
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 递归删除结点 */
|
|
|
|
|
/* 递归删除结点(辅助函数) */
|
|
|
|
|
private TreeNode removeHelper(TreeNode node, int val) {
|
|
|
|
|
// 1. 查找结点,并删除之
|
|
|
|
|
if (node == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (node == null) return null;
|
|
|
|
|
/* 1. 查找结点,并删除之 */
|
|
|
|
|
if (val < node.val)
|
|
|
|
|
node.left = removeHelper(node.left, val);
|
|
|
|
|
else if (val > node.val)
|
|
|
|
@ -135,11 +143,10 @@
|
|
|
|
|
node.val = temp.val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 2. 更新结点高度
|
|
|
|
|
updateHeight(node);
|
|
|
|
|
// 3. 执行旋转操作,使该子树重新恢复平衡
|
|
|
|
|
updateHeight(node); // 更新结点高度
|
|
|
|
|
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
|
|
|
|
node = rotate(node);
|
|
|
|
|
// 返回该子树的根节点
|
|
|
|
|
// 返回子树的根节点
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -159,18 +166,20 @@
|
|
|
|
|
// 循环查找,越过叶结点后跳出
|
|
|
|
|
while (cur != null) {
|
|
|
|
|
// 目标结点在 root 的右子树中
|
|
|
|
|
if (cur.val < val) cur = cur.right;
|
|
|
|
|
if (cur.val < val)
|
|
|
|
|
cur = cur.right;
|
|
|
|
|
// 目标结点在 root 的左子树中
|
|
|
|
|
else if (cur.val > val) cur = cur.left;
|
|
|
|
|
else if (cur.val > val)
|
|
|
|
|
cur = cur.left;
|
|
|
|
|
// 找到目标结点,跳出循环
|
|
|
|
|
else break;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// 返回目标结点
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class avl_tree {
|
|
|
|
|
static void testInsert(AVLTree tree, int val) {
|
|
|
|
|
tree.insert(val);
|
|
|
|
@ -215,4 +224,3 @@
|
|
|
|
|
System.out.println("\n查找到的结点对象为 " + node + ",结点值 = " + node.val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|