平衡二叉树java代码实现
实现以下功能:
- 创建平衡二叉树
- 添加结点,包含左旋,右旋,双旋转添加
- 查找结点
- 删除结点
已将各功能进行排序标号整理,防止过于杂乱。
public class AVLTreeDemo {
public static void main(String[] args) {
int[] arr = {10,11,7,6,8,9};
AVLTree avlTree = new AVLTree();
for (int i = 0; i < arr.length; i++){
avlTree.add(new AVLNode(arr[i]));
}
avlTree.infixOrder();
System.out.println("在平衡处理~~");
System.out.println("树的高度=" + avlTree.getRoot().height());
System.out.println("树的左子树高度=" + avlTree.getRoot().leftHeight());
System.out.println("树的右子树高度=" + avlTree.getRoot().rightHeight());
}
}
class AVLTree {
private AVLNode root;
public AVLNode getRoot() {
return root;
}
public void add(AVLNode node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("空树");
}
}
public AVLNode search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
public AVLNode searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
public void delete(int value) {
if (root == null) {
return;
} else {
AVLNode targetNode = search(value);
if (targetNode == null) {
return;
}
if (root.left == null && root.right == null) {
root = null;
return;
}
AVLNode parent = searchParent(value);
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else if (parent.right != null && parent.right.value == value) {
parent.right = null;
}
}
else if (targetNode.left != null && targetNode.right != null) {
int minVal = delRightTreeMin(targetNode.right);
targetNode.value = minVal;
}
else if (targetNode.left != null || targetNode.right != null) {
if (parent != null) {
if (targetNode.left != null) {
if (parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}
} else {
if (targetNode.left != null) {
root = targetNode.left;
} else {
root = targetNode.right;
}
}
}
}
}
public int delRightTreeMin(AVLNode avlNode) {
AVLNode target = avlNode;
while (target.left != null) {
target = target.left;
}
delete(target.value);
return target.value;
}
}
class AVLNode {
int value;
AVLNode left;
AVLNode right;
public AVLNode(int value) {
this.value = value;
}
public void add(AVLNode node) {
if (node == null) {
return;
}
if (node.value < this.value) {
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(bF() < -1){
if(right.bF() > 0){
right.rightRotate();
}
leftRotate();
}else if (bF() > 1){
if (left.bF() < 0) {
left.leftRotate();
}
rightRotate();
}
}
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
public AVLNode search(int value) {
if (value == this.value) {
return this;
}
if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.search(value);
} else {
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
public AVLNode searchParent(int value) {
if ((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)) {
return this;
} else {
if (value < this.value && this.left != null) {
return this.left.searchParent(value);
} else if (value >= this.value && this.right != null) {
return this.right.searchParent(value);
} else {
return null;
}
}
}
public int height() {
return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
}
public int leftHeight() {
if (left == null) {
return 0;
}
return left.height();
}
public int rightHeight() {
if (right == null) {
return 0;
}
return right.height();
}
public int bF() {
return leftHeight() - rightHeight();
}
private void leftRotate(){
AVLNode newNode = new AVLNode(value);
newNode.left = left;
newNode.right = right.left;
value = right.value;
right = right.right;
left = newNode;
}
private void rightRotate(){
AVLNode newNode = new AVLNode(value);
newNode.right = right;
newNode.left = left.right;
value = left.value;
left = left.left;
right = newNode;
}
@Override
public String toString() {
return "AVLNode{" +
"value=" + value +
'}';
}
}
参考:尚硅谷Java数据结构与java算法(Java数据结构与算法)_哔哩哔哩_bilibili
|