数的节点

 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. tomcat发布项目时,空文件夹未发布成功

    问题背景: 项目发布到服务器时,缺少文件夹,到时向此文件夹写数据时发生错误. 后来经查,缺少这个文件夹,项目部署发布时,并不会把空文件夹发布上去 解决: 1.在空文件中加入,一个文件.就可以发布成功 ...

  2. Scrum教练不应兼任product owner

    ScrumMasters Should Not Also Be Product Owners(中文翻译) December 2, 2014 by Mike Cohn 翻译:2015.2.18 by o ...

  3. (JavaScript实现)页面无操作倒计时退出

    项目前端页面需要实现,页面没人操作进入倒计时,以下为前端代码实现. //设置(倒计时功能)开关 var _mouseActiveListener_flag = true; beforecount:触发 ...

  4. python 数据结构

    Python的数据结构主要分为set(),list[],和dict{}.这篇文章主要记载这几种结果在使用过程中的一些技巧或其中一些函数的用法区别. 1.函数get()与setdefault()区别: ...

  5. cmd 命令行下复制、粘贴的快捷键

    1.单击左下角“开始”菜单,选择“运行”,输入“cmd”. 2.在弹出的cmd窗口的标题栏上点击“右键”,选择“属性”. 3.在弹出的对话框中选择“选项”这个选项卡,在“编辑选项”区域中勾选“快速编辑 ...

  6. LNMP安装WordPress3.4.2看不到主题解决方法

    喜欢LNMP配置环境的朋友,又是wordpress建立的博客程序,安装之后发现一个问题在网站后台看不到模板主题,重新下载了一款新的主题也查看不了.开始以为是程序的问题,于是我重新下载新版本的WordP ...

  7. smarty 的学习----ubuntu下初步配置

    转自:http://blog.csdn.net/ma332567575/article/details/7904124 首先去www.smarty.net下载最新版的Smarty 把下载后的压缩包在网 ...

  8. sed 简明教程

    做个标记 http://coolshell.cn/articles/9104.html sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊.sed基本上就是 ...

  9. 深入解析.NET框架

    一.AOP框架        Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种 ...

  10. Android 常用网站

    Android Design : http://www.sunjw.us/adchs/index.html Android Developers : http://developer.android. ...