package Demo;

 public class AVLtree  {
private Node root; //首先定义根节点 private static class Node{ //定义Node指针参数
private int key; //节点
private int balance; //平衡值
private int height; //树的高度
private Node left; //左节点
private Node right; //右节点
private Node parent; //父母节点 Node(int key, Node parent){ //构造器中引用该构造器正在初始化的对象
this.key = key;
this.parent = parent; }
}
public boolean insert(int key){ //判断这里是否能插入新的节点
if(root == null){
root = new Node(key,null);
return true;
} Node n = root;
while (true){ //如果根节点下的子节点和新插进来的子节点相同
if(n.key == key)
return false; //则不进行插入操作 Node parent = n; boolean goLeft = n.key > key; //判断新的节点插入父母节点的左边or右边
n = goLeft ? n.left : n.right; //小的话插左边,大的话插右边 if(n == null){
if(goLeft){
parent.left = new Node (key,parent);
}else{
parent.right = new Node(key,parent);
}
rebalance(parent);
break;
}
}
return true;
} private void delete(Node node){ //删除节点
if(node.left == null && node.right == null){
if(node.parent == null){
root = null;
}else{
Node parent = node.parent;
if(parent.left == node){ //如果父母节点的左孩子节点和根节点一样
parent.left = null; //则左节点为空
}else{
parent.right = null; //反之右节点为空
}
rebalance(parent);
}
return ;
} if(node.left != null){ //如果左节点不空
Node child = node.left;
while(child.right != null)child = child.right;
node.key = child.key;
delete(child);
}else{
Node child = node.right;
while (child.left != null)child = child.left;
node.key = child.key;
delete(child);
}
} public void Delete(int delKey){
if(root == null)
return; Node child = root;
while (child != null){
Node node = child; //交换根节点给node , 再判断新的孩子节点插在哪里
child = delKey >= node.key ? node.right : node.left;
if(delKey == node.key){
delete(node);
return;
}
}
} private void setBalance(Node... nodes){
for(Node n : nodes){
reheight(n);
n.balance = height(n.right) - height(n.left); //平衡因子,任意节点左右子树高度差
}
} private void rebalance (Node n){
setBalance(n); if(n.balance == -2){
if(height(n.left.left) >= height(n.left.right))
n = rotateRight(n);
else
n = rotateLeftThenRight(n) ; }else if(n.balance == 2){ //等于2和-2都是不平衡的,需要重新调整
if(height(n.right.right) >= height(n.right.left))
n = rotateLeft(n);
else
n = rotateRightThenLeft(n); } if(n.parent != null){
rebalance(n.parent);
}else{
root = n;
}
} private Node rotateLeft(Node a){ Node b = a.right;
b.parent = a.parent; a.right = b.left; if(a.right != null)
a.right.parent = a; b.left = a;
a.parent = b; if(b.parent != null){
if(b.parent.right == a){
b.parent.right = b;
}else{
b.parent.left = b;
}
} setBalance(a, b); return b;
} private Node rotateRight(Node a){ Node b = a.left;
b.parent = a.parent; a.left = b.right; if(a.left != null){
a.left.parent = a; b.right = a;
a.parent = b; if(b.parent.right == a){
b.parent.right = b;
}else{
b.parent.left = b;
}
} setBalance(a, b); return b;
} private Node rotateLeftThenRight(Node n){
n.left = rotateLeft(n.left);
return rotateRight(n);
} private Node rotateRightThenLeft(Node n){
n.right = rotateRight(n.right);
return rotateLeft(n);
} private int height (Node n){
if(n == null)
return -1;
return n.height;
} public void printBalance(){
printBalance(root);
} private void printBalance(Node n){
if(n != null){
printBalance(n.left);
System.out.printf("%s ",n.balance);
printBalance(n.right);
}
} private void reheight(Node node){
if(node != null){
node.height = 1 + Math.max(height(node.left),height(node.right)); //新的二叉平衡树高度为:
}
}
public static void main(String[] args) {
AVLtree tree = new AVLtree(); System.out.println("Inserting values 1 to 10"); //最后输出的结果代表平衡因子,0为左右子树高度相等,1为左右子树高度相差1层
for (int i = 1; i < 10; i++)
tree.insert(i); System.out.println("Print balance : ");
tree.printBalance();
}
}

    可以动手画一下生成的AVL树,亲测算法符合结果。


java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)的更多相关文章

  1. (4) 二叉平衡树, AVL树

    1.为什么要有平衡二叉树? 上一节我们讲了一般的二叉查找树, 其期望深度为O(log2n), 其各操作的时间复杂度O(log2n)同时也是由此决定的.但是在某些情况下(如在插入的序列是有序的时候), ...

  2. Algorithms: 二叉平衡树(AVL)

    二叉平衡树(AVL):   这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有.  后面两月又要忙了. ...

  3. 树-二叉搜索树-AVL树

    树-二叉搜索树-AVL树 树 树的基本概念 节点的度:节点的儿子数 树的度:Max{节点的度} 节点的高度:节点到各叶节点的最大路径长度 树的高度:根节点的高度 节点的深度(层数):根节点到该节点的路 ...

  4. 二叉平衡树AVL的插入与删除(java实现)

    二叉平衡树 全图基础解释参考链接:http://btechsmartclass.com/data_structures/avl-trees.html 二叉平衡树:https://www.cnblogs ...

  5. 高度平衡的二叉搜索树(AVL树)

    AVL树的基本概念 AVL树是一种高度平衡的(height balanced)二叉搜索树:对每一个结点x,x的左子树与右子树的高度差(平衡因子)至多为1. 有人也许要问:为什么要有AVL树呢?它有什么 ...

  6. 树-二叉平衡树AVL

    基本概念 AVL树:树中任何节点的两个子树的高度最大差别为1. AVL树的查找.插入和删除在平均和最坏情况下都是O(logn). AVL实现 AVL树的节点包括的几个组成对象: (01) key -- ...

  7. 各种查找算法的选用分析(顺序查找、二分查找、二叉平衡树、B树、红黑树、B+树)

    目录 顺序查找 二分查找 二叉平衡树 B树 红黑树 B+树 参考文档 顺序查找 给你一组数,最自然的效率最低的查找算法是顺序查找--从头到尾挨个挨个遍历查找,它的时间复杂度为O(n). 二分查找 而另 ...

  8. AVL树(二叉平衡树)详解与实现

    AVL树概念 前面学习二叉查找树和二叉树的各种遍历,但是其查找效率不稳定(斜树),而二叉平衡树的用途更多.查找相比稳定很多.(欢迎关注数据结构专栏) AVL树是带有平衡条件的二叉查找树.这个平衡条件必 ...

  9. 判断一颗二叉树是否为二叉平衡树 python 代码

    输入一颗二叉树,判断这棵树是否为二叉平衡树.首先来看一下二叉平衡树的概念:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.因此判断一颗二叉平衡树的关键在于 ...

随机推荐

  1. 汽车行业解决方案_K2助力车企实现费控/生产“端到端流程”

    如今汽车行业正面对一轮全球范围内新变革周期,这种“变革”一方面来源于在新能源技术.人工智能.信息技术.物联网技术等高新科技地猛烈敲击,另一方面源于全球的经济政策变幻莫测,贸易保护时代地到来,车企深陷发 ...

  2. flask第一章 flask启动 路由视图 FlaskRequest jinja2 FlaskSession

    一.简单了解flask web框架 优点: 小而精,组件只有session,第三方机构强烈支持flask,极其简单 缺点: 由于第三方软件的关系,稳定性相对较差,flask-session 扩展知识: ...

  3. 【webpack学习笔记】a05-模块热替换

    什么是模块热替换? 这个功能会在程序运行过程中替换.添加或删除模块,而无需重新加载整个页面 有什么用呢? 保留在完全重新加载页面时丢失的应用程序状态. 只更新变更内容,以节省宝贵的开发时间. 调整样式 ...

  4. 用户认证:基于jwt和session的区别和优缺点

    背景知识: Authentication和Authorization的区别: Authentication:用户认证,指的是验证用户的身份,例如你希望以小A的身份登录,那么应用程序需要通过用户名和密码 ...

  5. 外网win10 64位环境下 为内网win7 32位安装三方包的最靠谱手段:python64位、32位全安装。

    经过一周的各种折磨,如题.以下是我的经验和教训. 我的外网是win10 64位,内网环境win7 32位.由于未知原因,anaconda无法安装!!! 其实最靠谱的安装三方包的还是whl包.但是很有可 ...

  6. 关于react的分页

    基于antdesign分页:表格属性pagination <Table pagination={{ total: this.state.totalNum, showSizeChanger: tr ...

  7. 简述我理解的C#

    第一章:开发入门 一.基础知识    1.计算机语言发展进程:计算机语言大致经过了机器语言.汇编语言.高级语言三个阶段的发展历程. 汇编语言:使用助记符来替代机器指令机器语言这种反人类的设计,阻碍了软 ...

  8. Get teststep of specific type

    SoapUI Groovy : Check if test step is of specific type, such as : Wsdl, Rest, Jdbc, HTTP, Groovy etc ...

  9. JS 最简单数组去重

    ,,,,])) // 再把set转变成array console.log(newArr) // [1,2,3,4]

  10. c语言——鞍点

    描述 找出具有m行n列二维数组Array的“鞍点”,即该位置上的元素在该行上最大,在该列上最小,其中1<=m,n<=10. 输入 输入数据有多行,第一行有两个数m和n,下面有m行,每行有n ...