平衡二叉树(AVL树 Java实现)

1.平衡二叉树的前提需要实现二叉排序树(在添加节点的时候就实现排序)
2.平衡二叉树是为了解决某些二叉排序树查询效率效率低
3.此代码还包含了在平衡二叉树中删除叶子结点、删除有两棵子树的节点、删除只有一棵子树的节点的方法。
4.代码来自韩顺平老师Java数据结构。

测试类

public static void main(String[] args) {
		// int[] arr = { 4, 3, 6, 5, 7, 8 };
		// int[] arr = { 10, 12, 8, 9, 7, 6 };
		int[] arr = { 10, 11, 7, 6, 8, 9 };
		// 创建AVL对象
		Atree at = new Atree();
		// 添加节点
		for (int i = 0; i < arr.length; ++i) {
			at.paixu(new Node2(arr[i]));
		}
		System.out.println("中序遍历");
		at.infix();
		System.out.println("平衡前高度:");
		int res = at.getRoot().height();
		System.out.println(res);
		System.out.println("左子树高度=" + at.getRoot().leftHeight());
		System.out.println(at.getRoot());
		System.out.println("根节点的左子节点:" + at.getRoot().right.left);
	}

AVL树类

//二、创建AVL树
class Atree {
	private Node2 root;


	public Node2 getRoot() {
		return root;
	}

	public void setRoot(Node2 root) {
		this.root = root;
	}

	// 书写排序(添加节点)的方法
	public void paixu(Node2 node) {
		if (root == null) {
			// 如果root为空直接让root指向node
			root = node;
		} else {
			root.addNode(node);
		}
	}

	/**
	 * @param node 传入的节点(当做二叉排序树的根节点)
	 * @return 返回以node为根节点的二叉排序树的最小节点的值 还要删除node为根节点的二叉排序树的最小节点
	 */
	public int delRightTreeMin(Node2 node) {
		Node2 temp = node;
		while (temp.left != null) {
			temp = temp.left;
		}
		// 结束循环,找到最小节点
		delNode(temp.value);
		return temp.value;
	}

// 查找要删除节点的方法,进行封装
	public Node2 search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}

// 查找父节点
	public Node2 searchparent(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchParent(value);
		}
	}

// 删除节点
	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			// 1.找到要删除的节点
			Node2 targetNode = search(value);
			// 如果没有找到要删除的节点
			if (targetNode == null) {
				return;
			}
			// 2.判断这棵树是否只有一个节点
			if (root.left == null && root.right == null) {
				root = null;// 直接置空即可
				return;
			}
			// 3.去找到targetNode的父节点
			Node2 parent = searchparent(value);
			// A.如果要删除的节点是叶子结点
			if (targetNode.left == null && targetNode.right == null) {
				// 此时需要判断targetNode是parent的左子节点还是右子节点
				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) {// B要删除的节点有两棵子树
				int minVal = delRightTreeMin(targetNode.right);
				targetNode.value = minVal;// 将此时要删除的节点重置成刚才从右子树中找到的最小节点

			} else {// C.要删除的节点只有一棵子树
				// 1.如果要删除的节点有左子节点
				if (targetNode.left != null) {
					if (parent != null) {// 考虑只有一个root节点和root节点只有一棵子树的情况
						// 1.1如果targetNode是parent的左子节点
						if (parent.left.value == value) {
							parent.left = targetNode.left;
						} else {
							// 1.1如果targetNode是parent的右子节点
							parent.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}

				} else {
					if (parent == null) {
						// 2.如果要删除的节点有右子节点
						// 2.1如果targetNode是parent的左子节点
						if (parent.right.value == value) {
							parent.left = targetNode.right;
						} else {
							// 2.2如果targetNode是parent的右子节点
							parent.right = targetNode.right;

						}
					} else {
						root = targetNode.right;
					}

				}

			}
		}
	}

// 中序遍历
	public void infix() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("二叉排序树为空");
		}
		System.out.println("___________________________");
	}

// 前序遍历
	public void pre() {
		if (root != null) {
			root.preOrder();
		} else {
			System.out.println("二叉排序树为空");
	}
	}
}

Node节点类

//一、创建Node节点
class Node2 {
	int value;
	Node2 left;
	Node2 right;

	public Node2(int value) {
		super();
		this.value = value;
	}

	@Override
	public String toString() {
		return "Node2 [value=" + value + "]";
	}

	/*
	 * 新增方法:返回当前节点的高度,以该节点为根节点的树的高度
	 * 
	 */
	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();
	}

	/*
	 * 左旋转方法
	 */
	private void leftRotate() {
		// 创建新的节点,值等于当前节点的值
		Node2 newnode = new Node2(this.value);
		// 把新节点的左子树设置成当前节点的左子树
		newnode.left = this.left;
		// 把新节点的右子树设置成当前节点右子树的左子树
		newnode.right = this.right.left;
		// 把当前节点的值换为右子节点的值
		this.value = this.right.value;
		// 把当前节点的右子树设置成右子树的右子树
		this.right = this.right.right;
		// 把当前节点的左子树设置成为新节点
		this.left = newnode;
	}
	/*
	 * 右旋转
	 */
	public void rightRotate() {
		Node2 newnode = new Node2(this.value);
		newnode.right = this.right;
		newnode.left = this.left.right;
		this.value = this.left.value;
		this.left = this.left.left;
		this.right = newnode;
	}

//查找要删除的节点
	// (一),找到该节点
	/**
	 * @param value希望删除节点的值
	 * @return 如果找到就返回该节点,否则返回null
	 */
	public Node2 search(int value) {
		if (value == this.value) {
			return this;
		} else if (value < this.value) {// 如果查找的值小于当前节点,就向左子树递归
			// 还要判断左子节点是否为空,刚开始忘了
			if (this.left == null) {
				return null;
			} else {
				return this.left.search(value);
			}

		} else {
			if (this.right == null) {
				return null;
			} else {
				return this.right.search(value);
			}
		}
	}

//(二),找到该节点的父节点
	/**
	 * @param value希望删除节点的值
	 * @return 如果找到就返回该节点的父节点,否则返回null
	 */
	public Node2 searchParent(int value) {
		if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
			return this;
		} else {
			if (this.left != null && value < this.value) {
				return this.left.searchParent(value);
			} else if (this.right != null && value >= this.value) {
				return this.right.searchParent(value);
			} else {
				return null;// 如果没有父节点的情况
			}
		}
	}

	// 添加节点的方法
	public void addNode(Node2 node) {
		if (node == null) {
			return;
		}
		if (node.value < this.value) {
			// 添加到当前节点的左子树,此处需要递归添加
			if (this.left == null) {
				// 直接添加
				this.left = node;
			} else {
				// 不为空
				this.left.addNode(node);
			}
		}
		if (node.value > this.value) {
			// 添加到当前节点的左子树,此处需要递归添加
			if (this.right == null) {
				// 直接添加
				this.right = node;
			} else {
				// 不为空
				this.right.addNode(node);
			}
		}

		// 当添加完一个节点后,右子树的高度 - 左子树高度 > 1
		if (rightHeight() - leftHeight() > 1) {
			// 如果他的右子树的左子树高度大于他的右子树的右子树的高度
			if (this.right != null && this.right.leftHeight() > this.right.rightHeight()) {
				// 先对当前节点的右子树进行旋转
				this.right.rightRotate();
				// 再对当前节点进行左旋转
				leftRotate();
			} else {
				leftRotate();
			}
			return;// 处理好之后务必记得return,不然后面可能会引起不必要的bug
		}

		// 当添加完一个节点后,左子树的高度 - 右子树高度 > 1
		if (leftHeight() - rightHeight() > 1) {
			// 如果他的左子树的右子树高度大于他的左子树的左子树的高度
			if (this.left != null && this.left.rightHeight() > this.left.leftHeight()) {
				// 先对当前节点的左子树进行旋转
				this.left.leftRotate();
				// 再对当前节点进行右旋转
				rightRotate();
			} else {
				rightRotate();
			}
		}

	}

	// 遍历节点,中序
	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	// 前序遍历
	public void preOrder() {
		System.out.println(this);
		if (this.left != null) {
			this.left.preOrder();
		}
		if (this.right != null) {
			this.right.preOrder();
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值