package sy4;
public class Test1 {
public static void main(String[] args) {
// int[] arr = {4, 3, 6, 5, 7, 8};
int[] arr = {10, 11, 7, 6, 8, 9};
C c = new C();
for (int i = 0; i < arr.length; i++) {
c.add(new AvlNode(arr[i]));
}
c.order();
System.out.println();
System.out.println(c.getRoot().height());
System.out.println(c.getRoot().leftHeight());
System.out.println(c.getRoot().rightHeight());
}
}
class C {
private AvlNode root;
public AvlNode getRoot() {
return root;
}
public void add(AvlNode node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void order() {
if (root != null) {
root.order();
} else {
System.out.println("无法遍历");
}
}
public AvlNode search(int val) {
if (root == null) {
return null;
}
return root.search(val);
}
public AvlNode searchParent(int val) {
if (root == null) {
return null;
}
return root.searchParent(val);
}
/**
* 1. 返回的 以node 为根结点的二叉排序树的最小结点的值
* 2. 删除node 为根结点的二叉排序树的最小结点
*
* @param node (二叉树的根节点)
* @return (返回二叉树最小的值)
*/
public int delRightTreeMin(AvlNode node) {
AvlNode target = node;
while (target.left != null) {
target = target.left;
}
delNode(target.val);
return target.val;
}
/**
* 删除节点
*
* @param val
*/
public void delNode(int val) {
if (root == null) {
return;
} else {
AvlNode targetNode = search(val);
if (targetNode == null) {
return;
}
//只有一个节点
if (root.left == null && root.right == null) {
root = null;
return;
}
AvlNode parent = searchParent(val);
//如果是叶子节点
if (targetNode.left == null && targetNode.right == null) {
//判断是parent的左是右
if (parent.left != null && parent.left.val == val) { //是左节点
parent.left = null;
} else if (parent.right != null && parent.right.val == val) { //是右节点
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) { //删除有两颗子树的节点
int minVal = delRightTreeMin(root.right);
targetNode.val = minVal;
} else { //删除只有一个子树的节点
//如过果删除的是左孩子节点
if (targetNode.left != null) {
if (parent != null) {
// 如果 targetNode 是 parent 的左子结点
if (parent.left.val == val) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {
if (parent != null) {
if (parent.right.val == val) {
parent.right = targetNode.right;
} else {
parent.left = targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
}
}
class AvlNode {
int val;
AvlNode left;
AvlNode right;
public AvlNode() {
}
public AvlNode(int val) {
this.val = val;
}
/**
* 添加节点
*
* @param node
*/
public void add(AvlNode node) {
if (node == null) {
return;
}
if (node.val < this.val) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
//右边比左边高
if (rightHeight() - leftHeight() > 1) {
if (right != null && right.leftHeight() > right.rightHeight()) { //当前右子树左边比右边高
right.rightRotate(); //当前右子树右旋
leftRotate(); //整棵树左旋
} else {
leftRotate(); //左旋
}
return;
}
//左比右高
if (leftHeight() - rightHeight() > 1) {
if (left != null && left.rightHeight() > left.leftHeight()) { //左子树的右边比左边高
left.leftRotate(); //左子树左旋转
rightRotate();
} else {
rightRotate();
}
}
}
/**
* 中序遍历
*
* @param
*/
public void order() {
if (this.left != null) {
this.left.order();
}
System.out.print(this.val + " ");
if (this.right != null) {
this.right.order();
}
}
/**
* 左孩子高度
*
* @return
*/
public int leftHeight() {
if (left == null) {
return 0;
}
return left.height();
}
/**
* 求右子树的高度
*
* @return
*/
public int rightHeight() {
if (right == null) {
return 0;
}
return right.height();
}
/**
* 求高度
*
* @return
*/
public int height() {
return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
}
/**
* 找到要删除的节点
*
* @param val
* @return
*/
public AvlNode search(int val) {
if (this.val == val) {
return this;
} else if (val < this.val) {
if (this.left == null) {
return null;
}
return this.left.search(val);
} else {
if (this.right == null) {
return null;
}
return this.right.search(val);
}
}
/**
* 查找要删除的父节点
*
* @param val
* @return
*/
public AvlNode searchParent(int val) {
if (this.left != null && this.left.val == val || this.right != null && this.right.val == val) {
return this;
} else {
if (val < this.val && this.left != null) {
return this.left.searchParent(val);
} else if (val > this.val && this.right != null) {
return this.right.searchParent(val);
} else {
return null;
}
}
}
/**
* 左旋
*/
private void leftRotate() {
AvlNode newNode = new AvlNode(val);
newNode.left = left;
newNode.right = right.left;
val = right.val;
right = right.right;
left = newNode;
}
/**
* 右旋
*/
private void rightRotate() {
//以当前的节点的值创建一个新的节点
AvlNode newNode = new AvlNode(val);
//新的节点的右节点指向当前节点的右节点
newNode.right = right;
//新节点的右节点指向当前左节点的右节点
newNode.left = left.right;
//当前节点的值更改为当前左节点的值
val = left.val;
//当前组左节点更改为当前左节点的左节点
left = left.left;
//右节点更改为新的节点
right = newNode;
}
}
平衡二叉树的旋转
最新推荐文章于 2024-07-19 17:37:28 发布