数的节点

 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. .Net实现IO操作

    IO操作需要的web.config里的节点配置 <configuration>  <appSettings>    <!--上传文件类型要求-->    <a ...

  2. JAva Collections类方法详解

    http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...

  3. VS中监视窗口,即时窗口和输出窗口的使用

    一.监视窗口 1.配置应用程序,使应用程序处于调试状态. 2.点击“调试”----“窗口”----“监视”----“监视1”,打开监视窗口. 3.在监视窗口中“名称”栏中输入变量名称或html元素id ...

  4. How to Make LastPass Even More Secure with Google Authenticator

    Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...

  5. ORACLE 中ROWNUM用法总结!(转)

    对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=),并非说用>,>=,=,be ...

  6. 数据库连接报错之IO异常(The Network Adapter could not establish the connection)

    Io 异常: The Network Adapter could not establish the connection 有以下四个原因: 1.oracle配置 listener.ora 和tnsn ...

  7. [转]Mysql导入导出工具Mysqldump和Source命令用法详解

    Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...

  8. angular.js 数字

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  9. csv文本编辑引号问题

    今天发现一个csv的一个问题,csv工具类对于引号默认有特殊的处理.我希望写出来的结果是 1,"1",1 原来的代码是 CsvWriter cw=new CsvWriter(&qu ...

  10. oc 怎么接收NSSting字符的方法

    ]; //使用一个缓冲区 NSLog(@"请输入一个字符串:"); scanf("%s",buffer); NSString * str = [NSString ...