AVL树的Java实现
AVL树:平衡的二叉搜索树,其子树也是AVL树。
以下是我实现AVL树的源码(使用了泛型):
import java.util.Comparator;
public class AVLTree<T extends Comparable<T>> {
/*
AVL树:
左右子树高度绝对值最多差1的二叉搜索树
子树也是AVL树
*/
private Node<T> root;
class Node<T extends Comparable<T>>{
T key;
int height;
Node<T> left;
Node<T> right;
public Node(T key, int height, Node<T> left, Node<T> right) {
this.key = key;
this.height = height;
this.left = left;
this.right = right;
}
public Node(T key){
this.key = key;
this.height = 0;
}
}
public int getHeight(Node<T> node){
return node == null ? 0 : node.height;
}
public Node<T> LL(Node<T> node){
//左子树插入节点在左导致不平衡,需要旋转,以下不再赘述
Node<T> leftNode = node.left;
node.left = leftNode.right;
leftNode.right = node;
node.height = Math.max(node.left.height,node.right.height) + 1;
leftNode.height = Math.max(leftNode.left.height, leftNode.right.height) + 1;
return leftNode;
}
public Node<T> RR(Node<T> node) {
Node<T> rightNode;
rightNode = node.right;
node.right = rightNode.left;
rightNode.left = node;
node.height = Math.max( node.left.height, node.right.height) + 1;
rightNode.height = Math.max( rightNode.left.height, rightNode.right.height) + 1;
return rightNode;
}
public Node<T> LR(Node<T> node){
//LR先对左子树RR再对本树LL
node.left = RR(node.left);
return LL(node);
}
public Node<T> RL(Node<T> node){
node.right = LL(node.right);
return RR(node);
}
public Node<T> insert(Node<T> root,T key){
/*
插入:先判断插入点,再判断是否需要翻转
*/
if(root == null){
return new Node<T>(key);
}
else{
if(key.compareTo(root.key)<0)
//T是实现了comparable接口的,所以这里可以比较,但不能用大小于号
{
root.left = insert(root.left,key);
if(root.left.height - root.right.height == 2){
if (key.compareTo(root.left.key) < 0)
root = LL(root);
else
root = LR(root);
}
}else if(key.compareTo(root.key)>0){
root.right = insert(root.right,key);
if(root.left.height - root.right.height == -2){
if (key.compareTo(root.right.key) < 0)
root = RL(root);
else
root = RR(root);
}
}else{
return root;//相同的节点不添加
}
}
return root;
}
public Node<T> delete(Node<T> root,T key){
/*
删除:找到删除点,判断是否需要翻转
*/
if(root == null || key == null){
return root;
}
else{
if(key.compareTo(root.key)<0)
//T是实现了comparable接口的,所以这里可以比较,但不能用大小于号
{
root.left = delete(root.left,key);
if(root.left.height - root.right.height == -2){
Node<T> rightNode = root.right;
if (rightNode.right.height>root.left.height)
root = RL(root);
else
root = RR(root);
}
}else if(key.compareTo(root.key)>0){
root.right = delete(root.right,key);
if(root.left.height - root.right.height == 2){
Node<T> leftNode = root.left;
if (leftNode.left.height>leftNode.right.height)
root = LL(root);
else
root = LR(root);
}
}else {
//找到了要删除的节点
if (root.left == null) root = root.right;
else if (root.right == null) root = root.left;
else {
if (root.left.height < root.right.height) {
Node<T> tempNode = root.right;
while (tempNode.left != null) {
tempNode = tempNode.left;
}//为保证二叉树的平衡性、搜索性
//这里选择了高度较高的子树,选取中序遍历与root相邻的节点作为root,这样不会破坏搜索性
root.key = tempNode.key;
delete(tempNode, key);
} else {
Node<T> tempNode = root.left;
while (tempNode.right != null) {
tempNode = tempNode.right;
}//为保证二叉树的平衡性、搜索性
//这里选择了高度较高的子树,选取中序遍历与root相邻的节点作为root
root.key = tempNode.key;
delete(tempNode, key);
}
}
}
}
return root;
}
}
AVL树的Java实现的更多相关文章
- AVL树之 Java的实现
AVL树的介绍 AVL树是高度平衡的而二叉树.它的特点是:AVL树中任何节点的两个子树的高度最大差别为1. 上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1:而右边的不 ...
- AVL树的JAVA实现及AVL树的旋转算法
1,AVL树又称平衡二叉树,它首先是一颗二叉查找树,但在二叉查找树中,某个结点的左右子树高度之差的绝对值可能会超过1,称之为不平衡.而在平衡二叉树中,任何结点的左右子树高度之差的绝对值会小于等于 1. ...
- AVL树(三)之 Java的实现
概要 前面分别介绍了AVL树"C语言版本"和"C++版本",本章介绍AVL树的Java实现版本,它的算法与C语言和C++版本一样.内容包括:1. AVL树的介绍 ...
- AVL树(C++&Java)
目录 AVL Tree精讲专题 前言 一.AVL Tree for CPP(Coding) 1.AVL树原型 2.旋转的四种方式 二.完整版AVL Tree的CPP和JAVA实现 AVL Tree C ...
- 数据结构——二叉查找树、AVL树
二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- AVL树----java
AVL树----java AVL ...
- 【Java】 大话数据结构(12) 查找算法(3) (平衡二叉树(AVL树))
本文根据<大话数据结构>一书及网络资料,实现了Java版的平衡二叉树(AVL树). 平衡二叉树介绍 在上篇博客中所实现的二叉排序树(二叉搜索树),其查找性能取决于二叉排序树的形状,当二叉排 ...
- Java数据结构和算法(七)--AVL树
在上篇博客中,学习了二分搜索树:Java数据结构和算法(六)--二叉树,但是二分搜索树本身存在一个问题: 如果现在插入的数据为1,2,3,4,5,6,这样有序的数据,或者是逆序 这种情况下的二分搜索树 ...
随机推荐
- 关于模拟I2C的一些问题???
1.在调试BH1750时发现stm32f103rb单片机用模拟I2C通讯时引脚使用开漏模式能正常读出来数据,使用推挽模式则完全无法通讯,发送地址后从机没有应答? https://blog.csdn.n ...
- Spring系列(六) Spring Web MVC 应用构建分析
DispatcherServlet DispatcherServlet 是Spring MVC的前端控制器名称, 用户的请求到达这里进行集中处理, 在Spring MVC中, 它的作用是为不同请求匹配 ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 记录一次Python下Tensorflow安装过程,1.7带GPU加速版本
最近由于论文需要,急需搭建Tensorflow环境,16年底当时Tensorflow版本号还没有过1,我曾按照手册搭建过CPU版本.目前,1.7算是比较新的版本了(也可以从源码编译1.8版本的Tens ...
- django 实战篇之视图层
视图层(views.py) django必会三板斧 HttpResponse >>> 返回字符串 render >>> 支持模板语法,渲染页面,并返回给前端 red ...
- SpringBoot 请求参数后端校验
1.例如: package com.model.user; import com.model.PageEntity;import lombok.Getter;import lombok.Setter; ...
- docker报错:Failed to restart docker.service: Unit not found.
前言:我之前安装好docker了,但是关机重启后,发现docker就没了 报错:Failed to restart docker.service: Unit not found. 解决方法: 1. ...
- BsonJavaScript
BsonJavaScript主要应用于mongodb驱动中 1.进行数据分组 MongoClient client = new MongoClient(host.ConnectionString); ...
- MQTT服务器的搭建(Windows平台)
人工智能.智能家居越来越火,在服务器和多个终端进行通信的过程中使用传统的请求/回答(Request/Response)模式已经过时,伴随而来的是发布/订阅(Publish/Subscribe)模式-- ...
- numpy创建array【老鱼学numpy】
在上一篇文章中,我们已经看到了如何通过numpy创建numpy中的数组,这里再重复一下: import numpy as np # 数组 a = [[1, 2, 3], [4, 5, 6]] prin ...