数的节点

 package com.ydp.tree.AVLTree;
public class Node{
private int data = 0;
private Node lchild = null;
private Node rchild = null;
private Node parent = null; public Node(){};
public Node(int data){
this.data = data;
}
public Node(int data,Node parent){
this.data = data;
this.parent = parent;
} public boolean hasLChild(){
return lchild != null;
} public boolean hasRChild(){
return rchild != null;
}
//当前节点树的深度
public int getTreeDepth(){
return getTreeDepth(this);
}
//指定节点树的深度
protected int getTreeDepth(Node node){
int depth = 0;
if(node != null){
int ldepth = getTreeDepth(node.getLchild());
int rdepth = getTreeDepth(node.getRchild());
depth =1 + (ldepth>rdepth?ldepth:rdepth);
}
return depth;
} public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLchild() {
return lchild;
}
public void setLchild(Node lchild) {
this.lchild = lchild;
}
public Node getRchild() {
return rchild;
}
public void setRchild(Node rchild) {
this.rchild = rchild;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
} public boolean isLChild(){ return this.parent.hasLChild()?this.parent.getLchild().getData()==this.data:false;
} public boolean equals(Node node){
return this.data == node.getData();
} }

平衡二叉树的实现

 package com.ydp.tree.AVLTree;

 import java.util.Stack;

 public class AVLTree {

     Node root = null;

     public static void main(String[] args) {
AVLTree tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(30);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(60);
tree.insert(30);
tree.insert(45);
tree.insert(20);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(60);
tree.insert(70);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(60);
tree.insert(55);
tree.insert(70);
tree.insert(80);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(45);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(60);
tree.insert(30);
tree.insert(45);
tree.insert(47);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(60);
tree.insert(55);
tree.print(); tree = new AVLTree();
tree.insert(50);
tree.insert(40);
tree.insert(55);
tree.insert(53);
tree.insert(60);
tree.insert(70);
tree.print(); } public void print(){
System.out.println("树的深度:"+this.getRoot().getTreeDepth());
this.preOrder();
System.out.println();
this.midOrder();
System.out.println("\n");
} //插入节点数据
public void insert(int data){
if(this.root == null){
this.root = new Node(data);
}else{
insert(data,this.root);
}
}
//递归插入,将数据插入到合适的位置
protected void insert(int data,Node node){
if(data>node.getData()){
if(node.hasRChild()){
insert(data,node.getRchild());
}else{
node.setRchild(new Node(data,node));
}
if(getTreeDepth(node.getRchild())-getTreeDepth(node.getLchild())==2){
if(data>node.getRchild().getData()){
leftRotate(node); }else{
rightLeftRotate(node);
}
} }else if(data<node.getData()){
if(node.hasLChild()){
insert(data,node.getLchild());
}else{
node.setLchild(new Node(data,node));
}
if(getTreeDepth(node.getLchild())-getTreeDepth(node.getRchild())==2){
if(data<node.getLchild().getData()){
rightRotate(node);
}else{
leftRightRotate(node); }
} }
} public Node getRoot() {
return root;
} public void setRoot(Node root) {
this.root = root;
} //顺时针旋转
public void rightRotate(Node node){
System.out.println("顺时针:"+node.getData());
Node tmp = node.getLchild();
if(node.getParent() == null){
this.root=node.getLchild();
}else{
if(node.isLChild()){
node.getParent().setLchild(tmp);
}else{
node.getParent().setRchild(tmp);
}
}
tmp.setParent(node.getParent());
node.setLchild(tmp.getRchild());
node.setParent(tmp);
tmp.setRchild(node);
}
//先顺后逆时针
public void rightLeftRotate(Node node){
System.out.println("先顺后逆时针:"+node.getData());
rightRotate(node.getRchild());
leftRotate(node);
} //逆时针
public void leftRotate(Node node){
System.out.println("逆时针:"+node.getData()); Node tmp = node.getRchild();
if(node.getParent() == null){
this.root=node.getRchild();
}else{
if(node.isLChild()){
node.getParent().setLchild(tmp);
}else{
node.getParent().setRchild(tmp);
} }
tmp.setParent(node.getParent());
node.setRchild(tmp.getLchild());
node.setParent(tmp);
tmp.setLchild(node); } //逆时针
public void leftRightRotate(Node node){
System.out.println("先逆后顺时针:"+node.getData());
leftRotate(node.getLchild());
rightRotate(node);
} //先序遍历
public void preOrder(){
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || !stack.empty()){
while(node != null){
System.out.print(node.getData()+" ");
stack.push(node);
node = node.getLchild();
}
node = stack.pop();
node = node.getRchild();
}
} //中序遍历
public void midOrder(){
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || !stack.empty()){
while(node != null){
stack.push(node);
node = node.getLchild();
}
node = stack.pop();
System.out.print(node.getData()+" ");
node = node.getRchild();
}
} protected int getTreeDepth(Node node){
int depth = 0;
if(node != null){
int ldepth = getTreeDepth(node.getLchild());
int rdepth = getTreeDepth(node.getRchild());
depth =1 + (ldepth>rdepth?ldepth:rdepth);
}
return depth;
}
}

平衡二叉树(AVL)java实现的更多相关文章

  1. 平衡二叉树(AVL)的理解和实现(Java)

    AVL的定义 平衡二叉树:是一种特殊的二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1.从平衡二叉树的名字中可以看出来,它是一种高度平衡的二叉排序树.那么什么叫做高度平衡呢?意思就是要么它 ...

  2. Java 树结构实际应用 四(平衡二叉树/AVL树)

    平衡二叉树(AVL 树) 1 看一个案例(说明二叉排序树可能的问题) 给你一个数列{1,2,3,4,5,6},要求创建一颗二叉排序树(BST), 并分析问题所在.  左边 BST 存在的问题分析: ...

  3. 数据结构与算法--从平衡二叉树(AVL)到红黑树

    数据结构与算法--从平衡二叉树(AVL)到红黑树 上节学习了二叉查找树.算法的性能取决于树的形状,而树的形状取决于插入键的顺序.在最好的情况下,n个结点的树是完全平衡的,如下图"最好情况&q ...

  4. 二叉查找树(BST)、平衡二叉树(AVL树)(只有插入说明)

    二叉查找树(BST).平衡二叉树(AVL树)(只有插入说明) 二叉查找树(BST) 特殊的二叉树,又称为排序二叉树.二叉搜索树.二叉排序树. 二叉查找树实际上是数据域有序的二叉树,即对树上的每个结点, ...

  5. 平衡二叉树AVL - 插入节点后旋转方法分析

    平衡二叉树 AVL( 发明者为Adel'son-Vel'skii 和 Landis)是一种二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1. 首先我们知道,当插入一个节点,从此插入点到树根 ...

  6. 二叉查找树、平衡二叉树(AVL)、B+树、联合索引

    1. [定义] 二叉排序树(二拆查找树)中,左子树都比节点小,右子树都比节点大,递归定义. [性能] 二叉排序树的性能取决于二叉树的层数 最好的情况是 O(logn),存在于完全二叉排序树情况下,其访 ...

  7. java平衡二叉树AVL数

    平衡二叉树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树 右旋:在插入二叉树的时候,根节点的右侧高 ...

  8. 【数据结构】平衡二叉树—AVL树

    (百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...

  9. 平衡二叉树AVL删除

    平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...

  10. 平衡二叉树AVL插入

    平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...

随机推荐

  1. startActivityForResult和setResult详解

    http://www.cnblogs.com/lijunamneg/archive/2013/02/05/2892616.html startActivityForResult与startActivi ...

  2. Android OpenGL ES 3.0 纹理应用

    本文主要演示OpenGL ES 3.0 纹理演示.接口大部分和2.0没什么区别,脚本稍微有了点变化而已. 扩展GLSurfaceView package com.example.gles300; im ...

  3. angularjs-ngModel 控制页面的宽度

    js NiDialog.open({ windowClass: '', size:'elements', backdrop: 'static', keyboard: false, templateUr ...

  4. 分离数据库(Detach database).

    Many times, we often needs to detach our databases if we want to copy it to another database instanc ...

  5. TFS扩展开发中遇到的坑

    本码农最近开发一个VS扩展,其中有些功能涉及到文件的签出.我们公司用的是TFS,遇到了一些奇特的现象,将解决过程记录如下. 一.明明在线的连接却Offline属性等于True public stati ...

  6. mybatis for .net

    MyBatis For .NET学习笔记:开篇 http://chenkai.blog.51cto.com/2023960/763806 MyBatis For .NET学习笔记[2]:配置环境 ht ...

  7. Dell服务器MegaCli命令只返回Exit Code: 0x00问题分析

    今天同事给我说一台dell的服务器做了raid后,使用MegaCli看不到raid信息,上去看了一下确实不返回任何raid信息,但是确实机器上做了raid. 这就奇怪了,然后把MegaCli升级到最新 ...

  8. 掌握JS

    1.原生的js,好比全真教的武功,一步步从基础开始(先练气再御剑),很长一段时间内和jquery有很大差距,掌握以后发现jquery只不过是另外一种武功,看一遍既会.且当学原生到一定程度之后,可以自创 ...

  9. 【POJ2761】【区间第k大】Feed the dogs(吐槽)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

  10. C++ 语法规则

    C++ 中的布尔类型:布尔类型只占用一个bit ,但是如果连续定义多个布尔类型时,编译器可能会多个布尔类型定义在一起.true  编译器用1来表示.false  编译器用0来表示. 将一个其他类型的数 ...